From 2bba56fdf8c463e8d93353cd2e0fede3403411af Mon Sep 17 00:00:00 2001 From: PatchOfScotland Date: Fri, 14 Apr 2023 15:40:42 +0200 Subject: [PATCH] updated status updating to merge in a more accomodating manner --- functionality/file_io.py | 21 +++++++++++++++++---- tests/test_functionality.py | 2 ++ 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/functionality/file_io.py b/functionality/file_io.py index a7bd4ec..c631bb0 100644 --- a/functionality/file_io.py +++ b/functionality/file_io.py @@ -95,7 +95,6 @@ def write_yaml(source:Any, filename:str): with open(filename, 'w') as param_file: yaml.dump(source, param_file, default_flow_style=False) -# TODO test me def threadsafe_read_status(filepath:str): lock_path = f"{filepath}.lock" lock_handle = open(lock_path, 'a') @@ -111,7 +110,6 @@ def threadsafe_read_status(filepath:str): return status -# TODO test me def threadsafe_write_status(source:dict[str,Any], filepath:str): lock_path = f"{filepath}.lock" lock_handle = open(lock_path, 'a') @@ -125,7 +123,6 @@ def threadsafe_write_status(source:dict[str,Any], filepath:str): lock_handle.close() -# TODO test me def threadsafe_update_status(updates:dict[str,Any], filepath:str): lock_path = f"{filepath}.lock" lock_handle = open(lock_path, 'a') @@ -133,7 +130,8 @@ def threadsafe_update_status(updates:dict[str,Any], filepath:str): try: status = read_yaml(filepath) - write_yaml({**status, **updates}, filepath) + + write_yaml(generous_merge(status, updates), filepath) except Exception as e: lock_handle.close() raise e @@ -162,3 +160,18 @@ def lines_to_string(lines:List[str])->str: """Function to convert a list of str lines, into one continuous string separated by newline characters""" return "\n".join(lines) + +def generous_merge(source:dict[str,Any], update:dict[str,Any], top:bool=True): + result = {} + + for k, v in source.items(): + if k in update: + if isinstance(v, dict): + result[k] = generous_merge(v, update[k], top=False) + else: + result[k] = update[k] + + else: + result[k] = v + + return result \ No newline at end of file diff --git a/tests/test_functionality.py b/tests/test_functionality.py index 2b4d4b4..936418f 100644 --- a/tests/test_functionality.py +++ b/tests/test_functionality.py @@ -484,6 +484,7 @@ data""" 'A: a\n', 'B: 42\n', 'C:\n', + ' D: true\n', ' E:\n', ' - 1\n', ' - 2\n', @@ -493,6 +494,7 @@ data""" self.assertEqual(data, expected_bytes) + class HashingTests(unittest.TestCase): def setUp(self)->None: super().setUp()