o R `@sdZeZgdZddlZddlmZddlZddlZddl Z ddl Z ddl Z ddl Z zddl mZWneyAddlmZYnwz ddlmmZWney_ddlmmZYnwddlmZmZddlmZmZddlmZd Zd Z d d Z!d dZ"ddZ#Gddde$Z%Gddde%Z&Gddde%Z'Gddde(Z)GdddZ*Gddde)Z+Gddde+Z,Gdd d e)Z-Gd!d"d"e)e*Z.Gd#d$d$e*Z/Gd%d&d&e+e*Z0Gd'd(d(e)Z1Gd)d*d*e)Z2Gd+d,d,e+Z3Gd-d.d.e)Z4Gd/d0d0e)Z5dS)1aNavigate 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. ) ApplicationLinkMethodNoBoundRepresentationError ParameterRepresentationDefinitionResponseDefinitionResource ResourceType WADLErrorN)quote) urlencode)URImerge) _make_unicode _string_types) iso_strptimez xmlns:mapz http://www.w3.org/2001/XMLSchemacCsd|S)z)Scope a tag name with the WADL namespace.z&{http://research.sun.com/wadl/2006/10}tag_namerr5/usr/lib/python3/dist-packages/wadllib/application.pywadl_tagHsrcCs dt|S)z#Turn a tag name into an XPath path.z./)rrrrr wadl_xpathMs rcGs$i}|D] }|dur||q|S)zs z-HasParametersMixin.params..)r/ ValueErrortagfindallr)selfr0r/ param_tagsrr.rparamsys  zHasParametersMixin.paramsTc Ks t||}i}|D]h}|j}|jdur/||vr*|||jkr*td||||jf|j||<dd|jD}t|dkrU||vrU|||vrUtd|||d|f|rd|jrd||vrdtd|||vrq||||<||=q t|dkrtd d ||S) aMake sure the given valueset is valid. A valueset might be invalid because it contradicts a fixed value or (if enforce_completeness is True) because it lacks a required value. :param params: A list of Parameter objects. :param param_values: A dictionary of parameter values. May include paramters whose names are not valid Python identifiers. :param enforce_completeness: If True, this method will raise an exception when the given value set lacks a value for a required parameter. :param kw_param_values: A dictionary of parameter values. :return: A dictionary of validated parameter values. Nz=Value '%s' for parameter '%s' conflicts with fixed value '%s'cSg|]}|jqSr)value)r,optionrrrr1z.r z=Invalid value '%s' for parameter '%s': valid values are: "%s"z", "z$No value for required parameter '%s'zUnrecognized parameter(s): '%s'z', ') rname fixed_valuer2optionslenjoin is_requiredkeys) r5r7 param_valuesenforce_completenesskw_param_valuesvalidated_valuesr(r<r>rrrvalidate_param_valuessD        z(HasParametersMixin.validate_param_valuesN)T)r r!r"r#r7rGrrrrr'vs  r'c@s0eZdZdZddZddZddZdd Zd S) WADLResolvableDefinitionz=A base class for objects whose definitions may be references.cCsd|_||_dS)zInitialize with a WADL application. :param application: A WADLDefinition. Relative links are assumed to be relative to this object's URL. N) _definition application)r5rKrrr__init__ z!WADLResolvableDefinition.__init__cCs^|jdur|jS|}|dur||_|S|j|}||}|dur*td|||_|S)aReturn the definition of this object, wherever it is. Resource is a good example. A WADL tag may contain a large number of nested tags describing a resource, or it may just contain a 'type' attribute that references a 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. NNo such XML ID: "%s")rJ_get_definition_urlrK lookup_xml_id_definition_factoryKeyError)r5 object_urlxml_id definitionrrrresolve_definitions    z+WADLResolvableDefinition.resolve_definitioncCt)zxTransform an XML ID into a wadllib wrapper object. Which kind of object it is depends on the subclass. NotImplementedErrorr5idrrrrQz,WADLResolvableDefinition._definition_factorycCrW)zmFind the URL that identifies an external reference. How to do this depends on the subclass. rXr5rrrrOr\z,WADLResolvableDefinition._get_definition_urlN)r r!r"r#rLrVrQrOrrrrrIs  " rIcseZdZdZ   d'fdd ZeddZedd Zed d Z  d(d dZ ddZ  d)ddZ d*ddZ d*ddZ eddZd*ddZddZddZdd Zd!d"Zd*d#d$Zd%d&ZZS)+rz/A resource, possibly bound to a representation.NTcstt||||_t|tr|j|j|_n||_d|_ |dur<|dkr6|r2t t ||_ n ||_ nt d|||_|durU|durL||_dS||j|_dSdS)a[ :param application: A WADLApplication. :param url: The URL to this resource. :param resource_type: An ElementTree or 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. Napplication/jsonz?This resource doesn't define a representation for media type %s)superrrL_url isinstancerrKget_resource_typer3representationjsonloadsrr% media_typerepresentation_definitionget_representation_definition)r5rKurl resource_typercrfrepresentation_needs_processingrg __class__rrrLs2   zResource.__init__cCs|jS)z Return the URL to this resource.)r`r]rrrri,sz Resource.urlcCsb|jdurdS|jjd}|dur|S|jjd}|dur/t|jj}t|d|SdS)z@Return the URL to the type definition for this resource, if any.Ntyper[#)r3r*r+rrK markup_url ensureSlashstr)r5ritype_idbaserrrtype_url1s zResource.type_urlcCs |jjdS)zReturn the ID of this resource.r[)r3r*r]rrrr[C z Resource.idr^cCst|j|j|j||||S)aBind 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. )rrKrir3)r5rcrfrkrgrrrbindHs z Resource.bindcCsD|dj}|D]}|j}|jd|kr|Sqtd|)z tag's 'type' attribute will contain the URL to the that defines them. rnr3r*r+r]rrrrOzResource._get_definition_urlcCs>|jdur|j}|S|dur||}|Std)a3Get the most appropriate representation definition. If media_type is provided, the most appropriate definition is the definition of the representation of that media type. If this resource is bound to a representation, the most appropriate definition is the definition of that representation. Otherwise, the most appropriate definition is the definition of the representation served in response to a standard GET. :param media_type: Media type of the definition to find. Must be present unless the resource is bound to a representation. :raise NoBoundRepresentationError: If this resource is not bound to a representation and media_type was not provided. :return: A RepresentationDefinition NzSResource is not bound to any representation, and no media media type was specified.)rcrgrVrhr)r5rfrUrrrrs   z(Resource._find_representation_definitionccs*|j}|tdD]}|Vq dS)z+Iterate over this resource's tags.rN)rVr3r4r)r5rUrrrrr*s  zResource._method_tag_iter)NNTN)r^TN)NNNNrH)r r!r"r#rLpropertyrirur[rwrhrzrrrrrrrQrOrr __classcell__rrrlrrs>4      !   0 #rc@sneZdZdZddZeddZeddZedd Zed d Z dd dZ dddZ dddZ d S)rz*A wrapper around an XML tag. cCs||_|jj|_||_dS)zaInitialize with a tag. :param method_tag: An ElementTree tag. N)r/rKr3)r5r/rrrrrL4s  zMethod.__init__cCst||jtdS)z@Return the definition of a request that invokes the WADL method.request)RequestDefinitionr3findrr]rrrr=szMethod.requestcCst|j|jtdS)z9Return the definition of the response to the WADL method.r{)rr/r3rrr]rrrr{BszMethod.responsecCr)z)The XML ID of the WADL method definition.r[rr]rrrr[Hz Method.idcCs|jjdS)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. r<r3r*r+rr]rrrr<Msz Method.nameNcKs|jj|fi|S)4Return the request URL to use to invoke this method.)r build_url)r5rCrErrrbuild_request_urlVzMethod.build_request_urlcKs|jj||fi|S)zBuild a representation to be sent when invoking this method. :return: A 2-tuple of (media_type, representation). )rrc)r5rfrCrErrrbuild_representationZs zMethod.build_representationc Csd}|dur|j|}|durdS|dur:t|dkr:|j}|dur%dSz ||j|dWn ty9YdSw|dusDt|dkrFdS|durO||S|jjD]}z|||j |dWdStymYqSwdS)aReturns 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) rrhr?rGrr2rrepresentationsr7r/)r5rf query_valuesrepresentation_valuesrcrrrrrcsH     zMethod.is_described_byrHNN)NNN) r r!r"r#rLrrr{r[r<rrrrrrrr1s$       rc@sNeZdZdZddZeddZeddZdd d Zdd d Z dd dZ dS)rzBA wrapper around the description of the request invoking a method.cCs$||_|jj|_|jj|_||_dS)zInitialize with a tag. :param resource: The resource to which this request can be sent. :param request_tag: An ElementTree tag. N)rr/rKr3)r5r request_tagrrrrLs   zRequestDefinition.__init__cCs |dgS)z,Return the query parameters for this method.queryr7r]rrrrrvzRequestDefinition.query_paramsccs.|jtdD] }t|j|j|Vq dS)Nrc)r3r4rrrKr/)r5rUrrrrs  z!RequestDefinition.representationsNcCs*|jD]}|dus|j|kr|SqdS)z1Return the appropriate representation definition.N)rrfr5rfrcrrrrhs z/RequestDefinition.get_representation_definitioncKs0||}|durtd||j|fi|S)zBuild a representation to be sent along with this request. :return: A 2-tuple of (media_type, representation). Nz,Cannot build representation of media type %s)rh TypeErrorrw)r5rfrCrErUrrrrcs z RequestDefinition.representationcKsX|j|j|fi|}|jj}t|dkr*d|vrd}nd}||tt|7}|S)rr ?&)rGrr/rir?r sorteditems)r5rCrErFriappendrrrrs zRequestDefinition.build_urlrHr) r r!r"r#rLrrrrhrcrrrrrrs     rc@sBeZdZdZdddZddZddZd d Zd d Zd dZ dS)rz;A wrapper around the description of a response to a method.NcCs|j|_||_||_||_dS)zgInitialize with a tag. :param response_tag: An ElementTree tag. N)rKr/r3headers)r5r/ response_tagrrrrrLs zResponseDefinition.__init__ccs4td}|j|D] }t|jj|j|Vq dS)zGet an iterator over the representation definitions. These are the representations returned in response to an invocation of this method. rcN)rr3r4rr/rK)r5pathr}rrr__iter__s zResponseDefinition.__iter__cCst|j|j|S)zBind the response to a set of HTTP headers. A WADL response can have associated header parameters, but no other kind. )rr/r3)r5rrrrrwszResponseDefinition.bindcCsH|jtdD]}|jd|kr!|jddkr!t||SqdS)z,Find a header parameter within the response.r(r<r)headerN)r3r4rr*r+r)r5rr-rrrrs z ResponseDefinition.get_parametercCs8|jdur td|jdkrtd|j|j|jS)z:Find the value of a parameter, given the Parameter object.Nz,Response object is not bound to any headers.rr)rrr)rYr+r<)r5rrrrrs  z&ResponseDefinition.get_parameter_valuecCs.|jdurdS|D] }|j|kr|Sq dS)z8Get one of the possible representations of the response.N)r3rfrrrrrh s  z0ResponseDefinition.get_representation_definitionrH) r r!r"r#rLrrwrrrhrrrrrs    rcszeZdZdZfddZfddZddZedd Zd d Z d d Z dddZ ddZ ddZ ddZddZZS)rz2A definition of the structure of a representation.cs tt||||_||_dSrH)r_rrLr/r3)r5rKr/r}rlrrrLs z!RepresentationDefinition.__init__cstt|ddg|S)Nrr)r_rr7r5r/rlrrr7s zRepresentationDefinition.paramscCsdd||DS)z#Return the names of all parameters.cSr8r)r<)r,r(rrrr1$r;z.rrrrrr"rz(RepresentationDefinition.parameter_namescCs|jjdS)z4The media type of the representation described here.ry)rVr3r*r]rrrrf&sz#RepresentationDefinition.media_typec Cstttjd}d|}ttj}d||d}|dur!|S|}d} dt|d d }tj ||tj d s> |S|d t |}|d7}q&) z;Make a random boundary that does not appear in `all_parts`.rz%%0%ddz===============z==Nr Tz^--z(--)?$ascii)flags.) r?reprsysmaxsizerandom randrangereescapeencodesearch MULTILINErr) r5 all_parts_width_fmttokenboundarybcounterpatternrrr_make_boundary+s  z'RepresentationDefinition._make_boundarycCsP|D]\}}||d|d||d|dq|ddS)z$Write MIME headers to a file object.UTF-8s:  Nwriter)r5bufrkeyr9rrr_write_headers=s   z'RepresentationDefinition._write_headersFcCs6|d||d|r|d|ddS)z,Write a multipart boundary to a file object.s--rrNr)r5rrclosingrrr_write_boundaryFs  z(RepresentationDefinition._write_boundaryc Csg}|D]|\}}}t}|rd}dt|t|f}nd}dt|}||dd|fd|fg|rFt|ts@tdt|||n3t|t sStd t|t d |} | d d D]} || d |dq_|| d  d | |q|d|} t}dt| }||dd|fg|D]} ||| || |dq|j|| dd||fS)aVGenerate 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. zapplication/octet-streamz#form-data; name="%s"; filename="%s"ztext/plain; charset="utf-8"zform-data; name="%s")z MIME-Versionz1.0z Content-TypezContent-Dispositionzbytes payload expected: %szstring payload expected: %sz \r\n|\r|\nNrrz"multipart/form-data; boundary="%s"T)r)ioBytesIOr rrabytesrrnrrrrrrgetvaluerr@r) r5parts encoded_parts is_binaryr<r9rctypecdisplinesliner encoded_partrrr_generate_multipart_formNsT             z1RepresentationDefinition._generate_multipart_formc Ks|}||j}|j||fi|}|j}|dkr'tt|}||fS|dkrWg}t} |D]} | | j | } | | urK| | j dk| j | fq2| |\}}||fS|dkrdt|}||fStd|)zzBind the definition to parameter values, creating a document. :return: A 2-tuple (media_type, document). z!application/x-www-form-urlencodedzmultipart/form-databinaryr^zUnsupported media type: '%s')rVr7r/rGrfr rrobjectr+r<rrnrrddumpsr2) r5rCrErUr7rFrfdocrmissingr(r9rrrrws2    zRepresentationDefinition.bindcCr)z9Turn a representation ID into a RepresentationDefinition.)rKrepresentation_definitionsr+rZrrrrQrz,RepresentationDefinition._definition_factorycCr)aFind the URL containing the representation's 'real' definition. If a representation's structure is defined by reference, the tag's 'href' attribute will contain the URL to the that defines the structure. hrefrr]rrrrOrz,RepresentationDefinition._get_definition_url)F)r r!r"r#rLr7rrrfrrrrrwrQrOrrrrlrrs    Arc@seZdZdZddZeddZeddZedd Zed d Z ed d Z ddZ eddZ eddZ eddZdS)rz5One of the parameters of a representation definition.cCs|j|_||_||_dS)aInitialize 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 tag for this parameter. N)rKvalue_containerr3)r5rr3rrrrLs  zParameter.__init__cCr)zThe name of this parameter.r<rr]rrrr<rzParameter.namecCr)zThe style of this parameter.r)rr]rrrr)rzParameter.stylecCr)zThe XSD type of this parameter.rnrr]rrrrnrzParameter.typecCr)a9The 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. fixedrr]rrrr=s zParameter.fixed_valuecCs|jjdddvS)z6Whether or not a value for this parameter is required.requiredfalse)1truerr]rrrrAszParameter.is_requiredcCs |j|S)zThe value of this parameter in the bound representation/headers. :raise NoBoundRepresentationError: If this parameter's value container is not bound to a representation or a set of headers. )rrr]rrr get_values zParameter.get_valuecsfddjtdDS)z7Return the set of acceptable values for this parameter.csg|]}t|qSr)Option)r, option_tagr]rrr1sz%Parameter.options..r:)r3r4rr]rr]rr>s zParameter.optionscCs&|jtd}|durdSt||S)zGet the link to another resource. The link may be examined and, if its type is of a known WADL description, it may be followed. :return: A Link object, or None. linkN)r3rrr)r5link_tagrrrrs  zParameter.linkcCs|j}|dur td|jS)aFollow a link from this parameter to a new resource. This only works for parameters whose WADL definition includes a tag that points to a known WADL description. :return: A Resource object for the resource at the other end of the link. Nz(This parameter isn't a link to anything.)rr2follow)r5rrrrlinked_resources zParameter.linked_resourceN)r r!r"r#rLrr<r)rnr=rArr>rr rrrrrs(        rc@s$eZdZdZddZeddZdS)rz0One of a set of possible values for a parameter.cCs||_||_dS)z}Initialize the option. :param parameter: A Parameter. :param link_tag: An ElementTree