OLD | NEW |
1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 """Utilies for the processing of schema python structures. | 4 """Utilies for the processing of schema python structures. |
5 """ | 5 """ |
6 | 6 |
| 7 from json_parse import OrderedDict |
| 8 |
7 def CapitalizeFirstLetter(value): | 9 def CapitalizeFirstLetter(value): |
8 return value[0].capitalize() + value[1:] | 10 return value[0].capitalize() + value[1:] |
9 | 11 |
10 def GetNamespace(ref_type): | 12 def GetNamespace(ref_type): |
11 if '.' in ref_type: | 13 if '.' in ref_type: |
12 return ref_type[:ref_type.rindex('.')] | 14 return ref_type[:ref_type.rindex('.')] |
13 | 15 |
14 def StripSchemaNamespace(s): | 16 def StripSchemaNamespace(s): |
15 last_dot = s.rfind('.') | 17 last_dot = s.rfind('.') |
16 if not last_dot == -1: | 18 if not last_dot == -1: |
17 return s[last_dot + 1:] | 19 return s[last_dot + 1:] |
18 return s | 20 return s |
19 | 21 |
20 def PrefixSchemasWithNamespace(schemas): | 22 def PrefixSchemasWithNamespace(schemas): |
21 for s in schemas: | 23 for s in schemas: |
22 _PrefixWithNamespace(s.get("namespace"), s) | 24 _PrefixWithNamespace(s.get("namespace"), s) |
23 | 25 |
24 def _MaybePrefixFieldWithNamespace(namespace, schema, key): | 26 def _MaybePrefixFieldWithNamespace(namespace, schema, key): |
25 if type(schema) == dict and key in schema: | 27 if isinstance(schema, (dict, OrderedDict)) and key in schema: |
26 old_value = schema[key] | 28 old_value = schema[key] |
27 if not "." in old_value: | 29 if not "." in old_value: |
28 schema[key] = namespace + "." + old_value | 30 schema[key] = namespace + "." + old_value |
29 | 31 |
30 def _PrefixTypesWithNamespace(namespace, types): | 32 def _PrefixTypesWithNamespace(namespace, types): |
31 for t in types: | 33 for t in types: |
32 _MaybePrefixFieldWithNamespace(namespace, t, "id") | 34 _MaybePrefixFieldWithNamespace(namespace, t, "id") |
33 _MaybePrefixFieldWithNamespace(namespace, t, "customBindings") | 35 _MaybePrefixFieldWithNamespace(namespace, t, "customBindings") |
34 | 36 |
35 def _PrefixWithNamespace(namespace, schema): | 37 def _PrefixWithNamespace(namespace, schema): |
36 if type(schema) == dict: | 38 if isinstance(schema, (dict, OrderedDict)): |
37 if "types" in schema: | 39 if "types" in schema: |
38 _PrefixTypesWithNamespace(namespace, schema.get("types")) | 40 _PrefixTypesWithNamespace(namespace, schema.get("types")) |
39 _MaybePrefixFieldWithNamespace(namespace, schema, "$ref") | 41 _MaybePrefixFieldWithNamespace(namespace, schema, "$ref") |
40 for s in schema: | 42 for s in schema: |
41 _PrefixWithNamespace(namespace, schema[s]) | 43 _PrefixWithNamespace(namespace, schema[s]) |
42 elif type(schema) == list: | 44 elif type(schema) == list: |
43 for s in schema: | 45 for s in schema: |
44 _PrefixWithNamespace(namespace, s) | 46 _PrefixWithNamespace(namespace, s) |
45 | 47 |
46 def JsFunctionNameToClassName(namespace_name, function_name): | 48 def JsFunctionNameToClassName(namespace_name, function_name): |
47 """Transform a fully qualified function name like foo.bar.baz into FooBarBaz | 49 """Transform a fully qualified function name like foo.bar.baz into FooBarBaz |
48 | 50 |
49 Also strips any leading 'Experimental' prefix.""" | 51 Also strips any leading 'Experimental' prefix.""" |
50 parts = [] | 52 parts = [] |
51 full_name = namespace_name + "." + function_name | 53 full_name = namespace_name + "." + function_name |
52 for part in full_name.split("."): | 54 for part in full_name.split("."): |
53 parts.append(CapitalizeFirstLetter(part)) | 55 parts.append(CapitalizeFirstLetter(part)) |
54 if parts[0] == "Experimental": | 56 if parts[0] == "Experimental": |
55 del parts[0] | 57 del parts[0] |
56 class_name = "".join(parts) | 58 class_name = "".join(parts) |
57 return class_name | 59 return class_name |
OLD | NEW |