<s> """<STR_LIT>""" <EOL> from __future__ import generators <EOL> from zope . interface import implements <EOL> from nevow import inevow <EOL> class Proto ( str ) : <EOL> """<STR_LIT>""" <EOL> __slots__ = [ ] <EOL> def __call__ ( self , ** kw ) : <EOL> return Tag ( self ) ( ** kw ) <EOL> def __getitem__ ( self , children ) : <EOL> return Tag ( self ) [ children ] <EOL> def fillSlots ( self , slotName , slotValue ) : <EOL> return Tag ( self ) . fillSlots ( slotName , slotValue ) <EOL> def clone ( self , deep = True ) : <EOL> return self <EOL> class xml ( object ) : <EOL> """<STR_LIT>""" <EOL> __slots__ = [ '<STR_LIT:content>' ] <EOL> def __init__ ( self , content ) : <EOL> self . content = content <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % self . content <EOL> class raw ( str ) : <EOL> """<STR_LIT>""" <EOL> __slots__ = [ ] <EOL> def cdata ( data ) : <EOL> """<STR_LIT>""" <EOL> return xml ( '<STR_LIT>' % data ) <EOL> class directive ( object ) : <EOL> """<STR_LIT>""" <EOL> __slots__ = [ '<STR_LIT:name>' ] <EOL> def __init__ ( self , name ) : <EOL> self . name = name <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" % self . name <EOL> def __hash__ ( self ) : <EOL> return hash ( ( directive , self . name ) ) <EOL> def __cmp__ ( self , other ) : <EOL> if isinstance ( other , directive ) : <EOL> return cmp ( self . name , other . name ) <EOL> return NotImplemented <EOL> class slot ( object ) : <EOL> """<STR_LIT>""" <EOL> __slots__ = [ '<STR_LIT:name>' , '<STR_LIT>' , '<STR_LIT:default>' , '<STR_LIT:filename>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> def __init__ ( self , name , default = None , filename = None , lineNumber = None , columnNumber = None ) : <EOL> self . name = name <EOL> self . children = [ ] <EOL> self . default = default <EOL> self . filename = filename <EOL> self . lineNumber = lineNumber <EOL> self . columnNumber = columnNumber <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" % self . name <EOL> def __getitem__ ( self , children ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( children , ( list , tuple ) ) : <EOL> children = [ children ] <EOL> self . children . extend ( children ) <EOL> return self <EOL> def __iter__ ( self ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError , "<STR_LIT>" <EOL> class _PrecompiledSlot ( object ) : <EOL> """<STR_LIT>""" <EOL> __slots__ = [ <EOL> '<STR_LIT:name>' , '<STR_LIT>' , '<STR_LIT:default>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT:filename>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> def __init__ ( self , name , children , default , isAttrib , inURL , inJS , inJSSingleQuoteString , filename , lineNumber , columnNumber ) : <EOL> self . name = name <EOL> self . children = children <EOL> self . default = default <EOL> self . isAttrib = isAttrib <EOL> self . inURL = inURL <EOL> self . inJS = inJS <EOL> self . inJSSingleQuoteString = inJSSingleQuoteString <EOL> self . filename = filename <EOL> self . lineNumber = lineNumber <EOL> self . columnNumber = columnNumber <EOL> def __repr__ ( self ) : <EOL> return ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) % ( <EOL> self . name , self . isAttrib , self . inURL , self . inJS , <EOL> self . inJSSingleQuoteString ) <EOL> class Tag ( object ) : <EOL> """<STR_LIT>""" <EOL> implements ( inevow . IQ ) <EOL> specials = [ '<STR_LIT:data>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:key>' , '<STR_LIT>' ] <EOL> slotData = None <EOL> filename = None <EOL> lineNumber = None <EOL> columnNumber = None <EOL> def __init__ ( self , tag , attributes = None , children = None , specials = None , filename = None , lineNumber = None , columnNumber = None ) : <EOL> self . tagName = tag <EOL> if attributes is None : <EOL> self . attributes = { } <EOL> else : <EOL> self . attributes = attributes <EOL> if children is None : <EOL> self . children = [ ] <EOL> else : <EOL> self . children = children <EOL> if specials is None : <EOL> self . _specials = { } <EOL> else : <EOL> self . _specials = specials <EOL> if filename is not None : <EOL> self . filename = filename <EOL> if lineNumber is not None : <EOL> self . lineNumber = lineNumber <EOL> if columnNumber is not None : <EOL> self . columnNumber = columnNumber <EOL> def fillSlots ( self , slotName , slotValue ) : <EOL> """<STR_LIT>""" <EOL> if self . slotData is None : <EOL> self . slotData = { } <EOL> self . slotData [ slotName ] = slotValue <EOL> return self <EOL> def patternGenerator ( self , pattern , default = None ) : <EOL> """<STR_LIT>""" <EOL> patterner = _locatePatterns ( self , pattern , default ) <EOL> return PatternTag ( patterner ) <EOL> def allPatterns ( self , pattern ) : <EOL> """<STR_LIT>""" <EOL> return [ tag . clone ( deep = False , clearPattern = True ) for tag in <EOL> specialMatches ( self , '<STR_LIT>' , pattern ) ] <EOL> def onePattern ( self , pattern ) : <EOL> """<STR_LIT>""" <EOL> data = getattr ( self , '<STR_LIT>' , Unset ) <EOL> if data == pattern : <EOL> result = self <EOL> else : <EOL> result = _locateOne ( <EOL> pattern , <EOL> lambda pattern : specialMatches ( self , '<STR_LIT>' , pattern ) , <EOL> '<STR_LIT>' ) <EOL> return result . clone ( deep = False , clearPattern = True ) <EOL> def __call__ ( self , ** kw ) : <EOL> """<STR_LIT>""" <EOL> if not kw : <EOL> return self <EOL> for name in self . specials : <EOL> if kw . has_key ( name ) : <EOL> setattr ( self , name , kw [ name ] ) <EOL> del kw [ name ] <EOL> for k , v in kw . iteritems ( ) : <EOL> if k [ - <NUM_LIT:1> ] == '<STR_LIT:_>' : <EOL> k = k [ : - <NUM_LIT:1> ] <EOL> elif k [ <NUM_LIT:0> ] == '<STR_LIT:_>' : <EOL> k = k [ <NUM_LIT:1> : ] <EOL> self . attributes [ k ] = v <EOL> return self <EOL> def __getitem__ ( self , children ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( children , ( list , tuple ) ) : <EOL> children = [ children ] <EOL> self . children . extend ( children ) <EOL> return self <EOL> def __iter__ ( self ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError , "<STR_LIT>" <EOL> def _clearSpecials ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _specials = { } <EOL> def precompilable ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . render is Unset and self . pattern is Unset <EOL> def _clone ( self , obj , deep ) : <EOL> if hasattr ( obj , '<STR_LIT>' ) : <EOL> return obj . clone ( deep ) <EOL> elif isinstance ( obj , ( list , tuple ) ) : <EOL> return [ self . _clone ( x , deep ) <EOL> for x in obj ] <EOL> else : <EOL> return obj <EOL> def clone ( self , deep = True , clearPattern = False ) : <EOL> """<STR_LIT>""" <EOL> if deep : <EOL> newchildren = [ self . _clone ( x , True ) for x in self . children ] <EOL> else : <EOL> newchildren = self . children [ : ] <EOL> newattrs = self . attributes . copy ( ) <EOL> for key in newattrs : <EOL> newattrs [ key ] = self . _clone ( newattrs [ key ] , True ) <EOL> newslotdata = None <EOL> if self . slotData : <EOL> newslotdata = self . slotData . copy ( ) <EOL> for key in newslotdata : <EOL> newslotdata [ key ] = self . _clone ( newslotdata [ key ] , True ) <EOL> newtag = Tag ( <EOL> self . tagName , <EOL> attributes = newattrs , <EOL> children = newchildren , <EOL> specials = self . _specials . copy ( ) , <EOL> filename = self . filename , <EOL> lineNumber = self . lineNumber , <EOL> columnNumber = self . columnNumber ) <EOL> newtag . slotData = newslotdata <EOL> if clearPattern : <EOL> newtag . pattern = None <EOL> return newtag <EOL> def clear ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _specials = { } <EOL> self . children = [ ] <EOL> return self <EOL> def __repr__ ( self ) : <EOL> rstr = '<STR_LIT>' <EOL> if self . attributes : <EOL> rstr += '<STR_LIT>' % self . attributes <EOL> if self . _specials : <EOL> rstr += '<STR_LIT>' % self . _specials <EOL> if self . children : <EOL> rstr += '<STR_LIT>' % self . children <EOL> return "<STR_LIT>" % ( self . tagName , rstr ) <EOL> def freeze ( self ) : <EOL> """<STR_LIT>""" <EOL> def forever ( ) : <EOL> while True : <EOL> yield self . clone ( ) <EOL> return PatternTag ( forever ( ) ) <EOL> class UnsetClass : <EOL> def __nonzero__ ( self ) : <EOL> return False <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" <EOL> Unset = UnsetClass ( ) <EOL> def makeAccessors ( special ) : <EOL> def getSpecial ( self ) : <EOL> return self . _specials . get ( special , Unset ) <EOL> def setSpecial ( self , data ) : <EOL> self . _specials [ special ] = data <EOL> return getSpecial , setSpecial <EOL> for name in Tag . specials : <EOL> setattr ( Tag , name , property ( * makeAccessors ( name ) ) ) <EOL> del name <EOL> def visit ( root , visitor ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( root , list ) : <EOL> for t in root : <EOL> visit ( t , visitor ) <EOL> else : <EOL> visitor ( root ) <EOL> if isinstance ( root , Tag ) : <EOL> for ch in root . children : <EOL> visit ( ch , visitor ) <EOL> class NodeNotFound ( KeyError ) : <EOL> def __str__ ( self ) : <EOL> return "<STR_LIT>" % tuple ( self . args [ : <NUM_LIT:2> ] ) <EOL> class TooManyNodes ( Exception ) : <EOL> def __str__ ( self ) : <EOL> return "<STR_LIT>" % tuple ( self . args [ : <NUM_LIT:2> ] ) <EOL> class PatternTag ( object ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , patterner ) : <EOL> self . pat = patterner . next ( ) <EOL> self . patterner = patterner <EOL> def next ( self ) : <EOL> if self . pat : <EOL> p , self . pat = self . pat , None <EOL> return p <EOL> return self . patterner . next ( ) <EOL> def makeForwarder ( name ) : <EOL> return lambda self , * args , ** kw : getattr ( self . next ( ) , name ) ( * args , ** kw ) <EOL> for forward in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> setattr ( PatternTag , forward , makeForwarder ( forward ) ) <EOL> def _locatePatterns ( tag , pattern , default , loop = True ) : <EOL> """<STR_LIT>""" <EOL> gen = specialMatches ( tag , '<STR_LIT>' , pattern ) <EOL> produced = [ ] <EOL> for x in gen : <EOL> produced . append ( x ) <EOL> cloned = x . clone ( deep = False , clearPattern = True ) <EOL> yield cloned <EOL> gen = None <EOL> if produced : <EOL> if not loop : <EOL> return <EOL> while True : <EOL> for x in produced : <EOL> cloned = x . clone ( deep = False , clearPattern = True ) <EOL> yield cloned <EOL> if default is None : <EOL> raise NodeNotFound , ( "<STR_LIT>" , pattern ) <EOL> if hasattr ( default , '<STR_LIT>' ) : <EOL> while True : yield default . clone ( deep = False ) <EOL> else : <EOL> while True : yield default <EOL> Tag . _locatePatterns = _locatePatterns <EOL> def _locateOne ( name , locator , descr ) : <EOL> found = False <EOL> for node in locator ( name ) : <EOL> if found : <EOL> raise TooManyNodes ( descr , name ) <EOL> found = node <EOL> if not found : <EOL> raise NodeNotFound ( descr , name ) <EOL> return found <EOL> def specials ( tag , special ) : <EOL> """<STR_LIT>""" <EOL> for childOrContext in getattr ( tag , '<STR_LIT>' , [ ] ) : <EOL> child = getattr ( childOrContext , '<STR_LIT>' , childOrContext ) <EOL> if getattr ( child , special , Unset ) is not Unset : <EOL> yield child <EOL> else : <EOL> for match in specials ( child , special ) : <EOL> yield match <EOL> def specialMatches ( tag , special , pattern ) : <EOL> """<STR_LIT>""" <EOL> for childOrContext in getattr ( tag , '<STR_LIT>' , [ ] ) : <EOL> child = getattr ( childOrContext , '<STR_LIT>' , childOrContext ) <EOL> data = getattr ( child , special , Unset ) <EOL> if data == pattern : <EOL> yield child <EOL> elif data is Unset : <EOL> for match in specialMatches ( child , special , pattern ) : <EOL> yield match <EOL> class CommentProto ( Proto ) : <EOL> __slots__ = [ ] <EOL> def __call__ ( self , ** kw ) : <EOL> return Comment ( self ) ( ** kw ) <EOL> def __getitem__ ( self , children ) : <EOL> return Comment ( self ) [ children ] <EOL> class Comment ( Tag ) : <EOL> def __call__ ( self , ** kw ) : <EOL> raise NotImplementedError ( '<STR_LIT>' ) <EOL> invisible = Proto ( '<STR_LIT>' ) <EOL> class Entity ( object ) : <EOL> def __init__ ( self , name , num , description ) : <EOL> self . name = name <EOL> self . num = num <EOL> self . description = description <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" % ( self . name , self . num , self . description ) </s>
