R `ldZeZgdZddlZddlmZddlZddlZddl Z ddl Z ddl Z ddl Z ddl mZ ddlmcmZddlmZmZddlmZmZddlmZd Zd Z d Z!d Z"d Z#Gdde$Z%Gdde%Z&Gdde%Z'Gdde(Z)GddZ*Gdde)Z+Gdde+Z,Gdde)Z-Gdde)e*Z.Gd d!e*Z/Gd"d#e+e*Z0Gd$d%e)Z1Gd&d'e)Z2Gd(d)e+Z3Gd*d+e)Z4Gd,d-e)Z5y#e$r ddlmZYwxYw#e$r ddlmcmZYwxYw).aNavigate 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/XMLSchemac d|zS)z)Scope a tag name with the WADL namespace.z&{http://research.sun.com/wadl/2006/10}tag_names 5/usr/lib/python3/dist-packages/wadllib/application.pywadl_tagrHs 3h >>cdt|zS)z#Turn a tag name into an XPath path.z./)rrs r wadl_xpathrMs (8$ $$rc@i}|D]}||j||S)z%{{<+<+<+>?@A A!AsEN)T)r%r&r'r(r=rNrrrr/r/vsK < 48- rr/c(eZdZdZdZdZdZdZy)WADLResolvableDefinitionz=A base class for objects whose definitions may be references.c d|_||_y)zInitialize with a WADL application. :param application: A WADLDefinition. Relative links are assumed to be relative to this object's URL. N) _definition application)r9rTs r__init__z!WADLResolvableDefinition.__init__s  &rc|j |jS|j}| ||_|S|jj|}|j |}|t d|z||_|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. No such XML ID: "%s")rS_get_definition_urlrT lookup_xml_id_definition_factoryKeyError)r9 object_urlxml_id definitions rresolve_definitionz+WADLResolvableDefinition.resolve_definitions    '## #--/   $D K!!// ;--f5   1J>? ?%rct)zxTransform an XML ID into a wadllib wrapper object. Which kind of object it is depends on the subclass. NotImplementedErrorr9ids rrZz,WADLResolvableDefinition._definition_factory "##rct)zmFind the URL that identifies an external reference. How to do this depends on the subclass. rar9s rrXz,WADLResolvableDefinition._get_definition_urlrerN)r%r&r'r(rUr_rZrXrrrrQrQsG' D$$rrQceZdZdZ dfd ZedZedZedZ ddZ dZ ddZ dd Z dd Z ed Zdd Zd ZdZdZdZddZdZxZS)r z/A resource, possibly bound to a representation.ctt| |||_t |t r+|j j|j|_n||_d|_ |A|dk(r.|r$tjt||_ n||_ ntd|z||_|+|||_y|j!|j|_yy)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)superr rU_url isinstancerrTget_resource_typer5representationjsonloadsrr+ media_typerepresentation_definitionget_representation_definition) r9rTurl resource_typerorrrepresentation_needs_processingrs __class__s rrUzResource.__init__s* h&{3 m] 3''99-HLLDH%DH"  %//2*.**%n5+7D'+9D'/$&0122%  %(41J.66tG. &rc|jS)z Return the URL to this resource.)rlrgs rruz Resource.url,syyrc:|jy|jjjd}||S|jjjd}|>t|jj j }t|dz|zSy)z@Return the URL to the type definition for this resource, if any.Ntyperd#)r5r7r8rrT markup_url ensureSlashstr)r9rutype_idbases rtype_urlzResource.type_url1s 88 hhoo!!&) ?J((//%%d+  t''223??ADt9s?W, ,rc4|jjdS)zReturn the ID of this resource.rd)r5r7rgs rrdz Resource.idCsxxt$$rc `t|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. )r rTrur5)r9rorrrwrss rbindz Resource.bindHs0"(($((DHH& 713 3rc|jdj}|D]>}|jj}|jj d|k(s<|cSt d|z)z>2 &N!/!B!B!D!H!H !((,,[9ZG%% &()>@J)KL Lrc|jD]e}|jjddj}|||jk(sCt ||}|j |||sc|cSy)aLook 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. rAN)_method_tag_iterr7r8lowerris_described_by)r9 http_methodrr query_paramsrepresentation_params method_tagrAmethods rrzResource.get_methodhs0//1 "J$$((4::@ @ ??0 0'))!*122'' 7E +/+F+FMM9>>,3( y!%66!%99 4 ,U 3L5L!#G$(OO#45 5& 44$,$5$5"&--z"B1Q"G%JEL *4#-U"33 4 4s C D)C==DDc|d|vr|jdd\}}nd}|jt}|j|d}||fS)zSplits a value into namespace URI and value. :param tag: A tag to use as context when mapping namespace names to URIs. N:r)splitr8NS_MAP)r9r5rD namespacens_maprs rrzResource._dereference_namespacesR  ${{32 IuI 9d3 e##rcL|jjj|S)z-Given an ID, find a ResourceType for that ID.)rTresource_typesr8rcs rrZzResource._definition_factorys..22266rcL|jjjdS)zReturn the URL that shows where a resource is 'really' defined. If a resource's capabilities are defined by reference, the tag's 'type' attribute will contain the URL to the that defines them. r{r5r7r8rgs rrXzResource._get_definition_urlxx""6**rc|j*|jj}|jS|!|j|}|jSt d)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 zSResource is not bound to any representation, and no media media type was specified.)rorsr_rtr)r9rrr^s rrz(Resource._find_representation_definitionst*    *77JJLJ,,..  #;;JGJ ,,..-,- -rc#K|jj}|jtdD]}|yw)z+Iterate over this resource's tags.rN)r_r5r6r)r9r^rs rrzResource._method_tag_iter*s?,,.22 $,,Z-AB J  s?A)NNTN)rjTN)NNNNrO)r%r&r'r(rUpropertyrurrdrrtrrrrrrrrZrXrr __classcell__rxs@rr r s92615+/2Ih"%%/A-1'+3,LJN)-B % .++ &-5` $7+ /Frr cveZdZdZdZedZedZedZedZ d dZ d d Z d d Z y)rz*A wrapper around an XML tag. cV||_|jj|_||_y)zaInitialize with a tag. :param method_tag: An ElementTree tag. N)r3rTr5)r9r3rs rrUzMethod.__init__4s$ ! ==44rc^t||jjtdS)z@Return the definition of a request that invokes the WADL method.request)RequestDefinitionr5findrrgs rrzMethod.request=s#!txx}}Z 5J'KLLrcrt|j|jjt dS)z9Return the definition of the response to the WADL method.r)rr3r5rrrgs rrzMethod.responseBs."$--"&((-- :0F"GI IrcL|jjjdS)z)The XML ID of the WADL method definition.rdrrgs rrdz Method.idHsxx""4((rch|jjjdjS)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. rAr5r7r8rrgs rrAz Method.nameMs&xx""6*0022rNc <|jj|fi|S)4Return the request URL to use to invoke this method.)r build_url)r9rIrKs rbuild_request_urlzMethod.build_request_urlVs%t||%%lFoFFrc >|jj||fi|S)zBuild a representation to be sent when invoking this method. :return: A 2-tuple of (media_type, representation). )rro)r9rrrIrKs rbuild_representationzMethod.build_representationZs* +t||**  9(79 9rcd}||jj|}|y|;t|dkDr-|j}|y |j|j|d|t|dk(ry||j |S|jjD]0} |j|j|j|dyy#t $rYywxYw#t $rYNwxYw)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) rrtrErNrr4rrepresentationsr=r3)r9rr query_valuesrepresentation_valuesrors rrzMethod.is_described_bycs$  !!\\GGN%  #L(9A(=llG --((,? " )()Q.  %!11%' '"ll:: N 44"))$--8)52  '  "  s$C,C CC C)(C)rONN)NNN) r%r&r'r(rUrrrrdrArrrrrrrr1sMMII ))33G/3*.9=A.25rrcNeZdZdZdZedZedZd dZd dZ d dZ y) rzBA wrapper around the description of the request invoking a method.c||_|jj|_|jj|_||_y)zInitialize with a tag. :param resource: The resource to which this request can be sent. :param request_tag: An ElementTree tag. N)rr3rTr5)r9r request_tags rrUzRequestDefinition.__init__s5   ,, ==44rc&|jdgS)z,Return the query parameters for this method.query)r=rgs rrzRequestDefinition.query_paramss{{G9%%rc#K|jjtdD]%}t|j|j |'yw)Nro)r5r6rrrTr3)r9r^s rrz!RequestDefinition.representationssH((**:6F+GH =J*  $--= = =sAANcP|jD]}||j|k(s|cSy)z1Return the appropriate representation definition.N)rrrr9rrros rrtz/RequestDefinition.get_representation_definitions5"22 &N!^%>%>*%L%% &rc j|j|}|td|z|j|fi|S)zBuild a representation to be sent along with this request. :return: A 2-tuple of (media_type, representation). z,Cannot build representation of media type %s)rt TypeErrorr)r9rrrIrKr^s rroz RequestDefinition.representationsH 77 C  J()* *z|???rc |j|j|fi|}|jj}t |dkDr1d|vrd}nd}||t t |jzz }|S)rr ?&)rNrr3rurErsorteditems)r9rIrKrLruappends rrzRequestDefinition.build_urls5455   |@/>@mm  1 $cz 6If-=-C-C-E&FGG GC rrOr) r%r&r'r(rUrrrrtrorrrrrrsBL &&==  @ rrc6eZdZdZd dZdZdZdZdZdZ y) rz;A wrapper around the description of a response to a method.NcP|j|_||_||_||_y)zgInitialize with a tag. :param response_tag: An ElementTree tag. N)rTr3r5headers)r9r3 response_tagrs rrUzResponseDefinition.__init__s' $//   rc#Ktd}|jj|D]/}t|jj |j|1yw)zGet an iterator over the representation definitions. These are the representations returned in response to an invocation of this method. roN)rr5r6rr3rT)r9pathrs r__iter__zResponseDefinition.__iter__sY *+"&(("2"24"8 N * ))4==:LN N NsAAcDt|j|j|S)zBind the response to a set of HTTP headers. A WADL response can have associated header parameters, but no other kind. )rr3r5)r9rs rrzResponseDefinition.binds "$--7CCrc|jjtdD]M}|jj d|k(s"|jj ddk(sAt ||cSy)z,Find a header parameter within the response.r1rAr2headerN)r5r6rr7r8r)r9rr<s rrz ResponseDefinition.get_parametersi))*W*=> 2I  $$V, :$$((1X= y11 2rc|j td|jdk7rtd|jz|jj |j S)z:Find the value of a parameter, given the Parameter object.z,Response object is not bound to any headers.rr)rrr2rbr8rA)r9rs rrz&ResponseDefinition.get_parameter_valuesc << ,>@ @ ??h &%&__-. .|| //rcR|jy|D]}|j|k(s|cSy)z8Get one of the possible representations of the response.N)r5rrrs rrtz0ResponseDefinition.get_representation_definition s7 88 " &N((J6%% &rrO) r%r&r'r(rUrrrrrtrrrrrs%E ND 0rrcneZdZdZfdZfdZdZedZdZ dZ d dZ d Z d Z d Zd ZxZS)rz2A definition of the structure of a representation.cHtt| |||_||_yrO)rkrrUr3r5)r9rTr3rrxs rrUz!RepresentationDefinition.__init__s! &6{C  %rc0tt| ddg|S)Nrr)rkrr=)r9r3rxs rr=zRepresentationDefinition.paramss"-t; g * *rc^|j|Dcgc]}|jc}Scc}w)z#Return the names of all parameters.)r=rA)r9r3r1s rrz(RepresentationDefinition.parameter_names"s#(, H(=>u >>>s*cP|jjjdS)z4The media type of the representation described here.r)r_r5r7rgs rrrz#RepresentationDefinition.media_type&s$&&(,,33K@@rctttjdz }d|z}t j tj}d||zzdz}||S|}d} dt j|zdzjd}t j||t j s |S|d zt|z}|dz }j) z;Make a random boundary that does not appear in `all_parts`.rz%%0%ddz===============z==r z^--z(--)?$ascii)flags.) rEreprsysmaxsizerandom randrangereescapeencodesearch MULTILINEr) r9 all_parts_width_fmttokenboundarybcounterpatterns r_make_boundaryz'RepresentationDefinition._make_boundary+sT#++/*+&   -.5  O ryy|+h6>>wGG99Wir||D3W-A qLG rc|D]g\}}|j|jd|jd|j|jd|jdi|jdy)z$Write MIME headers to a file object.UTF-8s:  Nwriter)r9bufrkeyrDs r_write_headersz'RepresentationDefinition._write_headers=se! JC IIcjj) * IIe  IIell7+ , IIg    'rc|jd|j|jd|r|jd|jdy)z,Write a multipart boundary to a file object.s--rrNr)r9rr closings r_write_boundaryz(RepresentationDefinition._write_boundaryFs> % (//'*+  IIe  'rc6g}|D]Y\}}}tj}|rd}dt|dt|d}nd}dt|z}|j|dd|fd |fg|r9t |t st d t|z|j|nt |tst d t|ztjd |} | d dD]3} |j| jd|jd5|j| djd|j|j\|jdj!|} tj}dt| z}|j|dd|fg|D]6} |j#|| |j| |jd8|j#|| d||jfS)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-streamzform-data; name="z "; filename="r@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 rrmbytesrr{rrrrrrgetvaluerrFr) r9parts encoded_parts is_binaryrArDrctypecdisplinesliner  encoded_parts r_generate_multipart_formz1RepresentationDefinition._generate_multipart_formNs  &+ 1 "ItU**,C2 $Kt.6.t<   ''&.&  !%/#$@4;$NOO % !%7#5U CEE6!#2J'DIIdkk'23IIg&' %)**734   0= 1B&&w||M'BCjjl4uXF C # U #" * L  h / IIl # IIg   S(D9clln$$rc >|j}|j|j}|j||fi|}|j}|dk(r&t t |j}||fS|dk(rwg}t} |D]N} |j| j| } | | us$|j| jdk(| j| fP|j|\}}||fS|dk(rtj|}||fSt!d|z)zzBind the definition to parameter values, creating a document. :return: A 2-tuple (media_type, document). z!application/x-www-form-urlencodedzmultipart/form-databinaryrjzUnsupported media type: '%s')r_r=r3rNrrrrrobjectr8rArr{r(rpdumpsr4) r9rIrKr^r=rLrrdocr missingr1rDs rrzRepresentationDefinition.binds> ,,. ""4==15455 L5$35__ < <F#3#9#9#;<=C30 0EhG N(,,UZZA'LL%**"8%**e!LM N#;;EBOJ 3 - -**-.C3;jHI IrcL|jjj|S)z9Turn a representation ID into a RepresentationDefinition.)rTrepresentation_definitionsr8rcs rrZz,RepresentationDefinition._definition_factorys::>>rBBrcL|jjjdS)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. hrefrrgs rrXz,RepresentationDefinition._get_definition_urlrr)F)r%r&r'r(rUr=rrrrrrrr(rrZrXrrs@rrrsP<& *?AA$?%B4C+rrceZdZdZdZedZedZedZedZ edZ dZ ed Z ed Z ed Zy ) rz5One of the parameters of a representation definition.cB|j|_||_||_y)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)rTvalue_containerr5)r9r5r5s rrUzParameter.__init__s!+66.rcL|jjjdS)zThe name of this parameter.rArrgs rrAzParameter.namexx""6**rcL|jjjdS)zThe style of this parameter.r2rrgs rr2zParameter.stylesxx""7++rcL|jjjdS)zThe XSD type of this parameter.r{rrgs rr{zParameter.typer7rcL|jjjdS)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. fixedrrgs rrBzParameter.fixed_valuesxx""7++rcn|jjjddjdvS)z6Whether or not a value for this parameter is required.requiredfalse)1truerrgs rrGzParameter.is_requireds2##J8>>@ ! "rc8|jj|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. )r5rrgs r get_valuezParameter.get_values##77==rc|jjtdDcgc]}t||c}Scc}w)z7Return the set of acceptable values for this parameter.rM)r5r6rOption)r9 option_tags rrCzParameter.optionssC$(88#3#3Jx4H#IKtZ(K KKs=ch|jjtd}|yt||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)r5rrr)r9link_tags rrGzParameter.links188==F!34  D(##rcL|j}| 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. z(This parameter isn't a link to anything.)rGr4follow)r9rGs rlinked_resourcezParameter.linked_resources(yy <GH H{{rN)r%r&r'r(rUrrAr2r{rBrGrBrCrGrKrrrrrs? ++,,++,,"" >KK  $ $  rrc&eZdZdZdZedZy)rDz0One of a set of possible values for a parameter.c ||_||_y)z}Initialize the option. :param parameter: A Parameter. :param link_tag: An ElementTree