ó
l„~Lc           @   sÜ   d  Z  d d l Z d d l Td d l Td d l Td d l m Z d d l m Z d d l	 m
 Z
 d d l m Z d d l m Z d d l m Z m Z d d	 l m Z e e ƒ Z d
 d d „  ƒ  YZ d d d „  ƒ  YZ d S(   sL  
The I{schema} module provides a intelligent representation of
an XSD schema.  The I{raw} model is the XML tree and the I{model}
is the denormalized, objectified and intelligent view of the schema.
Most of the I{value-add} provided by the model is centered around
tranparent referenced type resolution and targeted denormalization.
iÿÿÿÿN(   t   *(   t   Factory(   t   SchemaObject(   t   DepList(   t   Element(   t   splitPrefixt	   Namespace(   t	   getLoggert   SchemaCollectionc           B   s_   e  Z d  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 d	 „  Z RS(
   s‚  
    A collection of schema objects.  This class is needed because WSDLs 
    may contain more then one <schema/> node.
    @ivar wsdl: A wsdl object.
    @type wsdl: L{suds.wsdl.Definitions}
    @ivar children: A list contained schemas.
    @type children: [L{Schema},...]
    @ivar namespaces: A dictionary of contained schemas by namespace.
    @type namespaces: {str:L{Schema}}
    c         C   s   | |  _  g  |  _ i  |  _ d S(   sZ   
        @param wsdl: A wsdl object.
        @type wsdl: L{suds.wsdl.Definitions}
        N(   t   wsdlt   childrent
   namespaces(   t   selfR	   (    (    sI   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/suds/xsd/schema.pyt   __init__5   s    		c         C   s€   | j  d } |  j j | ƒ } | d k rK |  j j | ƒ | |  j | <n1 | j j | j j 7_ | j j j | j j ƒ d S(   sÆ   
        Add a schema node to the collection.  Schema(s) within the same target
        namespace are consolidated.
        @param schema: A schema object.
        @type schema: (L{Schema})
        i   N(	   t   tnsR   t   gett   NoneR
   t   appendt   roott
   nsprefixest   update(   R   t   schemat   keyt   existing(    (    sI   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/suds/xsd/schema.pyt   add>   s    c         C   s£   | j  r |  j  ƒ  n  x |  j D] } | j ƒ  q  Wx |  j D] } | j | ƒ q> Wx |  j D] } | j ƒ  q_ Wt j d |  ƒ |  j ƒ  } t j d | ƒ | S(   s  
        Load the schema objects for the root nodes.
            - de-references schemas
            - merge schemas
        @param options: An options dictionary.
        @type options: L{options.Options}
        @return: The merged schema.
        @rtype: L{Schema}
        s
   loaded:
%ss
   MERGED:
%s(   t	   autoblendR
   t   buildt   open_importst   dereferencet   logt   debugt   merge(   R   t   optionst   childt   merged(    (    sI   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/suds/xsd/schema.pyt   loadN   s    
	c         C   sÉ   |  j  j ƒ  } x³ |  j D]¨ } xŸ | D]— } | j j d ƒ } | | k rP q& n  x5 | j j d ƒ D]! } | j d ƒ | k rc qc qc qc Wt d d t j ƒ} | j	 d | ƒ | j j
 | ƒ q& Wq W|  S(   s°   
        Ensure that all schemas within the collection
        import each other which has a blending effect.
        @return: self
        @rtype: L{SchemaCollection}
        t   targetNamespacet   importt	   namespacet   ns(   R   t   keysR
   R   R   t   getChildrenR   R   t   xsdnst   setR   (   R   R   t   sR'   R   t   imp(    (    sI   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/suds/xsd/schema.pyR   e   s    
c         C   s   |  j  j | d ƒ S(   s$  
        Find a schema by namespace.  Only the URI portion of
        the namespace is compared to each schema's I{targetNamespace}
        @param ns: A namespace.
        @type ns: (prefix,URI)
        @return: The schema matching the namesapce, else None.
        @rtype: L{Schema}
        i   (   R   R   (   R   R'   (    (    sI   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/suds/xsd/schema.pyt   locatez   s    	c         C   sJ   t  |  ƒ rB |  j d } x" |  j d D] } | j | ƒ q' W| Sd Sd S(   su   
        Merge the contained schemas into one.
        @return: The merged schema.
        @rtype: L{Schema}
        i    i   N(   t   lenR
   R   R   (   R   R   R,   (    (    sI   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/suds/xsd/schema.pyR   …   s    c         C   s   t  |  j ƒ S(   N(   R/   R
   (   R   (    (    sI   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/suds/xsd/schema.pyt   __len__“   s    c         C   s   t  |  ƒ j d ƒ S(   Ns   utf-8(   t   unicodet   encode(   R   (    (    sI   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/suds/xsd/schema.pyt   __str__–   s    c         C   s@   d g } x' |  j  D] } | j | j d ƒ ƒ q Wd j | ƒ S(   Ns   
schema collectioni   s   
(   R
   R   t   strt   join(   R   t   resultR,   (    (    sI   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/suds/xsd/schema.pyt   __unicode__™   s    	(   t   __name__t
   __module__t   __doc__R   R   R#   R   R.   R   R0   R3   R7   (    (    (    sI   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/suds/xsd/schema.pyR   )   s   
									t   Schemac           B   sž   e  Z d  Z d Z d d „ Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 d „  Z d d	 „ Z d d
 „ Z d „  Z d d „ Z d „  Z d „  Z d „  Z RS(   sÉ  
    The schema is an objectification of a <schema/> (xsd) definition.
    It provides inspection, lookup and type resolution.
    @ivar root: The root node.
    @type root: L{sax.element.Element}
    @ivar baseurl: The I{base} URL for this schema.
    @type baseurl: str
    @ivar container: A schema collection containing this schema.
    @type container: L{SchemaCollection}
    @ivar children: A list of direct top level children.
    @type children: [L{SchemaObject},...]
    @ivar all: A list of all (includes imported) top level children.
    @type all: [L{SchemaObject},...]
    @ivar types: A schema types cache.
    @type types: {name:L{SchemaObject}}
    @ivar imports: A list of import objects.
    @type imports: [L{SchemaObject},...]
    @ivar elements: A list of <element/> objects.
    @type elements: [L{SchemaObject},...]
    @ivar attributes: A list of <attribute/> objects.
    @type attributes: [L{SchemaObject},...]
    @ivar groups: A list of group objects.
    @type groups: [L{SchemaObject},...]
    @ivar agrps: A list of attribute group objects.
    @type agrps: [L{SchemaObject},...]
    @ivar form_qualified: The flag indicating:
        (@elementFormDefault).
    @type form_qualified: bool
    R   c         C   s0  | |  _  t |  ƒ |  _ |  j ƒ  |  _ | |  _ | |  _ g  |  _ g  |  _ i  |  _	 g  |  _
 i  |  _ i  |  _ i  |  _ i  |  _ | j d k	 r£ | j j | ƒ n  |  j  j d ƒ } | d k rÍ t |  _ n | d k |  _ | d k r,|  j ƒ  |  j | ƒ t j d |  ƒ |  j ƒ  t j d |  ƒ n  d S(   si  
        @param root: The xml root.
        @type root: L{sax.element.Element}
        @param baseurl: The base url used for importing.
        @type baseurl: basestring
        @param options: An options dictionary.
        @type options: L{options.Options}
        @param container: An optional container.
        @type container: L{SchemaCollection}
        t   elementFormDefaultt	   qualifieds	   built:
%ss   dereferenced:
%sN(   R   t   objidt   idt   mktnsR   t   baseurlt	   containerR
   t   allt   typest   importst   elementst
   attributest   groupst   agrpst   doctorR   t   examineR   t   Falset   form_qualifiedR   R   R   R   R   (   R   R   RA   R    RB   t   form(    (    sI   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/suds/xsd/schema.pyR   Á   s2    											

c         C   sO   d |  j j d ƒ g } | d d k	 rE |  j j | d ƒ | d <n  t | ƒ S(   s´   
        Make the schema's target namespace.
        @return: The namespace representation of the schema's
            targetNamespace value.
        @rtype: (prefix, uri)
        R$   i   i    N(   R   R   R   t
   findPrefixt   tuple(   R   R   (    (    sI   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/suds/xsd/schema.pyR@   ç   s    c         C   s‰   t  j |  j |  ƒ |  _ t  j |  j ƒ } | d |  _ | d |  _ | d |  _ | d |  _ | d |  _ | d |  _	 | d |  _
 d S(	   s£   
        Build the schema (object graph) using the root node
        using the factory.
            - Build the graph.
            - Collate the children.
        i    i   i   i   i   i   i   N(   t   BasicFactoryR   R   R
   t   collateRG   RE   RF   RD   RH   RI   (   R   t   collated(    (    sI   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/suds/xsd/schema.pyR   ó   s    c         C   sÙ  xY | j  j ƒ  D]H } | d |  j  k r/ q n  |  j j | d ƒ | d |  j  | d <q WxY | j j ƒ  D]H } | d |  j k r‹ ql n  |  j j | d ƒ | d |  j | d <ql WxY | j j ƒ  D]H } | d |  j k rç qÈ n  |  j j | d ƒ | d |  j | d <qÈ WxY | j j ƒ  D]H } | d |  j k rCq$n  |  j j | d ƒ | d |  j | d <q$WxY | j j ƒ  D]H } | d |  j k rŸq€n  |  j j | d ƒ | d |  j | d <q€Wt | _	 |  S(   s  
        Merge the contents from the schema.  Only objects not already contained
        in this schema's collections are merged.  This is to provide for bidirectional
        import which produce cyclic includes.
        @returns: self
        @rtype: L{Schema} 
        i    i   (
   RG   t   itemsRC   R   RF   RD   RH   RI   t   TrueR"   (   R   R   t   item(    (    sI   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/suds/xsd/schema.pyR     s6    	c         C   sc   x\ |  j  D]Q } | j | ƒ } | d k r1 q
 n  | j | ƒ t j d | ƒ |  j | ƒ q
 Wd S(   s  
        Instruct all contained L{sxbasic.Import} children to import
        the schema's which they reference.  The contents of the
        imported schema are I{merged} in.
        @param options: An options dictionary.
        @type options: L{options.Options}
        s   imported:
%sN(   RE   t   openR   R   R   R   R   (   R   R    R-   t   imported(    (    sI   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/suds/xsd/schema.pyR   (  s    c   
      C   s  g  } i  } x |  j  D] } | j | ƒ q Wt ƒ  } xS | D]K } | j ƒ  | j ƒ  \ } } | t | ƒ f } | j | ƒ | | | <q= Wx{ | j ƒ  D]m \ } } | j | ƒ } | d k rÆ q™ n  | | }	 t
 j d |  j d t | ƒ t |	 ƒ ƒ | j |	 ƒ q™ Wd S(   sA   
        Instruct all children to perform dereferencing.
        s   (%s) merging %s <== %si   N(   R
   t   contentR   t   qualifyt   dependenciesRP   R   t   sortR   R   R   R   R   t   ReprR   (
   R   RC   t   indexesR!   t   deplistt   xt   midxt   depsRV   t   d(    (    sI   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/suds/xsd/schema.pyR   8  s$    	
 
)c         C   s'   |  j  d k	 r |  j  j | ƒ Sd Sd S(   sU  
        Find a schema by namespace.  Only the URI portion of
        the namespace is compared to each schema's I{targetNamespace}.
        The request is passed to the container.
        @param ns: A namespace.
        @type ns: (prefix,URI)
        @return: The schema matching the namesapce, else None.
        @rtype: L{Schema}
        N(   RB   R   R.   (   R   R'   (    (    sI   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/suds/xsd/schema.pyR.   N  s    
c         C   s%   | d k r t S|  j | | ƒ Sd S(   sÞ   
        Get whether the specified reference is B{not} an (xs) builtin.
        @param ref: A str or qref.
        @type ref: (str|qref)
        @return: True if B{not} a builtin, else False.
        @rtype: bool 
        N(   R   RU   t   builtin(   R   t   reft   context(    (    sI   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/suds/xsd/schema.pyt   custom]  s    c         C   s¦   d } y‘ t  | ƒ r? | d } | d t j k o> | j | ƒ S| d k rW |  j } n  t | ƒ d } | j | d ƒ } | | k o• | d t j k SWn t SXd S(   sÎ   
        Get whether the specified reference is an (xs) builtin.
        @param ref: A str or qref.
        @type ref: (str|qref)
        @return: True if builtin, else False.
        @rtype: bool 
        s   http://www.w3.orgi   i    t
   startswithN(	   t   isqrefR   t   tagsRh   R   R   R   t   findPrefixesRL   (   R   Re   Rf   t   w3R'   t   prefixt   prefixes(    (    sI   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/suds/xsd/schema.pyRd   j  s    
 !c         C   s   t  | | | ƒ S(   sÕ  
        Create and return an new schema object using the
        specified I{root} and I{url}.
        @param root: A schema root node.
        @type root: L{sax.element.Element}
        @param baseurl: A base URL.
        @type baseurl: str
        @param options: An options dictionary.
        @type options: L{options.Options}
        @return: The newly created schema object.
        @rtype: L{Schema}
        @note: This is only used by Import children.
        (   R;   (   R   R   RA   R    (    (    sI   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/suds/xsd/schema.pyt   instance  s    i    c         C   s»   d | d d f } g  } | j  d | |  j f ƒ | j  d | ƒ | j  |  j j | d ƒ ƒ | j  d | ƒ x+ |  j D]  } | j  | j | d ƒ ƒ q} W| j  d ƒ d j | ƒ S(	   Ns   %*si   t    s   %s%ss   %s(raw)i   s	   %s(model)s   
(   R   R?   R   R4   R
   R5   (   R   t   indentt   tabR6   t   c(    (    sI   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/suds/xsd/schema.pyR4   �  s    c         C   s'   d |  j  |  j d f } | j d ƒ S(   Ns   <%s tns="%s"/>i   s   utf-8(   R?   R   R2   (   R   t   myrep(    (    sI   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/suds/xsd/schema.pyt   __repr__›  s    c         C   s   t  |  ƒ j d ƒ S(   Ns   utf-8(   R1   R2   (   R   (    (    sI   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/suds/xsd/schema.pyR3   Ÿ  s    c         C   s
   |  j  ƒ  S(   N(   R4   (   R   (    (    sI   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/suds/xsd/schema.pyR7   ¢  s    N(   R8   R9   R:   t   TagR   R   R@   R   R   R   R   R.   Rg   Rd   Ro   R4   Ru   R3   R7   (    (    (    sI   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/suds/xsd/schema.pyR;       s    &			$						(    (    (   R:   t   suds.metricst   sudst   suds.xsdt   suds.xsd.sxbuiltint   suds.xsd.sxbasicR   RQ   t   BuiltinFactoryt   suds.xsd.sxbaseR   t   suds.xsd.deplistR   t   suds.sax.elementR   t   suds.saxR   R   t   loggingR   R8   R   R   R;   (    (    (    sI   /opt/xtal/ccp4-6.5-linux32/lib/python2.7/site-packages/suds/xsd/schema.pyt   <module>   s   


w