
    :hO                        S r / SQrSSKrSSKJr  SSKrSSKrSSKrSSKrSSK	r	SSK
r
SSKJr  SSKJs  Jr  SSKJrJr  SSKJr  SrS	rS
 rS rS r " S S\5      r " S S\5      r " S S\5      r " S S5      r " S S5      r  " S S\5      r! " S S\!5      r" " S S\5      r# " S S\\ 5      r$ " S S \ 5      r% " S! S"\!\ 5      r& " S# S$\5      r' " S% S&\5      r( " S' S(\!5      r) " S) S*\5      r* " S+ S,\5      r+g)-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)iso_strptimez	xmlns:mapz http://www.w3.org/2001/XMLSchemac                     SU -   $ )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   =   s    3h>>    c                     S[        U 5      -   $ )z#Turn a tag name into an XPath path.z./)r   r   s    r   
wadl_xpathr   B   s    (8$$$r   c                  H    0 nU  H  nUc  M  UR                  U5        M     U$ )z<Merge any number of dictionaries, some of which may be None.)update)dictsfinaldicts      r   _merge_dictsr    G   s*    ELL  Lr   c                       \ rS rSrSrSrg)r   P   zAAn exception having to do with the state of the WADL application.r   N__name__
__module____qualname____firstlineno____doc____static_attributes__r   r   r   r   r   P   s    Kr   r   c                       \ rS rSrSrSrg)r   U   a-  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.
r   Nr#   r   r   r   r   r   U   s    r   r   c                       \ rS rSrSrSrg)UnsupportedMediaTypeError_   zA media type was given that's not supported in this context.

A resource can only be bound to media types it has representations
of.
r   Nr#   r   r   r   r-   r-   _   s    r   r-   c                       \ rS rSrSrSrg)WADLBaseg   z?A base class for objects that contain WADL-derived information.r   Nr#   r   r   r   r0   r0   g   s    Ir   r0   c                   .    \ rS rSrSrSS jr SS jrSrg)	HasParametersMixink   zAA mixin class for objects that have associated Parameter objects.Nc                 0   Uc  U R                   nUc  [        S5      eU R                  c  / $ U R                  R                  [	        S5      5      nUc  / $ U Vs/ s H/  nUR
                  R                  S5      U;   d  M$  [        X$5      PM1     sn$ s  snf )z6Find subsidiary parameters that have the given styles.z&Could not find any particular resourceparamstyle)resource
ValueErrortagfindallr   attribgetr   )selfstylesr8   
param_tags	param_tags        r   paramsHasParametersMixin.paramsn   s    }}HEFF88IXX%%j&9:
I!+<!+I##''0F: /	(.!+< 	< <s   #BBc                    [        X$5      n0 nU GH  nUR                  nUR                  bI  Xr;   a6  X'   UR                  :w  a$  [        SX'   < SU< SUR                  < S35      eUR                  X''   UR                   Vs/ s H  oR
                  PM     n	n[        U	5      S:  a6  Xr;   a1  X'   U	;  a)  [        SX'   < SU< SSR                  U	5      < S	35      eU(       a$  UR                  (       a  Xr;  a  [        S
U-  5      eXr;   d  GM  X'   XW'   X'	 GM     [        U5      S:  a+  [        SSR                  UR                  5       5      -  5      eU$ s  snf )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.
zValue 'z' for parameter 'z' conflicts with fixed value ''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_valuer9   optionsvaluelenjoinis_requiredkeys)
r>   rB   param_valuesenforce_completenesskw_param_valuesvalidated_valuesr6   rG   optionrI   s
             r   validate_param_values(HasParametersMixin.validate_param_values}   sf   " $LBE::D  ,((.%2C2CC$(4(:D(-(9(9&; < < &+%6%6"27--@-||-G@Gq T%9$*'9 $*D&++g2F"H I I %):):0 !G#'"( ) )#)5); & &- . |q >%{{<+<+<+>?@ A A! As   Er   N)T)r$   r%   r&   r'   r(   rB   rT   r)   r   r   r   r3   r3   k   s    K<  48- r   r3   c                   0    \ rS rSrSrS rS rS rS rSr	g)	WADLResolvableDefinition   z=A base class for objects whose definitions may be references.c                     SU l         Xl        g)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>   r]   s     r   __init__!WADLResolvableDefinition.__init__   s      &r   c                     U R                   b  U R                   $ U R                  5       nUc  X l         U $ U R                  R                  U5      nU R	                  U5      nUc  [        SU-  5      eX0l         U$ )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.
No such XML ID: "%s")r\   _get_definition_urlr]   lookup_xml_id_definition_factoryKeyError)r>   
object_urlxml_id
definitions       r   resolve_definition+WADLResolvableDefinition.resolve_definition   s     '###--/
  $K !!//
;--f5
 1J>??%r   c                     [        5       e)zhTransform an XML ID into a wadllib wrapper object.

Which kind of object it is depends on the subclass.
NotImplementedErrorr>   ids     r   rd   ,WADLResolvableDefinition._definition_factory       
 "##r   c                     [        5       e)z]Find the URL that identifies an external reference.

How to do this depends on the subclass.
rl   r>   s    r   rb   ,WADLResolvableDefinition._get_definition_url   rq   r   r[   N)
r$   r%   r&   r'   r(   r^   ri   rd   rb   r)   r   r   r   rX   rX      s    G' D$$r   rX   c                      ^  \ rS rSrSr   SU 4S jjr\S 5       r\S 5       r\S 5       r	   SS jr
S r  SS	 jrSS
 jrSS jr\S 5       rSS jrS rS rS rS rSS jrS rSrU =r$ )r	      z/A resource, possibly bound to a representation.c                 <  > [         TU ]  U5        X l        [        U[        5      (       a+  U R
                  R                  U5      R                  U l        OX0l        SU l        Ub  US:X  an  U(       a`  [        US5      (       a*  [        R                  " UR                  5       5      U l        O:[        R                  " [	        U5      5      U l        OX@l        O[        SU-  5      eXPl        Ub+  Ub  Xpl        gU R!                  U R                  5      U l        gg)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/jsondecodez?This resource doesn't define a representation for media type %s)superr^   _url
isinstancestrr]   get_resource_typer:   representationhasattrjsonloadsry   r-   
media_typerepresentation_definitionget_representation_definition)	r>   r]   urlresource_typer   r   representation_needs_processingr   	__class__s	           r   r^   Resource.__init__   s   * 	%	mS)) ''99-HLLDH %H"%//2~x88.2jj*113/5+ /3jj^9L.M+*8'/$&012 2 %%(41J. 66tG .	 &r   c                     U R                   $ )z Return the URL to this resource.)r{   rs   s    r   r   Resource.url$  s     yyr   c                 @   U R                   c  gU R                   R                  R                  S5      nUb  U$ U R                   R                  R                  S5      nUb>  [        U R                  R
                  5      R                  5       n[        U5      S-   U-   $ g)z@Return the URL to the type definition for this resource, if any.Ntypero   #)r:   r<   r=   r   r]   
markup_urlensureSlashr}   )r>   r   type_idbases       r   type_urlResource.type_url)  s     88hhoo!!&)?J((//%%d+t''223??ADt9s?W,, r   c                 4    U R                   R                  S   $ )zReturn the ID of this resource.ro   )r:   r<   rs   s    r   ro   Resource.id;  s     xxt$$r   c           	      ^    [        U R                  U R                  U R                  XUU5      $ )a7  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	   r]   r   r:   )r>   r   r   r   r   s        r   bindResource.bind@  s.    " (($((DHH&713 	3r   c                     U R                  S5      R                  nU H@  nUR                  5       R                  nUR                  R                  S5      U:X  d  M>  Us  $    [        SU-  5      e)z<Get a description of one of this resource's representations.GET	mediaTypez4No definition for representation with media type %s.)
get_methodresponseri   r:   r<   r=   r-   )r>   r   default_get_responser   representation_tags        r   r   &Resource.get_representation_definitionV  st    #u5>>2N!/!B!B!D!H!H!((,,[9ZG%% 3 ( )>@J)K L 	Lr   c                    U R                  5        Hk  nUR                  R                  SS5      R                  5       nUb  XaR                  5       :X  d  ME  [	        X5      nUR                  X#U5      (       d  Mi  Us  $    g)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_iterr<   r=   lowerr   is_described_by)r>   http_methodr   query_paramsrepresentation_params
method_tagrG   methods           r   r   Resource.get_method`  sz    0 //1J$$((4::<D"d.?.?.A&A1))**?A A!M 2 r   c                 B    U R                  U5      R                  U 5      $ )aN  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_definitionrB   r>   r   s     r   
parametersResource.parameters  s#     33t	%r   c                 B    U R                  U5      R                  U 5      $ )aR  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   Resource.parameter_names  s$     33'-	.r   c              #   T   #    U R                  5        H  n[        X5      v   M     g7f)z6An iterator over the methods defined on this resource.N)r   r   )r>   r   s     r   method_iterResource.method_iter  s%      //1J** 2s   &(c                     U R                  U5      nUR                  nUR                  [        S5      5       H/  nUR                  R                  S5      U:X  d  M$  [        X5      s  $    g)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.
r6   rG   N)r   r:   r;   r   r<   r=   r   )r>   
param_namer   rh   r   rA   s         r   get_parameterResource.get_parameter  sb     99*E
'^^+33Jw4GHI##F+z9 11 I r   c                 F   U R                   c  [        S5      eU R                  S:X  a  UR                  S:w  a  [	        SUR                  -  5      eU R                   UR
                     nUbF  U R                  UR                  UR                  5      u  p4U[        :X  a  US;   a   [        U5      nU$ U$ [	        S	U R                  -  5      e! [         aI     [        R                  " [        R                  " US5      SS 6 n U$ ! [         a    [        U5      ef = ff = f)
zFind the value of a parameter, given the Parameter object.

:raise ValueError: If the parameter value can't be converted into
its defined type.
z,Resource is not bound to any representation.rx   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.)r   r   r   r7   rm   rG   _dereference_namespacer:   r   XML_SCHEMA_NS_URIr   r9   datetimetimestrptime)r>   	parameterrJ   namespace_url	data_types        r   get_parameter_valueResource.get_parameter_value  sC    &,>@ @??00 '))!*12 2 ''	7E +/+F+FMM9>>,3(!%66%)==4 ,U 3 L5L! #G$(OO#4 5 	5 & 	44$,$5$5"&--z"B1Q"G%JE L  * 4 #-U"33	4	4s   &C 
D +DDD c                     Ub  SU;   a  UR                  SS5      u  p2OSnUR                  [        5      nUR                  US5      nXR4$ )zxSplits a value into namespace URI and value.

:param tag: A tag to use as context when mapping namespace
names to URIs.
N:   r   )splitr=   NS_MAP)r>   r:   rJ   	namespacens_mapr   s         r   r   Resource._dereference_namespace  sP     ${{32IuI

9d3##r   c                 L    U R                   R                  R                  U5      $ )z-Given an ID, find a ResourceType for that ID.)r]   resource_typesr=   rn   s     r   rd   Resource._definition_factory  s    ..22266r   c                 L    U R                   R                  R                  S5      $ )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:   r<   r=   rs   s    r   rb   Resource._get_definition_url       xx""6**r   c                     U R                   b*  U R                  R                  5       nUR                  5       $ Ub!  U R                  U5      nUR                  5       $ [	        S5      e)a  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
zSResource is not bound to any representation, and no media media type was specified.)r   r   ri   r   r   )r>   r   rh   s      r   r   (Resource._find_representation_definition  st    * * 77JJLJ ,,.. #;;JGJ
 ,,.. -,- -r   c              #      #    U R                  5       R                  nUR                  [        S5      5       Sh  vN   g N7f)z+Iterate over this resource's <method> tags.r   N)ri   r:   r;   r   r>   rh   s     r   r   Resource._method_tag_iter   s2     ,,.22
%%j&:;;;s   8AA A)r{   r   r   r   r:   )NNTN)rx   TN)NNNNrV   )r$   r%   r&   r'   r(   r^   propertyr   r   ro   r   r   r   r   r   r   r   r   r   rd   rb   r   r   r)   __classcell__r   s   @r   r	   r	      s    9 2615+/5In    " % % /A-1'+3,L JN)-B%. + +
&-5^$7+ /D< <r   r	   c                       \ rS rSrSrS r\S 5       r\S 5       r\S 5       r	\S 5       r
SS	 jr  SS
 jr  SS jrSrg)r   i&  z*A wrapper around an XML <method> tag.
    c                 R    Xl         U R                   R                  U l        X l        g)zQInitialize with a <method> tag.

:param method_tag: An ElementTree <method> tag.
N)r8   r]   r:   )r>   r8   r   s      r   r^   Method.__init__)  s     
 !==44r   c                 \    [        X R                  R                  [        S5      5      5      $ )z@Return the definition of a request that invokes the WADL method.request)RequestDefinitionr:   findr   rs   s    r   r   Method.request2  s!     !xx}}Z	5J'KLLr   c                 r    [        U R                  U R                  R                  [	        S5      5      5      $ )z9Return the definition of the response to the WADL method.r   )r   r8   r:   r   r   rs   s    r   r   Method.response7  s.     "$--"&((--
:0F"GI 	Ir   c                 L    U R                   R                  R                  S5      $ )z)The XML ID of the WADL method definition.ro   r   rs   s    r   ro   	Method.id=  s     xx""4((r   c                 h    U R                   R                  R                  S5      R                  5       $ )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:   r<   r=   r   rs   s    r   rG   Method.nameB  s&     xx""6*0022r   Nc                 <    U R                   R                  " U40 UD6$ )4Return the request URL to use to invoke this method.)r   	build_url)r>   rO   rQ   s      r   build_request_urlMethod.build_request_urlK  s    ||%%lFoFFr   c                 <    U R                   R                  " X40 UD6$ )zrBuild a representation to be sent when invoking this method.

:return: A 2-tuple of (media_type, representation).
)r   r   )r>   r   rO   rQ   s       r   build_representationMethod.build_representationO  s&     ||**9(79 	9r   c                    SnUb  U R                   R                  U5      nUc  gUb=  [        U5      S:  a.  U R                   nUc  g UR                  UR                  US5        Ub  [        U5      S:X  a  gUb  UR                  U5      $ U R                   R                   H0  n UR                  UR                  U R                  5      US5          g   g! [
         a     gf = f! [
         a     MR  f = f)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   r   rK   rT   r   r9   r   representationsrB   r8   )r>   r   query_valuesrepresentation_valuesr   r   s         r   r   Method.is_described_byX  s   $ !!\\GGN%#L(9A(=llG --((,? "),-2%!11%' '"ll::N44"))$--8)52  ; '  "  s$   C &,C'
C$#C$'
C54C5)r]   r8   r:   rV   NN)NNN)r$   r%   r&   r'   r(   r^   r   r   r   ro   rG   r   r   r   r)   r   r   r   r   r   &  s     M M I I
 ) ) 3 3G /3*.9 =A.25r   r   c                   \    \ rS rSrSrS r\S 5       r\S 5       rSS jr	SS jr
SS	 jrS
rg)r   i  zBA wrapper around the description of the request invoking a method.c                     Xl         U R                   R                  U l        U R                  R                  U l        X l        g)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   r]   r:   )r>   r   request_tags      r   r^   RequestDefinition.__init__  s1     ,,==44r   c                 &    U R                  S/5      $ )z,Return the query parameters for this method.query)rB   rs   s    r   r   RequestDefinition.query_params  s     {{G9%%r   c              #      #    U R                   R                  [        S5      5       H&  n[        U R                  U R
                  U5      v   M(     g 7f)Nr   )r:   r;   r   r   r]   r8   r   s     r   r   !RequestDefinition.representations  sC     ((**:6F+GHJ*  $--= = Is   AANc                 X    U R                    H  nUb  UR                  U:X  d  M  Us  $    g)z1Return the appropriate representation definition.N)r   r   r>   r   r   s      r   r   /RequestDefinition.get_representation_definition  s0    "22N!^%>%>*%L%% 3 r   c                 l    U R                  U5      nUc  [        SU-  5      eUR                  " U40 UD6$ )zpBuild a representation to be sent along with this request.

:return: A 2-tuple of (media_type, representation).
z,Cannot build representation of media type %s)r   	TypeErrorr   )r>   r   rO   rQ   rh   s        r   r    RequestDefinition.representation  sF     77
C
J() * *|???r   c                     U R                   " U R                  U40 UD6nU R                  R                  n[	        U5      S:  a2  SU;   a  SnOSnXE[        [        UR                  5       5      5      -   -  nU$ )r   r   ?&)rT   r   r8   r   rK   r   sorteditems)r>   rO   rQ   rR   r   appends         r   r   RequestDefinition.build_url  s|    55|@/>@mm 1$czIf-=-C-C-E&FGGGC
r   )r]   r   r8   r:   rV   r   )r$   r%   r&   r'   r(   r^   r   r   r   r   r   r   r)   r   r   r   r   r     sB    L	 & & = =

@r   r   c                   @    \ rS rSrSrSS jrS rS rS rS r	S	 r
S
rg)r   i  z;A wrapper around the description of a response to a method.Nc                 J    UR                   U l         Xl        X l        X0l        g)zWInitialize with a <response> tag.

:param response_tag: An ElementTree <response> tag.
N)r]   r8   r:   headers)r>   r8   response_tagr  s       r   r^   ResponseDefinition.__init__  s!    
 $// r   c              #      #    [        S5      nU R                  R                  U5       H0  n[        U R                  R
                  U R                  U5      v   M2     g7f)zGet an iterator over the representation definitions.

These are the representations returned in response to an
invocation of this method.
r   N)r   r:   r;   r   r8   r]   )r>   pathr   s      r   __iter__ResponseDefinition.__iter__  sQ      *+"&(("2"24"8*))4==:LN N #9s   AAc                 D    [        U R                  U R                  U5      $ )zwBind 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   ResponseDefinition.bind  s     "$--7CCr   c                     U R                   R                  [        S5      5       HP  nUR                  R	                  S5      U:X  d  M$  UR                  R	                  S5      S:X  d  ME  [        X5      s  $    g)z,Find a header parameter within the response.r6   rG   r7   headerN)r:   r;   r   r<   r=   r   )r>   r   rA   s      r   r    ResponseDefinition.get_parameter  sb    ))*W*=>I  $$V,
:!((,,W5A 11 ? r   c                     U R                   c  [        S5      eUR                  S:w  a  [        SUR                  -  5      eU R                   R	                  UR
                  5      $ )z:Find the value of a parameter, given the Parameter object.z,Response object is not bound to any headers.r#  r   )r  r   r7   rm   r=   rG   )r>   r   s     r   r   &ResponseDefinition.get_parameter_value  sc    <<,>@ @??h&%&__-. . ||	//r   c                 Z    U R                   c  gU  H  nUR                  U:X  d  M  Us  $    g)z8Get one of the possible representations of the response.N)r:   r   r  s      r   r   0ResponseDefinition.get_representation_definition  s2    88"N((J6%% # r   )r]   r  r8   r:   rV   )r$   r%   r&   r'   r(   r^   r  r   r   r   r   r)   r   r   r   r   r     s%    E	ND	0r   r   c                   |   ^  \ rS rSrSrU 4S jrU 4S jrS r\S 5       r	S r
S rSS	 jrS
 rS rS rS rSrU =r$ )r   i  z2A definition of the structure of a representation.c                 <   > [         TU ]  U5        X l        X0l        g rV   )rz   r^   r8   r:   )r>   r]   r8   r   r   s       r   r^   !RepresentationDefinition.__init__  s    % %r   c                 (   > [         TU ]  SS/U5      $ )Nr  r   )rz   rB   )r>   r8   r   s     r   rB   RepresentationDefinition.params  s    w~w0(;;r   c                 b    U R                  U5       Vs/ s H  o"R                  PM     sn$ s  snf )z#Return the names of all parameters.)rB   rG   )r>   r8   r6   s      r   r   (RepresentationDefinition.parameter_names  s'    (,H(=>(=u

(=>>>s   ,c                 P    U R                  5       R                  R                  S   $ )z4The media type of the representation described here.r   )ri   r:   r<   rs   s    r   r   #RepresentationDefinition.media_type  s$     &&(,,33K@@r   c                    [        [        [        R                  S-
  5      5      nSU-  n[        R
                  " [        R                  5      nSX4-  -   S-   nUc  U$ UnSn S[        R                  " U5      -   S-   R                  S5      n[        R                  " X[        R                  S	9(       d   U$ US
-   [        U5      -   nUS-  nMn  )z;Make a random boundary that does not appear in `all_parts`.r   z%%0%ddz===============z==r   z^--z(--)?$ascii)flags.)rK   reprsysmaxsizerandom	randrangereescapeencodesearch	MULTILINEr}   )	r>   	all_parts_width_fmttokenboundarybcounterpatterns	            r   _make_boundary'RepresentationDefinition._make_boundary  s    T#++/*+&   -.5Oryy|+h6>>wGG99Wr||D  3W-AqLG r   c                     U Hg  u  p4UR                  UR                  S5      5        UR                  S5        UR                  UR                  S5      5        UR                  S5        Mi     UR                  S5        g)z$Write MIME headers to a file object.UTF-8s   :    
Nwriter=  )r>   bufr  keyrJ   s        r   _write_headers'RepresentationDefinition._write_headers1  s^    !JCIIcjj)*IIeIIell7+,IIg	 "
 			'r   c                     UR                  S5        UR                  UR                  S5      5        U(       a  UR                  S5        UR                  S5        g)z,Write a multipart boundary to a file object.s   --rK  rL  NrM  )r>   rO  rD  closings       r   _write_boundary(RepresentationDefinition._write_boundary:  s>    		%		(//'*+IIe		'r   c                 f   / nU GHs  u  p4n[         R                  " 5       nU(       a   SnS[        U5      < S[        U5      < S3nOSnS[        U5      -  nU R                  USSU4S	U4/5        U(       a>  [	        U[
        5      (       d  [        S
[        U5      -  5      eUR                  U5        O[	        U[        5      (       d  [        S[        U5      -  5      e[        R                  " SU5      n	U	SS  H4  n
UR                  U
R                  S5      5        UR                  S5        M6     UR                  U	S   R                  S5      5        UR                  UR                  5       5        GMv     U R                  SR!                  U5      5      n[         R                  " 5       nS[        U5      -  nU R                  USSU4/5        U H6  nU R#                  Xk5        UR                  U5        UR                  S5        M8     U R#                  XkSS9  XvR                  5       4$ )a  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|\nNrK  rL  z"multipart/form-data; boundary="%s"T)rT  )ioBytesIOr   rQ  r|   bytesr  r   rN  r}   r;  r   r=  r  getvaluerH  rL   rU  )r>   partsencoded_parts	is_binaryrG   rJ   rO  ctypecdisplineslinerD  encoded_parts                r   _generate_multipart_form1RepresentationDefinition._generate_multipart_formB  s    &+"IU**,C2 $Kt. 6.t<''&.& 
 !%//#$@4;$NOO		% !%--#5UCE E6!#2JDIIdkk'23IIg& ' 		%)**734  0? ',D &&w||M'BC jjl4uXFC#U#" 	 *L  /IIl#IIg * 	SD9lln$$r   c                 B   U R                  5       nUR                  U R                  5      nU R                  " XA40 UD6nU R                  nUS:X  a%  [        [        UR                  5       5      5      nXg4$ US:X  ax  / n[        5       n	U HP  n
UR                  U
R                  U	5      nXLd  M%  UR                  U
R                  S:H  U
R                  U45        MR     U R                  U5      u  pgXg4$ US:X  a  [        R                  " U5      nXg4$ [!        SU-  5      e)zjBind the definition to parameter values, creating a document.

:return: A 2-tuple (media_type, document).
z!application/x-www-form-urlencodedzmultipart/form-databinaryrx   zUnsupported media type: '%s')ri   rB   r8   rT   r   r   r  r  objectr=   rG   r  r   re  r   dumpsr9   )r>   rO   rQ   rh   rB   rR   r   docr]  missingr6   rJ   s               r   r   RepresentationDefinition.bind  s)   
 ,,.
""4==1555$35__
<<F#3#9#9#;<=C  00EhG(,,UZZA'LL%**"8%**e!LM   #;;EBOJ
 	 --**-.C  ;jHIIr   c                 L    U R                   R                  R                  U5      $ )z9Turn a representation ID into a RepresentationDefinition.)r]   representation_definitionsr=   rn   s     r   rd   ,RepresentationDefinition._definition_factory  s    ::>>rBBr   c                 L    U R                   R                  R                  S5      $ )zFind 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   rs   s    r   rb   ,RepresentationDefinition._get_definition_url  r   r   )r8   r:   )F)r$   r%   r&   r'   r(   r^   rB   r   r   r   rH  rQ  rU  re  r   rd   rb   r)   r   r   s   @r   r   r     sV    <&
<? A A$@%D4C+ +r   r   c                       \ rS rSrSrS r\S 5       r\S 5       r\S 5       r	\S 5       r
\S 5       rS	 r\S
 5       r\S 5       r\S 5       rSrg)r   i  z5One of the parameters of a representation definition.c                 >    UR                   U l         Xl        X l        g)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)r]   value_containerr:   )r>   rv  r:   s      r   r^   Parameter.__init__  s     +66.r   c                 L    U R                   R                  R                  S5      $ )zThe name of this parameter.rG   r   rs   s    r   rG   Parameter.name       xx""6**r   c                 L    U R                   R                  R                  S5      $ )zThe style of this parameter.r7   r   rs   s    r   r7   Parameter.style  s     xx""7++r   c                 L    U R                   R                  R                  S5      $ )zThe XSD type of this parameter.r   r   rs   s    r   r   Parameter.type  rz  r   c                 L    U R                   R                  R                  S5      $ )a  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   rs   s    r   rH   Parameter.fixed_value  s     xx""7++r   c                 p    U R                   R                  R                  SS5      R                  5       S;   $ )z6Whether or not a value for this parameter is required.requiredfalse)1truer   rs   s    r   rM   Parameter.is_required  s2     ##J8>>@ ! 	"r   c                 8    U R                   R                  U 5      $ )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.
)rv  r   rs   s    r   	get_valueParameter.get_value  s     ##77==r   c                     U R                   R                  [        S5      5       Vs/ s H  n[        X5      PM     sn$ s  snf )z7Return the set of acceptable values for this parameter.rS   )r:   r;   r   Option)r>   
option_tags     r   rI   Parameter.options  sF     #'(("2"2:h3G"HJ"HJ t("HJ 	J Js   ?c                 h    U R                   R                  [        S5      5      nUc  g[        X5      $ )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   r  Parameter.link  s/     88==F!34D##r   c                 N    U R                   nUc  [        S5      eUR                  $ )zFollow 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.
z(This parameter isn't a link to anything.)r  r9   follow)r>   r  s     r   linked_resourceParameter.linked_resource  s(     yy<GHH{{r   )r]   r:   rv  N)r$   r%   r&   r'   r(   r^   r   rG   r7   r   rH   rM   r  rI   r  r  r)   r   r   r   r   r     s    ? + + , , + + , , " "
> J J
 $ $  r   r   c                   .    \ rS rSrSrS r\S 5       rSrg)r  i
  z0One of a set of possible values for a parameter.c                     Xl         X l        g)zeInitialize the option.

:param parameter: A Parameter.
:param link_tag: An ElementTree <option> tag.
Nr   r:   )r>   r   r  s      r   r^   Option.__init__  s     #r   c                 L    U R                   R                  R                  S5      $ )NrJ   r   rs   s    r   rJ   Option.value  s    xx""7++r   r  N)	r$   r%   r&   r'   r(   r^   r   rJ   r)   r   r   r   r  r  
  s    : , ,r   r  c                   X   ^  \ rS rSrSrU 4S jr\S 5       r\S 5       rS r	S r
SrU =r$ )	r   i  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                 P   > [         TU ]  UR                  5        Xl        X l        g)zaInitialize the link.

:param parameter: A Parameter.
:param link_tag: An ElementTree <link> tag.
N)rz   r^   r]   r   r:   )r>   r   r  r   s      r   r^   Link.__init__"  s"     	../"r   c                 Z    U R                   (       d  [        S5      eU R                  5       $ )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   ri   rs   s    r   r  Link.follow,  s.      . / / &&((r   c                 F     U R                  5         g! [         a     gf = f)z|Can this link be followed within wadllib?

wadllib can follow a link if it points to a resource that has
a WADL definition.
FT)rb   r   rs   s    r   r  Link.can_follow5  s,    	$$&   		s    
  c                     [        U R                  U R                  R                  5       U R                  R                  R                  U5      R                  5      $ )z,Turn a resource type ID into a ResourceType.)r	   r]   r   r  r   r=   r:   rn   s     r   rd   Link._definition_factoryB  sG    dnn668++//3779 	9r   c                 l    U R                   R                  R                  S5      nUc  [        S5      eU$ )z(Find the URL containing the definition .r   zIParameter is a link, but not to a resource with a known WADL description.)r:   r<   r=   r   )r>   r   s     r   rb   Link._get_definition_urlH  s6    xx""?3< = > >r   r  )r$   r%   r&   r'   r(   r^   r   r  r  rd   rb   r)   r   r   s   @r   r   r     sD     ) ) 
 
9 r   r   c                       \ rS rSrSrS rSrg)r
   iQ  z,A wrapper around an XML <resource_type> tag.c                     Xl         g)zfInitialize with a <resource_type> tag.

:param resource_type_tag: An ElementTree <resource_type> tag.
Nr:   )r>   resource_type_tags     r   r^   ResourceType.__init__T  s	    
 %r   r  N)r$   r%   r&   r'   r(   r^   r)   r   r   r   r
   r
   Q  s
    6%r   r
   c                   <    \ rS rSrSrS rS rS rS rS r	S r
S	rg
)r   i\  z1A WADL document made programmatically accessible.c                    Xl         [        US5      (       a  U R                  U5      U l        OU R	                  U5      U l        U R                  R                  [        S5      5      U l        U R                  R                  R                  S5      U l
        0 U l        0 U l        U R                  R                  [        S5      5       H>  nUR                  R                  S5      nUc  M#  [        U SU5      nXPR                  U'   M@     U R                  R                  [        S5      5       H*  nUR                  S   n[        U5      U R                  U'   M,     g)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   r   ro   Nr   )r   r   _from_streamrk  _from_stringr   r   r  r<   r=   resource_basero  r   r;   r   r
   )r>   r   markupr   ro   rh   r   s          r   r^   Application.__init___  s     %66""((0DH((0DHz+'>?!^^2266v>*,' "hh..z:J/KLN&&**40B~5$0
6@//3 M "XX--j.IJM%%d+B&2=&AD# Kr   c                 2   SnSn/ n[         R                  " X5       Hb  u  pVUS:X  a  UR                  U5        M  US:X  a  UR                  5         M6  US:X  d  M>  Uc  UnUR	                  [
        [        U5      5        Md     [         R                  " U5      $ )zbTurns markup into a document.

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

'!<Df. 8 ~~d##r   c                     [        U[        5      (       d  UR                  S5      nU R                  [        R
                  " U5      5      $ )zTurns markup into a document.rK  )r|   r[  r=  r  rY  rZ  )r>   r  s     r   r  Application._from_string  s7    &%((]]7+F  F!344r   c                     U R                  U5      nU R                  R                  U5      nUc  [        SU-  5      eU$ )z7Retrieve a resource type by the URL of its description.ra   )rc   r   r=   re   )r>   resource_type_urlrg   r   s       r   r~   Application.get_resource_type  sG    ##$56++//7 14EEFFr   c                 (   [        U R                  5      R                  5       nSUl        UR	                  S5      (       a  [        U5      R                  5       nOUR                  U5      nUR                  nSUl        X2:X  a  U$ [        SU-  5      e)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resolverm   )r>   r   
markup_urithis_uripossible_xml_ids        r   rc   Application.lookup_xml_id  s     )779
"
>>&!!3x--/H "))#.H"++ ! #" " #-/2#3 4 	4r   c                    U R                    Vs/ s H  nUR                  S   U:X  d  M  UPM     nn[        U5      S:  a  g[        U5      S:  a  [        SU-  5      e[	        U [        U R                  US5      US   5      $ s  snf )z`Locate one of the resources described by this document.

:param path: The path to the resource.
r  r   Nz+More than one resource defined with path %sTr   )r  r<   rK   r   r	   r   r  )r>   r  r8   matchings       r   get_resource_by_path Application.get_resource_by_path  s     .2^^ 8^v.$6 ^ 8x=1x=1I"# $ $%**D$7!F 	F8s
   BB)rk  r   ro  r  r   r  N)r$   r%   r&   r'   r(   r^   r  r  r~   rc   r  r)   r   r   r   r   r   \  s&    ;B2$(54@Fr   r   ),r(   __all__r   email.utilsr   rY  r   r9  r;  r7  r   urllib.parser   xml.etree.ElementTreeetreer  r  lazr.urir   r   wadllib.iso_strptimer   r   r   r   r   r    	Exceptionr   r   r-   r0   r3   rX   r	   r   r   r   r   r   r  r   r
   r   r   r   r   <module>r     s1  "
   	   	 
  " " "  -	6 ?
%
		 	
 	 J J?  ? D:$x :$zy<' y<x	gX gT6"4 6r?+ ?D^+79K ^+B[ [|,X ,"3# 3l%8 %nF( nFr   