Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 # Copyright 2014 The Chromium Authors. All rights reserved. | |
| 2 # Use of this source code is governed by a BSD-style license that can be | |
| 3 # found in the LICENSE file. | |
| 4 | |
| 5 """Generates Python source files from a mojom.Module.""" | |
| 6 | |
| 7 import re | |
| 8 | |
| 9 import mojom.generate.generator as generator | |
| 10 import mojom.generate.module as mojom | |
| 11 from mojom.generate.template_expander import UseJinja | |
| 12 | |
| 13 | |
| 14 def NameToComponent(name): | |
|
pkl (ping after 24h if needed)
2014/09/01 12:52:02
style: Function names should be name_to_component(
qsr
2014/09/01 14:19:38
The style of all the python files in mojo/public/t
pkl (ping after 24h if needed)
2014/09/01 14:35:10
Ack. Chromium has it's own style guide http://www.
| |
| 15 # insert '_' between anything and a Title name (e.g, HTTPEntry2FooBar -> | |
| 16 # HTTP_Entry2_FooBar) | |
| 17 name = re.sub('([^_])([A-Z][^A-Z_]+)', r'\1_\2', name) | |
| 18 # insert '_' between non upper and start of upper blocks (e.g., | |
| 19 # HTTP_Entry2_FooBar -> HTTP_Entry2_Foo_Bar) | |
| 20 name = re.sub('([^A-Z_])([A-Z])', r'\1_\2', name) | |
| 21 return [x.lower() for x in name.split('_')] | |
| 22 | |
| 23 def CapitalizeFirst(string): | |
| 24 return string[0].upper() + string[1:] | |
| 25 | |
| 26 def UpperCamelCase(name): | |
| 27 return ''.join([CapitalizeFirst(x) for x in NameToComponent(name)]) | |
|
pkl (ping after 24h if needed)
2014/09/01 12:52:02
why not just use x.capitalize() ? No need to defin
qsr
2014/09/01 14:19:38
Done.
| |
| 28 | |
| 29 def CamelCase(name): | |
| 30 uccc = UpperCamelCase(name) | |
| 31 return uccc[0].lower() + uccc[1:] | |
| 32 | |
| 33 def ConstantStyle(name): | |
| 34 components = NameToComponent(name) | |
| 35 if components[0] == 'k': | |
| 36 components = components[1:] | |
| 37 return '_'.join([x.upper() for x in components]) | |
| 38 | |
| 39 def GetNameForElement(element): | |
| 40 if isinstance(element, (mojom.NamedValue, | |
| 41 mojom.Constant)): | |
| 42 return ConstantStyle(element.name) | |
| 43 raise Exception("Unexpected element: " % element) | |
|
pkl (ping after 24h if needed)
2014/09/01 12:52:02
style: Please use " and ' for string constants con
qsr
2014/09/01 14:19:38
Done.
| |
| 44 | |
| 45 def TranslateConstants(token): | |
| 46 if isinstance(token, (mojom.EnumValue, mojom.NamedValue)): | |
| 47 # Both variable and enum constants are constructed like: | |
| 48 # NamespaceUid.Struct[.Enum].CONSTANT_NAME | |
| 49 name = [] | |
| 50 if token.imported_from: | |
| 51 name.append(token.imported_from["python_module"]) | |
| 52 if token.parent_kind: | |
| 53 name.append(GetNameForElement(token.parent_kind)) | |
| 54 if isinstance(token, mojom.EnumValue): | |
| 55 name.append(GetNameForElement(token)) | |
| 56 else: | |
| 57 name.append(token.name) | |
| 58 return ".".join(name) | |
| 59 | |
| 60 if isinstance(token, mojom.BuiltinValue): | |
| 61 if token.value == "double.INFINITY" or token.value == "float.INFINITY": | |
| 62 return "float('inf')"; | |
| 63 if token.value == "double.NEGATIVE_INFINITY" or \ | |
|
pkl (ping after 24h if needed)
2014/09/01 12:52:02
style: do not use backslash for continuation. Use:
qsr
2014/09/01 14:19:38
Done.
| |
| 64 token.value == "float.NEGATIVE_INFINITY": | |
| 65 return "float('-inf')" | |
| 66 if token.value == "double.NAN" or token.value == "float.NAN": | |
| 67 return "float('nan')"; | |
| 68 | |
| 69 return token | |
| 70 | |
| 71 | |
| 72 def ExpressionToText(value): | |
| 73 return TranslateConstants(value) | |
| 74 | |
| 75 | |
| 76 class Generator(generator.Generator): | |
| 77 | |
| 78 python_filters = { | |
| 79 "expression_to_text": ExpressionToText, | |
| 80 "name": GetNameForElement, | |
| 81 } | |
| 82 | |
| 83 @UseJinja("python_templates/module.py.tmpl", filters=python_filters) | |
| 84 def GeneratePythonModule(self): | |
| 85 return { | |
| 86 "imports": self.GetImports(), | |
| 87 "module": self.module, | |
| 88 } | |
| 89 | |
| 90 def GenerateFiles(self, args): | |
| 91 self.Write(self.GeneratePythonModule(), "%s.py" % self.module.name.replace(' .mojom', '_mojom')) | |
|
pkl (ping after 24h if needed)
2014/09/01 12:52:02
line length > 80
qsr
2014/09/01 14:19:38
Done.
| |
| 92 | |
| 93 def GetImports(self): | |
| 94 for each in self.module.imports: | |
| 95 each["python_module"] = each['module_name'].replace('.mojom', '_mojom') | |
| 96 return self.module.imports | |
| 97 | |
| 98 def GetJinjaParameters(self): | |
| 99 return { | |
| 100 'lstrip_blocks': True, | |
| 101 'trim_blocks': True, | |
| 102 } | |
| OLD | NEW |