ó
ÞzlOc        
   @   sg  d  Z  e Z d d d d d d d d d	 d
 g
 Z y d d l m Z Wn! e k
 rg d d l m Z n Xd d l	 Z	 y$ d d l
 m Z d d l m Z Wn1 e k
 rË d d l m Z d d l m Z n Xd d l Z y d d l m Z Wn! e k
 rd d l m Z n Xy d d l Z Wn e k
 r?d d l Z n Xy d d l j j Z WnD e k
 rœy d d l Z Wn  e k
 r˜d d l j Z n Xn Xd 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( d
 e) f d „  ƒ  YZ* d e* f d „  ƒ  YZ+ d e* f d „  ƒ  YZ, d e- f d „  ƒ  YZ. d  f  d! „  ƒ  YZ/ d" e. f d# „  ƒ  YZ0 d e0 f d$ „  ƒ  YZ1 d e. f d% „  ƒ  YZ2 d& e. e/ f d' „  ƒ  YZ3 d e/ f d( „  ƒ  YZ4 d e0 e/ f d) „  ƒ  YZ5 d e. f d* „  ƒ  YZ6 d+ e. f d, „  ƒ  YZ7 d e0 f d- „  ƒ  YZ8 d	 e. f d. „  ƒ  YZ9 d e. f d/ „  ƒ  YZ: d S(0   sû  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.
t   Applicationt   Linkt   Methodt   NoBoundRepresentationErrort	   Parametert   RepresentationDefinitiont   ResponseDefinitiont   Resourcet   ResourceTypet	   WADLErroriÿÿÿÿ(   t   StringIO(   t   BytesION(   t   MIMEMultipart(   t   MIMENonMultipart(   t	   urlencode(   t   URIt   merge(   t   _make_unicodet   _string_types(   t   iso_strptimes	   xmlns:maps    http://www.w3.org/2001/XMLSchemac         C   s   d |  S(   s)   Scope a tag name with the WADL namespace.s&   {http://research.sun.com/wadl/2006/10}(    (   t   tag_name(    (    s7   /usr/lib/python2.7/dist-packages/wadllib/application.pyt   wadl_tagW   s    c         C   s   d t  |  ƒ S(   s#   Turn a tag name into an XPath path.s   ./(   R   (   R   (    (    s7   /usr/lib/python2.7/dist-packages/wadllib/application.pyt
   wadl_xpath\   s    c          G   s7   i  } x* |  D]" } | d k	 r | j | ƒ q q W| S(   s<   Merge any number of dictionaries, some of which may be None.N(   t   Nonet   update(   t   dictst   finalt   dict(    (    s7   /usr/lib/python2.7/dist-packages/wadllib/application.pyt   _merge_dictsa   s
    c           B   s   e  Z d  Z RS(   sA   An exception having to do with the state of the WADL application.(   t   __name__t
   __module__t   __doc__(    (    (    s7   /usr/lib/python2.7/dist-packages/wadllib/application.pyR	   j   s   c           B   s   e  Z d  Z RS(   sA  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   R   R   (    (    (    s7   /usr/lib/python2.7/dist-packages/wadllib/application.pyR   o   s   t   UnsupportedMediaTypeErrorc           B   s   e  Z d  Z RS(   s‘   A 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   R   R   (    (    (    s7   /usr/lib/python2.7/dist-packages/wadllib/application.pyR    y   s   t   WADLBasec           B   s   e  Z d  Z RS(   s?   A base class for objects that contain WADL-derived information.(   R   R   R   (    (    (    s7   /usr/lib/python2.7/dist-packages/wadllib/application.pyR!      s   t   HasParametersMixinc           B   s&   e  Z d  Z d d „ Z e d „ Z RS(   sA   A mixin class for objects that have associated Parameter objects.c         C   s¦   | d k r |  j } n  | d k r3 t d ƒ ‚ n  |  j d k rF g  S|  j j t d ƒ ƒ } | d k rn g  Sg  | D]- } | j j d ƒ | k ru t | | ƒ ^ qu S(   s6   Find subsidiary parameters that have the given styles.s&   Could not find any particular resourcet   paramt   styleN(	   R   t   resourcet
   ValueErrort   tagt   findallR   t   attribt   getR   (   t   selft   stylesR%   t
   param_tagst	   param_tag(    (    s7   /usr/lib/python2.7/dist-packages/wadllib/application.pyt   paramsˆ   s    
c   
   
   K   s  t  | | ƒ } i  } x@| D]8} | j } | j d k	 rŒ | | k r| | | | j k r| t d | | | | j f ƒ ‚ n  | j | | <n  g  | j D] } | j ^ q– }	 t |	 ƒ d k r| | k r| | |	 k rt d | | | d j |	 ƒ f ƒ ‚ n  | r0| j	 r0| | k r0t d | ƒ ‚ n  | | k r | | | | <| | =q q Wt | ƒ d k rŒt d d j | j
 ƒ  ƒ ƒ ‚ n  | S(	   s¼  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.
        s=   Value '%s' for parameter '%s' conflicts with fixed value '%s'i    s=   Invalid value '%s' for parameter '%s': valid values are: "%s"s   ", "s$   No value for required parameter '%s's   Unrecognized parameter(s): '%s's   ', 'N(   R   t   namet   fixed_valueR   R&   t   optionst   valuet   lent   joint   is_requiredt   keys(
   R+   R/   t   param_valuest   enforce_completenesst   kw_param_valuest   validated_valuesR#   R0   t   optionR2   (    (    s7   /usr/lib/python2.7/dist-packages/wadllib/application.pyt   validate_param_values—   s6    	
#N(   R   R   R   R   R/   t   TrueR=   (    (    (    s7   /usr/lib/python2.7/dist-packages/wadllib/application.pyR"   …   s   t   WADLResolvableDefinitionc           B   s2   e  Z d  Z d „  Z d „  Z d „  Z d „  Z RS(   s=   A base class for objects whose definitions may be references.c         C   s   d |  _ | |  _ d S(   s§   Initialize with a WADL application.

        :param application: A WADLDefinition. Relative links are
            assumed to be relative to this object's URL.
        N(   R   t   _definitiont   application(   R+   RA   (    (    s7   /usr/lib/python2.7/dist-packages/wadllib/application.pyt   __init__Ê   s    	c         C   sˆ   |  j  d k	 r |  j  S|  j ƒ  } | d k r; |  |  _  |  S|  j j | ƒ } |  j | ƒ } | d k r{ t d | ƒ ‚ n  | |  _  | S(   sÞ  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.
        s   No such XML ID: "%s"N(   R@   R   t   _get_definition_urlRA   t   lookup_xml_idt   _definition_factoryt   KeyError(   R+   t
   object_urlt   xml_idt
   definition(    (    s7   /usr/lib/python2.7/dist-packages/wadllib/application.pyt   resolve_definitionÓ   s    		c         C   s   t  ƒ  ‚ d S(   sx   Transform an XML ID into a wadllib wrapper object.

        Which kind of object it is depends on the subclass.
        N(   t   NotImplementedError(   R+   t   id(    (    s7   /usr/lib/python2.7/dist-packages/wadllib/application.pyRE   õ   s    c         C   s   t  ƒ  ‚ d S(   sm   Find the URL that identifies an external reference.

        How to do this depends on the subclass.
        N(   RK   (   R+   (    (    s7   /usr/lib/python2.7/dist-packages/wadllib/application.pyRC   ü   s    (   R   R   R   RB   RJ   RE   RC   (    (    (    s7   /usr/lib/python2.7/dist-packages/wadllib/application.pyR?   Ç   s
   			"	c           B   sì   e  Z d  Z d d e d d „ Z e d „  ƒ Z e d „  ƒ Z e d „  ƒ Z	 d e d d „ Z
 d „  Z d d d 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 RS(   s/   A resource, possibly bound to a representation.c         C   sü   t  t |  ƒ j | ƒ | |  _ t | t ƒ rI |  j j | ƒ j |  _ n	 | |  _ d |  _
 | d k	 r³ | d k r  | r” t j t | ƒ ƒ |  _
 q° | |  _
 q³ t d | ƒ ‚ n  | |  _ | d k	 rø | d k	 rà | |  _ qø |  j |  j ƒ |  _ n  d S(   s[  
        :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.
        s   application/jsons?   This resource doesn't define a representation for media type %sN(   t   superR   RB   t   _urlt
   isinstanceR   RA   t   get_resource_typeR'   R   t   representationt   jsont   loadsR   R    t
   media_typet   representation_definitiont   get_representation_definition(   R+   RA   t   urlt   resource_typeRQ   RT   t   representation_needs_processingRU   (    (    s7   /usr/lib/python2.7/dist-packages/wadllib/application.pyRB     s(    				c         C   s   |  j  S(   s    Return the URL to this resource.(   RN   (   R+   (    (    s7   /usr/lib/python2.7/dist-packages/wadllib/application.pyRW   ;  s    c         C   s‡   |  j  d k r d S|  j  j j d ƒ } | d k	 r8 | S|  j  j j d ƒ } | d k	 rƒ t |  j j ƒ j ƒ  } t | ƒ d | Sd S(   s@   Return the URL to the type definition for this resource, if any.t   typeRL   t   #N(	   R'   R   R)   R*   R   RA   t
   markup_urlt   ensureSlasht   str(   R+   RW   t   type_idt   base(    (    s7   /usr/lib/python2.7/dist-packages/wadllib/application.pyt   type_url@  s    c         C   s   |  j  j d S(   s   Return the ID of this resource.RL   (   R'   R)   (   R+   (    (    s7   /usr/lib/python2.7/dist-packages/wadllib/application.pyRL   R  s    s   application/jsonc         C   s%   t  |  j |  j |  j | | | | ƒ S(   s—  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   RA   RW   R'   (   R+   RQ   RT   RY   RU   (    (    s7   /usr/lib/python2.7/dist-packages/wadllib/application.pyt   bindW  s    c         C   sb   |  j  d ƒ j } x9 | D]1 } | j ƒ  j } | j j d ƒ | k r | Sq Wt d | ƒ ‚ d S(   s<   Get a description of one of this resource's representations.t   GETt	   mediaTypes4   No definition for representation with media type %s.N(   t
   get_methodt   responseRJ   R'   R)   R*   R    (   R+   RT   t   default_get_responseRQ   t   representation_tag(    (    s7   /usr/lib/python2.7/dist-packages/wadllib/application.pyRV   m  s    c         C   s   xx |  j  ƒ  D]j } | j j d d ƒ j ƒ  } | d k sL | | j ƒ  k r t |  | ƒ } | j | | | ƒ rw | Sq q Wd S(   s   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.
        R0   t    N(   t   _method_tag_iterR)   R*   t   lowerR   R   t   is_described_by(   R+   t   http_methodRT   t   query_paramst   representation_paramst
   method_tagR0   t   method(    (    s7   /usr/lib/python2.7/dist-packages/wadllib/application.pyRe   w  s    	c         C   s   |  j  | ƒ j |  ƒ S(   s~  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.
        (   t   _find_representation_definitionR/   (   R+   RT   (    (    s7   /usr/lib/python2.7/dist-packages/wadllib/application.pyt
   parameters˜  s    
c         C   s   |  j  | ƒ j |  ƒ S(   s‚  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.
        (   Rr   t   parameter_names(   R+   RT   (    (    s7   /usr/lib/python2.7/dist-packages/wadllib/application.pyRt   ¥  s    
c         c   s)   x" |  j  ƒ  D] } t |  | ƒ Vq Wd S(   s6   An iterator over the methods defined on this resource.N(   Rj   R   (   R+   Rp   (    (    s7   /usr/lib/python2.7/dist-packages/wadllib/application.pyt   method_iter²  s    c         C   sa   |  j  | ƒ } | j } xB | j t d ƒ ƒ D]+ } | j j d ƒ | k r. t |  | ƒ Sq. Wd S(   sÈ  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.
        R#   R0   N(   Rr   R'   R(   R   R)   R*   R   R   (   R+   t
   param_nameRT   RI   Rh   R.   (    (    s7   /usr/lib/python2.7/dist-packages/wadllib/application.pyt   get_parameter¸  s    	c         C   s/  |  j  d k r t d ƒ ‚ n  |  j d k r| j d k rR t d | j ƒ ‚ n  |  j  | j } | d k	 r|  j | j | j	 ƒ \ } } | t
 k r| d k ry t | ƒ } Wqt k
 ry& t j t j | d ƒ d d	 !Œ  } Wqt k
 r	t | ƒ ‚ qXqXqn  | St d
 |  j ƒ ‚ d S(   s¨   Find the value of a parameter, given the Parameter object.

        :raise ValueError: If the parameter value can't be converted into
        its defined type.
        s,   Resource is not bound to any representation.s   application/jsont   plains8   Don't know how to find value for a parameter of type %s.t   dateTimet   dates   %Y-%m-%di    i   sE   Path traversal not implemented for a representation of media type %s.N(   Ry   Rz   (   RQ   R   R   RT   R$   RK   R0   t   _dereference_namespaceR'   RZ   t   XML_SCHEMA_NS_URIR   R&   t   datetimet   timet   strptime(   R+   t	   parameterR3   t   namespace_urlt	   data_type(    (    s7   /usr/lib/python2.7/dist-packages/wadllib/application.pyt   get_parameter_valueË  s2     c         C   sd   | d k	 r3 d | k r3 | j d d ƒ \ } } n d } | j t ƒ } | j | d ƒ } | | f S(   s   Splits a value into namespace URI and value.

        :param tag: A tag to use as context when mapping namespace
        names to URIs.
        t   :i   Ri   N(   R   t   splitR*   t   NS_MAP(   R+   R'   R3   t	   namespacet   ns_mapR   (    (    s7   /usr/lib/python2.7/dist-packages/wadllib/application.pyR{   û  s    c         C   s   |  j  j j | ƒ S(   s-   Given an ID, find a ResourceType for that ID.(   RA   t   resource_typesR*   (   R+   RL   (    (    s7   /usr/lib/python2.7/dist-packages/wadllib/application.pyRE   	  s    c         C   s   |  j  j j d ƒ S(   sý   Return 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.
        RZ   (   R'   R)   R*   (   R+   (    (    s7   /usr/lib/python2.7/dist-packages/wadllib/application.pyRC     s    c         C   sU   |  j  d k	 r! |  j j ƒ  } n* | d k	 r? |  j | ƒ } n t d ƒ ‚ | j ƒ  S(   s3  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
        sS   Resource is not bound to any representation, and no media media type was specified.N(   RQ   R   RU   RJ   RV   R   (   R+   RT   RI   (    (    s7   /usr/lib/python2.7/dist-packages/wadllib/application.pyRr     s    	c         c   s8   |  j  ƒ  j } x" | j t d ƒ ƒ D] } | Vq% Wd S(   s+   Iterate over this resource's <method> tags.Rq   N(   RJ   R'   R(   R   (   R+   RI   Rp   (    (    s7   /usr/lib/python2.7/dist-packages/wadllib/application.pyRj   9  s    N(   R   R   R   R   R>   RB   t   propertyRW   Ra   RL   Rb   RV   Re   Rs   Rt   Ru   Rw   Rƒ   R{   RE   RC   Rr   Rj   (    (    (    s7   /usr/lib/python2.7/dist-packages/wadllib/application.pyR     s.   1	
	 	0				#c           B   s€   e  Z d  Z d „  Z e d „  ƒ Z e d „  ƒ Z e d „  ƒ Z e d „  ƒ Z d	 d „ Z
 d	 d	 d „ Z d	 d	 d	 d „ Z RS(
   s*   A wrapper around an XML <method> tag.
    c         C   s%   | |  _  |  j  j |  _ | |  _ d S(   sa   Initialize with a <method> tag.

        :param method_tag: An ElementTree <method> tag.
        N(   R%   RA   R'   (   R+   R%   Rp   (    (    s7   /usr/lib/python2.7/dist-packages/wadllib/application.pyRB   C  s    	c         C   s   t  |  |  j j t d ƒ ƒ ƒ S(   s@   Return the definition of a request that invokes the WADL method.t   request(   t   RequestDefinitionR'   t   findR   (   R+   (    (    s7   /usr/lib/python2.7/dist-packages/wadllib/application.pyR‹   L  s    c         C   s"   t  |  j |  j j t d ƒ ƒ ƒ S(   s9   Return the definition of the response to the WADL method.Rf   (   R   R%   R'   R   R   (   R+   (    (    s7   /usr/lib/python2.7/dist-packages/wadllib/application.pyRf   Q  s    	c         C   s   |  j  j j d ƒ S(   s)   The XML ID of the WADL method definition.RL   (   R'   R)   R*   (   R+   (    (    s7   /usr/lib/python2.7/dist-packages/wadllib/application.pyRL   W  s    c         C   s   |  j  j j d ƒ j ƒ  S(   s«   The 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.
        R0   (   R'   R)   R*   Rk   (   R+   (    (    s7   /usr/lib/python2.7/dist-packages/wadllib/application.pyR0   \  s    c         K   s   |  j  j | |  S(   s4   Return the request URL to use to invoke this method.(   R‹   t	   build_url(   R+   R8   R:   (    (    s7   /usr/lib/python2.7/dist-packages/wadllib/application.pyt   build_request_urle  s    c         K   s   |  j  j | | |  S(   s‚   Build a representation to be sent when invoking this method.

        :return: A 2-tuple of (media_type, representation).
        (   R‹   RQ   (   R+   RT   R8   R:   (    (    s7   /usr/lib/python2.7/dist-packages/wadllib/application.pyt   build_representationi  s    	c         C   s1  d } | d k	 r7 |  j j | ƒ } | d k r7 t Sn  | d k	 r  t | ƒ d k r  |  j } | d k rn t Sy | j | j | t ƒ Wq  t k
 rœ t SXn  | d k s¾ t | ƒ d k rÂ t S| d k	 rÛ | j	 | ƒ SxO |  j j
 D]A } y' | j | j |  j ƒ | t ƒ t SWqè t k
 r(qè Xqè Wt S(   s  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.
        i    N(   R   R‹   RV   t   FalseR4   R=   Rn   R&   R>   Rl   t   representationsR/   R%   (   R+   RT   t   query_valuest   representation_valuesRQ   R‹   (    (    s7   /usr/lib/python2.7/dist-packages/wadllib/application.pyRl   r  s<    			
N(   R   R   R   RB   RŠ   R‹   Rf   RL   R0   R   R   R   Rl   (    (    (    s7   /usr/lib/python2.7/dist-packages/wadllib/application.pyR   @  s   			RŒ   c           B   s\   e  Z d  Z d „  Z e d „  ƒ Z e d „  ƒ Z d d „ Z d d d „ Z	 d d „ Z
 RS(   sB   A wrapper around the description of the request invoking a method.c         C   s4   | |  _  |  j  j |  _ |  j j |  _ | |  _ d S(   s­   Initialize with a <request> tag.

        :param resource: The resource to which this request can be sent.
        :param request_tag: An ElementTree <request> tag.
        N(   Rq   R%   RA   R'   (   R+   Rq   t   request_tag(    (    s7   /usr/lib/python2.7/dist-packages/wadllib/application.pyRB   ¬  s    	c         C   s   |  j  d g ƒ S(   s,   Return the query parameters for this method.t   query(   R/   (   R+   (    (    s7   /usr/lib/python2.7/dist-packages/wadllib/application.pyRn   ·  s    c         c   s>   x7 |  j  j t d ƒ ƒ D] } t |  j |  j | ƒ Vq Wd  S(   NRQ   (   R'   R(   R   R   RA   R%   (   R+   RI   (    (    s7   /usr/lib/python2.7/dist-packages/wadllib/application.pyR’   ¼  s    c         C   s7   x0 |  j  D]% } | d k s+ | j | k r
 | Sq
 Wd S(   s1   Return the appropriate representation definition.N(   R’   R   RT   (   R+   RT   RQ   (    (    s7   /usr/lib/python2.7/dist-packages/wadllib/application.pyRV   Â  s    c         K   s>   |  j  | ƒ } | d k r. t d | ƒ ‚ n  | j | |  S(   s€   Build a representation to be sent along with this request.

        :return: A 2-tuple of (media_type, representation).
        s,   Cannot build representation of media type %sN(   RV   R   t	   TypeErrorRb   (   R+   RT   R8   R:   RI   (    (    s7   /usr/lib/python2.7/dist-packages/wadllib/application.pyRQ   É  s
    c         K   sl   |  j  |  j | |  } |  j j } t | ƒ d k rh d | k rK d } n d } | | t | ƒ 7} n  | S(   s4   Return the request URL to use to invoke this method.i    t   ?t   &(   R=   Rn   R%   RW   R4   R   (   R+   R8   R:   R;   RW   t   append(    (    s7   /usr/lib/python2.7/dist-packages/wadllib/application.pyRŽ   Õ  s    	N(   R   R   R   RB   RŠ   Rn   R’   R   RV   RQ   RŽ   (    (    (    s7   /usr/lib/python2.7/dist-packages/wadllib/application.pyRŒ   ª  s   	c           B   sG   e  Z d  Z d d „ Z d „  Z d „  Z d „  Z d „  Z d „  Z	 RS(   s;   A wrapper around the description of a response to a method.c         C   s+   | j  |  _  | |  _ | |  _ | |  _ d S(   sg   Initialize with a <response> tag.

        :param response_tag: An ElementTree <response> tag.
        N(   RA   R%   R'   t   headers(   R+   R%   t   response_tagR›   (    (    s7   /usr/lib/python2.7/dist-packages/wadllib/application.pyRB   ë  s    		c         c   sG   t  d ƒ } x4 |  j j | ƒ D]  } t |  j j |  j | ƒ Vq Wd S(   s¢   Get an iterator over the representation definitions.

        These are the representations returned in response to an
        invocation of this method.
        RQ   N(   R   R'   R(   R   R%   RA   (   R+   t   pathRh   (    (    s7   /usr/lib/python2.7/dist-packages/wadllib/application.pyt   __iter__õ  s    c         C   s   t  |  j |  j | ƒ S(   s   Bind the response to a set of HTTP headers.

        A WADL response can have associated header parameters, but no
        other kind.
        (   R   R%   R'   (   R+   R›   (    (    s7   /usr/lib/python2.7/dist-packages/wadllib/application.pyRb      s    c         C   sd   x] |  j  j t d ƒ ƒ D]C } | j j d ƒ | k r | j j d ƒ d k r t |  | ƒ Sq Wd S(   s,   Find a header parameter within the response.R#   R0   R$   t   headerN(   R'   R(   R   R)   R*   R   R   (   R+   Rv   R.   (    (    s7   /usr/lib/python2.7/dist-packages/wadllib/application.pyRw     s
    c         C   sV   |  j  d k r t d ƒ ‚ n  | j d k rC t d | j ƒ ‚ n  |  j  j | j ƒ S(   s:   Find the value of a parameter, given the Parameter object.s,   Response object is not bound to any headers.RŸ   s8   Don't know how to find value for a parameter of type %s.N(   R›   R   R   R$   RK   R*   R0   (   R+   R€   (    (    s7   /usr/lib/python2.7/dist-packages/wadllib/application.pyRƒ     s    c         C   s;   |  j  d k r d Sx! |  D] } | j | k r | Sq Wd S(   s8   Get one of the possible representations of the response.N(   R'   R   RT   (   R+   RT   RQ   (    (    s7   /usr/lib/python2.7/dist-packages/wadllib/application.pyRV     s    N(
   R   R   R   R   RB   Rž   Rb   Rw   Rƒ   RV   (    (    (    s7   /usr/lib/python2.7/dist-packages/wadllib/application.pyR   ã  s   
				c           B   sS   e  Z d  Z d „  Z d „  Z d „  Z e d „  ƒ Z d „  Z d „  Z	 d „  Z
 RS(   s2   A definition of the structure of a representation.c         C   s,   t  t |  ƒ j | ƒ | |  _ | |  _ d  S(   N(   RM   R   RB   R%   R'   (   R+   RA   R%   Rh   (    (    s7   /usr/lib/python2.7/dist-packages/wadllib/application.pyRB   (  s    	c         C   s   t  t |  ƒ j d d g | ƒ S(   NR–   Rx   (   RM   R   R/   (   R+   R%   (    (    s7   /usr/lib/python2.7/dist-packages/wadllib/application.pyR/   -  s    c         C   s#   g  |  j  | ƒ D] } | j ^ q S(   s#   Return the names of all parameters.(   R/   R0   (   R+   R%   R#   (    (    s7   /usr/lib/python2.7/dist-packages/wadllib/application.pyRt   1  s    c         C   s   |  j  ƒ  j j d S(   s4   The media type of the representation described here.Rd   (   RJ   R'   R)   (   R+   (    (    s7   /usr/lib/python2.7/dist-packages/wadllib/application.pyRT   5  s    c         K   s»  |  j  ƒ  } | j |  j ƒ } |  j | | |  } |  j } | d k rW t | ƒ } nZ| d k rƒt ƒ  } | j d ƒ t ƒ  }	 x¬ | D]¤ }
 | j	 |
 j
 |	 ƒ } | |	 k	 r‰ |
 j d k rÔ d \ } } i  } n d \ } } i d d	 6} t | | |  } | j | ƒ d
 |
 j
 | d <| j | ƒ q‰ q‰ Wt | ƒ } | j d ƒ rf| | j d ƒ d } n  | j ƒ  d | j ƒ  } n. | d k r¡t j | ƒ } n t d | ƒ ‚ | | f S(   sz   Bind the definition to parameter values, creating a document.

        :return: A 2-tuple (media_type, document).
        s!   application/x-www-form-urlencodeds   multipart/form-datat   binaryRA   s   octet-streamt   textRx   s   utf-8t   charsets   form-data; name="%s"s   Content-Dispositions   From s   
i   s   ; boundary="%s"s   application/jsons   Unsupported media type: '%s'(   s   applications   octet-stream(   s   texts   plain(   RJ   R/   R%   R=   RT   R   R   t   set_typet   objectR*   R0   RZ   R   t   set_payloadt   attachR^   t
   startswithR   t   get_content_typet   get_boundaryRR   t   dumpsR&   (   R+   R8   R:   RI   R/   R;   RT   t   doct   outert   missingR#   R3   t   maintypet   subtypet   inner(    (    s7   /usr/lib/python2.7/dist-packages/wadllib/application.pyRb   :  s@    					c         C   s   |  j  j j | ƒ S(   s9   Turn a representation ID into a RepresentationDefinition.(   RA   t   representation_definitionsR*   (   R+   RL   (    (    s7   /usr/lib/python2.7/dist-packages/wadllib/application.pyRE   e  s    c         C   s   |  j  j j d ƒ S(   s  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.
        t   href(   R'   R)   R*   (   R+   (    (    s7   /usr/lib/python2.7/dist-packages/wadllib/application.pyRC   i  s    (   R   R   R   RB   R/   Rt   RŠ   RT   Rb   RE   RC   (    (    (    s7   /usr/lib/python2.7/dist-packages/wadllib/application.pyR   %  s   				+	c           B   s˜   e  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 RS(   s5   One of the parameters of a representation definition.c         C   s"   | j  |  _  | |  _ | |  _ d S(   sÏ  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(   RA   t   value_containerR'   (   R+   R³   R'   (    (    s7   /usr/lib/python2.7/dist-packages/wadllib/application.pyRB   v  s    
	c         C   s   |  j  j j d ƒ S(   s   The name of this parameter.R0   (   R'   R)   R*   (   R+   (    (    s7   /usr/lib/python2.7/dist-packages/wadllib/application.pyR0   „  s    c         C   s   |  j  j j d ƒ S(   s   The style of this parameter.R$   (   R'   R)   R*   (   R+   (    (    s7   /usr/lib/python2.7/dist-packages/wadllib/application.pyR$   ‰  s    c         C   s   |  j  j j d ƒ S(   s   The XSD type of this parameter.RZ   (   R'   R)   R*   (   R+   (    (    s7   /usr/lib/python2.7/dist-packages/wadllib/application.pyRZ   Ž  s    c         C   s   |  j  j j d ƒ S(   s9  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.
        t   fixed(   R'   R)   R*   (   R+   (    (    s7   /usr/lib/python2.7/dist-packages/wadllib/application.pyR1   “  s    	c         C   s"   |  j  j j d d ƒ j ƒ  d k S(   s6   Whether or not a value for this parameter is required.t   requiredt   falset   1t   true(   R·   R¸   (   R'   R)   R*   Rk   (   R+   (    (    s7   /usr/lib/python2.7/dist-packages/wadllib/application.pyR6   ž  s    c         C   s   |  j  j |  ƒ S(   sí   The 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ƒ   (   R+   (    (    s7   /usr/lib/python2.7/dist-packages/wadllib/application.pyt	   get_value¤  s    c         C   s2   g  |  j  j t d ƒ ƒ D] } t |  | ƒ ^ q S(   s7   Return the set of acceptable values for this parameter.R<   (   R'   R(   R   t   Option(   R+   t
   option_tag(    (    s7   /usr/lib/python2.7/dist-packages/wadllib/application.pyR2   ­  s    c         C   s5   |  j  j t d ƒ ƒ } | d k r( d St |  | ƒ S(   sÃ   Get 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.
        t   linkN(   R'   R   R   R   R   (   R+   t   link_tag(    (    s7   /usr/lib/python2.7/dist-packages/wadllib/application.pyR¼   ³  s    	c         C   s+   |  j  } | d k r$ t d ƒ ‚ n  | j S(   s  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.
        s(   This parameter isn't a link to anything.N(   R¼   R   R&   t   follow(   R+   R¼   (    (    s7   /usr/lib/python2.7/dist-packages/wadllib/application.pyt   linked_resourceÁ  s    
	(   R   R   R   RB   RŠ   R0   R$   RZ   R1   R6   R¹   R2   R¼   R¿   (    (    (    s7   /usr/lib/python2.7/dist-packages/wadllib/application.pyR   s  s   			Rº   c           B   s&   e  Z d  Z d „  Z e d „  ƒ Z RS(   s0   One of a set of possible values for a parameter.c         C   s   | |  _  | |  _ d S(   s}   Initialize the option.

        :param parameter: A Parameter.
        :param link_tag: An ElementTree <option> tag.
        N(   R€   R'   (   R+   R€   R»   (    (    s7   /usr/lib/python2.7/dist-packages/wadllib/application.pyRB   Ó  s    	c         C   s   |  j  j j d ƒ S(   NR3   (   R'   R)   R*   (   R+   (    (    s7   /usr/lib/python2.7/dist-packages/wadllib/application.pyR3   Ü  s    (   R   R   R   RB   RŠ   R3   (    (    (    s7   /usr/lib/python2.7/dist-packages/wadllib/application.pyRº   Ð  s   		c           B   sG   e  Z d  Z d „  Z e d „  ƒ Z e d „  ƒ Z d „  Z d „  Z RS(   sµ   A 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         C   s/   t  t |  ƒ j | j ƒ | |  _ | |  _ d S(   sy   Initialize the link.

        :param parameter: A Parameter.
        :param link_tag: An ElementTree <link> tag.
        N(   RM   R   RB   RA   R€   R'   (   R+   R€   R½   (    (    s7   /usr/lib/python2.7/dist-packages/wadllib/application.pyRB   è  s    	c         C   s"   |  j  s t d ƒ ‚ n  |  j ƒ  S(   s$   Follow the link to another Resource.sf   Cannot follow a link when the target has no WADL description. Try using a general HTTP client instead.(   t
   can_followR	   RJ   (   R+   (    (    s7   /usr/lib/python2.7/dist-packages/wadllib/application.pyR¾   ò  s    	c         C   s)   y |  j  ƒ  } Wn t k
 r$ t SXt S(   s”   Can this link be followed within wadllib?

        wadllib can follow a link if it points to a resource that has
        a WADL definition.
        (   RC   R	   R‘   R>   (   R+   t   definition_url(    (    s7   /usr/lib/python2.7/dist-packages/wadllib/application.pyRÀ   û  s
    c         C   s.   t  |  j |  j j ƒ  |  j j j | ƒ j ƒ S(   s,   Turn a resource type ID into a ResourceType.(   R   RA   R€   R¹   R‰   R*   R'   (   R+   RL   (    (    s7   /usr/lib/python2.7/dist-packages/wadllib/application.pyRE     s    c         C   s4   |  j  j j d ƒ } | d k r0 t d ƒ ‚ n  | S(   s(   Find the URL containing the definition .RX   sI   Parameter is a link, but not to a resource with a known WADL description.N(   R'   R)   R*   R   R	   (   R+   RZ   (    (    s7   /usr/lib/python2.7/dist-packages/wadllib/application.pyRC     s    (	   R   R   R   RB   RŠ   R¾   RÀ   RE   RC   (    (    (    s7   /usr/lib/python2.7/dist-packages/wadllib/application.pyR   á  s   	
		c           B   s   e  Z d  Z d „  Z RS(   s,   A wrapper around an XML <resource_type> tag.c         C   s   | |  _  d S(   sv   Initialize with a <resource_type> tag.

        :param resource_type_tag: An ElementTree <resource_type> tag.
        N(   R'   (   R+   t   resource_type_tag(    (    s7   /usr/lib/python2.7/dist-packages/wadllib/application.pyRB     s    (   R   R   R   RB   (    (    (    s7   /usr/lib/python2.7/dist-packages/wadllib/application.pyR     s   c           B   sD   e  Z d  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z RS(   s1   A WADL document made programmatically accessible.c         C   s.  | |  _  t | d ƒ r- |  j | ƒ |  _ n |  j | ƒ |  _ |  j j t d ƒ ƒ |  _ |  j j j	 d ƒ |  _
 i  |  _ i  |  _ x` |  j j t d ƒ ƒ D]F } | j j	 d ƒ } | d k	 r t |  d | ƒ } | |  j | <q q Wx@ |  j j t d ƒ ƒ D]& } | j d } t | ƒ |  j | <q Wd S(   sÝ   Parse 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.
        t   readt	   resourcesR`   RQ   RL   RX   N(   R\   t   hasattrt   _from_streamR«   t   _from_stringR   R   RÄ   R)   R*   t   resource_baseR±   R‰   R(   R   R   R   (   R+   R\   t   markupRQ   RL   RI   RX   (    (    s7   /usr/lib/python2.7/dist-packages/wadllib/application.pyRB   %  s"    			c         C   s±   d } d } g  } x t j | | ƒ D]{ \ } } | d k rM | j | ƒ q% | d k rf | j ƒ  q% | d k r% | d k r‡ | } n  | j t t | ƒ ƒ q% q% Wt j | ƒ S(   sr   Turns markup into a document.

        Just a wrapper around ElementTree which keeps track of namespaces.
        t   starts   start-nss   end-ns(   s   starts   start-nss   end-nsN(	   R   t   ETt	   iterparseRš   t   popt   setR†   R   t   ElementTree(   R+   t   streamt   eventst   rootRˆ   t   eventt   elem(    (    s7   /usr/lib/python2.7/dist-packages/wadllib/application.pyRÆ   >  s    	c         C   s   |  j  t | ƒ ƒ S(   s   Turns markup into a document.(   RÆ   R   (   R+   RÉ   (    (    s7   /usr/lib/python2.7/dist-packages/wadllib/application.pyRÇ   R  s    c         C   sD   |  j  | ƒ } |  j j | ƒ } | d k r@ t d | ƒ ‚ n  | S(   s7   Retrieve a resource type by the URL of its description.s   No such XML ID: "%s"N(   RD   R‰   R*   R   RF   (   R+   t   resource_type_urlRH   RX   (    (    s7   /usr/lib/python2.7/dist-packages/wadllib/application.pyRP   V  s
    c         C   s‡   t  |  j ƒ j ƒ  } d | _ | j d ƒ rB t  | ƒ j ƒ  } n | j | ƒ } | j } d | _ | | k rs | St d | ƒ ‚ d S(   sÖ   A 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.
        t   https,   Can't look up definition in another url (%s)N(   R   R\   t   ensureNoSlashR   t   fragmentR§   t   resolveRK   (   R+   RW   t
   markup_urit   this_urit   possible_xml_id(    (    s7   /usr/lib/python2.7/dist-packages/wadllib/application.pyRD   ^  s    			c         C   s   g  |  j  D] } | j d | k r
 | ^ q
 } t | ƒ d k  rE d St | ƒ d k rj t d | ƒ ‚ n  t |  t |  j | t ƒ | d ƒ S(   sp   Locate one of the resources described by this document.

        :param path: The path to the resource.
        R   i   s+   More than one resource defined with path %si    N(	   RÄ   R)   R4   R   R	   R   R   RÈ   R>   (   R+   R   R%   t   matching(    (    s7   /usr/lib/python2.7/dist-packages/wadllib/application.pyt   get_resource_by_path~  s    (	   R   R   R   RB   RÆ   RÇ   RP   RD   RÞ   (    (    (    s7   /usr/lib/python2.7/dist-packages/wadllib/application.pyR    "  s   					 (;   R   RZ   t   __metaclass__t   __all__t	   cStringIOR
   R   t   ImportErrort   ioR}   t   email.mime.multipartR   t   email.mime.nonmultipartR   t   email.MIMEMultipartt   email.MIMENonMultipartR~   t   urllib.parseR   t   urllibt
   simplejsonRR   t   xml.etree.cElementTreet   etreet   cElementTreeRË   t   elementtree.ElementTreeRÏ   t   lazr.uriR   R   t   wadllibR   R   t   wadllib.iso_strptimeR   R†   R|   R   R   R   t	   ExceptionR	   R   R    R¤   R!   R"   R?   R   R   RŒ   R   R   R   Rº   R   R   R    (    (    (    s7   /usr/lib/python2.7/dist-packages/wadllib/application.pyt   <module>   s~   					
B=ÿ =j9BN]6