Schemas for an API.
| 69 | |
| 70 | |
| 71 | class Schemas(object): |
| 72 | """Schemas for an API.""" |
| 73 | |
| 74 | def __init__(self, discovery): |
| 75 | """Constructor. |
| 76 | |
| 77 | Args: |
| 78 | discovery: object, Deserialized discovery document from which we pull |
| 79 | out the named schema. |
| 80 | """ |
| 81 | self.schemas = discovery.get("schemas", {}) |
| 82 | |
| 83 | # Cache of pretty printed schemas. |
| 84 | self.pretty = {} |
| 85 | |
| 86 | @util.positional(2) |
| 87 | def _prettyPrintByName(self, name, seen=None, dent=0): |
| 88 | """Get pretty printed object prototype from the schema name. |
| 89 | |
| 90 | Args: |
| 91 | name: string, Name of schema in the discovery document. |
| 92 | seen: list of string, Names of schema already seen. Used to handle |
| 93 | recursive definitions. |
| 94 | |
| 95 | Returns: |
| 96 | string, A string that contains a prototype object with |
| 97 | comments that conforms to the given schema. |
| 98 | """ |
| 99 | if seen is None: |
| 100 | seen = [] |
| 101 | |
| 102 | if name in seen: |
| 103 | # Do not fall into an infinite loop over recursive definitions. |
| 104 | return "# Object with schema name: %s" % name |
| 105 | seen.append(name) |
| 106 | |
| 107 | if name not in self.pretty: |
| 108 | self.pretty[name] = _SchemaToStruct( |
| 109 | self.schemas[name], seen, dent=dent |
| 110 | ).to_str(self._prettyPrintByName) |
| 111 | |
| 112 | seen.pop() |
| 113 | |
| 114 | return self.pretty[name] |
| 115 | |
| 116 | def prettyPrintByName(self, name): |
| 117 | """Get pretty printed object prototype from the schema name. |
| 118 | |
| 119 | Args: |
| 120 | name: string, Name of schema in the discovery document. |
| 121 | |
| 122 | Returns: |
| 123 | string, A string that contains a prototype object with |
| 124 | comments that conforms to the given schema. |
| 125 | """ |
| 126 | # Return with trailing comma and newline removed. |
| 127 | return self._prettyPrintByName(name, seen=[], dent=0)[:-2] |
| 128 |
no outgoing calls
searching dependent graphs…