!;i(jdZddlmZdZddlmZddlmZGdde Z Gdd e Z d S) aSchema processing for discovery based APIs Schemas holds an APIs discovery schemas. It can return those schema as deserialized JSON objects, or pretty print them as prototype objects that conform to the schema. For example, given the schema: schema = """{ "Foo": { "type": "object", "properties": { "etag": { "type": "string", "description": "ETag of the collection." }, "kind": { "type": "string", "description": "Type of the collection ('calendar#acl').", "default": "calendar#acl" }, "nextPageToken": { "type": "string", "description": "Token used to access the next page of this result. Omitted if no further results are available." } } } }""" s = Schemas(schema) print s.prettyPrintByName('Foo') Produces the following output: { "nextPageToken": "A String", # Token used to access the # next page of this result. Omitted if no further results are available. "kind": "A String", # Type of the collection ('calendar#acl'). "etag": "A String", # ETag of the collection. }, The constructor takes a discovery document in which to look up named schema. )absolute_importz$jcgregorio@google.com (Joe Gregorio)) OrderedDict)_helpersceZdZdZdZejdd dZdZejdd dZ d Z d d Z dS) SchemaszSchemas for an API.cJ|di|_i|_dS)zConstructor. Args: discovery: object, Deserialized discovery document from which we pull out the named schema. schemasN)getr pretty)self discoverys JC:\PYTHON\MyICR_Workspace\venv\Lib\site-packages\googleapiclient/schema.py__init__zSchemas.__init__Js&!}}Y33  Nrc|g}||vrd|zS||||jvr=t|j||||j|j|<||j|S)aGet pretty printed object prototype from the schema name. Args: name: string, Name of schema in the discovery document. seen: list of string, Names of schema already seen. Used to handle recursive definitions. Returns: string, A string that contains a prototype object with comments that conforms to the given schema. Nz# Object with schema name: %sdent)appendr _SchemaToStructr to_str_prettyPrintByNamepop)r nameseenrs rrzSchemas._prettyPrintByNameVs <D 4<<2T9 9 D t{ " " / T"Dt!!!fT,-- K   {4  rcB||gdddS)aGet pretty printed object prototype from the schema name. Args: name: string, Name of schema in the discovery document. Returns: string, A string that contains a prototype object with comments that conforms to the given schema. r)rrN)r)r rs rprettyPrintByNamezSchemas.prettyPrintByNamets(&&t"1&==crcBBrc^|g}t||||jS)aoGet pretty printed object prototype of schema. Args: schema: object, Parsed JSON schema. seen: list of string, Names of schema already seen. Used to handle recursive definitions. Returns: string, A string that contains a prototype object with comments that conforms to the given schema. Nr)rrrr schemarrs r_prettyPrintSchemazSchemas._prettyPrintSchemas4 <Dvt$777>>t?VWWWrc@||dddS)zGet pretty printed object prototype of schema. Args: schema: object, Parsed JSON schema. Returns: string, A string that contains a prototype object with comments that conforms to the given schema. rrNr)r")r r!s rprettyPrintSchemazSchemas.prettyPrintSchemas&&&vA&66ss;;rc8|j||S)zGet deserialized JSON schema from the schema name. Args: name: string, Schema name. default: object, return value if name not found. )r r )r rdefaults rr z Schemas.gets|g...r)Nr)N) __name__ __module__ __qualname____doc__rutil positionalrrr"r$r rrrrGs   T_Q!!!!: C C CT_QXXXX" < < <//////rrcpeZdZdZejdd dZdZdZdZ dZ d Z d Z d Z d S)rz%Convert schema to a prototype object.rcZg|_d|_||_||_d|_||_dS)zConstructor. Args: schema: object, Parsed JSON schema. seen: list, List of names of schema already seen while parsing. Used to handle recursive definitions. dent: int, Initial indentation depth. N)valuestringr!r from_cacherr s rrz_SchemaToStruct.__init__s<     rcP|jd|jz|dgdS)zaAdd text as a line to the output. Args: text: string, Text to output.   Nr1extendrr texts remitz_SchemaToStruct.emits. 4$)+T4899999rcN|jd|jz|gdS)ztAdd text to the output, but with no line terminator. Args: text: string, Text to output. r5Nr7r9s r emitBeginz_SchemaToStruct.emitBegins, 4$)+T233333rc|rhdd|jdzzzdz}|}d|D}||}|j|d|dgdS|j|dgdS)zAdd text and comment to the output with line terminator. Args: text: string, Text to output. comment: string, Python comment. r6r5rz# c6g|]}|Sr-)rstrip).0xs r z+_SchemaToStruct.emitEnd..s ///AQXXZZ///rz # N)r splitlinesjoinr1r8)r r:commentdividerliness remitEndz_SchemaToStruct.emitEnds  ,TTY]33d:G&&((E/////Ell5))G J  tUGT: ; ; ; ; ; J  tTl + + + + +rc&|xjdz c_dS)zIncrease indentation level.Nrr s rindentz_SchemaToStruct.indent Q rc&|xjdzc_dS)zDecrease indentation level.rKNrrLs rundentz_SchemaToStruct.undentrNrcB |d}|dkr-|d|dd|d|vr|di}tt |}|D]2\}}|d|z||3n4d|vr0|d ||d|| d n1d |vr|d }|dd}| ||j } | } || d || ddD])} | | *n|dkrR|dd} |dt| z|ddn5|dkrR|dd} |dt| z|ddn|dkrR|dd} |dt| z|ddn|dkrR|dd} |dt| z|ddn-|dkr+|d|ddn|dkr+|d|ddn|dkr|d |d||d||d!|| d"n.| d#|z|ddd|j|_|jS)$zPrototype object based on the schema, in Python code with comments. Args: schema: object, Parsed JSON schema file. Returns: Prototype object based on the schema, in Python code with comments. typeobject{ description propertiesz"%s": additionalPropertiesz "a_key": z},z$ref)rrrKNbooleanr&z True or Falsez%s,r2zA Stringz"%s",integer42numberz3.14nullzNone,anyz"",array[itemsz],zUnknown type! %s)r rIrMrsortedrar= _to_str_implrPr;r3rrDr@strrEr1r2) r r!styperWsorted_propertiespnamepschema schemaNamerUspartsliner1s rrcz_SchemaToStruct._to_str_impls1 6"" H   LLfjj;; < < < KKMMMv%%#ZZ b99 $/z7G7G7I7I0J0J$K$K!&7&=&=&?&?//NE7NN8e#3444%%g..../(611{+++!!&)?"@AAA KKMMM IIdOOOO v  J **]B77K ;;ALLNNE LLq; / / /abb  ) ) $++--(((( ) i  JJy/::E LLU+VZZ r-J-J K K K K h  JJy*55E LL3u::-vzz-/L/L M M M M i  JJy$//E LLU+VZZ r-J-J K K K K h  JJy&11E LLU+VZZ r-J-J K K K K f__ LL&**]B"?"? @ @ @ @ e^^ LL =" = = > > > > g   LLfjj77 8 8 8 KKMMM NN2      fWo . . . KKMMM IIdOOOO II(50 1 1 1 LLR ggdj)) {rcD||_||jS)aPrototype object based on the schema, in Python code with comments. Args: from_cache: callable(name, seen), Callable that retrieves an object prototype for a schema with the given name. Seen is a list of schema names already seen as we recursively descend the schema definition. Returns: Prototype object based on the schema, in Python code with comments. The lines of the code will all be properly indented. )r3rcr!)r r3s rrz_SchemaToStruct.to_str0s!%  ---rN)r)r'r(r)r*r+r,rr;r=rIrMrPrcrr-rrrrs//T_Q8:::444,,, <<<| . . . . .rrN) r* __future__r __author__ collectionsrgoogleapiclientrr+rSrrr-rrrrs++X'&&&&&4 $#####,,,,,,`/`/`/`/`/f`/`/`/FS.S.S.S.S.fS.S.S.S.S.r