ó
$âJMc        
   @   sÙ  d  Z  e Z d d d d d d d d d	 d
 g
 Z d d l m Z d 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 d d l Z d d l Z y 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 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  „  ƒ  YZ' d e' f d! „  ƒ  YZ( d e% f d" „  ƒ  YZ) d# e% e& f d$ „  ƒ  YZ* d e& f d% „  ƒ  YZ+ d e' e& f d& „  ƒ  YZ, d e% f d' „  ƒ  YZ- d( e% f d) „  ƒ  YZ. d e' f d* „  ƒ  YZ/ d	 e% f d+ „  ƒ  YZ0 d e% f d, „  ƒ  YZ1 d S(-   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   StringION(   t   MIMEMultipart(   t   MIMENonMultipart(   t   URIt   merge(   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_tagG   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_xpathL   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_dictsQ   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	   Z   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   _   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   i   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   q   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   paramsx   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   R,   t   optionR.   (    (    s7   /usr/lib/python2.7/dist-packages/wadllib/application.pyt   validate_param_values‡   s6    	
#N(   R   R   R   R   R+   t   TrueR9   (    (    (    s7   /usr/lib/python2.7/dist-packages/wadllib/application.pyR   u   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'   R=   (    (    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_urlR=   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.pyRA   å   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(   RG   (   R'   (    (    s7   /usr/lib/python2.7/dist-packages/wadllib/application.pyR?   ì   s    (   R   R   R   R>   RF   RA   R?   (    (    (    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   R>   t   _urlt
   isinstancet
   basestringR=   t   get_resource_typeR#   R   t   representationt
   simplejsont   loadst   unicodeR   t
   media_typet   representation_definitiont   get_representation_definition(   R'   R=   t   urlt   resource_typeRN   RR   t   representation_needs_processingRS   (    (    s7   /usr/lib/python2.7/dist-packages/wadllib/application.pyR>   ÷   s&    				c         C   s   |  j  S(   s    Return the URL to this resource.(   RJ   (   R'   (    (    s7   /usr/lib/python2.7/dist-packages/wadllib/application.pyRU   *  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   typeRH   t   #N(	   R#   R   R%   R&   R   R=   t
   markup_urlt   ensureSlasht   str(   R'   RU   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.RH   (   R#   R%   (   R'   (    (    s7   /usr/lib/python2.7/dist-packages/wadllib/application.pyRH   A  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   R=   RU   R#   (   R'   RN   RR   RW   RS   (    (    s7   /usr/lib/python2.7/dist-packages/wadllib/application.pyt   bindF  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   responseRF   R#   R%   R&   R   (   R'   RR   t   default_get_responseRN   t   representation_tag(    (    s7   /usr/lib/python2.7/dist-packages/wadllib/application.pyRT   \  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.
        R,   t    N(   t   _method_tag_iterR%   R&   t   lowerR   R   t   is_described_by(   R'   t   http_methodRR   t   query_paramst   representation_paramst
   method_tagR,   t   method(    (    s7   /usr/lib/python2.7/dist-packages/wadllib/application.pyRc   f  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'   RR   (    (    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.
        (   Rp   t   parameter_names(   R'   RR   (    (    s7   /usr/lib/python2.7/dist-packages/wadllib/application.pyRr   ”  s    
c         c   s)   x" |  j  ƒ  D] } t |  | ƒ Vq Wd S(   s6   An iterator over the methods defined on this resource.N(   Rh   R   (   R'   Rn   (    (    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   R,   N(   Rp   R#   R$   R   R%   R&   R   R   (   R'   t
   param_nameRR   RE   Rf   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(   Rw   Rx   (   RN   R   R   RR   R    RG   R,   t   _dereference_namespaceR#   RX   t   XML_SCHEMA_NS_URIR   R"   t   datetimet   timet   strptime(   R'   t	   parameterR/   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   Rg   N(   R   t   splitR&   t   NS_MAP(   R'   R#   R/   t	   namespacet   ns_mapR   (    (    s7   /usr/lib/python2.7/dist-packages/wadllib/application.pyRy   ê  s    c         C   s   |  j  j j | ƒ S(   s-   Given an ID, find a ResourceType for that ID.(   R=   t   resource_typesR&   (   R'   RH   (    (    s7   /usr/lib/python2.7/dist-packages/wadllib/application.pyRA   ø  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.
        RX   (   R#   R%   R&   (   R'   (    (    s7   /usr/lib/python2.7/dist-packages/wadllib/application.pyR?   ü  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(   RN   R   RS   RF   RT   R   (   R'   RR   RE   (    (    s7   /usr/lib/python2.7/dist-packages/wadllib/application.pyRp     s    	c         c   s8   |  j  ƒ  j } x" | j t d ƒ ƒ D] } | Vq% Wd S(   s+   Iterate over this resource's <method> tags.Ro   N(   RF   R#   R$   R   (   R'   RE   Rn   (    (    s7   /usr/lib/python2.7/dist-packages/wadllib/application.pyRh   (  s    N(   R   R   R   R   R:   R>   t   propertyRU   R_   RH   R`   RT   Rc   Rq   Rr   Rs   Ru   R   Ry   RA   R?   Rp   Rh   (    (    (    s7   /usr/lib/python2.7/dist-packages/wadllib/application.pyR   ô   s.   0	
	 	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!   R=   R#   (   R'   R!   Rn   (    (    s7   /usr/lib/python2.7/dist-packages/wadllib/application.pyR>   2  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‰   ;  s    c         C   s"   t  |  j |  j j t d ƒ ƒ ƒ S(   s9   Return the definition of the response to the WADL method.Rd   (   R   R!   R#   R‹   R   (   R'   (    (    s7   /usr/lib/python2.7/dist-packages/wadllib/application.pyRd   @  s    	c         C   s   |  j  j j d ƒ S(   s)   The XML ID of the WADL method definition.RH   (   R#   R%   R&   (   R'   (    (    s7   /usr/lib/python2.7/dist-packages/wadllib/application.pyRH   F  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.
        R,   (   R#   R%   R&   Ri   (   R'   (    (    s7   /usr/lib/python2.7/dist-packages/wadllib/application.pyR,   K  s    c         K   s   |  j  j | |  S(   s4   Return the request URL to use to invoke this method.(   R‰   t	   build_url(   R'   R4   R6   (    (    s7   /usr/lib/python2.7/dist-packages/wadllib/application.pyt   build_request_urlT  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‰   RN   (   R'   RR   R4   R6   (    (    s7   /usr/lib/python2.7/dist-packages/wadllib/application.pyt   build_representationX  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‰   RT   t   FalseR0   R9   Rl   R"   R:   Rj   t   representationsR+   R!   (   R'   RR   t   query_valuest   representation_valuesRN   R‰   (    (    s7   /usr/lib/python2.7/dist-packages/wadllib/application.pyRj   a  s<    			
N(   R   R   R   R>   Rˆ   R‰   Rd   RH   R,   R   R   RŽ   Rj   (    (    (    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(   Ro   R!   R=   R#   (   R'   Ro   t   request_tag(    (    s7   /usr/lib/python2.7/dist-packages/wadllib/application.pyR>   ›  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.pyRl   ¦  s    c         c   s>   x7 |  j  j t d ƒ ƒ D] } t |  j |  j | ƒ Vq Wd  S(   NRN   (   R#   R$   R   R   R=   R!   (   R'   RE   (    (    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   RR   (   R'   RR   RN   (    (    s7   /usr/lib/python2.7/dist-packages/wadllib/application.pyRT   ±  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(   RT   R   t	   TypeErrorR`   (   R'   RR   R4   R6   RE   (    (    s7   /usr/lib/python2.7/dist-packages/wadllib/application.pyRN   ¸  s
    c         K   so   |  j  |  j | |  } |  j j } t | ƒ d k rk d | k rK d } n d } | | t j | ƒ 7} n  | S(   s4   Return the request URL to use to invoke this method.i    t   ?t   &(   R9   Rl   R!   RU   R0   t   urllibt	   urlencode(   R'   R4   R6   R7   RU   t   append(    (    s7   /usr/lib/python2.7/dist-packages/wadllib/application.pyRŒ   Ä  s    	N(   R   R   R   R>   Rˆ   Rl   R   R   RT   RN   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(   R=   R!   R#   t   headers(   R'   R!   t   response_tagR›   (    (    s7   /usr/lib/python2.7/dist-packages/wadllib/application.pyR>   Ú  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.
        RN   N(   R   R#   R$   R   R!   R=   (   R'   t   pathRf   (    (    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.pyR`   ï  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   R,   R    t   headerN(   R#   R$   R   R%   R&   R   R   (   R'   Rt   R*   (    (    s7   /usr/lib/python2.7/dist-packages/wadllib/application.pyRu   ÷  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    RG   R&   R,   (   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   RR   (   R'   RR   RN   (    (    s7   /usr/lib/python2.7/dist-packages/wadllib/application.pyRT   
  s    N(
   R   R   R   R   R>   Rž   R`   Ru   R   RT   (    (    (    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(   RI   R   R>   R!   R#   (   R'   R=   R!   Rf   (    (    s7   /usr/lib/python2.7/dist-packages/wadllib/application.pyR>     s    	c         C   s   t  t |  ƒ j d d g | ƒ S(   NR”   Rv   (   RI   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+   R,   (   R'   R!   R   (    (    s7   /usr/lib/python2.7/dist-packages/wadllib/application.pyRr      s    c         C   s   |  j  ƒ  j j d S(   s4   The media type of the representation described here.Rb   (   RF   R#   R%   (   R'   (    (    s7   /usr/lib/python2.7/dist-packages/wadllib/application.pyRR   $  s    c         K   s¬  |  j  ƒ  } | j |  j ƒ } |  j | | |  } |  j } | d k rZ t j | ƒ } nH| d k rt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 ƒ d } | 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   binaryR=   s   octet-streamt   textRv   s   utf-8t   charsets   form-data; name="%s"s   Content-Dispositions   
i   s   ; boundary="%s"s   application/jsons   Unsupported media type: '%s'(   s   applications   octet-stream(   s   texts   plain(   RF   R+   R!   R9   RR   R˜   R™   R   t   set_typet   objectR&   R,   RX   R   t   set_payloadt   attachR\   R‹   t   get_content_typet   get_boundaryRO   t   dumpsR"   (   R'   R4   R6   RE   R+   R7   RR   t   doct   outert   missingR   R/   t   maintypet   subtypet   inner(    (    s7   /usr/lib/python2.7/dist-packages/wadllib/application.pyR`   )  s>    					c         C   s   |  j  j j | ƒ S(   s9   Turn a representation ID into a RepresentationDefinition.(   R=   t   representation_definitionsR&   (   R'   RH   (    (    s7   /usr/lib/python2.7/dist-packages/wadllib/application.pyRA   S  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.pyR?   W  s    (   R   R   R   R>   R+   Rr   Rˆ   RR   R`   RA   R?   (    (    (    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(   R=   t   value_containerR#   (   R'   R²   R#   (    (    s7   /usr/lib/python2.7/dist-packages/wadllib/application.pyR>   d  s    
	c         C   s   |  j  j j d ƒ S(   s   The name of this parameter.R,   (   R#   R%   R&   (   R'   (    (    s7   /usr/lib/python2.7/dist-packages/wadllib/application.pyR,   r  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    w  s    c         C   s   |  j  j j d ƒ S(   s   The XSD type of this parameter.RX   (   R#   R%   R&   (   R'   (    (    s7   /usr/lib/python2.7/dist-packages/wadllib/application.pyRX   |  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.pyR-     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&   Ri   (   R'   (    (    s7   /usr/lib/python2.7/dist-packages/wadllib/application.pyR2   Œ  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.R8   (   R#   R$   R   t   Option(   R'   t
   option_tag(    (    s7   /usr/lib/python2.7/dist-packages/wadllib/application.pyR.   ›  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   R>   Rˆ   R,   R    RX   R-   R2   R¸   R.   R»   R¾   (    (    (    s7   /usr/lib/python2.7/dist-packages/wadllib/application.pyR   a  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.pyR>   Á  s    	c         C   s   |  j  j j d ƒ S(   NR/   (   R#   R%   R&   (   R'   (    (    s7   /usr/lib/python2.7/dist-packages/wadllib/application.pyR/   Ê  s    (   R   R   R   R>   Rˆ   R/   (    (    (    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(   RI   R   R>   R=   R~   R#   (   R'   R~   R¼   (    (    s7   /usr/lib/python2.7/dist-packages/wadllib/application.pyR>   Ö  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	   RF   (   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.
        (   R?   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   R=   R~   R¸   R‡   R&   R#   (   R'   RH   (    (    s7   /usr/lib/python2.7/dist-packages/wadllib/application.pyRA   ö  s    c         C   s4   |  j  j j d ƒ } | d k r0 t d ƒ ‚ n  | S(   s(   Find the URL containing the definition .RV   sI   Parameter is a link, but not to a resource with a known WADL description.N(   R#   R%   R&   R   R	   (   R'   RX   (    (    s7   /usr/lib/python2.7/dist-packages/wadllib/application.pyR?   ü  s    (	   R   R   R   R>   Rˆ   R½   R¿   RA   R?   (    (    (    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.pyR>     s    (   R   R   R   R>   (    (    (    s7   /usr/lib/python2.7/dist-packages/wadllib/application.pyR     s   c           B   s;   e  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^   RN   RH   RV   N(   RZ   t   hasattrRÂ   t   _from_stringRª   R‹   R   RÃ   R%   R&   t   resource_baseR°   R‡   R$   R   R   R   (   R'   RZ   t   markupRN   RH   RE   RV   (    (    s7   /usr/lib/python2.7/dist-packages/wadllib/application.pyR>     s"    			c         C   s·   d } d } g  } x• t j t | ƒ | ƒ D]{ \ } } | d k rS | j | ƒ q+ | d k rl | 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
   Rš   t   popt   setR„   R   t   ElementTree(   R'   RÇ   t   eventst   rootR†   t   eventt   elem(    (    s7   /usr/lib/python2.7/dist-packages/wadllib/application.pyRÅ   +  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(   R@   R‡   R&   R   RB   (   R'   t   resource_type_urlRD   RV   (    (    s7   /usr/lib/python2.7/dist-packages/wadllib/application.pyRM   @  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   RZ   t   ensureNoSlashR   t   fragmentt
   startswitht   resolveRG   (   R'   RU   t
   markup_urit   this_urit   possible_xml_id(    (    s7   /usr/lib/python2.7/dist-packages/wadllib/application.pyR@   H  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%   R0   R   R	   R   R   RÆ   R:   (   R'   R   R!   t   matching(    (    s7   /usr/lib/python2.7/dist-packages/wadllib/application.pyt   get_resource_by_pathh  s    (   R   R   R   R>   RÅ   RM   R@   RÜ   (    (    (    s7   /usr/lib/python2.7/dist-packages/wadllib/application.pyR      s   				 (2   R   RX   t   __metaclass__t   __all__t	   cStringIOR
   R{   t   email.mime.multipartR   t   email.mime.nonmultipartR   t   ImportErrort   email.MIMEMultipartt   email.MIMENonMultipartR|   R˜   RO   t   xml.etree.cElementTreet   etreet   cElementTreeRÉ   t   elementtree.ElementTreeRÍ   t   lazr.uriR   R   R   R„   Rz   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>   sj   					
B=ÿ <j9BM]6