-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
248 additions
and
105 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
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,8 @@ | ||
""" | ||
.. include:: ../README.md | ||
""" | ||
from importlib.metadata import version | ||
|
||
__author__ = "Ling" | ||
__email__ = "[email protected]" | ||
__version__ = version("configen") |
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,88 @@ | ||
#!/usr/bin/env python | ||
"""Entry point for program""" | ||
import argparse | ||
import logging | ||
import json | ||
import sys | ||
|
||
from configen.parsers import JsonParser, YamlParser | ||
from configen.utils import merge | ||
|
||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
def entry(args): | ||
"""Command line interface entry point. | ||
Example: | ||
configen config.json | ||
""" | ||
parser = argparse.ArgumentParser() | ||
parser.add_argument( | ||
"path", help="path to the config file/folder", type=str) | ||
parser.add_argument( | ||
"-o", "--output", help="path to save the loaded config", type=str) | ||
parser.add_argument( | ||
"-f", "--format", help="config output format", | ||
choices=["json", "yaml"], default="json", type=str) | ||
parser.add_argument( | ||
"-i", "--ignored", | ||
nargs="*", | ||
help="list of files to be ignored, support regex", type=str) | ||
parser.add_argument( | ||
"-v", "--verbose", | ||
help="debug level", type=str, default="INFO") | ||
parser.add_argument( | ||
"-a", "--append", | ||
help="append arbitrary dictionary in json format", type=str | ||
) | ||
parser.add_argument( | ||
"-r", "--read", | ||
nargs="*", | ||
help="which filetype to read", type=str, default=["*"] | ||
) | ||
args = parser.parse_args(args) | ||
|
||
# variables needed | ||
config_path = args.path | ||
output_path = args.output | ||
output_format = args.format | ||
ignored = tuple(args.ignored) if args.ignored else () | ||
append_dict = json.loads(args.append) if args.append else {} | ||
read_format = args.read | ||
|
||
logging.basicConfig( | ||
datefmt='%m/%d/%Y %I:%M:%S %p', | ||
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', | ||
level=args.verbose | ||
) | ||
mega_config = {} | ||
|
||
# initate parsers | ||
config_parser_dict = { | ||
"json": JsonParser(), | ||
"yaml": YamlParser() | ||
} | ||
if read_format == "*": | ||
read_format = list(config_parser_dict.keys()) | ||
|
||
# load config | ||
for config_parser_name in read_format: | ||
config_parser = config_parser_dict[config_parser_name] | ||
config = config_parser.load(config=config_path, ignored=ignored) | ||
merge(mega_config, config.config) | ||
|
||
# override the append dict | ||
if append_dict: | ||
logger.info(f"Override dictionary value with {append_dict}") | ||
mega_config.update(append_dict) | ||
|
||
# save config | ||
if output_path is not None: | ||
config_parser_dict[output_format].write(output_path, mega_config) | ||
|
||
|
||
if __name__ == "__main__": | ||
logger.info("Start Configen command line interface") | ||
entry(sys.argv[1:]) |
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,6 @@ | ||
from configen.parsers.json_parser import JsonParser | ||
from configen.parsers.yaml_parser import YamlParser | ||
|
||
parser_list = [JsonParser, YamlParser] | ||
|
||
__all__ = ["JsonParser", "YamlParser"] |
File renamed without changes.
10 changes: 5 additions & 5 deletions
10
configen/yaml_parser.py → src/configen/parsers/yaml_parser.py
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 |
---|---|---|
@@ -1,27 +1,27 @@ | ||
from ruamel.yaml import YAML | ||
from configen.base_parser import Parser | ||
|
||
_yaml = YAML() | ||
_yaml.indent(mapping=2, sequence=4, offset=2) | ||
|
||
|
||
class YamlParser(Parser): | ||
"""Yaml parser.""" | ||
extension = "yml" | ||
|
||
_yaml = YAML() | ||
_yaml.indent(mapping=2, sequence=4, offset=2) | ||
|
||
def _write_method(self, filename: str) -> Parser: | ||
# check if the given path ends with a yaml file extension | ||
filename = self._append_extension(filename) | ||
|
||
with open(filename, "w") as file: | ||
_yaml.dump(self.config, file) | ||
self._yaml.dump(self.config, file) | ||
|
||
return self | ||
|
||
def _load_method(self, filename: str) -> dict: | ||
filename = self._append_extension(filename) | ||
|
||
with open(filename, "r") as file: | ||
config = _yaml.load(file.read()) | ||
config = self._yaml.load(file.read()) | ||
|
||
return config |
Oops, something went wrong.