<s> """<STR_LIT>""" <EOL> try : <EOL> reduce <EOL> except NameError : <EOL> from functools import reduce <EOL> import sys <EOL> from itertools import chain <EOL> import operator <EOL> from genshi . util import plaintext , stripentities , striptags , stringrepr <EOL> __all__ = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' ] <EOL> __docformat__ = '<STR_LIT>' <EOL> class StreamEventKind ( str ) : <EOL> """<STR_LIT>""" <EOL> __slots__ = [ ] <EOL> _instances = { } <EOL> def __new__ ( cls , val ) : <EOL> return cls . _instances . setdefault ( val , str . __new__ ( cls , val ) ) <EOL> class Stream ( object ) : <EOL> """<STR_LIT>""" <EOL> __slots__ = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> START = StreamEventKind ( '<STR_LIT>' ) <EOL> END = StreamEventKind ( '<STR_LIT>' ) <EOL> TEXT = StreamEventKind ( '<STR_LIT>' ) <EOL> XML_DECL = StreamEventKind ( '<STR_LIT>' ) <EOL> DOCTYPE = StreamEventKind ( '<STR_LIT>' ) <EOL> START_NS = StreamEventKind ( '<STR_LIT>' ) <EOL> END_NS = StreamEventKind ( '<STR_LIT>' ) <EOL> START_CDATA = StreamEventKind ( '<STR_LIT>' ) <EOL> END_CDATA = StreamEventKind ( '<STR_LIT>' ) <EOL> PI = StreamEventKind ( '<STR_LIT>' ) <EOL> COMMENT = StreamEventKind ( '<STR_LIT>' ) <EOL> def __init__ ( self , events , serializer = None ) : <EOL> """<STR_LIT>""" <EOL> self . events = events <EOL> self . serializer = serializer <EOL> def __iter__ ( self ) : <EOL> return iter ( self . events ) <EOL> def __or__ ( self , function ) : <EOL> """<STR_LIT>""" <EOL> return Stream ( _ensure ( function ( self ) ) , serializer = self . serializer ) <EOL> def filter ( self , * filters ) : <EOL> """<STR_LIT>""" <EOL> return reduce ( operator . or_ , ( self , ) + filters ) <EOL> def render ( self , method = None , encoding = None , out = None , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> from genshi . output import encode <EOL> if method is None : <EOL> method = self . serializer or '<STR_LIT>' <EOL> generator = self . serialize ( method = method , ** kwargs ) <EOL> return encode ( generator , method = method , encoding = encoding , out = out ) <EOL> def select ( self , path , namespaces = None , variables = None ) : <EOL> """<STR_LIT>""" <EOL> from genshi . path import Path <EOL> return Path ( path ) . select ( self , namespaces , variables ) <EOL> def serialize ( self , method = '<STR_LIT>' , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> from genshi . output import get_serializer <EOL> if method is None : <EOL> method = self . serializer or '<STR_LIT>' <EOL> return get_serializer ( method , ** kwargs ) ( _ensure ( self ) ) <EOL> def __str__ ( self ) : <EOL> return self . render ( ) <EOL> def __unicode__ ( self ) : <EOL> return self . render ( encoding = None ) <EOL> def __html__ ( self ) : <EOL> return self <EOL> START = Stream . START <EOL> END = Stream . END <EOL> TEXT = Stream . TEXT <EOL> XML_DECL = Stream . XML_DECL <EOL> DOCTYPE = Stream . DOCTYPE <EOL> START_NS = Stream . START_NS <EOL> END_NS = Stream . END_NS <EOL> START_CDATA = Stream . START_CDATA <EOL> END_CDATA = Stream . END_CDATA <EOL> PI = Stream . PI <EOL> COMMENT = Stream . COMMENT <EOL> def _ensure ( stream ) : <EOL> """<STR_LIT>""" <EOL> stream = iter ( stream ) <EOL> event = stream . next ( ) <EOL> if type ( event ) is not tuple or len ( event ) != <NUM_LIT:3> : <EOL> for event in chain ( [ event ] , stream ) : <EOL> if hasattr ( event , '<STR_LIT>' ) : <EOL> event = event . totuple ( ) <EOL> else : <EOL> event = TEXT , unicode ( event ) , ( None , - <NUM_LIT:1> , - <NUM_LIT:1> ) <EOL> yield event <EOL> return <EOL> yield event <EOL> for event in stream : <EOL> yield event <EOL> class Attrs ( tuple ) : <EOL> """<STR_LIT>""" <EOL> __slots__ = [ ] <EOL> def __contains__ ( self , name ) : <EOL> """<STR_LIT>""" <EOL> for attr , _ in self : <EOL> if attr == name : <EOL> return True <EOL> return False <EOL> def __getitem__ ( self , i ) : <EOL> """<STR_LIT>""" <EOL> items = tuple . __getitem__ ( self , i ) <EOL> if type ( i ) is slice : <EOL> return Attrs ( items ) <EOL> return items <EOL> def __getslice__ ( self , i , j ) : <EOL> """<STR_LIT>""" <EOL> return Attrs ( tuple . __getslice__ ( self , i , j ) ) <EOL> def __or__ ( self , attrs ) : <EOL> """<STR_LIT>""" <EOL> remove = set ( [ an for an , av in attrs if av is None ] ) <EOL> replace = dict ( [ ( an , av ) for an , av in attrs <EOL> if an in self and av is not None ] ) <EOL> return Attrs ( [ ( sn , replace . get ( sn , sv ) ) for sn , sv in self <EOL> if sn not in remove ] + <EOL> [ ( an , av ) for an , av in attrs <EOL> if an not in self and an not in remove ] ) <EOL> def __repr__ ( self ) : <EOL> if not self : <EOL> return '<STR_LIT>' <EOL> return '<STR_LIT>' % '<STR_LIT:U+002CU+0020>' . join ( [ repr ( item ) for item in self ] ) <EOL> def __sub__ ( self , names ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( names , basestring ) : <EOL> names = ( names , ) <EOL> return Attrs ( [ ( name , val ) for name , val in self if name not in names ] ) <EOL> def get ( self , name , default = None ) : <EOL> """<STR_LIT>""" <EOL> for attr , value in self : <EOL> if attr == name : <EOL> return value <EOL> return default <EOL> def totuple ( self ) : <EOL> """<STR_LIT>""" <EOL> return TEXT , '<STR_LIT>' . join ( [ x [ <NUM_LIT:1> ] for x in self ] ) , ( None , - <NUM_LIT:1> , - <NUM_LIT:1> ) <EOL> class Markup ( unicode ) : <EOL> """<STR_LIT>""" <EOL> __slots__ = [ ] <EOL> def __add__ ( self , other ) : <EOL> return Markup ( unicode . __add__ ( self , escape ( other ) ) ) <EOL> def __radd__ ( self , other ) : <EOL> return Markup ( unicode . __add__ ( escape ( other ) , self ) ) <EOL> def __mod__ ( self , args ) : <EOL> if isinstance ( args , dict ) : <EOL> args = dict ( zip ( args . keys ( ) , map ( escape , args . values ( ) ) ) ) <EOL> elif isinstance ( args , ( list , tuple ) ) : <EOL> args = tuple ( map ( escape , args ) ) <EOL> else : <EOL> args = escape ( args ) <EOL> return Markup ( unicode . __mod__ ( self , args ) ) <EOL> def __mul__ ( self , num ) : <EOL> return Markup ( unicode . __mul__ ( self , num ) ) <EOL> __rmul__ = __mul__ <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" % ( type ( self ) . __name__ , unicode . __repr__ ( self ) ) <EOL> def join ( self , seq , escape_quotes = True ) : <EOL> """<STR_LIT>""" <EOL> return Markup ( unicode . join ( self , [ escape ( item , quotes = escape_quotes ) <EOL> for item in seq ] ) ) <EOL> @ classmethod <EOL> def escape ( cls , text , quotes = True ) : <EOL> """<STR_LIT>""" <EOL> if not text : <EOL> return cls ( ) <EOL> if type ( text ) is cls : <EOL> return text <EOL> if hasattr ( text , '<STR_LIT>' ) : <EOL> return cls ( text . __html__ ( ) ) <EOL> text = text . replace ( '<STR_LIT:&>' , '<STR_LIT>' ) . replace ( '<STR_LIT:<>' , '<STR_LIT>' ) . replace ( '<STR_LIT:>>' , '<STR_LIT>' ) <EOL> if quotes : <EOL> text = text . replace ( '<STR_LIT:">' , '<STR_LIT>' ) <EOL> return cls ( text ) <EOL> def unescape ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self : <EOL> return '<STR_LIT>' <EOL> return unicode ( self ) . replace ( '<STR_LIT>' , '<STR_LIT:">' ) . replace ( '<STR_LIT>' , '<STR_LIT:>>' ) . replace ( '<STR_LIT>' , '<STR_LIT:<>' ) . replace ( '<STR_LIT>' , '<STR_LIT:&>' ) <EOL> def stripentities ( self , keepxmlentities = False ) : <EOL> """<STR_LIT>""" <EOL> return Markup ( stripentities ( self , keepxmlentities = keepxmlentities ) ) <EOL> def striptags ( self ) : <EOL> """<STR_LIT>""" <EOL> return Markup ( striptags ( self ) ) <EOL> try : <EOL> from genshi . _speedups import Markup <EOL> except ImportError : <EOL> pass <EOL> escape = Markup . escape <EOL> def unescape ( text ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( text , Markup ) : <EOL> return text <EOL> return text . unescape ( ) <EOL> class Namespace ( object ) : <EOL> """<STR_LIT>""" <EOL> def __new__ ( cls , uri ) : <EOL> if type ( uri ) is cls : <EOL> return uri <EOL> return object . __new__ ( cls ) <EOL> def __getnewargs__ ( self ) : <EOL> return ( self . uri , ) <EOL> def __getstate__ ( self ) : <EOL> return self . uri <EOL> def __setstate__ ( self , uri ) : <EOL> self . uri = uri <EOL> def __init__ ( self , uri ) : <EOL> self . uri = unicode ( uri ) <EOL> def __contains__ ( self , qname ) : <EOL> return qname . namespace == self . uri <EOL> def __ne__ ( self , other ) : <EOL> return not self == other <EOL> def __eq__ ( self , other ) : <EOL> if isinstance ( other , Namespace ) : <EOL> return self . uri == other . uri <EOL> return self . uri == other <EOL> def __getitem__ ( self , name ) : <EOL> return QName ( self . uri + '<STR_LIT:}>' + name ) <EOL> __getattr__ = __getitem__ <EOL> def __hash__ ( self ) : <EOL> return hash ( self . uri ) <EOL> if sys . version_info [ <NUM_LIT:0> ] == <NUM_LIT:2> : <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % ( type ( self ) . __name__ , stringrepr ( self . uri ) ) <EOL> else : <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % ( type ( self ) . __name__ , self . uri ) <EOL> def __str__ ( self ) : <EOL> return self . uri . encode ( '<STR_LIT:utf-8>' ) <EOL> def __unicode__ ( self ) : <EOL> return self . uri <EOL> XML_NAMESPACE = Namespace ( '<STR_LIT>' ) <EOL> class QName ( unicode ) : <EOL> """<STR_LIT>""" <EOL> __slots__ = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> def __new__ ( cls , qname ) : <EOL> """<STR_LIT>""" <EOL> if type ( qname ) is cls : <EOL> return qname <EOL> qname = qname . lstrip ( '<STR_LIT:{>' ) <EOL> parts = qname . split ( '<STR_LIT:}>' , <NUM_LIT:1> ) <EOL> if len ( parts ) > <NUM_LIT:1> : <EOL> self = unicode . __new__ ( cls , '<STR_LIT>' % qname ) <EOL> self . namespace , self . localname = map ( unicode , parts ) <EOL> else : <EOL> self = unicode . __new__ ( cls , qname ) <EOL> self . namespace , self . localname = None , unicode ( qname ) <EOL> return self <EOL> def __getnewargs__ ( self ) : <EOL> return ( self . lstrip ( '<STR_LIT:{>' ) , ) <EOL> if sys . version_info [ <NUM_LIT:0> ] == <NUM_LIT:2> : <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % ( type ( self ) . __name__ , stringrepr ( self . lstrip ( '<STR_LIT:{>' ) ) ) <EOL> else : <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % ( type ( self ) . __name__ , self . lstrip ( '<STR_LIT:{>' ) ) </s>
