convert_ini.py 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #!/usr/bin/env -S python3 -B
  2. #
  3. # Copyright (c) 2021 Project CHIP Authors
  4. # All rights reserved.
  5. #
  6. # Licensed under the Apache License, Version 2.0 (the "License");
  7. # you may not use this file except in compliance with the License.
  8. # You may obtain a copy of the License at
  9. #
  10. # http://www.apache.org/licenses/LICENSE-2.0
  11. #
  12. # Unless required by applicable law or agreed to in writing, software
  13. # distributed under the License is distributed on an "AS IS" BASIS,
  14. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. # See the License for the specific language governing permissions and
  16. # limitations under the License.
  17. #
  18. import json
  19. from configparser import ConfigParser
  20. import click
  21. import typing
  22. import re
  23. def convert_ini_to_json(ini_dir: str, json_path: str):
  24. """ Converts chip-tool INI files found in 'ini_dir' into the JSON format expected by the matter python REPL
  25. and associated test infrastructure.
  26. Specifically, this will take the three INI files that correspond to the three example CAs (alpha, beta, gamma)
  27. and munge them together into a single REPL-compatible JSON file.
  28. """
  29. python_json_store = {}
  30. python_json_store['repl-config'] = {
  31. 'fabricAdmins': {
  32. '1': {
  33. 'fabricId': 1,
  34. 'vendorId': 65521
  35. },
  36. '2': {
  37. 'fabricId': 2,
  38. 'vendorId': 65521
  39. },
  40. '3': {
  41. 'fabricId': 3,
  42. 'vendorId': 65521
  43. }
  44. }
  45. }
  46. python_json_store['sdk-config'] = {}
  47. load_ini_into_dict(ini_file=ini_dir + '/chip_tool_config.alpha.ini',
  48. json_dict=python_json_store['sdk-config'], replace_suffix='1')
  49. load_ini_into_dict(ini_file=ini_dir + '/chip_tool_config.beta.ini',
  50. json_dict=python_json_store['sdk-config'], replace_suffix='2')
  51. load_ini_into_dict(ini_file=ini_dir + '/chip_tool_config.gamma.ini',
  52. json_dict=python_json_store['sdk-config'], replace_suffix='3')
  53. json_file = open(json_path, 'w')
  54. json.dump(python_json_store, json_file, ensure_ascii=True, indent=4)
  55. def load_ini_into_dict(ini_file: str, json_dict: typing.Dict, replace_suffix: str):
  56. """ Loads the specific INI file into the provided dictionary. A 'replace_suffix' string
  57. has to be provided to convert the existing numerical suffix to a different value.
  58. """
  59. config = ConfigParser()
  60. # Enable case-sensitive keys.
  61. config.optionxform = str
  62. config.read(ini_file)
  63. for key in config['Default']:
  64. value = config['Default'][key]
  65. key = re.sub(r'(.*?)[0-9]+', r'\1', key) + replace_suffix
  66. json_dict[key] = value
  67. @click.command()
  68. @click.option('--ini-dir', type=click.Path(exists=True), show_default=True, default='/tmp', help='Path to directory containing INI files that chip-tool uses for its tests')
  69. @click.option('--json-path', type=click.Path(exists=False), show_default=True, default='/tmp/repl-storage.json', help='Path to JSON file used by Python infrastructure')
  70. def main(ini_dir: str, json_path: str):
  71. convert_ini_to_json(ini_dir, json_path)
  72. return 0
  73. if __name__ == '__main__':
  74. main()