diff --git a/main.py b/main.py index b7d5441..04b82bb 100644 --- a/main.py +++ b/main.py @@ -4,12 +4,14 @@ import json import logging import contentapi import toml +import myutils CONFIGFILE="config.toml" # The entire config object with all defaults config = { "api" : "https://oboy.smilebasicsource.com/api", + "expire_seconds" : 31536000, # 365 days in seconds, expiration for token "tokenfile" : ".qcstoken" } diff --git a/myutils.py b/myutils.py new file mode 100644 index 0000000..f31530f --- /dev/null +++ b/myutils.py @@ -0,0 +1,10 @@ + +# Merge the "new_values" dictionary recursively into the "base_values" dictionary, +# assigning where new_values is assigned but not fully overwriting nested dictionaries +# (hence recursive) +def merge_dictionary(new_values, base_values): + for key in new_values: + if key in base_values and isinstance(base_values[key], dict) and isinstance(new_values[key], dict): + merge_dictionary(new_values[key], base_values[key]) + else: + base_values[key] = new_values[key] \ No newline at end of file diff --git a/test.bat b/test.bat new file mode 100644 index 0000000..6b80946 --- /dev/null +++ b/test.bat @@ -0,0 +1,7 @@ +@echo off + +REM Change python whatever +set pyexe=python34\python.exe + +REM And now, run all the various tests we have +%pyexe% test_myutils.py \ No newline at end of file diff --git a/test_myutils.py b/test_myutils.py new file mode 100644 index 0000000..13429eb --- /dev/null +++ b/test_myutils.py @@ -0,0 +1,47 @@ +import unittest +import myutils + +class TestMergeDictionary(unittest.TestCase): + + def test_basic(self): + # Test case 1: simple merge + base = {'a': 1, 'b': 2} + new = {'b': 3, 'c': 4} + expected = {'a': 1, 'b': 3, 'c': 4} + myutils.merge_dictionary(new, base) + self.assertDictEqual(base, expected) + + def test_nested(self): + # Test case 2: nested dictionary merge + base = {'a': {'b': 1, 'c': 2}, 'd': 3} + new = {'a': {'b': 4, 'e': 5}, 'f': 6} + expected = {'a': {'b': 4, 'c': 2, 'e': 5}, 'd': 3, 'f': 6} + myutils.merge_dictionary(new, base) + self.assertDictEqual(base, expected) + + def test_emptybase(self): + # Test case 3: empty base dictionary + base = {} + new = {'a': 1, 'b': {'c': 2}} + expected = {'a': 1, 'b': {'c': 2}} + myutils.merge_dictionary(new, base) + self.assertDictEqual(base, expected) + + def test_emptynew(self): + # Test case 4: empty new dictionary + base = {'a': 1, 'b': {'c': 2}} + new = {} + expected = {'a': 1, 'b': {'c': 2}} + myutils.merge_dictionary(new, base) + self.assertDictEqual(base, expected) + + def test_nooverlap(self): + # Test case 5: no overlapping keys + base = {'a': 1} + new = {'b': 2} + expected = {'a': 1, 'b': 2} + myutils.merge_dictionary(new, base) + self.assertDictEqual(base, expected) + +if __name__ == '__main__': + unittest.main() \ No newline at end of file