Source code for parse_config
##
# @file parse_config.py
# @author Douglas Quigg (dstroy0 dquigg123@gmail.com)
# @brief parses InputHandler's config.h
# @version 1.0
# @date 2023-05-22
# @copyright Copyright (c) 2023
# Copyright (C) 2023 Douglas Quigg (dstroy0) <dquigg123@gmail.com>
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# version 3 as published by the Free Software Foundation.
# imports
import re
import copy
import json
# parses inputhandler config
[docs]
class ParseInputHandlerConfig(object):
"""InputHandler config.h parser
Args:
object (object): base object specialization
"""
## the constructor
def __init__(self) -> None:
"""the constructor"""
super(ParseInputHandlerConfig, self).__init__()
ParseInputHandlerConfig.logger = self.get_child_logger(__name__)
## parses input config files for use
[docs]
def parse_config_header_file(self, path: str):
"""parses the config at path if path is not None,
if path is None, parse the config in the library
Args:
path (str): a valid os path
"""
ParseInputHandlerConfig.logger.debug("Attempt parse config.h")
config_path = ""
if path == "":
config_path = self.default_config_path
else:
config_path = path
file_line_list = []
config_file = open(config_path, "r")
file_line_list = config_file.readlines()
config_file.close()
remove_number_of_lines = 18
if "autogenerated" in file_line_list[0]:
remove_number_of_lines = 19
del file_line_list[0:remove_number_of_lines]
for line in range(len(file_line_list)):
file_line_list[line] = file_line_list[line].rstrip()
self.input_config_file_lines = self.generate_docstring_list_for_filename(
"config.h", "InputHandler autogenerated config.h"
)
self.input_config_file_lines = self.input_config_file_lines + file_line_list
debug_regexp = "(\s*#define\s*)(DEBUG_\S* )(\S*.*)"
opt_method_regexp = "(\s*#define\s*)(DISABLE_\S* )(\S*.*)"
setting_regexp = "(\s*#define\s*)(?!\S*PGM_LEN)(IH_\S*\s*)(\d*.*)"
progmem_regexp = "(\s*#define\s*)(IH_\S*PGM_LEN\s*)(\d*.*)"
regexp_dict = {
"library settings": setting_regexp,
"progmem settings": progmem_regexp,
"debug methods": debug_regexp,
"optional methods": opt_method_regexp,
}
index = {
"library settings": "0",
"progmem settings": "0",
"optional methods": "0",
"debug methods": "0",
}
fields = {"value": "", "lineno": "", "index": ""}
line_num = 0
for line in self.input_config_file_lines:
for key in regexp_dict:
regexp = regexp_dict[key]
match = re.match(regexp, line)
if bool(match):
value = str(match[3])
if value == "true":
value = True
elif value == "false":
value = False
entry_key = str(index[key])
text = str(match[2].strip())
entry = {text: copy.deepcopy(fields)}
entry[text]["value"] = value
entry[text]["lineno"] = str(line_num)
entry[text]["index"] = str(entry_key)
self.cli_options["config"]["var"][key].update(entry)
self.default_settings_tree_values.update(entry)
tmp = int(index[key])
tmp += 1
index[key] = str(tmp)
line_num += 1
ParseInputHandlerConfig.logger.debug(
str(json.dumps(self.cli_options["config"], indent=2))
)
# end of file