From 032196104a59761bd75b5d594440d1e8ef16965a Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Sat, 1 Aug 2020 17:58:32 -0500 Subject: Add ConfigFilePath to refactor out file specific code --- config_file/config_file_path.py | 117 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 117 insertions(+) create mode 100644 config_file/config_file_path.py (limited to 'config_file/config_file_path.py') diff --git a/config_file/config_file_path.py b/config_file/config_file_path.py new file mode 100644 index 0000000..a40cc40 --- /dev/null +++ b/config_file/config_file_path.py @@ -0,0 +1,117 @@ +import os +from pathlib import Path as _Path_ +from pathlib import _posix_flavour, _windows_flavour +from typing import Type + +from .exceptions import UnrecognizedFileError +from .parsers.abstract_parser import AbstractParser +from .parsers.ini_parser import IniParser +from .parsers.json_parser import JsonParser +from .parsers.toml_parser import TomlParser +from .parsers.yaml_parser import YamlParser +from .utils import split_on_dot + + +class ConfigFilePath(_Path_): + """A ConfigFilePath object, which subclasses Path. + + It provides us easy ways to grab the original config file + path, file extension, validate the path, or determine the parser + that should be use for the given file type. + + See https://codereview.stackexchange.com/questions/162426/subclassing-pathlib-path + for most details on subclassing the pathlib.Path object. + """ + + _flavour = _windows_flavour if os.name == "nt" else _posix_flavour + + @property + def original_path(self) -> "ConfigFilePath": + """Computes the original configuration file path. + + e.g. ConfigFilePath("./config.ini").original_path -> "./config.original.ini" + + Returns: + The path to the original configuration file, which + may or may not exist. + """ + file_parts = split_on_dot(self, only_last_dot=True) + file_parts.insert(-1, "original") + return ConfigFilePath(".".join(file_parts)) + + @property + def extension(self) -> str: + """Retrieves the extension of the file + path we passed in, without the '.'. + + e.g. ConfigFilePath("./pyproject.toml").extension -> "toml" + + Returns: + The extension of the current file path. + """ + _, extension = os.path.splitext(str(self)) + return extension[1:] + + @property + def parser(self) -> Type[AbstractParser]: + """Determine what parser should be used for this file. + + Raises: + UnrecognizedFileError: If the extension of the file + is not recognized. + + Returns: + The instantiated parser that should be used for this file. + """ + if self.extension == "": + raise UnrecognizedFileError( + "Tried to determine a parser to use, but the file at " + f"{self} does not have an extension." + ) + + if self.extension == "ini": + return IniParser(self.contents) + elif self.extension == "json": + return JsonParser(self.contents) + elif self.extension == "yaml" or self.extension == "yml": + return YamlParser(self.contents) + elif self.extension == "toml": + return TomlParser(self.contents) + else: + raise UnrecognizedFileError( + f"File path at `{self}` contains an unrecognized file type." + ) + + @property + def contents(self) -> str: + """Retrieve the contents of the file. + + Returns: + The contents of the file at this path. + """ + with open(self, "r") as file: + return file.read() + + def validate(self) -> "ConfigFilePath": + """Validates that the current path exists, is a directory, + and expands any home tidles. + + Raises: + FileNotFoundError: If the path does not exist. + ValueError: If the path leads to a directory. + + Returns: + This ConfigFilePath object + """ + if len(self.parts) >= 1 and self.parts[0] == "~": + self = ConfigFilePath(self.expanduser()) + + if not self.exists(): + raise FileNotFoundError( + f"The specified config file ({self}) does not exist." + ) + + if self.is_dir(): + raise ValueError(f"The specified config file ({self}) is a directory") + + return self -- cgit v1.2.3 From 59eeb1321fbad41dce7c971778e37ac1943fda3b Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Wed, 5 Aug 2020 20:39:28 -0500 Subject: Redo package to have a flat package structure By having a flat package structure like this, it'll avoid exposing packages and things we don't want to be exposed. --- config_file/config_file_path.py | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) (limited to 'config_file/config_file_path.py') diff --git a/config_file/config_file_path.py b/config_file/config_file_path.py index a40cc40..9f2b87d 100644 --- a/config_file/config_file_path.py +++ b/config_file/config_file_path.py @@ -3,12 +3,13 @@ from pathlib import Path as _Path_ from pathlib import _posix_flavour, _windows_flavour from typing import Type -from .exceptions import UnrecognizedFileError -from .parsers.abstract_parser import AbstractParser -from .parsers.ini_parser import IniParser -from .parsers.json_parser import JsonParser -from .parsers.toml_parser import TomlParser -from .parsers.yaml_parser import YamlParser +from config_file.abstract_parser import AbstractParser +from config_file.ini_parser import IniParser +from config_file.json_parser import JsonParser +from config_file.toml_parser import TomlParser +from config_file.yaml_parser import YamlParser + +from .exceptions import UnsupportedFileTypeError from .utils import split_on_dot @@ -20,7 +21,7 @@ class ConfigFilePath(_Path_): that should be use for the given file type. See https://codereview.stackexchange.com/questions/162426/subclassing-pathlib-path - for most details on subclassing the pathlib.Path object. + for more details on subclassing the pathlib.Path object. """ _flavour = _windows_flavour if os.name == "nt" else _posix_flavour @@ -57,14 +58,14 @@ class ConfigFilePath(_Path_): """Determine what parser should be used for this file. Raises: - UnrecognizedFileError: If the extension of the file + UnsupportedFileTypeError: If the extension of the file is not recognized. Returns: The instantiated parser that should be used for this file. """ if self.extension == "": - raise UnrecognizedFileError( + raise UnsupportedFileTypeError( "Tried to determine a parser to use, but the file at " f"{self} does not have an extension." ) @@ -78,7 +79,7 @@ class ConfigFilePath(_Path_): elif self.extension == "toml": return TomlParser(self.contents) else: - raise UnrecognizedFileError( + raise UnsupportedFileTypeError( f"File path at `{self}` contains an unrecognized file type." ) -- cgit v1.2.3 From 19d23dc13636b30f6282e8b3f02b659bcdb2bb11 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Wed, 5 Aug 2020 21:48:08 -0500 Subject: Remove MissingDependencyError in favor of ImportError --- config_file/config_file_path.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'config_file/config_file_path.py') diff --git a/config_file/config_file_path.py b/config_file/config_file_path.py index 9f2b87d..42466d9 100644 --- a/config_file/config_file_path.py +++ b/config_file/config_file_path.py @@ -102,7 +102,7 @@ class ConfigFilePath(_Path_): ValueError: If the path leads to a directory. Returns: - This ConfigFilePath object + A ConfigFilePath object with any ~'s expanded. """ if len(self.parts) >= 1 and self.parts[0] == "~": self = ConfigFilePath(self.expanduser()) -- cgit v1.2.3 From c24f1741a91950480fd169a6583e6c2b675ee08e Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Fri, 7 Aug 2020 08:49:03 -0500 Subject: Change UnsupportedFileTypeError to ValueError --- config_file/config_file_path.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) (limited to 'config_file/config_file_path.py') diff --git a/config_file/config_file_path.py b/config_file/config_file_path.py index 42466d9..11a5daa 100644 --- a/config_file/config_file_path.py +++ b/config_file/config_file_path.py @@ -9,7 +9,6 @@ from config_file.json_parser import JsonParser from config_file.toml_parser import TomlParser from config_file.yaml_parser import YamlParser -from .exceptions import UnsupportedFileTypeError from .utils import split_on_dot @@ -58,14 +57,13 @@ class ConfigFilePath(_Path_): """Determine what parser should be used for this file. Raises: - UnsupportedFileTypeError: If the extension of the file - is not recognized. + ValueError: If the extension of the file is not recognized. Returns: The instantiated parser that should be used for this file. """ if self.extension == "": - raise UnsupportedFileTypeError( + raise ValueError( "Tried to determine a parser to use, but the file at " f"{self} does not have an extension." ) @@ -79,7 +77,7 @@ class ConfigFilePath(_Path_): elif self.extension == "toml": return TomlParser(self.contents) else: - raise UnsupportedFileTypeError( + raise ValueError( f"File path at `{self}` contains an unrecognized file type." ) -- cgit v1.2.3