diff options
| author | Eugene Triguba <eugenetriguba@gmail.com> | 2020-08-01 08:57:17 -0500 |
|---|---|---|
| committer | Eugene Triguba <eugenetriguba@gmail.com> | 2020-08-01 08:57:17 -0500 |
| commit | ba655e72f74bc488cb8e5988766fef8f5dd45bcc (patch) | |
| tree | f967eb203b0a6e389022c5383a7eb23414059051 /config_file/parsers | |
| parent | Add name to security workflow (diff) | |
| parent | Fix wording of docstring in missing dependency error (diff) | |
| download | config-file-ba655e72f74bc488cb8e5988766fef8f5dd45bcc.zip | |
Merge branch 'dev' into dev
Diffstat (limited to 'config_file/parsers')
| -rw-r--r-- | config_file/parsers/base_parser.py | 39 | ||||
| -rw-r--r-- | config_file/parsers/ini_parser.py | 44 | ||||
| -rw-r--r-- | config_file/parsers/ini_parser_compat.py | 42 | ||||
| -rw-r--r-- | config_file/parsers/parse_value.py | 63 | ||||
| -rw-r--r-- | config_file/parsers/toml_parser.py | 2 | ||||
| -rw-r--r-- | config_file/parsers/yaml_parser.py | 19 |
6 files changed, 134 insertions, 75 deletions
diff --git a/config_file/parsers/base_parser.py b/config_file/parsers/base_parser.py index 09fbbbf..546450c 100644 --- a/config_file/parsers/base_parser.py +++ b/config_file/parsers/base_parser.py @@ -1,4 +1,5 @@ from typing import Any +from types import ModuleType from config_file.exceptions import MissingKeyError, ParsingError from config_file.nested_lookup import get_occurrence_of_key @@ -8,7 +9,9 @@ from .abstract_parser import AbstractParser class BaseParser(AbstractParser): - def __init__(self, file_contents: str, module, decode_error): + def __init__( + self, file_contents: str, module: ModuleType, decode_error: Exception + ): super().__init__(file_contents) self.__module = module self.__decode_error = decode_error @@ -39,9 +42,9 @@ class BaseParser(AbstractParser): for key in split_keys: key_causing_error = key result = result[key] - except TypeError: + except (KeyError, TypeError): raise ParsingError( - f"Cannot get {search_key} because " + f"Cannot `get` {search_key} because " f"{key_causing_error} is not subscriptable." ) @@ -59,9 +62,13 @@ class BaseParser(AbstractParser): content_reference[key] = value return - content_reference = content_reference[key] + try: + content_reference = content_reference[key] + except KeyError: + content_reference[key] = {} + content_reference = content_reference[key] - def delete(self, key) -> None: + def delete(self, key: str) -> None: try: if "." not in key: del self.parsed_content[key] @@ -69,18 +76,26 @@ class BaseParser(AbstractParser): indexes = split_on_dot(key) indexes_length = len(indexes) - ref = self.parsed_content + content_reference = self.parsed_content for index, key in enumerate(indexes): if index == indexes_length - 1: - del ref[key] + del content_reference[key] break - ref = ref[key] - except KeyError: - raise MissingKeyError(f"The specified key '{key}' was not found.") + content_reference = content_reference[key] + except (KeyError, TypeError): + raise MissingKeyError(f"The specified key '{key}' to delete was not found.") def stringify(self) -> str: return self.__module.dumps(self.parsed_content) - def has(self, search_key: str) -> bool: - return get_occurrence_of_key(self.parsed_content, key=search_key) > 0 + def has(self, search_key: str, wild: bool = False) -> bool: + if wild: + return get_occurrence_of_key(self.parsed_content, key=search_key) > 0 + + try: + self.get(search_key) + return True + except (KeyError, ParsingError): + return False + diff --git a/config_file/parsers/ini_parser.py b/config_file/parsers/ini_parser.py index 2a29ba7..8dc7f47 100644 --- a/config_file/parsers/ini_parser.py +++ b/config_file/parsers/ini_parser.py @@ -1,47 +1,7 @@ import configparser -from io import StringIO from .base_parser import BaseParser - - -class IniParserCompatibility: - """ - Provides compatibility with the json, yaml, - and toml API so we can use the BaseParser - easily, while still being able to leverage - the built-in configparser to parse the file. - - It is used as the `module` parameter - for BaseParser, which requires us to have a - `dumps` and `loads` method. - """ - - @staticmethod - def loads(file_contents: str) -> dict: - parser = configparser.ConfigParser() - parser.read_string(file_contents) - - return IniParserCompatibility.__create_configparser_dict(parser) - - @staticmethod - def dumps(file_contents: dict) -> str: - parser = configparser.ConfigParser() - buffer = StringIO() - - parser.read_dict(file_contents) - parser.write(buffer) - - return buffer.getvalue() - - @staticmethod - def __create_configparser_dict(parser: configparser.ConfigParser) -> dict: - result = {} - items = dict(parser.items()) - - for section in parser.sections(): - result.update({str(section): dict(items[section])}) - - return result +from .ini_parser_compat import IniParserCompatibility class IniParser(BaseParser): @@ -50,4 +10,4 @@ class IniParser(BaseParser): file_contents=file_contents, module=IniParserCompatibility, decode_error=configparser.Error, - ) + )
\ No newline at end of file diff --git a/config_file/parsers/ini_parser_compat.py b/config_file/parsers/ini_parser_compat.py new file mode 100644 index 0000000..9dfea2e --- /dev/null +++ b/config_file/parsers/ini_parser_compat.py @@ -0,0 +1,42 @@ +import configparser +from io import StringIO + + +class IniParserCompatibility: + """ + Provides compatibility with the json, yaml, + and toml API so we can use the BaseParser + easily, while still being able to leverage + the built-in configparser to parse the file. + + It is used as the `module` parameter + for BaseParser, which requires us to have a + `dumps` and `loads` method. + """ + + @staticmethod + def loads(file_contents: str) -> dict: + parser = configparser.ConfigParser() + parser.read_string(file_contents) + + return IniParserCompatibility.__create_configparser_dict(parser) + + @staticmethod + def dumps(file_contents: dict) -> str: + parser = configparser.ConfigParser() + buffer = StringIO() + + parser.read_dict(file_contents) + parser.write(buffer) + + return buffer.getvalue() + + @staticmethod + def __create_configparser_dict(parser: configparser.ConfigParser) -> dict: + result = {} + items = dict(parser.items()) + + for section in parser.sections(): + result.update({str(section): dict(items[section])}) + + return result diff --git a/config_file/parsers/parse_value.py b/config_file/parsers/parse_value.py index 26bdf2e..aa8d1fe 100644 --- a/config_file/parsers/parse_value.py +++ b/config_file/parsers/parse_value.py @@ -19,20 +19,12 @@ Example: can_be_parsed_as_int("-5") -> True parse_value("-5") -> -5 """ +import ast import re from distutils.util import strtobool def parse_value(value): - if can_be_parsed_as_int(value): - return int(value) - - if can_be_parsed_as_float(value): - return float(value) - - if can_be_parsed_as_bool(value): - return value if type(value) is bool else bool(strtobool(value)) - if type(value) is dict: parsed = {} @@ -49,6 +41,21 @@ def parse_value(value): return parsed + if can_be_parsed_as_int(value): + return int(value) + + if can_be_parsed_as_float(value): + return float(value) + + if can_be_parsed_as_bool(value): + return value if type(value) is bool else bool(strtobool(value)) + + if can_be_parsed_as_dict(value): + return parse_value(ast.literal_eval(value)) + + if can_be_parsed_as_list(value): + return parse_value(ast.literal_eval(value)) + return value @@ -75,7 +82,7 @@ def can_be_parsed_as_float(value) -> bool: return False if value.startswith("-"): - return bool(re.match(FLOAT_REGEX, value[1:])) + value = value[1:] return bool(re.match(FLOAT_REGEX, value)) @@ -88,3 +95,39 @@ def can_be_parsed_as_bool(value) -> bool: return False return value.lower() == "true" or value.lower() == "false" + + +def can_be_parsed_as_dict(value) -> bool: + if type(value) is dict: + return True + + if type(value) is not str: + return False + + value = value.strip() + if value[0] == "{" and value[-1] == "}": + try: + ast.literal_eval(value) + return True + except SyntaxError: + return False + + return False + + +def can_be_parsed_as_list(value) -> bool: + if type(value) is dict: + return True + + if type(value) is not str: + return False + + value = value.strip() + if value[0] == "[" and value[-1] == "]": + try: + ast.literal_eval(value) + return True + except SyntaxError: + return False + + return False diff --git a/config_file/parsers/toml_parser.py b/config_file/parsers/toml_parser.py index d369003..1c7f8a7 100644 --- a/config_file/parsers/toml_parser.py +++ b/config_file/parsers/toml_parser.py @@ -14,7 +14,7 @@ class TomlParser(BaseParser): def __init__(self, file_contents: str): if not TOML_AVAILABLE: raise MissingDependencyError( - "It doesn't appear `toml` is installed, but a toml " + "It doesn't appear `tomlkit` is installed, but a toml " "file was attempted to be used. Install the `toml` " "extra first with `pip install config-file[toml]`." ) diff --git a/config_file/parsers/yaml_parser.py b/config_file/parsers/yaml_parser.py index 77b59fe..babe873 100644 --- a/config_file/parsers/yaml_parser.py +++ b/config_file/parsers/yaml_parser.py @@ -1,12 +1,7 @@ try: - import yaml + from ruamel.yaml import YAML, YAMLError, round_trip_dump, round_trip_load YAML_AVAILABLE = True - - # Conform yaml to json and - # toml package syntax - yaml.loads = yaml.load - yaml.dumps = yaml.dump except ImportError: YAML_AVAILABLE = False @@ -19,11 +14,15 @@ class YamlParser(BaseParser): def __init__(self, file_contents: str): if not YAML_AVAILABLE: raise MissingDependencyError( - "It doesn't appear `PyYaml` is installed, but a yaml " - "file was attempted to be used. Install the `toml` " - "extra first with `pip install config-file[toml]`." + "It doesn't appear `ruamel.yaml` is installed, but a yaml " + "file was attempted to be used. Install the `yaml` " + "extra first with `pip install config-file[yaml]`." ) + yaml = YAML(typ="safe") + yaml.loads = round_trip_load + yaml.dumps = round_trip_dump + super().__init__( - file_contents=file_contents, module=yaml, decode_error=yaml.YAMLError + file_contents=file_contents, module=yaml, decode_error=YAMLError ) |