-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of github.com:noahbroyles/secsie-conf
- Loading branch information
Showing
6 changed files
with
206 additions
and
182 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,4 +38,4 @@ jobs: | |
- name: Test with pytest | ||
run: | | ||
pytest | ||
python -m pytest |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
""" | ||
exceptions.py | ||
Contains all the custom exceptions thrown by secsie. | ||
""" | ||
|
||
|
||
class InvalidSyntax(SyntaxError): | ||
""" | ||
This happens when the config you're trying to parse is invalid. | ||
""" | ||
|
||
def __init__(self, error_message: str, line_number: int): | ||
super().__init__(f"Invalid syntax on line {line_number}: {error_message}") | ||
self.lineno = line_number |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
from pathlib import Path | ||
|
||
|
||
def generate_config(conf_obj: dict) -> str: | ||
""" | ||
Generate and return a valid config from an object. | ||
Will save the config if an output file is passed. | ||
This WILL NOT currently write valid .ini files, so don't even try. The only output format is secsie. | ||
:param conf_obj: The dictionary to parse into a configuration language string | ||
:return: a string of configuration code | ||
""" | ||
|
||
conf = '' | ||
for key, value in conf_obj.items(): | ||
if isinstance(value, dict): | ||
conf += f"\n[{key.replace(' ', '')}]\n" | ||
for k, v in value.items(): | ||
if isinstance(v, list): | ||
conf += f'\t{k} = {", ".join(v)}\n' | ||
else: | ||
conf += f"{';' if v == '' else ''}\t{k} = {v}\n" | ||
conf += "\n" | ||
continue | ||
elif isinstance(value, list): | ||
conf += f'{key} = {", ".join(value)}\n' | ||
conf += f"{key} = {value}\n" | ||
|
||
return conf | ||
|
||
|
||
def generate_config_file(conf_obj: dict, output_file: str): | ||
""" | ||
Generate and write a config file from a dictionary of keys and values | ||
:param conf_obj: The dictionary to render into the secsie config language | ||
:param output_file: The file to write to | ||
""" | ||
output_file = Path(output_file) | ||
conf = generate_config(conf_obj) | ||
|
||
with open(output_file, 'w') as f: | ||
f.write(f"# {output_file.name} auto-generated by secsie\n{conf}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
""" | ||
modes.py | ||
Contains a dictionary of regular expressions used to match special types, such as numbers, booleans, and nulls. | ||
""" | ||
import re | ||
|
||
|
||
MODES = { | ||
"secsie": dict( | ||
SECTION_EX=re.compile(r"^\[([a-zA-Z\d_-]+)]$"), | ||
FLOAT_EX=re.compile(r"^(-?\d+[.]\d*)$"), | ||
FALSE_EX=re.compile(r"^(false|no)$", re.IGNORECASE), | ||
NULL_EX=re.compile(r"^null$", re.IGNORECASE), | ||
TRUE_EX=re.compile(r"^(true|yes)$", re.IGNORECASE), | ||
INT_EX=re.compile(r"^-?\d+$"), | ||
), | ||
"ini": dict( | ||
SECTION_EX=re.compile(r"^\[([a-zA-Z\d _-]+)]$"), | ||
FLOAT_EX=re.compile(r"^(-?\d+[.]\d*)$"), | ||
FALSE_EX=re.compile(r"^(false|no)$", re.IGNORECASE), | ||
NULL_EX=re.compile(r"^null$", re.IGNORECASE), | ||
TRUE_EX=re.compile(r"^(true|yes)$", re.IGNORECASE), | ||
INT_EX=re.compile(r"^-?\d+$"), | ||
), | ||
} |
Oops, something went wrong.