blob: e8b6585411e96f4dd8f4915dacdb923882f949d3 [file] [log] [blame]
Andrew Lambeceaec02020-02-11 14:16:41 -07001# Copyright 2020 The Chromium OS 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"""IO-related helper functions."""
5
Andrew Lamb5d132052020-05-29 14:49:06 -06006import json
7import pathlib
Andrew Lambeceaec02020-02-11 14:16:41 -07008
Sean McAllister576d80c2020-05-29 13:00:19 -06009from typing import Any, Dict
10
David Burger54f704e2020-04-30 13:33:47 -060011from google.protobuf import json_format
Sean McAllister576d80c2020-05-29 13:00:19 -060012from google.protobuf.message import Message
13
Andrew Lamb0ed1b902020-06-08 17:03:47 -060014from chromiumos.config.payload import config_bundle_pb2
Sean McAllisterd2d91882020-10-12 10:49:19 -060015from chromiumos.config.payload import flat_config_pb2
Andrew Lamb0ed1b902020-06-08 17:03:47 -060016
Sean McAllister576d80c2020-05-29 13:00:19 -060017
18def write_message_json(message: Message, path: pathlib.Path, \
19 default_fields=False):
20 """Take a Message and write it to a file as json.
21
22 Args:
23 message: protobuf message to write to file
24 path: output file write json to
25 default_fields: If true, include default values for fields
26 """
27 # ow this is a long parameter
28 opts = {'including_default_value_fields': default_fields, 'sort_keys': True}
29
30 with open(path, 'w') as outfile:
31 outfile.write(json_format.MessageToJson(message, **opts))
David Burger54f704e2020-04-30 13:33:47 -060032
Andrew Lambeceaec02020-02-11 14:16:41 -070033
Andrew Lamb9328c402020-03-05 12:57:59 -070034def read_config(path: str) -> config_bundle_pb2.ConfigBundle:
Sean McAllisterd2d91882020-10-12 10:49:19 -060035 """Reads a ConfigBundle message from a jsonpb file.
Andrew Lamb9328c402020-03-05 12:57:59 -070036
Sean McAllister576d80c2020-05-29 13:00:19 -060037 Args:
Sean McAllisterd2d91882020-10-12 10:49:19 -060038 path: Path to the json proto.
Sean McAllister576d80c2020-05-29 13:00:19 -060039
40 Returns:
41 ConfigBundle parsed from file.
42 """
David Burger54f704e2020-04-30 13:33:47 -060043 project_config = config_bundle_pb2.ConfigBundle()
44 with open(path, 'r') as f:
45 json_format.Parse(f.read(), project_config)
46 return project_config
Andrew Lamb5d132052020-05-29 14:49:06 -060047
48
Sean McAllisterd2d91882020-10-12 10:49:19 -060049def read_flat_config(path: str) -> flat_config_pb2.FlatConfigList:
50 """Reads a FlatConfigList message from a jsonpb file.
51
52 Args:
53 path: Path to the json proto.
54
55 Returns:
56 FlatConfigList parsed from file.
57 """
58 flat_config = flat_config_pb2.FlatConfigList()
59 with open(path, 'r') as f:
60 json_format.Parse(f.read(), flat_config)
61 return flat_config
62
63
Sean McAllister576d80c2020-05-29 13:00:19 -060064def read_model_sku_json(factory_dir: pathlib.Path) -> Dict[str, Any]:
Andrew Lamb5d132052020-05-29 14:49:06 -060065 """Reads and parses the model_sku.json file.
66
67 Args:
Sean McAllister576d80c2020-05-29 13:00:19 -060068 factory_dir: Path to a project's factory dir.
69
70 Returns:
71 Parsed model_sku.json as a dict
Andrew Lamb5d132052020-05-29 14:49:06 -060072 """
73 with open(factory_dir.joinpath('generated', 'model_sku.json')) as f:
74 return json.load(f)