<s> from __future__ import with_statement <EOL> import logging <EOL> import os <EOL> import sys <EOL> import jinja2 <EOL> import scio . client <EOL> from scio import static <EOL> log = logging . getLogger ( __name__ ) <EOL> TEMPLATE = os . path . abspath ( <EOL> os . path . join ( os . path . dirname ( __file__ ) , '<STR_LIT>' ) ) <EOL> def main ( ) : <EOL> """<STR_LIT>""" <EOL> logging . basicConfig ( level = logging . DEBUG ) <EOL> for wsdl_file in sys . argv [ <NUM_LIT:1> : ] : <EOL> with open ( wsdl_file , '<STR_LIT:r>' ) as fh : <EOL> client = scio . client . Client ( fh ) <EOL> print gen ( client ) <EOL> def gen ( client , template = TEMPLATE ) : <EOL> """<STR_LIT>""" <EOL> template = jinja2 . Template ( open ( template , '<STR_LIT:r>' ) . read ( ) ) <EOL> ctx = { } <EOL> ctx [ '<STR_LIT>' ] = [ methodinfo ( getattr ( client . service , s ) . method ) <EOL> for s in dir ( client . service ) <EOL> if ( not s . startswith ( '<STR_LIT:_>' ) and not <EOL> s == '<STR_LIT>' ) ] <EOL> types = list ( sort_deps ( [ typeinfo ( entry , getattr ( client . type , entry ) ) <EOL> for entry in dir ( client . type ) <EOL> if not entry . startswith ( '<STR_LIT:_>' ) ] ) ) <EOL> types = list ( sort_deps ( types , <EOL> key = lambda t : t [ '<STR_LIT>' ] + t [ '<STR_LIT>' ] , <EOL> allow_refs = True ) ) <EOL> log . debug ( "<STR_LIT>" , [ t [ '<STR_LIT>' ] for t in types ] ) <EOL> ctx [ '<STR_LIT>' ] = mark_resolved_refs ( types ) <EOL> ctx [ '<STR_LIT>' ] = types <EOL> return template . render ( ** ctx ) <EOL> def typeinfo ( name , typecls ) : <EOL> info = { } <EOL> info [ '<STR_LIT:name>' ] = name <EOL> info [ '<STR_LIT>' ] = [ ] <EOL> info [ '<STR_LIT>' ] = static . safe_id ( typecls . __name__ ) <EOL> info [ '<STR_LIT>' ] = name != typecls . __name__ <EOL> info [ '<STR_LIT>' ] = svc_qual_classname ( typecls ) <EOL> info [ '<STR_LIT>' ] = [ dep_class ( x , info [ '<STR_LIT>' ] ) for x in typecls . __bases__ ] <EOL> info [ '<STR_LIT>' ] = [ ] <EOL> info [ '<STR_LIT>' ] = set ( ) <EOL> info [ '<STR_LIT>' ] = fields = [ ] <EOL> if hasattr ( typecls , '<STR_LIT>' ) : <EOL> info [ '<STR_LIT>' ] = typecls . _schema <EOL> quoted_fields = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> for field in quoted_fields : <EOL> if field in typecls . __dict__ : <EOL> fields . append ( ( field , repr ( getattr ( typecls , field ) ) ) ) <EOL> children = [ ] <EOL> if hasattr ( typecls , '<STR_LIT>' ) : <EOL> for ch in typecls . _children : <EOL> children . append ( static . safe_id ( ch . name ) ) <EOL> fields . append ( ( static . safe_id ( ch . name ) , <EOL> Attr ( ch . name , Ref ( dep_class ( ch . type , info [ '<STR_LIT>' ] ) ) , <EOL> ch . min , ch . max , ch . namespace ) ) <EOL> ) <EOL> if children : <EOL> fields . append ( ( '<STR_LIT>' , '<STR_LIT>' % '<STR_LIT:U+002CU+0020>' . join ( children ) ) ) <EOL> attributes = [ ] <EOL> if hasattr ( typecls , '<STR_LIT>' ) : <EOL> for ch in typecls . _attributes : <EOL> attributes . append ( static . safe_id ( ch . name ) ) <EOL> fields . append ( ( static . safe_id ( ch . name ) , <EOL> Attr ( ch . name , Ref ( dep_class ( ch . type , info [ '<STR_LIT>' ] ) ) , <EOL> ch . min , ch . max , ch . namespace ) ) <EOL> ) <EOL> if attributes : <EOL> fields . append ( ( '<STR_LIT>' , '<STR_LIT>' % '<STR_LIT:U+002CU+0020>' . join ( attributes ) ) ) <EOL> if hasattr ( typecls , '<STR_LIT>' ) : <EOL> subs = { } <EOL> for name , scls in typecls . _substitutions . items ( ) : <EOL> subs [ name ] = Ref ( dep_class ( scls , info [ '<STR_LIT>' ] ) ) <EOL> if subs : <EOL> fields . append ( ( '<STR_LIT>' , subs ) ) <EOL> type_fields = ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> for field in type_fields : <EOL> if hasattr ( typecls , field ) and getattr ( typecls , field ) is not None : <EOL> info [ field ] = Ref ( dep_class ( getattr ( typecls , field ) , info [ '<STR_LIT>' ] ) ) <EOL> fields . append ( ( field , info [ field ] ) ) <EOL> if hasattr ( typecls , '<STR_LIT>' ) : <EOL> for val in typecls . _values : <EOL> fields . append ( ( static . safe_id ( val ) , repr ( val ) ) ) <EOL> return info <EOL> def mark_resolved_refs ( types ) : <EOL> unresolved = False <EOL> for t in types : <EOL> if not t [ '<STR_LIT>' ] : <EOL> continue <EOL> for field , val in t [ '<STR_LIT>' ] : <EOL> if isinstance ( val , Attr ) : <EOL> if val . ref_type . ref_type in t [ '<STR_LIT>' ] : <EOL> val . ref_type . resolved = False <EOL> unresolved = True <EOL> elif isinstance ( val , dict ) : <EOL> for k , v in val . items ( ) : <EOL> if isinstance ( v , Ref ) : <EOL> if v . ref_type in t [ '<STR_LIT>' ] : <EOL> v . resolved = False <EOL> unresolved = True <EOL> return unresolved <EOL> class Ref ( object ) : <EOL> def __init__ ( self , ref_type ) : <EOL> self . ref_type = ref_type <EOL> self . resolved = True <EOL> def __str__ ( self ) : <EOL> if self . resolved : <EOL> return self . ref_type <EOL> else : <EOL> return '<STR_LIT>' % self . ref_type <EOL> def __repr__ ( self ) : <EOL> return self . __str__ ( ) <EOL> class Attr ( object ) : <EOL> def __init__ ( self , name , ref_type , min , max , namespace ) : <EOL> self . name = name <EOL> self . ref_type = ref_type <EOL> self . min = min <EOL> self . max = max <EOL> self . namespace = namespace <EOL> def __str__ ( self ) : <EOL> info = { <EOL> '<STR_LIT:name>' : self . name , <EOL> '<STR_LIT>' : self . min , <EOL> '<STR_LIT>' : self . max , <EOL> '<STR_LIT>' : self . namespace , <EOL> '<STR_LIT:type>' : self . ref_type <EOL> } <EOL> tpl = '<STR_LIT>' <EOL> if self . min is not None : <EOL> tpl += '<STR_LIT>' <EOL> if self . max is not None : <EOL> tpl += '<STR_LIT>' <EOL> if self . namespace is not None : <EOL> tpl += '<STR_LIT>' <EOL> tpl += '<STR_LIT:)>' <EOL> return tpl % info <EOL> def dep_class ( cls , deplist ) : <EOL> """<STR_LIT>""" <EOL> qn = qualifed_classname ( cls ) <EOL> if ( not qn . lower ( ) . startswith ( '<STR_LIT>' ) and <EOL> qn != '<STR_LIT:None>' and <EOL> qn not in __builtins__ ) : <EOL> deplist . append ( qn ) <EOL> return qn <EOL> def svc_qual_classname ( cls ) : <EOL> qn = qualifed_classname ( cls ) <EOL> if qn == '<STR_LIT:None>' : <EOL> return qn <EOL> if qn . lower ( ) . startswith ( '<STR_LIT>' ) : <EOL> return qn <EOL> return '<STR_LIT>' % qn <EOL> def qualifed_classname ( cls ) : <EOL> if cls is None : <EOL> return '<STR_LIT:None>' <EOL> if isinstance ( cls , scio . client . AnyType ) : <EOL> return '<STR_LIT>' <EOL> if cls . __name__ in dir ( scio . client ) : <EOL> return "<STR_LIT>" % cls . __name__ <EOL> return static . safe_id ( cls . __name__ ) <EOL> def sort_deps ( types , key = lambda t : t [ '<STR_LIT>' ] , allow_refs = False ) : <EOL> ready = [ ] <EOL> deps = [ ] <EOL> pushed = set ( ) <EOL> for t in types : <EOL> if key ( t ) : <EOL> deps . append ( t ) <EOL> else : <EOL> ready . append ( t ) <EOL> if not deps : <EOL> for t in types : <EOL> yield t <EOL> return <EOL> while ready : <EOL> t = ready . pop ( ) <EOL> if not t [ '<STR_LIT>' ] in pushed : <EOL> yield t <EOL> pushed . add ( t [ '<STR_LIT>' ] ) <EOL> log . debug ( "<STR_LIT>" % t [ '<STR_LIT>' ] ) <EOL> for dt in deps : <EOL> if dt [ '<STR_LIT>' ] in pushed : <EOL> continue <EOL> if _ready ( dt , key , pushed ) : <EOL> ready . append ( dt ) <EOL> log . debug ( "<STR_LIT>" % dt [ '<STR_LIT>' ] ) <EOL> not_pushed = set ( [ t [ '<STR_LIT>' ] for t in deps ] ) - pushed <EOL> if not_pushed : <EOL> log . debug ( "<STR_LIT>" , set ( not_pushed ) ) <EOL> unresolved = { } <EOL> missing_types = [ ] <EOL> for t in deps : <EOL> missing = set ( key ( t ) ) - pushed <EOL> if missing : <EOL> t [ '<STR_LIT>' ] = missing <EOL> unresolved [ t [ '<STR_LIT>' ] ] = sorted ( list ( missing ) ) <EOL> missing_types . append ( t ) <EOL> if not allow_refs : <EOL> raise RuntimeError ( "<STR_LIT>" % unresolved ) <EOL> for t in missing_types : <EOL> yield t <EOL> def _ready ( t , key , pushed ) : <EOL> if set ( key ( t ) ) - pushed : <EOL> return False <EOL> return True <EOL> def methodinfo ( m ) : <EOL> info = { '<STR_LIT:location>' : m . location , <EOL> '<STR_LIT:name>' : m . name , <EOL> '<STR_LIT:action>' : m . action , <EOL> '<STR_LIT:input>' : { '<STR_LIT>' : m . input . tag , <EOL> '<STR_LIT>' : m . input . namespace , <EOL> '<STR_LIT>' : m . input . style , <EOL> '<STR_LIT>' : m . input . literal , <EOL> '<STR_LIT>' : [ ( p [ <NUM_LIT:0> ] , svc_qual_classname ( p [ <NUM_LIT:1> ] ) ) <EOL> for p in m . input . parts ] , <EOL> '<STR_LIT>' : [ ( p [ <NUM_LIT:0> ] , svc_qual_classname ( p [ <NUM_LIT:1> ] ) ) <EOL> for p in m . input . headers ] } , <EOL> '<STR_LIT>' : { '<STR_LIT>' : m . output . tag , <EOL> '<STR_LIT>' : m . output . namespace , <EOL> '<STR_LIT>' : [ ( p [ <NUM_LIT:0> ] , svc_qual_classname ( p [ <NUM_LIT:1> ] ) ) <EOL> for p in m . output . parts ] , <EOL> '<STR_LIT>' : [ ( p [ <NUM_LIT:0> ] , svc_qual_classname ( p [ <NUM_LIT:1> ] ) ) <EOL> for p in m . output . headers ] <EOL> } <EOL> } <EOL> return info <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> main ( ) </s>
