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 import json_parse | 7 import json_parse |
8 | 8 |
9 def CapitalizeFirstLetter(value): | 9 def CapitalizeFirstLetter(value): |
10 return value[0].capitalize() + value[1:] | 10 return value[0].capitalize() + value[1:] |
11 | 11 |
12 def GetNamespace(ref_type): | 12 def GetNamespace(ref_type): |
13 if '.' in ref_type: | 13 if '.' in ref_type: |
14 return ref_type[:ref_type.rindex('.')] | 14 return ref_type[:ref_type.rindex('.')] |
15 | 15 |
16 def StripSchemaNamespace(s): | 16 def StripSchemaNamespace(s): |
17 last_dot = s.rfind('.') | 17 last_dot = s.rfind('.') |
18 if not last_dot == -1: | 18 if not last_dot == -1: |
19 return s[last_dot + 1:] | 19 return s[last_dot + 1:] |
20 return s | 20 return s |
21 | 21 |
22 def PrefixSchemasWithNamespace(schemas): | |
23 for s in schemas: | |
24 _PrefixWithNamespace(s.get("namespace"), s) | |
25 | |
26 def _MaybePrefixFieldWithNamespace(namespace, schema, key): | |
27 if json_parse.IsDict(schema) and key in schema: | |
28 old_value = schema[key] | |
29 if not "." in old_value: | |
30 schema[key] = namespace + "." + old_value | |
31 | |
32 def _PrefixTypesWithNamespace(namespace, types): | |
33 for t in types: | |
34 _MaybePrefixFieldWithNamespace(namespace, t, "id") | |
35 _MaybePrefixFieldWithNamespace(namespace, t, "customBindings") | |
36 | |
37 def _PrefixWithNamespace(namespace, schema): | |
38 if json_parse.IsDict(schema): | |
39 if "types" in schema: | |
40 _PrefixTypesWithNamespace(namespace, schema.get("types")) | |
41 _MaybePrefixFieldWithNamespace(namespace, schema, "$ref") | |
42 for s in schema: | |
43 _PrefixWithNamespace(namespace, schema[s]) | |
44 elif type(schema) == list: | |
45 for s in schema: | |
46 _PrefixWithNamespace(namespace, s) | |
47 | |
48 def JsFunctionNameToClassName(namespace_name, function_name): | 22 def JsFunctionNameToClassName(namespace_name, function_name): |
49 """Transform a fully qualified function name like foo.bar.baz into FooBarBaz | 23 """Transform a fully qualified function name like foo.bar.baz into FooBarBaz |
50 | 24 |
51 Also strips any leading 'Experimental' prefix.""" | 25 Also strips any leading 'Experimental' prefix.""" |
52 parts = [] | 26 parts = [] |
53 full_name = namespace_name + "." + function_name | 27 full_name = namespace_name + "." + function_name |
54 for part in full_name.split("."): | 28 for part in full_name.split("."): |
55 parts.append(CapitalizeFirstLetter(part)) | 29 parts.append(CapitalizeFirstLetter(part)) |
56 if parts[0] == "Experimental": | 30 if parts[0] == "Experimental": |
57 del parts[0] | 31 del parts[0] |
58 class_name = "".join(parts) | 32 class_name = "".join(parts) |
59 return class_name | 33 return class_name |
OLD | NEW |