
    R
`                        d Z eZg dZddlZddlmZ ddlZddlZddl	Z	ddl
Z
ddlZddlZ	 ddlmZ n# e$ r	 ddlmZ Y nw xY w	 ddlmc mZ n# e$ r ddlmc mZ Y nw xY wddlmZmZ ddlmZmZ ddlmZ d	Zd
Z d Z!d Z"d Z# G d de$          Z% G d de%          Z& G d de%          Z' G d de(          Z) G d d          Z* G d de)          Z+ G d de+          Z, G d de)          Z- G d de)e*          Z. G d  d!e*          Z/ G d" d#e+e*          Z0 G d$ d%e)          Z1 G d& d'e)          Z2 G d( d)e+          Z3 G d* d+e)          Z4 G d, d-e)          Z5dS ).a  Navigate the resources exposed by a web service.

The wadllib library helps a web client navigate the resources
exposed by a web service. The service defines its resources in a
single WADL file. wadllib parses this file and gives access to the
resources defined inside. The client code can see the capabilities of
a given resource and make the corresponding HTTP requests.

If a request returns a representation of the resource, the client can
bind the string representation to the wadllib Resource object.
)
ApplicationLinkMethodNoBoundRepresentationError	ParameterRepresentationDefinitionResponseDefinitionResourceResourceType	WADLError    N)quote)	urlencode)URImerge)_make_unicode_string_types)iso_strptimez	xmlns:mapz http://www.w3.org/2001/XMLSchemac                     d| z   S )z)Scope a tag name with the WADL namespace.z&{http://research.sun.com/wadl/2006/10} tag_names    5/usr/lib/python3/dist-packages/wadllib/application.pywadl_tagr   H   s    3h>>    c                 &    dt          |           z   S )z#Turn a tag name into an XPath path.z./)r   r   s    r   
wadl_xpathr   M   s    (8$$$$r   c                  B    i }| D ]}||                     |           |S )z<Merge any number of dictionaries, some of which may be None.)update)dictsfinaldicts      r   _merge_dictsr"   R   s4    E  LLLr   c                       e Zd ZdZdS )r   zAAn exception having to do with the state of the WADL application.N__name__
__module____qualname____doc__r   r   r   r   r   [   s        KKDr   r   c                       e Zd ZdZdS )r   aA  An unbound resource was used where wadllib expected a bound resource.

    To obtain the value of a resource's parameter, you first must bind
    the resource to a representation. Otherwise the resource has no
    idea what the value is and doesn't even know if you've given it a
    parameter name that makes sense.
    Nr$   r   r   r   r   r   `   s           r   r   c                       e Zd ZdZdS )UnsupportedMediaTypeErrorzA media type was given that's not supported in this context.

    A resource can only be bound to media types it has representations
    of.
    Nr$   r   r   r   r+   r+   j   s           r   r+   c                       e Zd ZdZdS )WADLBasez?A base class for objects that contain WADL-derived information.Nr$   r   r   r   r-   r-   r   s        IIIIr   r-   c                   $    e Zd ZdZddZ	 ddZdS )HasParametersMixinzAA mixin class for objects that have associated Parameter objects.Nc                     | j         t          d          | j        g S | j                            t	          d                    }|g S fd|D             S )z6Find subsidiary parameters that have the given styles.Nz&Could not find any particular resourceparamc                 h    g | ].}|j                             d           v t          |          /S )style)attribgetr   ).0	param_tagresourcestyless     r   
<listcomp>z-HasParametersMixin.params.<locals>.<listcomp>   sI     < < <#''00F:: (I..:::r   )r8   
ValueErrortagfindallr   )selfr9   r8   
param_tagss    `` r   paramszHasParametersMixin.paramsy   s    }HEFFF8IX%%j&9&9::
I< < < < <!+< < < 	<r   Tc                    t          ||          }i }|D ]}|j        }|j        C||v r5||         |j        k    r$t          d||         d|d|j        d          |j        ||<   d |j        D             }t          |          dk    r@||v r<||         |vr2t          d||         d|d	d
                    |          d          |r|j        r||vrt          d|z            ||v r||         ||<   ||= t          |          dk    r7t          dd                    |                                          z            |S )a  Make sure the given valueset is valid.

        A valueset might be invalid because it contradicts a fixed
        value or (if enforce_completeness is True) because it lacks a
        required value.

        :param params: A list of Parameter objects.
        :param param_values: A dictionary of parameter values. May include
           paramters whose names are not valid Python identifiers.
        :param enforce_completeness: If True, this method will raise
           an exception when the given value set lacks a value for a
           required parameter.
        :param kw_param_values: A dictionary of parameter values.
        :return: A dictionary of validated parameter values.
        NzValue 'z' for parameter 'z' conflicts with fixed value ''c                     g | ]	}|j         
S r   )value)r6   options     r   r:   z<HasParametersMixin.validate_param_values.<locals>.<listcomp>   s    @@@v|@@@r   r   zInvalid value 'z': valid values are: "z", ""z$No value for required parameter '%s'zUnrecognized parameter(s): '%s'z', ')	r"   namefixed_valuer;   optionslenjoinis_requiredkeys)	r>   r@   param_valuesenforce_completenesskw_param_valuesvalidated_valuesr1   rG   rI   s	            r   validate_param_valuesz(HasParametersMixin.validate_param_values   s   " $L/BB 	' 	'E:D ,L(($T*e.???$*(4T(:(:(:DDD(-(9(9(9&; < < < &+%6T"@@%-@@@GGq  T\%9%9 &g55 j$T***DDD&++g2F2F2F2F"H I I I % )): ),, !G#'"( ) ) )|##)5d); & &|q  >%{{<+<+<+>+>??@ A A Ar   N)T)r%   r&   r'   r(   r@   rR   r   r   r   r/   r/   v   sG        KK< < < <  48-  -  -  -  -  - r   r/   c                   *    e Zd ZdZd Zd Zd Zd ZdS )WADLResolvableDefinitionz=A base class for objects whose definitions may be references.c                 "    d| _         || _        dS )zInitialize with a WADL application.

        :param application: A WADLDefinition. Relative links are
            assumed to be relative to this object's URL.
        N)_definitionapplication)r>   rX   s     r   __init__z!WADLResolvableDefinition.__init__   s      &r   c                     | j         | j         S |                                 }|	| | _         | S | j                            |          }|                     |          }|t          d|z            || _         |S )a  Return the definition of this object, wherever it is.

        Resource is a good example. A WADL <resource> tag
        may contain a large number of nested tags describing a
        resource, or it may just contain a 'type' attribute that
        references a <resource_type> which contains those same
        tags. Resource.resolve_definition() will return the original
        Resource object in the first case, and a
        ResourceType object in the second case.
        NNo such XML ID: "%s")rW   _get_definition_urlrX   lookup_xml_id_definition_factoryKeyError)r>   
object_urlxml_id
definitions       r   resolve_definitionz+WADLResolvableDefinition.resolve_definition   s     '##--//
  $DK !//
;;--f55
 1J>???%r   c                     t                      )zxTransform an XML ID into a wadllib wrapper object.

        Which kind of object it is depends on the subclass.
        NotImplementedErrorr>   ids     r   r^   z,WADLResolvableDefinition._definition_factory       
 "###r   c                     t                      )zmFind the URL that identifies an external reference.

        How to do this depends on the subclass.
        re   r>   s    r   r\   z,WADLResolvableDefinition._get_definition_url   ri   r   N)r%   r&   r'   r(   rY   rc   r^   r\   r   r   r   rU   rU      sW        GG' ' '     D$ $ $$ $ $ $ $r   rU   c                        e Zd ZdZ	 	 	 d fd	Zed             Zed             Zed             Z	 	 	 dd	Z	d
 Z
	 	 ddZddZddZed             ZddZd Zd Zd Zd ZddZd Z xZS )r	   z/A resource, possibly bound to a representation.NTc                    t          t          |                               |           || _        t	          |t
                    r%| j                            |          j        | _        n|| _        d| _	        |I|dk    r1|r't          j        t          |                    | _	        n|| _	        nt          d|z            || _        |,|	|| _        dS |                     | j                  | _        dS dS )a[  
        :param application: A WADLApplication.
        :param url: The URL to this resource.
        :param resource_type: An ElementTree <resource> or <resource_type> tag.
        :param representation: A string representation.
        :param media_type: The media type of the representation.
        :param representation_needs_processing: Set to False if the
            'representation' parameter should be used as
            is. Otherwise, it will be transformed from a string into
            an appropriate Python data structure, depending on its
            media type.
        :param representation_definition: A RepresentationDefinition
            object describing the structure of this
            representation. Used in cases when the representation
            isn't the result of sending a standard GET to the
            resource.
        Napplication/jsonz?This resource doesn't define a representation for media type %s)superr	   rY   _url
isinstancer   rX   get_resource_typer<   representationjsonloadsr   r+   
media_typerepresentation_definitionget_representation_definition)	r>   rX   urlresource_typers   rv   representation_needs_processingrw   	__class__s	           r   rY   zResource.__init__   s"   * 	h&&{333	m]33 	% '99-HHLDHH %DH"%///2 9*.*%n55+7 +7D'' +9D''/$&012 2 2 %%(41J... 66tGG ...	 &%r   c                     | j         S )z Return the URL to this resource.)rp   rk   s    r   ry   zResource.url,  s     yr   c                     | j         dS | j         j                            d          }||S | j         j                            d          }|@t          | j        j                                                  }t          |          dz   |z   S dS )z@Return the URL to the type definition for this resource, if any.Ntyperh   #)r<   r4   r5   r   rX   
markup_urlensureSlashstr)r>   ry   type_idbases       r   type_urlzResource.type_url1  s     84ho!!&))?J(/%%d++t'233??AADt99s?W,, tr   c                 &    | j         j        d         S )zReturn the ID of this resource.rh   )r<   r4   rk   s    r   rh   zResource.idC  s     xt$$r   rn   c           	      J    t          | j        | j        | j        ||||          S )a  Bind the resource to a representation of that resource.

        :param representation: A string representation
        :param media_type: The media type of the representation.
        :param representation_needs_processing: Set to False if the
            'representation' parameter should be used as
            is.
        :param representation_definition: A RepresentationDefinition
            object describing the structure of this
            representation. Used in cases when the representation
            isn't the result of sending a standard GET to the
            resource.
        :return: A Resource bound to a particular representation.
        )r	   rX   ry   r<   )r>   rs   rv   r{   rw   s        r   bindzResource.bindH  s.    " ($(DH&
713 3 	3r   c                     |                      d          j        }|D ]=}|                                j        }|j                            d          |k    r|c S >t          d|z            )z<Get a description of one of this resource's representations.GET	mediaTypez4No definition for representation with media type %s.)
get_methodresponserc   r<   r4   r5   r+   )r>   rv   default_get_responsers   representation_tags        r   rx   z&Resource.get_representation_definition^  s    #u55>2 	& 	&N!/!B!B!D!D!H!(,,[99ZGG%%%% H' )>@J)K L L 	Lr   c                    |                                  D ]t}|j                            dd                                          }|||                                k    r+t	          | |          }|                    |||          r|c S udS )a   Look up one of this resource's methods by HTTP method.

        :param http_method: The HTTP method used to invoke the desired
                            method. Case-insensitive and optional.

        :param media_type: The media type of the representation
                           accepted by the method. Optional.

        :param query_params: The names and values of any fixed query
                             parameters used to distinguish between
                             two methods that use the same HTTP
                             method. Optional.

        :param representation_params: The names and values of any
                             fixed representation parameters used to
                             distinguish between two methods that use
                             the same HTTP method and have the same
                             media type. Optional.

        :return: A MethodDefinition, or None if there's no definition
                  that fits the given constraints.
        rG    N)_method_tag_iterr4   r5   lowerr   is_described_by)r>   http_methodrv   query_paramsrepresentation_params
method_tagrG   methods           r   r   zResource.get_methodh  s    0 //11 	" 	"J$((44::<<D"dk.?.?.A.A&A&Aj11))*l*?A A "!MMMtr   c                 R    |                      |                              |           S )a~  A list of this resource's parameters.

        :param media_type: Media type of the representation definition
            whose parameters are being named. Must be present unless
            this resource is bound to a representation.

        :raise NoBoundRepresentationError: If this resource is not
            bound to a representation and media_type was not provided.
        )_find_representation_definitionr@   r>   rv   s     r   
parameterszResource.parameters  s*     33 t	%r   c                 R    |                      |                              |           S )a  A list naming this resource's parameters.

        :param media_type: Media type of the representation definition
            whose parameters are being named. Must be present unless
            this resource is bound to a representation.

        :raise NoBoundRepresentationError: If this resource is not
            bound to a representation and media_type was not provided.
        )r   parameter_namesr   s     r   r   zResource.parameter_names  s,     33 '--	.r   c              #   \   K   |                                  D ]}t          | |          V  dS )z6An iterator over the methods defined on this resource.N)r   r   )r>   r   s     r   method_iterzResource.method_iter  sF       //11 	+ 	+Jz******	+ 	+r   c                     |                      |          }|j        }|                    t          d                    D ]2}|j                            d          |k    rt          | |          c S 3dS )a  Find a parameter within a representation definition.

        :param param_name: Name of the parameter to find.

        :param media_type: Media type of the representation definition
            whose parameters are being named. Must be present unless
            this resource is bound to a representation.

        :raise NoBoundRepresentationError: If this resource is not
            bound to a representation and media_type was not provided.
        r1   rG   N)r   r<   r=   r   r4   r5   r   )r>   
param_namerv   rb   r   r7   s         r   get_parameterzResource.get_parameter  s     99*EE
'^+33Jw4G4GHH 	2 	2I##F++z99 y11111 :tr   c                    | j         t          d          | j        dk    r|j        dk    rt	          d|j        z            | j         |j                 }||                     |j        |j                  \  }}|t          k    rm|dv ri	 t          |          }nX# t          $ rK 	 t          j        t          j        |d          dd	          }n# t          $ r t          |          w xY wY nw xY w|S t	          d
| j        z            )zFind the value of a parameter, given the Parameter object.

        :raise ValueError: If the parameter value can't be converted into
        its defined type.
        Nz,Resource is not bound to any representation.rn   plain8Don't know how to find value for a parameter of type %s.)dateTimedatez%Y-%m-%dr      zEPath traversal not implemented for a representation of media type %s.)rs   r   rv   r3   rf   rG   _dereference_namespacer<   r   XML_SCHEMA_NS_URIr   r;   datetimetimestrptime)r>   	parameterrD   namespace_url	data_types        r   get_parameter_valuezResource.get_parameter_value  sl    &,>@ @ @?000 ')))!*12 2 2 '	7E +/+F+FM9>,3 ,3(y!%666!%9994 ,U 3 3% 	4 	4 	44$,$5"&-z"B"B1Q3"G%JEE) 4 4 4 #-U"3"33	4 "E	4 L! #G$(O#4 5 5 	5s*   B 
C0&)CC0C**C0/C0c                     |d|v r|                     dd          \  }}nd}|                    t                    }|                    |d          }||fS )zSplits a value into namespace URI and value.

        :param tag: A tag to use as context when mapping namespace
        names to URIs.
        N:   r   )splitr5   NS_MAP)r>   r<   rD   	namespacens_mapr   s         r   r   zResource._dereference_namespace  s^     ${{322IuuI

9d33e##r   c                 @    | j         j                            |          S )z-Given an ID, find a ResourceType for that ID.)rX   resource_typesr5   rg   s     r   r^   zResource._definition_factory  s    .222666r   c                 @    | j         j                            d          S )zReturn the URL that shows where a resource is 'really' defined.

        If a resource's capabilities are defined by reference, the
        <resource> tag's 'type' attribute will contain the URL to the
        <resource_type> that defines them.
        r   r<   r4   r5   rk   s    r   r\   zResource._get_definition_url       x""6***r   c                     | j         | j                                        }n'||                     |          }nt	          d          |                                S )a3  Get the most appropriate representation definition.

        If media_type is provided, the most appropriate definition is
        the definition of the representation of that media type.

        If this resource is bound to a representation, the most
        appropriate definition is the definition of that
        representation. Otherwise, the most appropriate definition is
        the definition of the representation served in response to a
        standard GET.

        :param media_type: Media type of the definition to find. Must
            be present unless the resource is bound to a
            representation.

        :raise NoBoundRepresentationError: If this resource is not
            bound to a representation and media_type was not provided.

        :return: A RepresentationDefinition
        NzSResource is not bound to any representation, and no media media type was specified.)rs   rw   rc   rx   r   )r>   rv   rb   s      r   r   z(Resource._find_representation_definition  sk    * * 7JJLLJJ#;;JGGJJ,,- - - ,,...r   c              #      K   |                                  j        }|                    t          d                    D ]}|V  dS )z+Iterate over this resource's <method> tags.r   N)rc   r<   r=   r   )r>   rb   r   s      r   r   zResource._method_tag_iter*  sX      ,,..2
$,,Z-A-ABB 	 	J	 	r   )NNTN)rn   TN)NNNNrS   )r%   r&   r'   r(   rY   propertyry   r   rh   r   rx   r   r   r   r   r   r   r   r^   r\   r   r   __classcell__r|   s   @r   r	   r	      s       99 2615+/2I 2I 2I 2I 2I 2Ih   X   X" % % X% /A-1'+3 3 3 3,L L L JN)-   B% % % %. . . . + + X+
   &-5 -5 -5`$ $ $7 7 7+ + + /  /  /  /F      r   r	   c                       e Zd ZdZd Zed             Zed             Zed             Zed             Z	ddZ
	 	 dd	Z	 	 dd
ZdS )r   z*A wrapper around an XML <method> tag.
    c                 D    || _         | j         j        | _        || _        dS )zaInitialize with a <method> tag.

        :param method_tag: An ElementTree <method> tag.
        N)r8   rX   r<   )r>   r8   r   s      r   rY   zMethod.__init__4  s#    
 !=4r   c                 l    t          | | j                            t          d                              S )z@Return the definition of a request that invokes the WADL method.request)RequestDefinitionr<   findr   rk   s    r   r   zMethod.request=  s*     !tx}}Z	5J5J'K'KLLLr   c                 v    t          | j        | j                            t	          d                              S )z9Return the definition of the response to the WADL method.r   )r   r8   r<   r   r   rk   s    r   r   zMethod.responseB  s6     "$-"&(--
:0F0F"G"GI I 	Ir   c                 @    | j         j                            d          S )z)The XML ID of the WADL method definition.rh   r   rk   s    r   rh   z	Method.idH  s     x""4(((r   c                 d    | j         j                            d                                          S )zThe name of the WADL method definition.

        This is also the name of the HTTP method (GET, POST, etc.)
        that should be used to invoke the WADL method.
        rG   r<   r4   r5   r   rk   s    r   rG   zMethod.nameM  s(     x""6**00222r   Nc                 (     | j         j        |fi |S )4Return the request URL to use to invoke this method.)r   	build_url)r>   rN   rP   s      r   build_request_urlzMethod.build_request_urlV  s    %t|%lFFoFFFr   c                 *     | j         j        ||fi |S )zBuild a representation to be sent when invoking this method.

        :return: A 2-tuple of (media_type, representation).
        )r   rs   )r>   rv   rN   rP   s       r   build_representationzMethod.build_representationZ  s/     +t|*9 9(79 9 	9r   c                    d}|| j                             |          }|dS |Mt          |          dk    r:| j         }|dS 	 |                    |j        |d           n# t
          $ r Y dS w xY w|t          |          dk    rdS ||                    |          S | j         j        D ]D}	 |                    |                    | j	                  |d            dS # t
          $ r Y Aw xY wdS )a  Returns true if this method fits the given constraints.

        :param media_type: The method must accept this media type as a
                           representation.

        :param query_values: These key-value pairs must be acceptable
                           as values for this method's query
                           parameters. This need not be a complete set
                           of parameters acceptable to the method.

        :param representation_values: These key-value pairs must be
                           acceptable as values for this method's
                           representation parameters. Again, this need
                           not be a complete set of parameters
                           acceptable to the method.
        NFr   T)
r   rx   rJ   rR   r   r;   r   representationsr@   r8   )r>   rv   query_valuesrepresentation_valuesrs   r   s         r   r   zMethod.is_described_byc  sf   $ !!\GG N%u#L(9(9A(=(=lG u--(,? ? ? ?   uu ")())Q..4%!11%' ' '"l: 	 	N44"))$-88)52 2 2 tt   us$   A! !
A/.A///C!!
C.-C.rS   NN)NNN)r%   r&   r'   r(   rY   r   r   r   rh   rG   r   r   r   r   r   r   r   r   1  s            M M XM I I XI
 ) ) X) 3 3 X3G G G G /3*.9 9 9 9 =A.25 5 5 5 5 5r   r   c                   \    e Zd ZdZd Zed             Zed             Zd	dZd
dZ	d	dZ
dS )r   zBA wrapper around the description of the request invoking a method.c                 f    || _         | j         j        | _        | j        j        | _        || _        dS )zInitialize with a <request> tag.

        :param resource: The resource to which this request can be sent.
        :param request_tag: An ElementTree <request> tag.
        N)r   r8   rX   r<   )r>   r   request_tags      r   rY   zRequestDefinition.__init__  s/     ,=4r   c                 .    |                      dg          S )z,Return the query parameters for this method.queryr@   rk   s    r   r   zRequestDefinition.query_params  s     {{G9%%%r   c              #      K   | j                             t          d                    D ]}t          | j        | j        |          V   d S )Nrs   )r<   r=   r   r   rX   r8   )r>   rb   s     r   r   z!RequestDefinition.representations  se      (**:6F+G+GHH 	= 	=J* $-= = = = = =	= 	=r   Nc                 <    | j         D ]}||j        |k    r|c S dS )z1Return the appropriate representation definition.N)r   rv   r>   rv   rs   s      r   rx   z/RequestDefinition.get_representation_definition  s>    "2 	& 	&N!^%>*%L%L%%%% &Mtr   c                 p    |                      |          }|t          d|z             |j        |fi |S )zBuild a representation to be sent along with this request.

        :return: A 2-tuple of (media_type, representation).
        Nz,Cannot build representation of media type %s)rx   	TypeErrorr   )r>   rv   rN   rP   rb   s        r   rs   z RequestDefinition.representation  sT     77
CC
J() * * *z|?????r   c                      | j         | j        |fi |}| j        j        }t	          |          dk    r=d|v rd}nd}||t          t          |                                                    z   z  }|S )r   r   ?&)rR   r   r8   ry   rJ   r   sorteditems)r>   rN   rP   rQ   ry   appends         r   r   zRequestDefinition.build_url  s    545|@ @/>@ @m  1$$czz6If-=-C-C-E-E&F&FGGGGC
r   rS   r   )r%   r&   r'   r(   rY   r   r   r   rx   rs   r   r   r   r   r   r     s        LL	 	 	 & & X& = = X=
   
@ 
@ 
@ 
@     r   r   c                   8    e Zd ZdZd	dZd Zd Zd Zd Zd Z	dS )
r   z;A wrapper around the description of a response to a method.Nc                 H    |j         | _         || _        || _        || _        dS )zgInitialize with a <response> tag.

        :param response_tag: An ElementTree <response> tag.
        N)rX   r8   r<   headers)r>   r8   response_tagr   s       r   rY   zResponseDefinition.__init__  s(    
 $/ r   c              #      K   t          d          }| j                            |          D ]$}t          | j        j        | j        |          V  %dS )zGet an iterator over the representation definitions.

        These are the representations returned in response to an
        invocation of this method.
        rs   N)r   r<   r=   r   r8   rX   )r>   pathr   s      r   __iter__zResponseDefinition.__iter__  sz       *++"&("2"24"8"8 	N 	N*)4=:LN N N N N N	N 	Nr   c                 8    t          | j        | j        |          S )zBind the response to a set of HTTP headers.

        A WADL response can have associated header parameters, but no
        other kind.
        )r   r8   r<   )r>   r   s     r   r   zResponseDefinition.bind  s     "$-7CCCr   c                     | j                             t          d                    D ]P}|j                            d          |k    r0|j                            d          dk    rt          | |          c S QdS )z,Find a header parameter within the response.r1   rG   r3   headerN)r<   r=   r   r4   r5   r   )r>   r   r7   s      r   r   z ResponseDefinition.get_parameter  s}    ))*W*=*=>> 	2 	2I $$V,,
::$((11X== y11111tr   c                     | j         t          d          |j        dk    rt          d|j        z            | j                             |j                  S )z:Find the value of a parameter, given the Parameter object.Nz,Response object is not bound to any headers.r   r   )r   r   r3   rf   r5   rG   )r>   r   s     r   r   z&ResponseDefinition.get_parameter_value  sj    <,>@ @ @?h&&%&_-. . . |	///r   c                 @    | j         dS | D ]}|j        |k    r|c S dS )z8Get one of the possible representations of the response.N)r<   rv   r   s      r   rx   z0ResponseDefinition.get_representation_definition  sB    84" 	& 	&N(J66%%%% 7tr   rS   )
r%   r&   r'   r(   rY   r   r   r   r   rx   r   r   r   r   r     s        EE   	N 	N 	ND D D  	0 	0 	0    r   r   c                   t     e Zd ZdZ fdZ fdZd Zed             Zd Z	d Z
dd	Zd
 Zd Zd Zd Z xZS )r   z2A definition of the structure of a representation.c                 t    t          t          |                               |           || _        || _        d S rS   )ro   r   rY   r8   r<   )r>   rX   r8   r   r|   s       r   rY   z!RepresentationDefinition.__init__  s4    &--66{CCC %r   c                 Z    t          t          |                               ddg|          S )Nr   r   )ro   r   r@   )r>   r8   r|   s     r   r@   zRepresentationDefinition.params  s0    -t44;;g* * 	*r   c                 @    d |                      |          D             S )z#Return the names of all parameters.c                     g | ]	}|j         
S r   )rG   )r6   r1   s     r   r:   z<RepresentationDefinition.parameter_names.<locals>.<listcomp>$  s    >>>u
>>>r   r   )r>   r8   s     r   r   z(RepresentationDefinition.parameter_names"  s"    >>H(=(=>>>>r   c                 J    |                                  j        j        d         S )z4The media type of the representation described here.r   )rc   r<   r4   rk   s    r   rv   z#RepresentationDefinition.media_type&  s!     &&((,3K@@r   c                    t          t          t          j        dz
                      }d|z  }t	          j        t          j                  }d||z  z   dz   }||S |}d}	 dt          j        |          z   d	z                       d
          }t          j	        ||t          j
                  sn|dz   t          |          z   }|dz  }j|S )z;Make a random boundary that does not appear in `all_parts`.r   z%%0%ddz===============z==Nr   Tz^--z(--)?$ascii)flags.)rJ   reprsysmaxsizerandom	randrangereescapeencodesearch	MULTILINEr   )	r>   	all_parts_width_fmttokenboundarybcounterpatterns	            r   _make_boundaryz'RepresentationDefinition._make_boundary+  s    T#+/**++&  --.5O	ry||+h6>>wGGG9Wir|DDD 3W-AqLG	 r   c                 4   |D ]\  }}|                     |                    d                     |                     d           |                     |                    d                     |                     d           |                     d           dS )z$Write MIME headers to a file object.UTF-8s   :    
Nwriter	  )r>   bufr   keyrD   s        r   _write_headersz'RepresentationDefinition._write_headers=  s    ! 	 	JCIIcjj))***IIeIIell7++,,,IIg		'r   Fc                     |                     d           |                     |                    d                     |r|                     d           |                     d           dS )z,Write a multipart boundary to a file object.s   --r  r  Nr  )r>   r  r  closings       r   _write_boundaryz(RepresentationDefinition._write_boundaryF  sd    		%		(//'**+++ 	IIe		'r   c                    g }|D ]\  }}}t          j                    }|r&d}dt          |          dt          |          d}nd}dt          |          z  }|                     |dd|fd	|fg           |rJt	          |t
                    st          d
t          |          z            |                    |           nt	          |t                    st          dt          |          z            t          j        d|          }	|	dd         D ]?}
|                    |
                    d                     |                    d           @|                    |	d                             d                     |                    |                                           |                     d                    |                    }t          j                    }dt          |          z  }|                     |dd|fg           |D ]B}|                     ||           |                    |           |                    d           C|                     ||d           ||                                fS )aV  Generate a multipart/form-data message.

        This is very loosely based on the email module in the Python standard
        library.  However, that module doesn't really support directly embedding
        binary data in a form: various versions of Python have mangled line
        separators in different ways, and none of them get it quite right.
        Since we only need a tiny subset of MIME here, it's easier to implement
        it ourselves.

        :return: a tuple of two elements: the Content-Type of the message, and
            the entire encoded message as a byte string.
        zapplication/octet-streamzform-data; name="z"; filename="rF   ztext/plain; charset="utf-8"zform-data; name="%s")zMIME-Versionz1.0zContent-TypezContent-Dispositionzbytes payload expected: %szstring payload expected: %sz
\r\n|\r|\nNr  r  z"multipart/form-data; boundary="%s"T)r  )ioBytesIOr   r  rq   bytesr   r   r  r   r  r   r	  r   getvaluer  rK   r  )r>   partsencoded_parts	is_binaryrG   rD   r  ctypecdisplinesliner  encoded_parts                r   _generate_multipart_formz1RepresentationDefinition._generate_multipart_formN  s    &+ 	1 	1"ItU*,,C 	=2
 $KKKKt. 6.t<''&.&   
  5!%// P#$@4;;$NOOO		%    !%77 E#5UCE E E66!#2#J ' 'DIIdkk'22333IIg&&&&		%)**733444  0000 &&w||M'B'BCC jll4uXFC#U#" 	 	 	 * 	 	L  h///IIl###IIgS(D999cllnn$$r   c                 H   |                                  }|                    | j                  } | j        ||fi |}| j        }|dk    r/t          t          |                                                    }n|dk    rsg }t                      }	|D ]G}
|	                    |
j
        |	          }||	ur&|                    |
j        dk    |
j
        |f           H|                     |          \  }}n-|dk    rt          j        |          }nt!          d|z            ||fS )zzBind the definition to parameter values, creating a document.

        :return: A 2-tuple (media_type, document).
        z!application/x-www-form-urlencodedzmultipart/form-databinaryrn   zUnsupported media type: '%s')rc   r@   r8   rR   rv   r   r   r   objectr5   rG   r   r   r.  rt   dumpsr;   )r>   rN   rP   rb   r@   rQ   rv   docr&  missingr1   rD   s               r   r   zRepresentationDefinition.bind  sM   
 ,,..
""4=11545L5 5$35 5_
<<<F#3#9#9#;#;<<==CC000EhhG N N(,,UZAA''LL%*"8%*e!LMMM";;EBBOJ---*-..CC;jHIII3r   c                 @    | j         j                            |          S )z9Turn a representation ID into a RepresentationDefinition.)rX   representation_definitionsr5   rg   s     r   r^   z,RepresentationDefinition._definition_factory  s    :>>rBBBr   c                 @    | j         j                            d          S )a  Find the URL containing the representation's 'real' definition.

        If a representation's structure is defined by reference, the
        <representation> tag's 'href' attribute will contain the URL
        to the <representation> that defines the structure.
        hrefr   rk   s    r   r\   z,RepresentationDefinition._get_definition_url  r   r   )F)r%   r&   r'   r(   rY   r@   r   r   rv   r  r  r  r.  r   r^   r\   r   r   s   @r   r   r     s        <<& & & & &
* * * * *? ? ? A A XA  $     ?% ?% ?%B  4C C C+ + + + + + +r   r   c                       e Zd ZdZd Zed             Zed             Zed             Zed             Z	ed             Z
d Zed	             Zed
             Zed             ZdS )r   z5One of the parameters of a representation definition.c                 :    |j         | _         || _        || _        dS )a  Initialize with respect to a value container.

        :param value_container: Usually the resource whose representation
            has this parameter. If the resource is bound to a representation,
            you'll be able to find the value of this parameter in the
            representation. This may also be a server response whose headers
            define a value for this parameter.
        :tag: The ElementTree <param> tag for this parameter.
        N)rX   value_containerr<   )r>   r;  r<   s      r   rY   zParameter.__init__  s"     +6.r   c                 @    | j         j                            d          S )zThe name of this parameter.rG   r   rk   s    r   rG   zParameter.name       x""6***r   c                 @    | j         j                            d          S )zThe style of this parameter.r3   r   rk   s    r   r3   zParameter.style  s     x""7+++r   c                 @    | j         j                            d          S )zThe XSD type of this parameter.r   r   rk   s    r   r   zParameter.type  r=  r   c                 @    | j         j                            d          S )a9  The value to which this parameter is fixed, if any.

        A fixed parameter must be present in invocations of a WADL
        method, and it must have a particular value. This is commonly
        used to designate one parameter as containing the name of the
        server-side operation to be invoked.
        fixedr   rk   s    r   rH   zParameter.fixed_value  s     x""7+++r   c                 j    | j         j                            dd                                          dv S )z6Whether or not a value for this parameter is required.requiredfalse)1truer   rk   s    r   rL   zParameter.is_required  s4     ##J88>>@@ ! 	"r   c                 6    | j                             |           S )zThe value of this parameter in the bound representation/headers.

        :raise NoBoundRepresentationError: If this parameter's value
               container is not bound to a representation or a set of
               headers.
        )r;  r   rk   s    r   	get_valuezParameter.get_value  s     #77===r   c                 j      fd j                             t          d                    D             S )z7Return the set of acceptable values for this parameter.c                 0    g | ]}t          |          S r   )Option)r6   
option_tagr>   s     r   r:   z%Parameter.options.<locals>.<listcomp>  s9     K K K tZ(( K K Kr   rE   )r<   r=   r   rk   s   `r   rI   zParameter.options  sN    K K K K#'8#3#3Jx4H4H#I#IK K K 	Kr   c                 x    | j                             t          d                    }|dS t          | |          S )zGet the link to another resource.

        The link may be examined and, if its type is of a known WADL
        description, it may be followed.

        :return: A Link object, or None.
        linkN)r<   r   r   r   )r>   link_tags     r   rN  zParameter.link  s:     8==F!3!3444D(###r   c                 @    | j         }|t          d          |j        S )a  Follow a link from this parameter to a new resource.

        This only works for parameters whose WADL definition includes a
        <link> tag that points to a known WADL description.

        :return: A Resource object for the resource at the other end
        of the link.
        Nz(This parameter isn't a link to anything.)rN  r;   follow)r>   rN  s     r   linked_resourcezParameter.linked_resource  s'     y<GHHH{r   N)r%   r&   r'   r(   rY   r   rG   r3   r   rH   rL   rH  rI   rN  rR  r   r   r   r   r     s       ??   + + X+ , , X, + + X+ , , X, " " X"
> > > K K XK
 $ $ X$   X  r   r   c                   .    e Zd ZdZd Zed             ZdS )rK  z0One of a set of possible values for a parameter.c                 "    || _         || _        dS )z}Initialize the option.

        :param parameter: A Parameter.
        :param link_tag: An ElementTree <option> tag.
        N)r   r<   )r>   r   rL  s      r   rY   zOption.__init__  s     #r   c                 @    | j         j                            d          S )NrD   r   rk   s    r   rD   zOption.value   s    x""7+++r   N)r%   r&   r'   r(   rY   r   rD   r   r   r   rK  rK    sD        ::   , , X, , ,r   rK  c                   Z     e Zd ZdZ fdZed             Zed             Zd Zd Z	 xZ
S )r   zA link from one resource to another.

    Calling resolve_definition() on a Link will give you a Resource for the
    type of resource linked to. An alias for this is 'follow'.
    c                 ~    t          t          |                               |j                   || _        || _        dS )zyInitialize the link.

        :param parameter: A Parameter.
        :param link_tag: An ElementTree <link> tag.
        N)ro   r   rY   rX   r   r<   )r>   r   rO  r|   s      r   rY   zLink.__init__,  s8     	dD""9#8999"r   c                 V    | j         st          d          |                                 S )z$Follow the link to another Resource.zfCannot follow a link when the target has no WADL description. Try using a general HTTP client instead.)
can_followr   rc   rk   s    r   rQ  zLink.follow6  s9      	/ . / / / &&(((r   c                 T    	 |                                  }n# t          $ r Y dS w xY wdS )zCan this link be followed within wadllib?

        wadllib can follow a link if it points to a resource that has
        a WADL definition.
        FT)r\   r   )r>   definition_urls     r   rY  zLink.can_follow?  sA    	!5577NN 	 	 	55	ts    
%%c                     t          | j        | j                                        | j        j                            |          j                  S )z,Turn a resource type ID into a ResourceType.)r	   rX   r   rH  r   r5   r<   rg   s     r   r^   zLink._definition_factoryL  sD    dn6688+//3379 9 	9r   c                 f    | j         j                            d          }|t          d          |S )z(Find the URL containing the definition .rz   NzIParameter is a link, but not to a resource with a known WADL description.)r<   r4   r5   r   )r>   r   s     r   r\   zLink._get_definition_urlR  s:    x""?33< = > > >r   )r%   r&   r'   r(   rY   r   rQ  rY  r^   r\   r   r   s   @r   r   r   %  s              ) ) X) 
 
 X
9 9 9      r   r   c                       e Zd ZdZd ZdS )r
   z,A wrapper around an XML <resource_type> tag.c                     || _         dS )zvInitialize with a <resource_type> tag.

        :param resource_type_tag: An ElementTree <resource_type> tag.
        N)r<   )r>   resource_type_tags     r   rY   zResourceType.__init__^  s    
 %r   N)r%   r&   r'   r(   rY   r   r   r   r
   r
   [  s)        66% % % % %r   r
   c                   6    e Zd ZdZd Zd Zd Zd Zd Zd Z	dS )	r   z1A WADL document made programmatically accessible.c                    || _         t          |d          r|                     |          | _        n|                     |          | _        | j                            t          d                    | _        | j        j        	                    d          | _
        i | _        i | _        | j                            t          d                    D ]9}|j        	                    d          }|t          | d|          }|| j        |<   :| j                            t          d                    D ]&}|j        d         }t          |          | j        |<   'dS )zParse WADL and find the most important parts of the document.

        :param markup_url: The URL from which this document was obtained.
        :param markup: The WADL markup itself, or an open filehandle to it.
        read	resourcesr   rs   rh   Nrz   )r   hasattr_from_streamr3  _from_stringr   r   rd  r4   r5   resource_baser6  r   r=   r   r
   )r>   r   markuprs   rh   rb   rz   s          r   rY   zApplication.__init__i  sT    %66"" 	1((00DHH((00DHz+'>'>??!^266v>>*,' "h..z:J/K/KLL 	A 	AN&**400B~5$0 0
6@/3!X--j.I.IJJ 	B 	BM%d+B&2=&A&AD##	B 	Br   c                 >   d}d}g }t          j        ||          D ]n\  }}|dk    r|                    |           !|dk    r|                                 <|dk    r,||}|                    t
          t          |                     ot          j        |          S )zrTurns markup into a document.

        Just a wrapper around ElementTree which keeps track of namespaces.
        )startstart-nsend-nsNrl  rm  rk  )ET	iterparser   popsetr   r!   ElementTree)r>   streameventsrootr   eventelems          r   rf  zApplication._from_stream  s    
 /<77 	/ 	/KE4
""d####(""

'!!<Df...~d###r   c                     t          |t                    s|                    d          }|                     t	          j        |                    S )zTurns markup into a document.r  )rq   r$  r	  rf  r"  r#  )r>   ri  s     r   rg  zApplication._from_string  sC    &%(( 	,]]7++F  F!3!3444r   c                     |                      |          }| j                            |          }|t          d|z            |S )z7Retrieve a resource type by the URL of its description.Nr[   )r]   r   r5   r_   )r>   resource_type_urlra   rz   s       r   rr   zApplication.get_resource_type  sM    ##$566+//77 14EEFFFr   c                 D   t          | j                                                  }d|_        |                    d          r"t          |                                          }n|                    |          }|j        }d|_        ||k    r|S t          d|z            )zA helper method for locating a part of a WADL document.

        :param url: The URL (with anchor) of the desired part of the
        WADL document.
        :return: The XML ID corresponding to the anchor.
        Nhttpz,Can't look up definition in another url (%s))r   r   ensureNoSlashfragment
startswithresolverf   )r>   ry   
markup_urithis_uripossible_xml_ids        r   r]   zApplication.lookup_xml_id  s     ))7799
"
>>&!! 	/3xx--//HH "))#..H"+ z!! #" " #-/2#3 4 4 	4r   c                     fd| j         D             }t          |          dk     rdS t          |          dk    rt          dz            t          | t	          | j        d          |d                   S )zpLocate one of the resources described by this document.

        :param path: The path to the resource.
        c                 6    g | ]}|j         d          k    |S )r   )r4   )r6   r8   r   s     r   r:   z4Application.get_resource_by_path.<locals>.<listcomp>  s4     8 8 8v.$66 666r   r   Nz+More than one resource defined with path %sTr   )rd  rJ   r   r	   r   rh  )r>   r   matchings    ` r   get_resource_by_pathz Application.get_resource_by_path  s    8 8 8 8T^ 8 8 8x==14x==1I"# $ $ $%*D$77!F F 	Fr   N)
r%   r&   r'   r(   rY   rf  rg  rr   r]   r  r   r   r   r   r   f  s}        ;;B B B2$ $ $(5 5 5  4 4 4@F F F F Fr   r   )6r(   r   __metaclass____all__r   email.utilsr   r"  rt   r  r  r  r   urllib.parser   ImportErrorurllibxml.etree.cElementTreeetreecElementTreern  xml.etree.ElementTreerr  lazr.urir   r   wadllibr   r   wadllib.iso_strptimer   r   r   r   r   r"   	Exceptionr   r   r+   r1  r-   r/   rU   r	   r   r   r   r   r   rK  r   r
   r   r   r   r   <module>r     s  "
 
           				   				 



 !&&&&&&& ! ! !        !''''''''''' ' ' '&&&&&&&&&&&'                 . - - - - -	6 ? ? ?
% % %
  	 	 	 	 		 	 	 	
           	   J J J J Jv J J J?  ?  ?  ?  ?  ?  ?  ? D:$ :$ :$ :$ :$x :$ :$ :$zy y y y y' y y yx	g g g g gX g g gT6 6 6 6 6"4 6 6 6r? ? ? ? ?+ ? ? ?D^+ ^+ ^+ ^+ ^+79K ^+ ^+ ^+B[ [ [ [ [ [ [ [z, , , , ,X , , ,"3 3 3 3 3# 3 3 3l% % % % %8 % % %nF nF nF nF nF( nF nF nF nF nFs!   3 A A	A A A 