<s> """<STR_LIT>""" <EOL> try : <EOL> import cStringIO as StringIO <EOL> except ImportError : <EOL> import StringIO <EOL> class LiteralValue ( str ) : <EOL> def value ( self , elem ) : <EOL> return self <EOL> class IndexValue : <EOL> def __init__ ( self , index ) : <EOL> self . index = int ( index ) - <NUM_LIT:1> <EOL> def value ( self , elem ) : <EOL> return elem . children [ self . index ] <EOL> class AttribValue : <EOL> def __init__ ( self , attribname ) : <EOL> self . attribname = attribname <EOL> if self . attribname == "<STR_LIT>" : <EOL> self . value = self . value_ns <EOL> def value_ns ( self , elem ) : <EOL> return elem . uri <EOL> def value ( self , elem ) : <EOL> if self . attribname in elem . attributes : <EOL> return elem . attributes [ self . attribname ] <EOL> else : <EOL> return None <EOL> class CompareValue : <EOL> def __init__ ( self , lhs , op , rhs ) : <EOL> self . lhs = lhs <EOL> self . rhs = rhs <EOL> if op == "<STR_LIT:=>" : <EOL> self . value = self . _compareEqual <EOL> else : <EOL> self . value = self . _compareNotEqual <EOL> def _compareEqual ( self , elem ) : <EOL> return self . lhs . value ( elem ) == self . rhs . value ( elem ) <EOL> def _compareNotEqual ( self , elem ) : <EOL> return self . lhs . value ( elem ) != self . rhs . value ( elem ) <EOL> class BooleanValue : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , lhs , op , rhs ) : <EOL> self . lhs = lhs <EOL> self . rhs = rhs <EOL> if op == "<STR_LIT>" : <EOL> self . value = self . _booleanAnd <EOL> else : <EOL> self . value = self . _booleanOr <EOL> def _booleanAnd ( self , elem ) : <EOL> """<STR_LIT>""" <EOL> return self . lhs . value ( elem ) and self . rhs . value ( elem ) <EOL> def _booleanOr ( self , elem ) : <EOL> """<STR_LIT>""" <EOL> return self . lhs . value ( elem ) or self . rhs . value ( elem ) <EOL> def Function ( fname ) : <EOL> """<STR_LIT>""" <EOL> klassname = "<STR_LIT>" % fname <EOL> c = globals ( ) [ klassname ] ( ) <EOL> return c <EOL> class _not_Function : <EOL> def __init__ ( self ) : <EOL> self . baseValue = None <EOL> def setParams ( self , baseValue ) : <EOL> self . baseValue = baseValue <EOL> def value ( self , elem ) : <EOL> return not self . baseValue . value ( elem ) <EOL> class _text_Function : <EOL> def setParams ( self ) : <EOL> pass <EOL> def value ( self , elem ) : <EOL> return str ( elem ) <EOL> class _Location : <EOL> def __init__ ( self ) : <EOL> self . predicates = [ ] <EOL> self . elementName = None <EOL> self . childLocation = None <EOL> def matchesPredicates ( self , elem ) : <EOL> if self . elementName != None and self . elementName != elem . name : <EOL> return <NUM_LIT:0> <EOL> for p in self . predicates : <EOL> if not p . value ( elem ) : <EOL> return <NUM_LIT:0> <EOL> return <NUM_LIT:1> <EOL> def matches ( self , elem ) : <EOL> if not self . matchesPredicates ( elem ) : <EOL> return <NUM_LIT:0> <EOL> if self . childLocation != None : <EOL> for c in elem . elements ( ) : <EOL> if self . childLocation . matches ( c ) : <EOL> return <NUM_LIT:1> <EOL> else : <EOL> return <NUM_LIT:1> <EOL> return <NUM_LIT:0> <EOL> def queryForString ( self , elem , resultbuf ) : <EOL> if not self . matchesPredicates ( elem ) : <EOL> return <EOL> if self . childLocation != None : <EOL> for c in elem . elements ( ) : <EOL> self . childLocation . queryForString ( c , resultbuf ) <EOL> else : <EOL> resultbuf . write ( str ( elem ) ) <EOL> def queryForNodes ( self , elem , resultlist ) : <EOL> if not self . matchesPredicates ( elem ) : <EOL> return <EOL> if self . childLocation != None : <EOL> for c in elem . elements ( ) : <EOL> self . childLocation . queryForNodes ( c , resultlist ) <EOL> else : <EOL> resultlist . append ( elem ) <EOL> def queryForStringList ( self , elem , resultlist ) : <EOL> if not self . matchesPredicates ( elem ) : <EOL> return <EOL> if self . childLocation != None : <EOL> for c in elem . elements ( ) : <EOL> self . childLocation . queryForStringList ( c , resultlist ) <EOL> else : <EOL> for c in elem . children : <EOL> if isinstance ( c , ( str , unicode ) ) : <EOL> resultlist . append ( c ) <EOL> class _AnyLocation : <EOL> def __init__ ( self ) : <EOL> self . predicates = [ ] <EOL> self . elementName = None <EOL> self . childLocation = None <EOL> def matchesPredicates ( self , elem ) : <EOL> for p in self . predicates : <EOL> if not p . value ( elem ) : <EOL> return <NUM_LIT:0> <EOL> return <NUM_LIT:1> <EOL> def listParents ( self , elem , parentlist ) : <EOL> if elem . parent != None : <EOL> self . listParents ( elem . parent , parentlist ) <EOL> parentlist . append ( elem . name ) <EOL> def isRootMatch ( self , elem ) : <EOL> if ( self . elementName == None or self . elementName == elem . name ) and self . matchesPredicates ( elem ) : <EOL> if self . childLocation != None : <EOL> for c in elem . elements ( ) : <EOL> if self . childLocation . matches ( c ) : <EOL> return True <EOL> else : <EOL> return True <EOL> return False <EOL> def findFirstRootMatch ( self , elem ) : <EOL> if ( self . elementName == None or self . elementName == elem . name ) and self . matchesPredicates ( elem ) : <EOL> if self . childLocation != None : <EOL> for c in elem . elements ( ) : <EOL> if self . childLocation . matches ( c ) : <EOL> return c <EOL> return None <EOL> else : <EOL> return elem <EOL> else : <EOL> for c in elem . elements ( ) : <EOL> if self . matches ( c ) : <EOL> return c <EOL> return None <EOL> def matches ( self , elem ) : <EOL> if self . isRootMatch ( elem ) : <EOL> return True <EOL> else : <EOL> for c in elem . elements ( ) : <EOL> if self . matches ( c ) : <EOL> return True <EOL> return False <EOL> def queryForString ( self , elem , resultbuf ) : <EOL> raise NotImplementedError ( <EOL> "<STR_LIT>" ) <EOL> def queryForNodes ( self , elem , resultlist ) : <EOL> if self . isRootMatch ( elem ) : <EOL> resultlist . append ( elem ) <EOL> for c in elem . elements ( ) : <EOL> self . queryForNodes ( c , resultlist ) <EOL> def queryForStringList ( self , elem , resultlist ) : <EOL> if self . isRootMatch ( elem ) : <EOL> for c in elem . children : <EOL> if isinstance ( c , ( str , unicode ) ) : <EOL> resultlist . append ( c ) <EOL> for c in elem . elements ( ) : <EOL> self . queryForStringList ( c , resultlist ) <EOL> class XPathQuery : <EOL> def __init__ ( self , queryStr ) : <EOL> self . queryStr = queryStr <EOL> from twisted . words . xish . xpathparser import parse <EOL> self . baseLocation = parse ( '<STR_LIT>' , queryStr ) <EOL> def __hash__ ( self ) : <EOL> return self . queryStr . __hash__ ( ) <EOL> def matches ( self , elem ) : <EOL> return self . baseLocation . matches ( elem ) <EOL> def queryForString ( self , elem ) : <EOL> result = StringIO . StringIO ( ) <EOL> self . baseLocation . queryForString ( elem , result ) <EOL> return result . getvalue ( ) <EOL> def queryForNodes ( self , elem ) : <EOL> result = [ ] <EOL> self . baseLocation . queryForNodes ( elem , result ) <EOL> if len ( result ) == <NUM_LIT:0> : <EOL> return None <EOL> else : <EOL> return result <EOL> def queryForStringList ( self , elem ) : <EOL> result = [ ] <EOL> self . baseLocation . queryForStringList ( elem , result ) <EOL> if len ( result ) == <NUM_LIT:0> : <EOL> return None <EOL> else : <EOL> return result <EOL> __internedQueries = { } <EOL> def internQuery ( queryString ) : <EOL> if queryString not in __internedQueries : <EOL> __internedQueries [ queryString ] = XPathQuery ( queryString ) <EOL> return __internedQueries [ queryString ] <EOL> def matches ( xpathstr , elem ) : <EOL> return internQuery ( xpathstr ) . matches ( elem ) <EOL> def queryForStringList ( xpathstr , elem ) : <EOL> return internQuery ( xpathstr ) . queryForStringList ( elem ) <EOL> def queryForString ( xpathstr , elem ) : <EOL> return internQuery ( xpathstr ) . queryForString ( elem ) <EOL> def queryForNodes ( xpathstr , elem ) : <EOL> return internQuery ( xpathstr ) . queryForNodes ( elem ) </s>
