<s> """<STR_LIT>""" <EOL> from pysphere . ZSI import _find_type , _get_element_nsuri_name , EvaluateException <EOL> from pysphere . ZSI . wstools . Utility import SplitQName <EOL> def _get_type_definition ( namespaceURI , name , ** kw ) : <EOL> return SchemaInstanceType . getTypeDefinition ( namespaceURI , name , ** kw ) <EOL> def _get_global_element_declaration ( namespaceURI , name , ** kw ) : <EOL> return SchemaInstanceType . getElementDeclaration ( namespaceURI , name , ** kw ) <EOL> def _get_substitute_element ( head , elt , ps ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( head , ElementDeclaration ) : <EOL> return None <EOL> return ElementDeclaration . getSubstitutionElement ( head , elt , ps ) <EOL> def _has_type_definition ( namespaceURI , name ) : <EOL> return SchemaInstanceType . getTypeDefinition ( namespaceURI , name ) is not None <EOL> def _is_substitute_element ( head , sub ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( head , ElementDeclaration ) or not isinstance ( sub , ElementDeclaration ) : <EOL> return False <EOL> try : <EOL> group = sub . substitutionGroup <EOL> except ( AttributeError , TypeError ) : <EOL> return False <EOL> ged = GED ( * group ) <EOL> print ( head . nspname == ged . nspname and head . pname == ged . pname ) <EOL> if head is ged or not ( head . nspname == ged . nspname and head . pname == ged . pname ) : <EOL> return False <EOL> return True <EOL> GED = _get_global_element_declaration <EOL> GTD = _get_type_definition <EOL> def WrapImmutable ( pyobj , what ) : <EOL> """<STR_LIT>""" <EOL> return _GetPyobjWrapper . WrapImmutable ( pyobj , what ) <EOL> def RegisterBuiltin ( arg ) : <EOL> """<STR_LIT>""" <EOL> _GetPyobjWrapper . RegisterBuiltin ( arg ) <EOL> _GetPyobjWrapper . RegisterAnyElement ( ) <EOL> def RegisterAnyElement ( ) : <EOL> """<STR_LIT>""" <EOL> return _GetPyobjWrapper . RegisterAnyElement ( ) <EOL> class SchemaInstanceType ( type ) : <EOL> """<STR_LIT>""" <EOL> types = { } <EOL> elements = { } <EOL> element_typecode_cache = { } <EOL> def __new__ ( cls , classname , bases , classdict ) : <EOL> """<STR_LIT>""" <EOL> if classname in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , ] : <EOL> return type . __new__ ( cls , classname , bases , classdict ) <EOL> if ElementDeclaration in bases : <EOL> if not '<STR_LIT>' in classdict or not '<STR_LIT>' in classdict : <EOL> raise AttributeError ( '<STR_LIT>' ) <EOL> key = ( classdict [ '<STR_LIT>' ] , classdict [ '<STR_LIT>' ] ) <EOL> if key in SchemaInstanceType . elements : <EOL> return SchemaInstanceType . elements [ key ] <EOL> ged = SchemaInstanceType . elements [ key ] = type . __new__ ( cls , classname , bases , classdict ) <EOL> return ged <EOL> if TypeDefinition in bases : <EOL> if not '<STR_LIT:type>' in classdict : <EOL> raise AttributeError ( '<STR_LIT>' ) <EOL> key = classdict [ '<STR_LIT:type>' ] <EOL> if not key in SchemaInstanceType . types : <EOL> SchemaInstanceType . types [ key ] = type . __new__ ( cls , classname , bases , classdict ) <EOL> return SchemaInstanceType . types [ key ] <EOL> if LocalElementDeclaration in bases : <EOL> return type . __new__ ( cls , classname , bases , classdict ) <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> def getTypeDefinition ( cls , namespaceURI , name , lazy = False ) : <EOL> """<STR_LIT>""" <EOL> if namespaceURI is None : namespaceURI = "<STR_LIT>" <EOL> klass = cls . types . get ( ( namespaceURI , name ) , None ) <EOL> if lazy and klass is not None : <EOL> return _Mirage ( klass ) <EOL> return klass <EOL> getTypeDefinition = classmethod ( getTypeDefinition ) <EOL> def getElementDeclaration ( cls , namespaceURI , name , isref = False , lazy = False ) : <EOL> """<STR_LIT>""" <EOL> key = ( namespaceURI , name ) <EOL> if isref : <EOL> klass = cls . elements . get ( key , None ) <EOL> if klass is not None and lazy is True : <EOL> return _Mirage ( klass ) <EOL> return klass <EOL> typecode = cls . element_typecode_cache . get ( key , None ) <EOL> if typecode is None : <EOL> tcls = cls . elements . get ( key , None ) <EOL> if tcls is not None : <EOL> typecode = cls . element_typecode_cache [ key ] = tcls ( ) <EOL> typecode . typed = False <EOL> return typecode <EOL> getElementDeclaration = classmethod ( getElementDeclaration ) <EOL> class ElementDeclaration : <EOL> """<STR_LIT>""" <EOL> __metaclass__ = SchemaInstanceType <EOL> def checkSubstitute ( self , typecode ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( typecode , ElementDeclaration ) : <EOL> return False <EOL> try : <EOL> nsuri , ncname = typecode . substitutionGroup <EOL> except ( AttributeError , TypeError ) : <EOL> return False <EOL> if ( nsuri , ncname ) != ( self . schema , self . literal ) : <EOL> if not nsuri and not self . schema and ncname == self . literal : <EOL> return True <EOL> return False <EOL> sub = GED ( self . schema , self . literal ) <EOL> if sub is None or sub is not typecode : <EOL> return False <EOL> return True <EOL> def getSubstitutionElement ( self , elt , ps ) : <EOL> """<STR_LIT>""" <EOL> nsuri , ncname = _get_element_nsuri_name ( elt ) <EOL> typecode = GED ( nsuri , ncname ) <EOL> if typecode is None : <EOL> return <EOL> try : <EOL> nsuri , ncname = typecode . substitutionGroup <EOL> except ( AttributeError , TypeError ) : <EOL> return <EOL> if ( ncname == self . pname ) and ( nsuri == self . nspname or <EOL> ( not nsuri and not self . nspname ) ) : <EOL> return typecode <EOL> return <EOL> class LocalElementDeclaration : <EOL> """<STR_LIT>""" <EOL> __metaclass__ = SchemaInstanceType <EOL> class TypeDefinition : <EOL> """<STR_LIT>""" <EOL> __metaclass__ = SchemaInstanceType <EOL> def getSubstituteType ( self , elt , ps ) : <EOL> """<STR_LIT>""" <EOL> pyclass = SchemaInstanceType . getTypeDefinition ( * self . type ) <EOL> if pyclass is None : <EOL> raise EvaluateException ( <EOL> '<STR_LIT>' % <EOL> ( self . type [ <NUM_LIT:0> ] , self . type [ <NUM_LIT:1> ] ) , ps . Backtrace ( elt ) ) <EOL> typeName = _find_type ( elt ) <EOL> prefix , typeName = SplitQName ( typeName ) <EOL> uri = ps . GetElementNSdict ( elt ) . get ( prefix ) <EOL> subclass = SchemaInstanceType . getTypeDefinition ( uri , typeName ) <EOL> if subclass is None : <EOL> raise EvaluateException ( <EOL> '<STR_LIT>' % <EOL> ( uri , typeName , self . type [ <NUM_LIT:0> ] , self . type [ <NUM_LIT:1> ] ) , ps . Backtrace ( elt ) ) <EOL> if not issubclass ( subclass , pyclass ) and subclass ( None ) and not issubclass ( subclass , pyclass ) : <EOL> raise TypeError ( <EOL> '<STR_LIT>' % <EOL> ( self . type [ <NUM_LIT:0> ] , self . type [ <NUM_LIT:1> ] , pyclass ) , ps . Backtrace ( elt ) ) <EOL> return subclass ( ( self . nspname , self . pname ) ) <EOL> class _Mirage : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , klass ) : <EOL> self . klass = klass <EOL> self . __reveal = False <EOL> self . __cache = None <EOL> if issubclass ( klass , ElementDeclaration ) : <EOL> self . __call__ = self . _hide_element <EOL> def __str__ ( self ) : <EOL> msg = "<STR_LIT>" <EOL> if issubclass ( self . klass , ElementDeclaration ) : <EOL> msg = "<STR_LIT>" <EOL> return msg % ( id ( self ) , self . klass ) <EOL> def _hide_type ( self , pname , aname , minOccurs = <NUM_LIT:0> , maxOccurs = <NUM_LIT:1> , nillable = False , <EOL> ** kw ) : <EOL> self . __call__ = self . _reveal_type <EOL> self . __reveal = True <EOL> self . __kw = kw <EOL> self . minOccurs , self . maxOccurs , self . nillable = minOccurs , maxOccurs , nillable <EOL> self . nspname , self . pname , self . aname = None , pname , aname <EOL> if isinstance ( self . pname , ( tuple , list ) ) : <EOL> self . nspname , self . pname = pname <EOL> return self <EOL> def _hide_element ( self , minOccurs = <NUM_LIT:0> , maxOccurs = <NUM_LIT:1> , nillable = False , ** kw ) : <EOL> self . __call__ = self . _reveal_element <EOL> self . __reveal = True <EOL> self . __kw = kw <EOL> self . nspname = self . klass . schema <EOL> self . pname = self . klass . literal <EOL> self . minOccurs , self . maxOccurs , self . nillable = minOccurs , maxOccurs , nillable <EOL> return self <EOL> def _reveal_type ( self ) : <EOL> if self . __cache is None : <EOL> pname = self . pname <EOL> if self . nspname != None : <EOL> pname = ( self . nspname , self . pname ) <EOL> self . __cache = self . klass ( pname = pname , <EOL> aname = self . aname , minOccurs = self . minOccurs , <EOL> maxOccurs = self . maxOccurs , nillable = self . nillable , <EOL> ** self . __kw ) <EOL> return self . __cache <EOL> def _reveal_element ( self ) : <EOL> if self . __cache is None : <EOL> self . __cache = self . klass ( minOccurs = self . minOccurs , <EOL> maxOccurs = self . maxOccurs , nillable = self . nillable , <EOL> ** self . __kw ) <EOL> return self . __cache <EOL> __call__ = _hide_type <EOL> class _GetPyobjWrapper : <EOL> """<STR_LIT>""" <EOL> types_dict = dict ( ) <EOL> def RegisterBuiltin ( cls , arg ) : <EOL> """<STR_LIT>""" <EOL> if arg in cls . types_dict : <EOL> raise RuntimeError ( '<STR_LIT>' % arg ) <EOL> class _Wrapper ( arg ) : <EOL> '<STR_LIT>' % ( arg , cls . __doc__ ) <EOL> _Wrapper . __name__ = '<STR_LIT>' % arg . __name__ <EOL> cls . types_dict [ arg ] = _Wrapper <EOL> RegisterBuiltin = classmethod ( RegisterBuiltin ) <EOL> def RegisterAnyElement ( cls ) : <EOL> """<STR_LIT>""" <EOL> for k , v in cls . types_dict . iteritems ( ) : <EOL> what = Any . serialmap . get ( k ) <EOL> if what is None : continue <EOL> if v in what . __class__ . seriallist : continue <EOL> what . __class__ . seriallist . append ( v ) <EOL> RegisterType ( what . __class__ , clobber = <NUM_LIT:1> , ** what . __dict__ ) <EOL> RegisterAnyElement = classmethod ( RegisterAnyElement ) <EOL> def WrapImmutable ( cls , pyobj , what ) : <EOL> """<STR_LIT>""" <EOL> d = cls . types_dict <EOL> if isinstance ( pyobj , bool ) : <EOL> pyclass = d [ int ] <EOL> elif type ( pyobj ) in d : <EOL> pyclass = d [ type ( pyobj ) ] <EOL> else : <EOL> raise TypeError ( <EOL> '<STR_LIT>' % ( <EOL> d . keys ( ) , type ( pyobj ) ) ) <EOL> newobj = pyclass ( pyobj ) <EOL> newobj . typecode = what <EOL> return newobj <EOL> WrapImmutable = classmethod ( WrapImmutable ) <EOL> from TC import Any , RegisterType </s>
