U
    	ڲg(                     @   sP   d Z ddlmZ dZddlmZ ddlmZ G dd de	Z
G dd	 d	e	Zd
S )a  Schema 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)_helpersc                   @   sZ   e Zd ZdZdd ZeddddZd	d
 ZeddddZ	dd Z
dddZdS )SchemaszSchemas for an API.c                 C   s   | di | _i | _dS )zConstructor.

        Args:
          discovery: object, Deserialized discovery document from which we pull
            out the named schema.
        schemasN)getr   pretty)selfZ	discovery r
   :/tmp/pip-unpacked-wheel-rfrvjkbl/googleapiclient/schema.py__init__J   s    zSchemas.__init__   Nr   c                 C   sd   |dkrg }||krd| S | | || jkrRt| j| ||d| j| j|< |  | j| S )a  Get 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: %sdent)appendr   _SchemaToStructr   to_str_prettyPrintByNamepop)r	   nameseenr   r
   r
   r   r   V   s    

  
zSchemas._prettyPrintByNamec                 C   s   | j |g dddd S )a  Get 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   )r   r   N)r   )r	   r   r
   r
   r   prettyPrintByNamet   s    zSchemas.prettyPrintByNamec                 C   s"   |dkrg }t |||d| jS )ao  Get 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   )r   r   r   r	   schemar   r   r
   r
   r   _prettyPrintSchema   s    zSchemas._prettyPrintSchemac                 C   s   | j |dddd S )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.
        r   r   Nr   )r   )r	   r   r
   r
   r   prettyPrintSchema   s    zSchemas.prettyPrintSchemac                 C   s   | 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	   r   defaultr
   r
   r   r      s    zSchemas.get)Nr   )Nr   )N)__name__
__module____qualname____doc__r   util
positionalr   r   r   r   r   r
   r
   r
   r   r   G   s   r   c                   @   s\   e Zd ZdZeddddZdd Zdd	 Zd
d Z	dd Z
dd Zdd Zdd ZdS )r   z%Convert schema to a prototype object.   r   c                 C   s(   g | _ d| _|| _|| _d| _|| _dS )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)valuestringr   r   
from_cacher   r   r
   r
   r   r      s    z_SchemaToStruct.__init__c                 C   s   | j d| j |dg dS )zaAdd text as a line to the output.

        Args:
          text: string, Text to output.
          
Nr%   extendr   r	   textr
   r
   r   emit   s    z_SchemaToStruct.emitc                 C   s   | j d| j |g dS )ztAdd text to the output, but with no line terminator.

        Args:
          text: string, Text to output.
        r(   Nr*   r,   r
   r
   r   	emitBegin   s    z_SchemaToStruct.emitBeginc                 C   sd   |rPdd| j d   d }| }dd |D }||}| j|d|dg n| j|dg dS )	zAdd text and comment to the output with line terminator.

        Args:
          text: string, Text to output.
          comment: string, Python comment.
        r)   r(   r   z# c                 S   s   g | ]}|  qS r
   )rstrip).0xr
   r
   r   
<listcomp>   s     z+_SchemaToStruct.emitEnd.<locals>.<listcomp>z # N)r   
splitlinesjoinr%   r+   )r	   r-   commentdividerlinesr
   r
   r   emitEnd   s    
z_SchemaToStruct.emitEndc                 C   s   |  j d7  _ dS )zIncrease indentation level.   Nr   r	   r
   r
   r   indent   s    z_SchemaToStruct.indentc                 C   s   |  j d8  _ dS )zDecrease indentation level.r:   Nr   r;   r
   r
   r   undent   s    z_SchemaToStruct.undentc                 C   s  | d}|dkr| d| dd |   d|kr~| di }tt| }| D ] \}}| d|  | | qZn d|kr| d	 | |d  |   | 	d
 nd|kr |d }| dd}| j
|| jd}	|	 }
| |
d | |
dd D ]}| 	|  qn~|dkrV| dd}| dt| | dd nH|dkr| dd}| dt| | dd n|dkr| dd}| dt| | dd n|dkr| dd}| dt| | dd n|dkr| d| dd n|dkr4| d| dd nj|dkr| d | d |   | d | |d!  |   | 	d" n| 	d#|  | dd d| j| _| jS )$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.
        typeobject{description 
propertiesz"%s": ZadditionalPropertiesz	"a_key": z},z$ref)r   r   r:   Nbooleanr   zTrue or Falsez%s,r&   zA Stringz"%s",integerZ42numberz3.14nullzNone,anyz"",array[itemsz],zUnknown type! %s)r   r9   r<   r   sortedrK   r/   _to_str_implr=   r.   r'   r   r4   r0   strr5   r%   r&   )r	   r   styperC   Zsorted_propertiesZpnameZpschemaZ
schemaNamerA   spartsliner%   r
   r
   r   rM      sd    	



 
 





z_SchemaToStruct._to_str_implc                 C   s   || _ | | jS )a  Prototype 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.
        )r'   rM   r   )r	   r'   r
   r
   r   r   0  s    z_SchemaToStruct.to_strN)r   )r   r   r    r!   r"   r#   r   r.   r/   r9   r<   r=   rM   r   r
   r
   r
   r   r      s   >r   N)r!   
__future__r   
__author__collectionsr   Zgoogleapiclientr   r"   r?   r   r   r
   r
   r
   r   <module>   s   ,c