ó
‚¾^Yc           @@  sœ   d  Z  d d l m Z d d l Z d Z d d l Z y d d l m Z Wn! e k
 rk d d l m	 Z n Xd e
 f d „  ƒ  YZ d	 e
 f d
 „  ƒ  YZ d S(   sŸ  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.
i    (   t   absolute_importNs$   jcgregorio@google.com (Joe Gregorio)(   t   util(   t   _helperst   Schemasc           B@  sn   e  Z d  Z d „  Z e j d ƒ d	 d d „ ƒ Z d „  Z e j d ƒ d	 d d „ ƒ Z	 d „  Z
 d „  Z RS(
   s   Schemas for an API.c         C@  s"   | j  d i  ƒ |  _ i  |  _ d S(   s†   Constructor.

    Args:
      discovery: object, Deserialized discovery document from which we pull
        out the named schema.
    t   schemasN(   t   getR   t   pretty(   t   selft	   discovery(    (    sH   /tmp/pip-build-kpPAdC/google-api-python-client/googleapiclient/schema.pyt   __init__O   s    i   i    c         C@  sŒ   | d k r g  } n  | | k r) d | S| j | ƒ | |  j k rw t |  j | | d | ƒj |  j ƒ |  j | <n  | j ƒ  |  j | S(   sn  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.
    s   # Object with schema name: %st   dentN(   t   Nonet   appendR   t   _SchemaToStructR   t   to_strt   _prettyPrintByNamet   pop(   R   t   namet   seenR
   (    (    sH   /tmp/pip-build-kpPAdC/google-api-python-client/googleapiclient/schema.pyR   [   s    	%
c         C@  s   |  j  | d g  d d ƒd  S(   s  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
   i   iþÿÿÿ(   R   (   R   R   (    (    sH   /tmp/pip-build-kpPAdC/google-api-python-client/googleapiclient/schema.pyt   prettyPrintByNamex   s    c         C@  s4   | d k r g  } n  t | | d | ƒj |  j ƒ S(   sO  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.
    R
   N(   R   R   R   R   (   R   t   schemaR   R
   (    (    sH   /tmp/pip-build-kpPAdC/google-api-python-client/googleapiclient/schema.pyt   _prettyPrintSchema…   s    	c         C@  s   |  j  | d d ƒd  S(   sç   Get 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
   i   iþÿÿÿ(   R   (   R   R   (    (    sH   /tmp/pip-build-kpPAdC/google-api-python-client/googleapiclient/schema.pyt   prettyPrintSchema—   s    c         C@  s   |  j  | S(   sc   Get deserialized JSON schema from the schema name.

    Args:
      name: string, Schema name.
    (   R   (   R   R   (    (    sH   /tmp/pip-build-kpPAdC/google-api-python-client/googleapiclient/schema.pyR   ¤   s    N(   t   __name__t
   __module__t   __doc__R	   R   t
   positionalR   R   R   R   R   R   (    (    (    sH   /tmp/pip-build-kpPAdC/google-api-python-client/googleapiclient/schema.pyR   L   s   			R   c           B@  sh   e  Z d  Z e j d ƒ d d „ ƒ Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 d	 „  Z d
 „  Z RS(   s%   Convert schema to a prototype object.i   i    c         C@  s:   g  |  _  d |  _ | |  _ | |  _ d |  _ | |  _ d S(   sæ   Constructor.

    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(   t   valueR   t   stringR   R
   t
   from_cacheR   (   R   R   R   R
   (    (    sH   /tmp/pip-build-kpPAdC/google-api-python-client/googleapiclient/schema.pyR	   °   s    					c         C@  s$   |  j  j d |  j | d g ƒ d S(   sU   Add text as a line to the output.

    Args:
      text: string, Text to output.
    s     s   
N(   R   t   extendR
   (   R   t   text(    (    sH   /tmp/pip-build-kpPAdC/google-api-python-client/googleapiclient/schema.pyt   emitÍ   s    c         C@  s!   |  j  j d |  j | g ƒ d S(   sj   Add text to the output, but with no line terminator.

    Args:
      text: string, Text to output.
      s     N(   R   R   R
   (   R   R   (    (    sH   /tmp/pip-build-kpPAdC/google-api-python-client/googleapiclient/schema.pyt	   emitBeginÕ   s    c         C@  s’   | rx d d |  j  d d } | j ƒ  } g  | D] } | j ƒ  ^ q2 } | j | ƒ } |  j j | d | d g ƒ n |  j j | d g ƒ d S(   s“   Add text and comment to the output with line terminator.

    Args:
      text: string, Text to output.
      comment: string, Python comment.
    s   
s     i   s   # s    # N(   R
   t
   splitlinest   rstript   joinR   R   (   R   R   t   commentt   dividert   linest   x(    (    sH   /tmp/pip-build-kpPAdC/google-api-python-client/googleapiclient/schema.pyt   emitEndÝ   s    c         C@  s   |  j  d 7_  d S(   s   Increase indentation level.i   N(   R
   (   R   (    (    sH   /tmp/pip-build-kpPAdC/google-api-python-client/googleapiclient/schema.pyt   indentí   s    c         C@  s   |  j  d 8_  d S(   s   Decrease indentation level.i   N(   R
   (   R   (    (    sH   /tmp/pip-build-kpPAdC/google-api-python-client/googleapiclient/schema.pyt   undentñ   s    c         C@  s‚  | j  d ƒ } | d k rá |  j d | j  d d ƒ ƒ |  j ƒ  d | k rš xw t j | j  d i  ƒ ƒ D]* \ } } |  j d | ƒ |  j | ƒ qi Wn- d | k rÇ |  j d	 ƒ |  j | d ƒ n  |  j ƒ  |  j d
 ƒ n…d | k rl| d } | j  d d ƒ } |  j	 | d |  j
 ƒ} | j ƒ  } |  j | d | ƒ x"| d D] }	 |  j |	 j ƒ  ƒ qLWnú| d k r³| j  d d ƒ }
 |  j d t |
 ƒ | j  d d ƒ ƒ n³| d k rú| j  d d ƒ }
 |  j d t |
 ƒ | j  d d ƒ ƒ nl| d k rA| j  d d ƒ }
 |  j d t |
 ƒ | j  d d ƒ ƒ n%| d k rˆ| j  d d ƒ }
 |  j d t |
 ƒ | j  d d ƒ ƒ nÞ | d k r³|  j d | j  d d ƒ ƒ n³ | d k rÞ|  j d | j  d d ƒ ƒ nˆ | d k rE|  j d | j  d ƒ ƒ |  j ƒ  |  j d ƒ |  j | d  ƒ |  j ƒ  |  j d! ƒ n! |  j d" | ƒ |  j d d ƒ d j |  j ƒ |  _ |  j S(#   sÚ   Prototype 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.
    t   typet   objectt   {t   descriptiont    t
   propertiess   "%s": t   additionalPropertiess	   "a_key": s   },s   $refR   i    i   t   booleant   defaults   True or Falses   %s,R   s   A Strings   "%s",t   integert   42t   numbers   3.14t   nulls   None,t   anys   "",t   arrayt   [t   itemss   ],s   Unknown type! %s(   R   R)   R*   t   sixt	   iteritemsR!   t   _to_str_implR+   R    R   R   R"   R#   t   strR$   R   R   (   R   R   t   stypet   pnamet   pschemat
   schemaNameR/   t   st   partst   lineR   (    (    sH   /tmp/pip-build-kpPAdC/google-api-python-client/googleapiclient/schema.pyR?   õ   s`    	
(

))))

c         C@  s   | |  _  |  j |  j ƒ S(   sË  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   R?   R   (   R   R   (    (    sH   /tmp/pip-build-kpPAdC/google-api-python-client/googleapiclient/schema.pyR   1  s    	(   R   R   R   R   R   R	   R    R!   R)   R*   R+   R?   R   (    (    (    sH   /tmp/pip-build-kpPAdC/google-api-python-client/googleapiclient/schema.pyR   ­   s   						<(   R   t
   __future__R    R=   t
   __author__t   copyt   oauth2clientR   t   ImportErrorR   R-   R   R   (    (    (    sH   /tmp/pip-build-kpPAdC/google-api-python-client/googleapiclient/schema.pyt   <module>:   s   a