Chromium Code Reviews| Index: scripts/slave/recipe_modules/luci_config/api.py |
| diff --git a/scripts/slave/recipe_modules/luci_config/api.py b/scripts/slave/recipe_modules/luci_config/api.py |
| index 5ccd0f39ad2f3b37e420c8c9e6d0ea8309547519..98c96fa41c2712a0df921b5d4bfe2aedcda1c59b 100644 |
| --- a/scripts/slave/recipe_modules/luci_config/api.py |
| +++ b/scripts/slave/recipe_modules/luci_config/api.py |
| @@ -5,6 +5,8 @@ |
| from recipe_engine import recipe_api |
| import base64 |
| +import collections |
| +import re |
| import json |
| class LuciConfigApi(recipe_api.RecipeApi): |
| @@ -71,3 +73,54 @@ class LuciConfigApi(recipe_api.RecipeApi): |
| result = json.loads(fetch_result) |
| result['content'] = base64.b64decode(result['content']) |
| return result |
| + |
| + def parse_protobuf(self, lines): |
|
iannucci
2016/06/14 21:46:09
can we rename this to `parse_textproto`? protobuf
martiniss
2016/06/14 23:11:33
Done.
|
| + """(badly) parses a protobuf. |
| + |
| + This is not real protobuf parsing at the moment; eventually, maybe it could |
| + be. For now, it's enough to just get by. |
| + |
| + We assume all fields are repeated since we don't have a proto spec to work |
| + with. |
| + |
| + Args: |
| + lines: a list of the lines to parse |
| + Returns: |
| + A recursive dictionary of lists. |
| + """ |
| + def parse_atom(text): |
| + # NOTE: Assuming we only have numbers and strings to avoid using |
| + # ast.literal_eval |
| + try: |
| + return int(text) |
| + except ValueError: |
| + return text.strip("'").strip('"') |
| + |
| + ret = {} |
| + while lines: |
| + line = lines.pop(0).strip() |
| + |
| + m = re.match(r'(\w+)\s*:\s*(.*)', line) |
| + if m: |
| + ret.setdefault(m.group(1), []).append(parse_atom(m.group(2))) |
| + continue |
| + |
| + m = re.match(r'(\w+)\s*{', line) |
| + if m: |
| + subparse = self.parse_protobuf(lines) |
| + ret.setdefault(m.group(1), []).append(subparse) |
| + continue |
| + |
| + if line == '}': |
| + return ret |
| + if line == '': |
| + continue |
| + |
| + raise ValueError( |
| + 'Could not understand line: <%s>' % line) # pragma: no cover |
| + return ret |
| + |
| + def get_project_metadata(self, project): |
| + mapping = self.get_projects() |
| + return mapping.get(project) |
| + |