aboutsummaryrefslogtreecommitdiffstats
path: root/config_file
diff options
context:
space:
mode:
authorEugene Triguba <eugenetriguba@gmail.com>2020-07-19 15:02:13 -0500
committerEugene Triguba <eugenetriguba@gmail.com>2020-07-19 15:02:13 -0500
commit461cb78f0a56b9293b5ade10fe90ec4aa77d23ad (patch)
tree8dc05802dd09b38b7766da886190871daf3b6667 /config_file
parentRemove zsh info and add extras sub header (diff)
downloadconfig-file-461cb78f0a56b9293b5ade10fe90ec4aa77d23ad.zip
Change name of BaseParser to AbstractParser
Diffstat (limited to 'config_file')
-rw-r--r--config_file/__init__.py4
-rw-r--r--config_file/config_file.py12
-rw-r--r--config_file/parsers/__init__.py14
-rw-r--r--config_file/parsers/abstract_parser.py20
-rw-r--r--config_file/parsers/ini_parser.py4
-rw-r--r--config_file/parsers/json_parser.py10
-rw-r--r--config_file/parsers/toml_parser.py104
-rw-r--r--config_file/parsers/yaml_parser.py5
8 files changed, 38 insertions, 135 deletions
diff --git a/config_file/__init__.py b/config_file/__init__.py
index 1b2094f..3c43d33 100644
--- a/config_file/__init__.py
+++ b/config_file/__init__.py
@@ -1,6 +1,6 @@
from config_file.config_file import ConfigFile
from config_file.exceptions import ConfigFileError, ParsingError
-from config_file.parsers import BaseParser
+from config_file.parsers import AbstractParser
__version__ = "0.10.0"
@@ -8,5 +8,5 @@ __all__ = [
"ConfigFile",
"ParsingError",
"ConfigFileError",
- "BaseParser",
+ "AbstractParser",
]
diff --git a/config_file/config_file.py b/config_file/config_file.py
index de8b851..34df561 100644
--- a/config_file/config_file.py
+++ b/config_file/config_file.py
@@ -4,7 +4,7 @@ from shutil import copyfile
from typing import Any, Type, Union
from config_file.exceptions import ParsingError
-from config_file.parsers import BaseParser, IniParser, JsonParser
+from config_file.parsers import AbstractParser, IniParser, JsonParser
from config_file.utils import create_config_path, read_file, split_on_dot
@@ -12,7 +12,7 @@ class ConfigFile:
def __init__(
self,
file_path: Type[Union[str, Type[PurePath]]],
- parser: Type[BaseParser] = None,
+ parser: Type[AbstractParser] = None,
) -> None:
"""
Saves the config file path and expands it if needed, reads in
@@ -37,8 +37,8 @@ class ConfigFile:
return self.__path
def __determine_parser(
- self, file_path: Type[PurePath], parser: Type[BaseParser] = None
- ) -> Type[BaseParser]:
+ self, file_path: Type[PurePath], parser: Type[AbstractParser] = None
+ ) -> Type[AbstractParser]:
if parser is not None and not inspect.isabstract(parser):
return parser(self.__contents)
@@ -108,7 +108,9 @@ class ConfigFile:
"""
return self.__parser.has(section_key)
- def restore_original(self, original_file_path: Union[str, Type[PurePath]] = None) -> bool:
+ def restore_original(
+ self, original_file_path: Union[str, Type[PurePath]] = None
+ ) -> bool:
"""
Restores the original the config file by deleting it and copying the original
back in its place. The internal contents of this config file object are then set
diff --git a/config_file/parsers/__init__.py b/config_file/parsers/__init__.py
index 8da520e..554b624 100644
--- a/config_file/parsers/__init__.py
+++ b/config_file/parsers/__init__.py
@@ -1,17 +1,17 @@
-from .base_parser import BaseParser
+from .abstract_parser import AbstractParser
from .ini_parser import IniParser
from .json_parser import JsonParser
-from .toml_parser import TomlParser
-from .yaml_parser import YamlParser
from .parse_value import (
- parse_value,
can_be_parsed_as_bool,
can_be_parsed_as_float,
- can_be_parsed_as_int
+ can_be_parsed_as_int,
+ parse_value,
)
+from .toml_parser import TomlParser
+from .yaml_parser import YamlParser
__all__ = [
- "BaseParser",
+ "AbstractParser",
"IniParser",
"JsonParser",
"TomlParser",
@@ -19,5 +19,5 @@ __all__ = [
"parse_value",
"can_be_parsed_as_bool",
"can_be_parsed_as_float",
- "can_be_parsed_as_int"
+ "can_be_parsed_as_int",
]
diff --git a/config_file/parsers/abstract_parser.py b/config_file/parsers/abstract_parser.py
index da6e90a..8f1bee1 100644
--- a/config_file/parsers/abstract_parser.py
+++ b/config_file/parsers/abstract_parser.py
@@ -2,7 +2,7 @@ from abc import ABC, abstractmethod
from typing import Any
-class BaseParser(ABC):
+class AbstractParser(ABC):
"""
The abstract base parser for all other parsers to implement.
@@ -26,6 +26,14 @@ class BaseParser(ABC):
self.parsed_content = self.parse(self.content)
@abstractmethod
+ def parse(self, file_contents: str) -> Any:
+ """
+ Parse the file_contents into an internal representation
+ the given parser can work with.
+ """
+ raise NotImplementedError
+
+ @abstractmethod
def get(self, key: str, parse_types: bool = False, all: bool = False) -> Any:
"""
Retrieve the value of a key in its native type.
@@ -62,14 +70,6 @@ class BaseParser(ABC):
using a section.key format.
Some formats, like JSON, do not have sections and therefore,
- it would only be checking if
- """
- raise NotImplementedError
-
- @abstractmethod
- def parse(self, file_contents: str) -> Any:
- """
- Parse the file_contents into an internal representation
- the given parser can work with.
+ it would only be checking if a certain key exists.
"""
raise NotImplementedError
diff --git a/config_file/parsers/ini_parser.py b/config_file/parsers/ini_parser.py
index 2db1ae8..164ff6d 100644
--- a/config_file/parsers/ini_parser.py
+++ b/config_file/parsers/ini_parser.py
@@ -2,12 +2,12 @@ import configparser
from io import StringIO
from config_file.exceptions import ParsingError
-from config_file.parsers.base_parser import BaseParser
+from config_file.parsers.abstract_parser import AbstractParser
from config_file.parsers.parse_value import parse_value
from config_file.utils import split_on_dot
-class IniParser(BaseParser):
+class IniParser(AbstractParser):
def __init__(self, file_contents: str):
"""Reads in the file contents into the configparser."""
super().__init__(file_contents)
diff --git a/config_file/parsers/json_parser.py b/config_file/parsers/json_parser.py
index 13b84b5..237c0b5 100644
--- a/config_file/parsers/json_parser.py
+++ b/config_file/parsers/json_parser.py
@@ -1,18 +1,17 @@
import json
+from config_file.exceptions import ParsingError
from config_file.nested_lookup import (
get_occurrence_of_key,
nested_delete,
nested_lookup,
nested_update,
)
-
-from config_file.exceptions import ParsingError
-from config_file.parsers import BaseParser, parse_value
+from config_file.parsers import AbstractParser, parse_value
from config_file.utils import split_on_dot
-class JsonParser(BaseParser):
+class JsonParser(AbstractParser):
"""
The JsonParser allows use of json files with ConfigFile. The syntax it
supports is a bit more flexible than the IniParser.
@@ -76,7 +75,8 @@ class JsonParser(BaseParser):
result = result[key]
except TypeError:
raise ParsingError(
- f"Cannot get {key} because {key_causing_error} is not subscriptable."
+ f"Cannot get {key} because {key_causing_error} "
+ "is not subscriptable."
)
else:
result = self.parsed_content[key]
diff --git a/config_file/parsers/toml_parser.py b/config_file/parsers/toml_parser.py
index ad3e7f4..4905aec 100644
--- a/config_file/parsers/toml_parser.py
+++ b/config_file/parsers/toml_parser.py
@@ -1,105 +1,5 @@
-import toml
+from config_file.parsers import AbstractParser
-from config_file.nested_lookup import (
- get_occurrence_of_key,
- nested_delete,
- nested_lookup,
- nested_update,
-)
-from config_file.exceptions import ParsingError
-from config_file.parsers import BaseParser, parse_value
-from config_file.utils import split_on_dot
-
-class TomlParser(BaseParser):
+class TomlParser(AbstractParser):
"""The TomlParser allows use of toml files with ConfigFile."""
-
- def __init__(self, file_contents: str):
- """Reads in the file contents into the parser."""
- super().__init__(file_contents)
-
- def parse(self, file_contents: str):
- try:
- return toml.loads(file_contents)
- except toml.TomlDecodeError as error:
- raise ParsingError(error)
-
- def get(self, key: str, parse_types: bool = False, all: bool = False):
- """
- Retrieve values using a dot syntax.
-
- :param key: The key to retrieve. e.g. 'foo.bar.boo'
-
- :param parse_types: Whether you'd like the type parsed into its native one.
-
- :param all: Specify whether you'd like to recursively receive
- all values that match the given key. Returned as a list.
- e.g. 'foo' would retrieve all values that have the key 'foo', anywhere
- in the json.
-
- :return: the value of the given key
- """
- if all:
- result = nested_lookup(key, self.parsed_content)
- elif "." in key:
- split_keys = split_on_dot(key)
- result = self.parsed_content
-
- # Used so we can more precisely specify what key is causing the error.
- key_for_error = None
- try:
- for key in split_keys:
- key_for_error = key
- result = result[key]
- except TypeError:
- raise ParsingError(
- f"Cannot get {key} because {key_for_error} is not subscriptable."
- )
- else:
- result = self.parsed_content[key]
-
- return parse_value(result) if parse_types else result
-
- def set(self, key: str, value, all: bool = False):
- if all:
- nested_update(self.parsed_content, key, value, in_place=True)
- elif "." in key:
- indexes = split_on_dot(key)
- indexes_length = len(indexes)
-
- ref = self.parsed_content
- for index, key in enumerate(indexes):
- if index == indexes_length - 1:
- ref[key] = value
- break
-
- ref = ref[key]
- else:
- self.parsed_content[key] = value
-
- return True
-
- def delete(self, key, all: bool = False):
- if all:
- nested_delete(self.parsed_content, key, in_place=True)
- elif "." in key:
- indexes = split_on_dot(key)
- indexes_length = len(indexes)
-
- ref = self.parsed_content
- for index, key in enumerate(indexes):
- if index == indexes_length - 1:
- del ref[key]
- break
-
- ref = ref[key]
- else:
- del self.parsed_content[key]
-
- return True
-
- def stringify(self) -> str:
- return json.dumps(self.parsed_content, indent=4)
-
- def has(self, search_key: str) -> bool:
- return get_occurrence_of_key(self.parsed_content, key=search_key) > 0
diff --git a/config_file/parsers/yaml_parser.py b/config_file/parsers/yaml_parser.py
index 48722b5..a45d7f6 100644
--- a/config_file/parsers/yaml_parser.py
+++ b/config_file/parsers/yaml_parser.py
@@ -1,4 +1,5 @@
-from config_file.parsers import BaseParser
+from config_file.parsers import AbstractParser
-class YamlParser(BaseParser):
+
+class YamlParser(AbstractParser):
pass