Chromium Code Reviews| Index: tools/grit/grit/format/rc_header.py |
| diff --git a/tools/grit/grit/format/rc_header.py b/tools/grit/grit/format/rc_header.py |
| index 74e7127b5840b57a90aa022737c721f638280b33..69bd8a7717d4134bc6c9c7cb126c3abbecc219eb 100755 |
| --- a/tools/grit/grit/format/rc_header.py |
| +++ b/tools/grit/grit/format/rc_header.py |
| @@ -77,6 +77,24 @@ def FormatDefines(root, output_all_resource_defines=True, |
| _cached_ids = {} |
| +_predetermined_tids = {} |
|
Nico
2017/02/17 21:44:08
nit: python style guide says 2 blank lines between
Alexei Svitkine (slow)
2017/02/17 22:07:51
Done.
|
| + |
| +def SetPredeterminedIdsFile(predetermined_ids_file): |
| + global _predetermined_tids |
| + if predetermined_ids_file: |
| + _predetermined_tids = _ReadIdsFromFile(predetermined_ids_file) |
| + else: |
| + _predetermined_tids = {} |
| + |
| + |
| +def _ReadIdsFromFile(path): |
| + with open(path, "r") as f: |
| + content = f.readlines() |
| + tids = {} # Maps textual id to numeric id |
| + for line in content: |
| + tid, id = line.split() |
| + tids[tid] = int(id) |
| + return tids |
| def GetIds(root): |
| @@ -85,15 +103,17 @@ def GetIds(root): |
| Args: |
| root: A GritNode. |
| ''' |
| + global _cached_ids |
| + global _predetermined_tids |
| # TODO(benrg): Since other formatters use this, it might make sense to move it |
| # and _ComputeIds to GritNode and store the cached ids as an attribute. On the |
| # other hand, GritNode has too much random stuff already. |
| if root not in _cached_ids: |
| - _cached_ids[root] = _ComputeIds(root) |
| + _cached_ids[root] = _ComputeIds(root, _predetermined_tids) |
| return _cached_ids[root] |
| -def _ComputeIds(root): |
| +def _ComputeIds(root, predetermined_tids): |
| from grit.node import empty, include, message, misc, structure |
| ids = {} # Maps numeric id to textual id |
| @@ -102,6 +122,11 @@ def _ComputeIds(root): |
| group = None |
| last_id = None |
| + predetermined_ids = {} |
| + if predetermined_ids: |
|
Nico
2017/02/17 21:44:08
given the previous lien, won't predetermined_ids a
Alexei Svitkine (slow)
2017/02/17 22:07:51
Woops, this is meant to be predetermined_tids orig
|
| + for tid, id in predetermined_tids.iteritems(): |
| + predetermined_ids[id] = tid |
| + |
| for item in root: |
| if isinstance(item, empty.GroupingNode): |
| # Note: this won't work if any GroupingNode can be contained inside |
| @@ -129,9 +154,13 @@ def _ComputeIds(root): |
| if tid in tids: |
| continue |
| + if predetermined_tids and tid in predetermined_tids: |
| + id = predetermined_tids[tid] |
| + reason = "from predetermined_tids map" |
| + |
| # Some identifier nodes can provide their own id, |
| # and we use that id in the generated header in that case. |
| - if hasattr(item, 'GetId') and item.GetId(): |
| + elif hasattr(item, 'GetId') and item.GetId(): |
| id = long(item.GetId()) |
| reason = 'returned by GetId() method' |
| @@ -197,6 +226,10 @@ def _ComputeIds(root): |
| print ('WARNING: Numeric resource IDs should be greater than 100 to\n' |
| 'avoid conflicts with system-defined resource IDs.') |
| + if tid not in predetermined_tids and id in predetermined_ids: |
| + raise exception.IdRangeOverlap('ID %d overlaps between %s and %s' |
| + % (id, tid, predetermined_ids[tid])) |
| + |
| ids[id] = tid |
| tids[tid] = id |
| id_reasons[id] = reason |