aboutsummaryrefslogtreecommitdiffstats
path: root/config_file
diff options
context:
space:
mode:
authorEugene Triguba <eugenetriguba@gmail.com>2020-06-21 11:12:26 -0500
committerEugene Triguba <eugenetriguba@gmail.com>2020-06-21 11:12:26 -0500
commit2c293cc0ac6678253c6af8101ba93866785f64c0 (patch)
tree04e74630e02fadac0bdcbe15d51975a1fe0197b6 /config_file
parentUse lowercase action name (diff)
downloadconfig-file-2c293cc0ac6678253c6af8101ba93866785f64c0.zip
WIP for refactoring tests
Diffstat (limited to 'config_file')
-rw-r--r--config_file/config_file.py33
-rw-r--r--config_file/parsers/__init__.py6
-rw-r--r--config_file/parsers/json_parser.py2
-rw-r--r--config_file/utils.py19
4 files changed, 28 insertions, 32 deletions
diff --git a/config_file/config_file.py b/config_file/config_file.py
index 6ded72b..e037a82 100644
--- a/config_file/config_file.py
+++ b/config_file/config_file.py
@@ -4,10 +4,8 @@ from shutil import copyfile
from typing import Any, Type, Union
from config_file.exceptions import ParsingError
-from config_file.parsers.base_parser import BaseParser
-from config_file.parsers.ini_parser import IniParser
-from config_file.parsers.json_parser import JsonParser
-from config_file.utils import read_file, split_on_dot
+from config_file.parsers import BaseParser, IniParser, JsonParser
+from config_file.utils import create_config_path, read_file, split_on_dot
class ConfigFile:
@@ -30,7 +28,7 @@ class ConfigFile:
if type(file_path) is not Path:
file_path = Path(file_path)
- self.__path = self.__create_config_path(file_path)
+ self.__path = create_config_path(file_path)
self.__contents = read_file(self.__path)
self.__parser = self.__determine_parser(file_path, parser)
@@ -38,29 +36,6 @@ class ConfigFile:
def path(self) -> Type[PurePath]:
return self.__path
- @property
- def contents(self) -> str:
- return self.__contents
-
- @property
- def parser(self) -> Type[BaseParser]:
- return self.__parser
-
- @staticmethod
- def __create_config_path(file_path: Type[PurePath], original: bool = False) -> str:
- if len(file_path.parts) >= 1 and file_path.parts[0] == "~":
- return file_path.expanduser()
-
- if original:
- file_parts = split_on_dot(file_path, only_last_dot=True)
- file_parts.insert(-1, "original")
- file_path = ".".join(file_parts)
-
- if Path(file_path).is_dir():
- raise ValueError(f"The specified config file ({file_path}) is a directory.")
-
- return file_path
-
def __determine_parser(
self, file_path: Type[PurePath], parser: Type[BaseParser] = None
) -> BaseParser:
@@ -149,7 +124,7 @@ class ConfigFile:
:raises SameFileError: If self.path and original_file_path are the same file.
"""
if original_file_path is None:
- original_file_path = self.__create_config_path(self.__path, original=True)
+ original_file_path = create_config_path(self.__path, original=True)
if not Path(original_file_path).exists():
raise FileNotFoundError(
diff --git a/config_file/parsers/__init__.py b/config_file/parsers/__init__.py
index e69de29..415f4f9 100644
--- a/config_file/parsers/__init__.py
+++ b/config_file/parsers/__init__.py
@@ -0,0 +1,6 @@
+from .base_parser import BaseParser
+from .ini_parser import IniParser
+from .json_parser import JsonParser
+from .parse_value import parse_value
+
+__all__ = ["BaseParser", "IniParser", "JsonParser", "parse_value"]
diff --git a/config_file/parsers/json_parser.py b/config_file/parsers/json_parser.py
index 10d5afc..584a2d0 100644
--- a/config_file/parsers/json_parser.py
+++ b/config_file/parsers/json_parser.py
@@ -124,7 +124,7 @@ class JsonParser(BaseParser):
return True
def stringify(self) -> str:
- return json.dumps(self.parsed_content)
+ 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/utils.py b/config_file/utils.py
index 9ed09b5..4a83a40 100644
--- a/config_file/utils.py
+++ b/config_file/utils.py
@@ -1,8 +1,8 @@
-from pathlib import PurePath
+from pathlib import Path, PurePath
from typing import Type, Union
-def split_on_dot(line: Union[str, Type[PurePath]], only_last_dot=False):
+def split_on_dot(line: Union[str, Type[PurePath]], only_last_dot=False) -> str:
"""
Split a string on the dot character (.).
@@ -23,3 +23,18 @@ def split_on_dot(line: Union[str, Type[PurePath]], only_last_dot=False):
def read_file(path: Union[str, Type[PurePath]]):
with open(path, "r") as file:
return file.read()
+
+
+def create_config_path(file_path: Type[PurePath], original: bool = False) -> Path:
+ if len(file_path.parts) >= 1 and file_path.parts[0] == "~":
+ return file_path.expanduser()
+
+ if original:
+ file_parts = split_on_dot(file_path, only_last_dot=True)
+ file_parts.insert(-1, "original")
+ file_path = Path(".".join(file_parts))
+
+ if file_path.is_dir():
+ raise ValueError(f"The specified config file ({file_path}) is a directory.")
+
+ return file_path