diff --git a/src/config.py b/src/config.py index 2945e34..28a8b26 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. + 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. """ - source_file_location: FileLocation - source_file_root: str - destination_file_content: 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.source_file_location.resolved_paths: + for src_file in self.location.resolved_paths: src_content = JsonFileType.load_from_file(src_file) - jsonpath_expr = parse(self.source_file_root) + jsonpath_expr = parse(self.node) retrieved_src_content.extend( [match.value for match in jsonpath_expr.find(src_content)] ) @@ -47,28 +47,24 @@ 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 + location: FileLocation def __post_init__(self): - if len(self.destination_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.destination_file_location.root_path - / self.destination_file_location.substituted_path + self.location.root_path / self.location.substituted_path ) - if ( - self.destination_file_location.root_path - / self.destination_file_location.substituted_path - ).exists() + if (self.location.root_path / self.location.substituted_path).exists() else {} ) @@ -83,37 +79,31 @@ 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.node != other_src.node: src_files_match = False break - if ( - src.source_file_location.substituted_path - != other_src.source_file_location.substituted_path - ): + if src.location.substituted_path != other_src.location.substituted_path: src_files_match = False break - if src.destination_file_content != other_src.destination_file_content: + if src.destination_node != other_src.destination_node: src_files_match = False break return ( - self.destination_file.destination_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 ) 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 + dest_content = self.destination_file.content for src in self.source_files: - jsonpath_expr = parse(src.destination_file_content) + jsonpath_expr = parse(src.destination_node) dest_content_matches = [ match.value for match in jsonpath_expr.find(dest_content) ] @@ -137,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.destination_file_location.substituted_path, + self.root_path / self.destination_file.location.substituted_path, ) @staticmethod diff --git a/tests/test_config.py b/tests/test_config.py index 5ebd04b..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( - source_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(), ), - source_file_root="$.AnotherKeyInTheFile", - destination_file_content="$", + node="$.AnotherKeyInTheFile", + destination_node="$", ), SourceFile( - source_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(), ), - source_file_root="$.AnotherKeyInTheFile", - destination_file_content="$", + node="$.AnotherKeyInTheFile", + destination_node="$", ), ], destination_file=DestinationFile(