summaryrefslogtreecommitdiff
path: root/src/config.py
diff options
context:
space:
mode:
authordavidovski <david@davidovski.xyz>2021-11-08 21:33:28 +0000
committerdavidovski <david@davidovski.xyz>2021-11-08 21:33:28 +0000
commita46c1d6cc58b847b9b140de2c59e1c7a8ba06655 (patch)
tree5a92f74188b250f38c6e2fbbd06281fdda3cc227 /src/config.py
parent1a5101a5c4beeda67a665964d171396eca6f9a5b (diff)
added config parsing
Diffstat (limited to 'src/config.py')
-rw-r--r--src/config.py90
1 files changed, 90 insertions, 0 deletions
diff --git a/src/config.py b/src/config.py
new file mode 100644
index 0000000..ed86dc9
--- /dev/null
+++ b/src/config.py
@@ -0,0 +1,90 @@
+"""xipkg config file parser
+
+ Simple Key value, supporting map-style objects and arrays
+
+ ```
+ key value
+ key2 another value
+
+ # this is a commment
+
+ map {
+ mapkey1 value
+ mapkey2 value
+ }
+
+ array [
+ item1
+ item2
+ item3
+ item4
+ ]
+ ```
+"""
+import sys
+# TODO: add more validation to this
+
+"""Parse a config file from a path into a python dict
+ Args:
+ file_path: (str) the path to the file
+ Returns:
+ (dict) the configuration
+
+
+"""
+def parse_file(file_path):
+ with open(file_path, "r") as config_file:
+ return _parse_config(config_file)
+
+
+"""Parse a config file's lines, is also used for dictionaries within the config
+ Args:
+ config_file: (file) a file with the readline function
+ Returns:
+ (dict) the configuration that has been parsed
+
+"""
+def _parse_config(config_file):
+ config = {}
+ line = config_file.readline()
+ while line:
+ line = line.strip()
+ if len(line) > 0 and (line[-1] == "}" or line[-1] == "]"):
+ return config
+ else:
+ values = _parse_line(line.strip(), config_file)
+ for k,v in values.items():
+ config[k] = v
+ line = config_file.readline()
+ return config
+
+"""Parse a single config ling
+ Args:
+ line: (str) the line to be parsed
+ config_file: (file) the file that the line has been taken from
+ Returns:
+ (dict) the configuration that has been parsed from the single line
+
+"""
+def _parse_line(line, config_file):
+ if len(line) == 0:
+ return {}
+ if line[0] == "#":
+ return {}
+ else:
+ split = line.split(" ")
+ key = split[0]
+ value = " " if len(split) == 1 else " ".join(line.split(" ")[1:])
+
+ # if starting with include, then include another file in the same config
+ if key == "include":
+ included = parse_conf(value)
+ return included
+ elif value[-1].endswith("{"):
+ return {key: _parse_config(config_file)}
+ elif value[-1].endswith("["):
+ return {key: [k for k in _parse_config(config_file).keys()]}
+ else:
+ return {key: value}
+
+