Chromium Code Reviews| Index: mojo/public/tools/bindings/pylib/mojom/generate/generator.py |
| diff --git a/mojo/public/tools/bindings/pylib/mojom/generate/generator.py b/mojo/public/tools/bindings/pylib/mojom/generate/generator.py |
| index e4ab37357e83dbd36fd16bb703b4492f2eb8a4e4..d9e81a04399e4391725f9f85b3bc099ca61e01bc 100644 |
| --- a/mojo/public/tools/bindings/pylib/mojom/generate/generator.py |
| +++ b/mojo/public/tools/bindings/pylib/mojom/generate/generator.py |
| @@ -29,10 +29,86 @@ def WriteFile(contents, full_path): |
| full_dir = os.path.dirname(full_path) |
| fileutil.EnsureDirectoryExists(full_dir) |
| + try: |
| + with open(full_path, "r") as f: |
| + if f.read() == contents: |
|
yzshen1
2017/02/01 20:25:57
Why do we need this check? It seems to be a fairly
Sam McNally
2017/02/02 22:01:46
Only when changing the bindings generator.
|
| + return |
| + except IOError: |
| + pass |
| + |
| # Dump the data to disk. |
| with open(full_path, "w+") as f: |
| f.write(contents) |
| + |
| +def NamespaceToArray(namespace): |
| + return namespace.split(".") if namespace else [] |
| + |
| + |
| +class NameFormatter(object): |
| + """A formatter for the names of kinds or values.""" |
| + |
| + def __init__(self, token, variant): |
| + self._token = token |
| + self._variant = variant |
| + |
| + def Format(self, separator, prefixed=False, |
|
yzshen1
2017/02/01 20:25:57
Could you please explain why we need to introduce
Sam McNally
2017/02/02 22:01:46
This was a leftover from a previous approach. Remo
|
| + include_variant=False, add_same_module_namespaces=False, **kwargs): |
| + parts = [] |
| + if self._ShouldIncludeNamespace(add_same_module_namespaces): |
| + if prefixed: |
| + parts.append("") |
| + parts.extend(self._GetNamespace()) |
| + if include_variant and self._variant: |
| + parts.append(self._variant) |
| + parts.extend(self._GetName(**kwargs)) |
| + return separator.join(parts) |
| + |
| + def FormatForMojom(self): |
| + return self.Format(".", add_same_module_namespaces=True) |
| + |
| + def _MapKindName(self, token, **_): |
| + return token.name |
| + |
| + def _GetName(self, flatten_nested_kind=False, **kwargs): |
| + name = [] |
| + if flatten_nested_kind and self._token.parent_kind: |
| + if mojom.IsEnumKind(self._token): |
| + name.append("%s_%s" % (self._token.parent_kind.name, |
| + self._MapKindName(self._token, **kwargs))) |
| + return name |
| + elif isinstance(self._token, mojom.EnumValue): |
| + return [self.__class__(self._token.enum, self._variant).Format( |
| + '', flatten_nested_kind=flatten_nested_kind, **kwargs), |
| + self._token.name] |
| + |
| + if self._token.parent_kind: |
| + name.append(self._MapKindName(self._token.parent_kind, **kwargs)) |
| + |
| + # Both variable and enum constants are constructed like: |
| + # Namespace::Struct::CONSTANT_NAME |
| + # For enums, CONSTANT_NAME is EnumName::ENUM_VALUE. |
| + if isinstance(self._token, mojom.EnumValue): |
| + name.extend([self._token.enum.name, self._token.name]) |
| + else: |
| + name.append(self._MapKindName(self._token, **kwargs)) |
| + return name |
| + |
| + def _ShouldIncludeNamespace(self, add_same_module_namespaces): |
| + return add_same_module_namespaces or self._token.imported_from |
| + |
| + def _GetNamespace(self): |
| + if self._token.imported_from: |
| + return NamespaceToArray(self._token.imported_from["namespace"]) |
| + elif hasattr(self._token, "module"): |
| + return NamespaceToArray(self._token.module.namespace) |
| + return [] |
| + |
| + |
| +def GetFullMojomNameForKind(kind, variant=None): |
| + return NameFormatter(kind, variant).FormatForMojom() |
| + |
| + |
| class Generator(object): |
| # Pass |output_dir| to emit files to disk. Omit |output_dir| to echo all |
| # files to stdout. |