Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(25)

Side by Side Diff: appengine/chromium_build_logs/third_party/googleapiclient/schema.py

Issue 1260293009: make version of ts_mon compatible with appengine (Closed) Base URL: https://chromium.googlesource.com/infra/infra.git@master
Patch Set: clean up code Created 5 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 # Copyright (C) 2010 Google Inc. 1 # Copyright 2014 Google Inc. All Rights Reserved.
2 # 2 #
3 # Licensed under the Apache License, Version 2.0 (the "License"); 3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License. 4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at 5 # You may obtain a copy of the License at
6 # 6 #
7 # http://www.apache.org/licenses/LICENSE-2.0 7 # http://www.apache.org/licenses/LICENSE-2.0
8 # 8 #
9 # Unless required by applicable law or agreed to in writing, software 9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS, 10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
49 49
50 { 50 {
51 "nextPageToken": "A String", # Token used to access the 51 "nextPageToken": "A String", # Token used to access the
52 # next page of this result. Omitted if no further results are available. 52 # next page of this result. Omitted if no further results are available.
53 "kind": "A String", # Type of the collection ('calendar#acl'). 53 "kind": "A String", # Type of the collection ('calendar#acl').
54 "etag": "A String", # ETag of the collection. 54 "etag": "A String", # ETag of the collection.
55 }, 55 },
56 56
57 The constructor takes a discovery document in which to look up named schema. 57 The constructor takes a discovery document in which to look up named schema.
58 """ 58 """
59 from __future__ import absolute_import
60 import six
59 61
60 # TODO(jcgregorio) support format, enum, minimum, maximum 62 # TODO(jcgregorio) support format, enum, minimum, maximum
61 63
62 __author__ = 'jcgregorio@google.com (Joe Gregorio)' 64 __author__ = 'jcgregorio@google.com (Joe Gregorio)'
63 65
64 import copy 66 import copy
65 from oauth2client.anyjson import simplejson 67
68 from oauth2client import util
66 69
67 70
68 class Schemas(object): 71 class Schemas(object):
69 """Schemas for an API.""" 72 """Schemas for an API."""
70 73
71 def __init__(self, discovery): 74 def __init__(self, discovery):
72 """Constructor. 75 """Constructor.
73 76
74 Args: 77 Args:
75 discovery: object, Deserialized discovery document from which we pull 78 discovery: object, Deserialized discovery document from which we pull
76 out the named schema. 79 out the named schema.
77 """ 80 """
78 self.schemas = discovery.get('schemas', {}) 81 self.schemas = discovery.get('schemas', {})
79 82
80 # Cache of pretty printed schemas. 83 # Cache of pretty printed schemas.
81 self.pretty = {} 84 self.pretty = {}
82 85
86 @util.positional(2)
83 def _prettyPrintByName(self, name, seen=None, dent=0): 87 def _prettyPrintByName(self, name, seen=None, dent=0):
84 """Get pretty printed object prototype from the schema name. 88 """Get pretty printed object prototype from the schema name.
85 89
86 Args: 90 Args:
87 name: string, Name of schema in the discovery document. 91 name: string, Name of schema in the discovery document.
88 seen: list of string, Names of schema already seen. Used to handle 92 seen: list of string, Names of schema already seen. Used to handle
89 recursive definitions. 93 recursive definitions.
90 94
91 Returns: 95 Returns:
92 string, A string that contains a prototype object with 96 string, A string that contains a prototype object with
93 comments that conforms to the given schema. 97 comments that conforms to the given schema.
94 """ 98 """
95 if seen is None: 99 if seen is None:
96 seen = [] 100 seen = []
97 101
98 if name in seen: 102 if name in seen:
99 # Do not fall into an infinite loop over recursive definitions. 103 # Do not fall into an infinite loop over recursive definitions.
100 return '# Object with schema name: %s' % name 104 return '# Object with schema name: %s' % name
101 seen.append(name) 105 seen.append(name)
102 106
103 if name not in self.pretty: 107 if name not in self.pretty:
104 self.pretty[name] = _SchemaToStruct(self.schemas[name], 108 self.pretty[name] = _SchemaToStruct(self.schemas[name],
105 seen, dent).to_str(self._prettyPrintByName) 109 seen, dent=dent).to_str(self._prettyPrintByName)
106 110
107 seen.pop() 111 seen.pop()
108 112
109 return self.pretty[name] 113 return self.pretty[name]
110 114
111 def prettyPrintByName(self, name): 115 def prettyPrintByName(self, name):
112 """Get pretty printed object prototype from the schema name. 116 """Get pretty printed object prototype from the schema name.
113 117
114 Args: 118 Args:
115 name: string, Name of schema in the discovery document. 119 name: string, Name of schema in the discovery document.
116 120
117 Returns: 121 Returns:
118 string, A string that contains a prototype object with 122 string, A string that contains a prototype object with
119 comments that conforms to the given schema. 123 comments that conforms to the given schema.
120 """ 124 """
121 # Return with trailing comma and newline removed. 125 # Return with trailing comma and newline removed.
122 return self._prettyPrintByName(name, seen=[], dent=1)[:-2] 126 return self._prettyPrintByName(name, seen=[], dent=1)[:-2]
123 127
128 @util.positional(2)
124 def _prettyPrintSchema(self, schema, seen=None, dent=0): 129 def _prettyPrintSchema(self, schema, seen=None, dent=0):
125 """Get pretty printed object prototype of schema. 130 """Get pretty printed object prototype of schema.
126 131
127 Args: 132 Args:
128 schema: object, Parsed JSON schema. 133 schema: object, Parsed JSON schema.
129 seen: list of string, Names of schema already seen. Used to handle 134 seen: list of string, Names of schema already seen. Used to handle
130 recursive definitions. 135 recursive definitions.
131 136
132 Returns: 137 Returns:
133 string, A string that contains a prototype object with 138 string, A string that contains a prototype object with
134 comments that conforms to the given schema. 139 comments that conforms to the given schema.
135 """ 140 """
136 if seen is None: 141 if seen is None:
137 seen = [] 142 seen = []
138 143
139 return _SchemaToStruct(schema, seen, dent).to_str(self._prettyPrintByName) 144 return _SchemaToStruct(schema, seen, dent=dent).to_str(self._prettyPrintByNa me)
140 145
141 def prettyPrintSchema(self, schema): 146 def prettyPrintSchema(self, schema):
142 """Get pretty printed object prototype of schema. 147 """Get pretty printed object prototype of schema.
143 148
144 Args: 149 Args:
145 schema: object, Parsed JSON schema. 150 schema: object, Parsed JSON schema.
146 151
147 Returns: 152 Returns:
148 string, A string that contains a prototype object with 153 string, A string that contains a prototype object with
149 comments that conforms to the given schema. 154 comments that conforms to the given schema.
150 """ 155 """
151 # Return with trailing comma and newline removed. 156 # Return with trailing comma and newline removed.
152 return self._prettyPrintSchema(schema, dent=1)[:-2] 157 return self._prettyPrintSchema(schema, dent=1)[:-2]
153 158
154 def get(self, name): 159 def get(self, name):
155 """Get deserialized JSON schema from the schema name. 160 """Get deserialized JSON schema from the schema name.
156 161
157 Args: 162 Args:
158 name: string, Schema name. 163 name: string, Schema name.
159 """ 164 """
160 return self.schemas[name] 165 return self.schemas[name]
161 166
162 167
163 class _SchemaToStruct(object): 168 class _SchemaToStruct(object):
164 """Convert schema to a prototype object.""" 169 """Convert schema to a prototype object."""
165 170
171 @util.positional(3)
166 def __init__(self, schema, seen, dent=0): 172 def __init__(self, schema, seen, dent=0):
167 """Constructor. 173 """Constructor.
168 174
169 Args: 175 Args:
170 schema: object, Parsed JSON schema. 176 schema: object, Parsed JSON schema.
171 seen: list, List of names of schema already seen while parsing. Used to 177 seen: list, List of names of schema already seen while parsing. Used to
172 handle recursive definitions. 178 handle recursive definitions.
173 dent: int, Initial indentation depth. 179 dent: int, Initial indentation depth.
174 """ 180 """
175 # The result of this parsing kept as list of strings. 181 # The result of this parsing kept as list of strings.
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
237 Args: 243 Args:
238 schema: object, Parsed JSON schema file. 244 schema: object, Parsed JSON schema file.
239 245
240 Returns: 246 Returns:
241 Prototype object based on the schema, in Python code with comments. 247 Prototype object based on the schema, in Python code with comments.
242 """ 248 """
243 stype = schema.get('type') 249 stype = schema.get('type')
244 if stype == 'object': 250 if stype == 'object':
245 self.emitEnd('{', schema.get('description', '')) 251 self.emitEnd('{', schema.get('description', ''))
246 self.indent() 252 self.indent()
247 for pname, pschema in schema.get('properties', {}).iteritems(): 253 if 'properties' in schema:
248 self.emitBegin('"%s": ' % pname) 254 for pname, pschema in six.iteritems(schema.get('properties', {})):
249 self._to_str_impl(pschema) 255 self.emitBegin('"%s": ' % pname)
256 self._to_str_impl(pschema)
257 elif 'additionalProperties' in schema:
258 self.emitBegin('"a_key": ')
259 self._to_str_impl(schema['additionalProperties'])
250 self.undent() 260 self.undent()
251 self.emit('},') 261 self.emit('},')
252 elif '$ref' in schema: 262 elif '$ref' in schema:
253 schemaName = schema['$ref'] 263 schemaName = schema['$ref']
254 description = schema.get('description', '') 264 description = schema.get('description', '')
255 s = self.from_cache(schemaName, self.seen) 265 s = self.from_cache(schemaName, seen=self.seen)
256 parts = s.splitlines() 266 parts = s.splitlines()
257 self.emitEnd(parts[0], description) 267 self.emitEnd(parts[0], description)
258 for line in parts[1:]: 268 for line in parts[1:]:
259 self.emit(line.rstrip()) 269 self.emit(line.rstrip())
260 elif stype == 'boolean': 270 elif stype == 'boolean':
261 value = schema.get('default', 'True or False') 271 value = schema.get('default', 'True or False')
262 self.emitEnd('%s,' % str(value), schema.get('description', '')) 272 self.emitEnd('%s,' % str(value), schema.get('description', ''))
263 elif stype == 'string': 273 elif stype == 'string':
264 value = schema.get('default', 'A String') 274 value = schema.get('default', 'A String')
265 self.emitEnd('"%s",' % str(value), schema.get('description', '')) 275 self.emitEnd('"%s",' % str(value), schema.get('description', ''))
(...skipping 28 matching lines...) Expand all
294 from_cache: callable(name, seen), Callable that retrieves an object 304 from_cache: callable(name, seen), Callable that retrieves an object
295 prototype for a schema with the given name. Seen is a list of schema 305 prototype for a schema with the given name. Seen is a list of schema
296 names already seen as we recursively descend the schema definition. 306 names already seen as we recursively descend the schema definition.
297 307
298 Returns: 308 Returns:
299 Prototype object based on the schema, in Python code with comments. 309 Prototype object based on the schema, in Python code with comments.
300 The lines of the code will all be properly indented. 310 The lines of the code will all be properly indented.
301 """ 311 """
302 self.from_cache = from_cache 312 self.from_cache = from_cache
303 return self._to_str_impl(self.schema) 313 return self._to_str_impl(self.schema)
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698