From 2d466c3b07a1723bd02f14717168c2f90ad02b3d Mon Sep 17 00:00:00 2001 From: ServerlessSam Date: Sun, 18 Dec 2022 08:27:44 -0500 Subject: [PATCH 1/4] Rename wordy config class attributes --- src/config.py | 46 +++++++++++++++++++++----------------------- tests/test_config.py | 12 ++++++------ 2 files changed, 28 insertions(+), 30 deletions(-) diff --git a/src/config.py b/src/config.py index 2945e34..09c9269 100644 --- a/src/config.py +++ b/src/config.py @@ -16,14 +16,14 @@ class SourceFile: """ Synopsis: A class for handling a source file definition for a build. Parameters: - source_file_location = a FileLocation object that provides one or more file locations for the build. - source_file_root = the jsonpath to the root node to copy from in each source file found. - destination_file_content = the jsonpath to the root node to copy to in the destination file. + file_location = a FileLocation object that provides one or more file locations for the build. + file_node = the jsonpath to the root node to copy from in each source file found. + destination_file_node = the jsonpath to the root node to copy to in the destination file. """ - source_file_location: FileLocation - source_file_root: str - destination_file_content: str + file_location: FileLocation + file_node: str + destination_file_node: str @cached_property def retrieved_src_content(self) -> list: @@ -33,9 +33,9 @@ def retrieved_src_content(self) -> list: Returns: A list of objects that will be merged within the destination file at the specified root node. """ retrieved_src_content = [] - for src_file in self.source_file_location.resolved_paths: + for src_file in self.file_location.resolved_paths: src_content = JsonFileType.load_from_file(src_file) - jsonpath_expr = parse(self.source_file_root) + jsonpath_expr = parse(self.file_node) retrieved_src_content.extend( [match.value for match in jsonpath_expr.find(src_content)] ) @@ -47,13 +47,13 @@ class DestinationFile: """ Synopsis: A class for handling the destination file definition for a build. Parameters: - destination_file_location = a FileLocation object that provides one single file location for the build. + file_location = a FileLocation object that provides one single file location for the build. """ - destination_file_location: FileLocation + file_location: FileLocation def __post_init__(self): - if len(self.destination_file_location.resolved_paths) > 1: + if len(self.file_location.resolved_paths) > 1: raise Exception( "Attempting to use multiple destination files. We don't support this (yet)!" ) @@ -62,12 +62,12 @@ def __post_init__(self): def file_content(self) -> dict | list: return ( JsonFileType.load_from_file( - self.destination_file_location.root_path - / self.destination_file_location.substituted_path + self.file_location.root_path + / self.file_location.substituted_path ) if ( - self.destination_file_location.root_path - / self.destination_file_location.substituted_path + self.file_location.root_path + / self.file_location.substituted_path ).exists() else {} ) @@ -83,21 +83,21 @@ def __eq__(self, other): src_files_match = True for src, other_src in zip(self.source_files, other.source_files): - if src.source_file_root != other_src.source_file_root: + if src.file_node != other_src.source_file_root: src_files_match = False break if ( - src.source_file_location.substituted_path + src.file_location.substituted_path != other_src.source_file_location.substituted_path ): src_files_match = False break - if src.destination_file_content != other_src.destination_file_content: + if src.destination_file_node != other_src.destination_file_content: src_files_match = False break return ( - self.destination_file.destination_file_location.substituted_path + self.destination_file.file_location.substituted_path == other.destination_file.destination_file_location.substituted_path and self.destination_file.file_content == other.destination_file.file_content @@ -107,13 +107,11 @@ def __eq__(self, other): def generate_new_dest_content(self) -> dict | list: """ Synopsis: Combines the current state of the desination file with desired source file content - Parameters: - src = A SourceFile object to be combined at the destination during a build. - Returns: The new destination file content. Note the file has not been saved to disk yet. + Returns: The new destination file content. Note the file has not been saved to disk yet. """ dest_content = self.destination_file.file_content for src in self.source_files: - jsonpath_expr = parse(src.destination_file_content) + jsonpath_expr = parse(src.destination_file_node) dest_content_matches = [ match.value for match in jsonpath_expr.find(dest_content) ] @@ -138,7 +136,7 @@ def write_content(self, content: dict): JsonFileType.save_to_file( content, self.root_path - / self.destination_file.destination_file_location.substituted_path, + / self.destination_file.file_location.substituted_path, ) @staticmethod diff --git a/tests/test_config.py b/tests/test_config.py index 5ebd04b..decd8ff 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -159,7 +159,7 @@ def test_config_load(self): expected_config = BuildConfig( source_files=[ SourceFile( - source_file_location=FileLocation( + file_location=FileLocation( path="test_files_directory/nested_directory/nested_${Sub1}_file_1.json", subs={ "Sub1": Substitution( @@ -168,17 +168,17 @@ def test_config_load(self): }, root_path=Path(__file__).parent.resolve(), ), - source_file_root="$.AnotherKeyInTheFile", - destination_file_content="$", + file_node="$.AnotherKeyInTheFile", + destination_file_node="$", ), SourceFile( - source_file_location=FileLocation( + file_location=FileLocation( path="test_files_directory/nested_directory/nested_${Sub1}_file_2.json", subs={"Sub1": Substitution(LiteralReferenceType(), "test")}, root_path=Path(__file__).parent.resolve(), ), - source_file_root="$.AnotherKeyInTheFile", - destination_file_content="$", + file_node="$.AnotherKeyInTheFile", + destination_file_node="$", ), ], destination_file=DestinationFile( From 7083f7748d42b5b8756a8356290e656a9c5ef82b Mon Sep 17 00:00:00 2001 From: ServerlessSam Date: Sun, 18 Dec 2022 08:31:58 -0500 Subject: [PATCH 2/4] Clean up missed refactored class attributes --- src/config.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/config.py b/src/config.py index 09c9269..d7ab448 100644 --- a/src/config.py +++ b/src/config.py @@ -83,12 +83,12 @@ def __eq__(self, other): src_files_match = True for src, other_src in zip(self.source_files, other.source_files): - if src.file_node != other_src.source_file_root: + if src.file_node != other_src.file_node: src_files_match = False break if ( src.file_location.substituted_path - != other_src.source_file_location.substituted_path + != other_src.file_location.substituted_path ): src_files_match = False break From be6ff9b2a3157693b4ccfe2a7bc908aa35b655ab Mon Sep 17 00:00:00 2001 From: ServerlessSam Date: Sun, 18 Dec 2022 08:40:22 -0500 Subject: [PATCH 3/4] Futher renaming of class attributes (shorter names) --- src/config.py | 52 ++++++++++++++++++++++---------------------- tests/test_config.py | 16 +++++++------- 2 files changed, 34 insertions(+), 34 deletions(-) diff --git a/src/config.py b/src/config.py index d7ab448..2b62f3c 100644 --- a/src/config.py +++ b/src/config.py @@ -16,14 +16,14 @@ class SourceFile: """ Synopsis: A class for handling a source file definition for a build. Parameters: - file_location = a FileLocation object that provides one or more file locations for the build. - file_node = the jsonpath to the root node to copy from in each source file found. - destination_file_node = the jsonpath to the root node to copy to in the destination file. + location = a FileLocation object that provides one or more file locations for the build. + node = the jsonpath to the root node to copy from in each source file found. + destination_node = the jsonpath to the root node to copy to in the destination file. """ - file_location: FileLocation - file_node: str - destination_file_node: str + location: FileLocation + node: str + destination_node: str @cached_property def retrieved_src_content(self) -> list: @@ -33,9 +33,9 @@ def retrieved_src_content(self) -> list: Returns: A list of objects that will be merged within the destination file at the specified root node. """ retrieved_src_content = [] - for src_file in self.file_location.resolved_paths: + for src_file in self.location.resolved_paths: src_content = JsonFileType.load_from_file(src_file) - jsonpath_expr = parse(self.file_node) + jsonpath_expr = parse(self.node) retrieved_src_content.extend( [match.value for match in jsonpath_expr.find(src_content)] ) @@ -50,24 +50,24 @@ class DestinationFile: file_location = a FileLocation object that provides one single file location for the build. """ - file_location: FileLocation + location: FileLocation def __post_init__(self): - if len(self.file_location.resolved_paths) > 1: + if len(self.location.resolved_paths) > 1: raise Exception( "Attempting to use multiple destination files. We don't support this (yet)!" ) @cached_property - def file_content(self) -> dict | list: + def content(self) -> dict | list: return ( JsonFileType.load_from_file( - self.file_location.root_path - / self.file_location.substituted_path + self.location.root_path + / self.location.substituted_path ) if ( - self.file_location.root_path - / self.file_location.substituted_path + self.location.root_path + / self.location.substituted_path ).exists() else {} ) @@ -83,24 +83,24 @@ def __eq__(self, other): src_files_match = True for src, other_src in zip(self.source_files, other.source_files): - if src.file_node != other_src.file_node: + if src.node != other_src.node: src_files_match = False break if ( - src.file_location.substituted_path - != other_src.file_location.substituted_path + src.location.substituted_path + != other_src.location.substituted_path ): src_files_match = False break - if src.destination_file_node != other_src.destination_file_content: + if src.destination_node != other_src.destination_node: src_files_match = False break return ( - self.destination_file.file_location.substituted_path - == other.destination_file.destination_file_location.substituted_path - and self.destination_file.file_content - == other.destination_file.file_content + self.destination_file.location.substituted_path + == other.destination_file.location.substituted_path + and self.destination_file.content + == other.destination_file.content and src_files_match ) @@ -109,9 +109,9 @@ def generate_new_dest_content(self) -> dict | list: Synopsis: Combines the current state of the desination file with desired source file content Returns: The new destination file content. Note the file has not been saved to disk yet. """ - dest_content = self.destination_file.file_content + dest_content = self.destination_file.content for src in self.source_files: - jsonpath_expr = parse(src.destination_file_node) + jsonpath_expr = parse(src.destination_node) dest_content_matches = [ match.value for match in jsonpath_expr.find(dest_content) ] @@ -136,7 +136,7 @@ def write_content(self, content: dict): JsonFileType.save_to_file( content, self.root_path - / self.destination_file.file_location.substituted_path, + / self.destination_file.location.substituted_path, ) @staticmethod diff --git a/tests/test_config.py b/tests/test_config.py index decd8ff..95ce96d 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -63,7 +63,7 @@ def test_destination_files(self): ) dest = DestinationFile(dest_file_location) - assert dest.file_content == { + assert dest.content == { "AKeyInTheFile": "A value in the file", "AnotherKeyInTheFile": {"UhOh": "This", "OneIs": "Nested"}, } @@ -75,7 +75,7 @@ def test_destination_file_doesnt_exist(self): root_path=Path(__file__).parent.resolve(), ) dest = DestinationFile(dest_file_location) - assert dest.file_content == {} + assert dest.content == {} class TestSourceFiles: @@ -159,7 +159,7 @@ def test_config_load(self): expected_config = BuildConfig( source_files=[ SourceFile( - file_location=FileLocation( + location=FileLocation( path="test_files_directory/nested_directory/nested_${Sub1}_file_1.json", subs={ "Sub1": Substitution( @@ -168,17 +168,17 @@ def test_config_load(self): }, root_path=Path(__file__).parent.resolve(), ), - file_node="$.AnotherKeyInTheFile", - destination_file_node="$", + node="$.AnotherKeyInTheFile", + destination_node="$", ), SourceFile( - file_location=FileLocation( + location=FileLocation( path="test_files_directory/nested_directory/nested_${Sub1}_file_2.json", subs={"Sub1": Substitution(LiteralReferenceType(), "test")}, root_path=Path(__file__).parent.resolve(), ), - file_node="$.AnotherKeyInTheFile", - destination_file_node="$", + node="$.AnotherKeyInTheFile", + destination_node="$", ), ], destination_file=DestinationFile( From 9a61bf4f054f5881cce55638cb58d970c807cbeb Mon Sep 17 00:00:00 2001 From: ServerlessSam Date: Sun, 18 Dec 2022 08:44:00 -0500 Subject: [PATCH 4/4] Update config.py --- src/config.py | 19 +++++-------------- 1 file changed, 5 insertions(+), 14 deletions(-) diff --git a/src/config.py b/src/config.py index 2b62f3c..28a8b26 100644 --- a/src/config.py +++ b/src/config.py @@ -62,13 +62,9 @@ def __post_init__(self): def content(self) -> dict | list: return ( JsonFileType.load_from_file( - self.location.root_path - / self.location.substituted_path + self.location.root_path / self.location.substituted_path ) - if ( - self.location.root_path - / self.location.substituted_path - ).exists() + if (self.location.root_path / self.location.substituted_path).exists() else {} ) @@ -86,10 +82,7 @@ def __eq__(self, other): if src.node != other_src.node: src_files_match = False break - if ( - src.location.substituted_path - != other_src.location.substituted_path - ): + if src.location.substituted_path != other_src.location.substituted_path: src_files_match = False break if src.destination_node != other_src.destination_node: @@ -99,8 +92,7 @@ def __eq__(self, other): return ( self.destination_file.location.substituted_path == other.destination_file.location.substituted_path - and self.destination_file.content - == other.destination_file.content + and self.destination_file.content == other.destination_file.content and src_files_match ) @@ -135,8 +127,7 @@ def write_content(self, content: dict): # JsonFileType.save_to_file(content, self.destination_file.destination_file_location.resolved_paths[0]) JsonFileType.save_to_file( content, - self.root_path - / self.destination_file.location.substituted_path, + self.root_path / self.destination_file.location.substituted_path, ) @staticmethod