diff --git a/CHANGELOG.md b/CHANGELOG.md index 6023539..5181e91 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ This project uses Semantic Versioning +## 0.2.0 + +Added automatic creation of path for file writing + ## 0.1.1 Added: diff --git a/deadsimplekv/__init__.py b/deadsimplekv/__init__.py index 54a2dd3..89e0805 100644 --- a/deadsimplekv/__init__.py +++ b/deadsimplekv/__init__.py @@ -1,4 +1,4 @@ -''' +""" DeadSimpleKv. An extremely simple key-value storage system with cache Copyright (C) 2019 Kevin Froman @@ -14,13 +14,13 @@ You should have received a copy of the GNU General Public License along with this program. If not, see . -''' +""" import json, time, math, os, atexit def _is_serializable(data): - ''' + """ Test if something is able to be in JSON format - ''' + """ try: json.dumps(data) return True @@ -29,12 +29,12 @@ def _is_serializable(data): class DeadSimpleKV: def __init__(self, file_path=None, refresh_seconds=0, flush_seconds=0, flush_on_exit=True): - ''' + """ Accepts a file path and refresh. If file_path is not set, no data will be written to disk. Refresh is an integer specifying how many seconds should pass before data is re-read from disk. If refresh_seconds or flush_seconds are set to None they will not be done automatically (except where flush_on_exit applies) - ''' + """ temp_time = DeadSimpleKV._get_epoch() # Initialization time self.file_path = file_path # The file path where we write our data in JSON format self.refresh_seconds = refresh_seconds # How often to refresh the @@ -43,6 +43,10 @@ class DeadSimpleKV: self._last_refresh = temp_time # time last refreshed from disk self._last_flush = temp_time # time last flushed to disk + if not file_path is None: + abs_path = os.path.dirname(os.path.abspath(file_path)) + if not os.path.exists(abs_path): os.makedirs(abs_path) + try: if os.path.exists(file_path): self.refresh() @@ -53,9 +57,9 @@ class DeadSimpleKV: atexit.register(self.flush) def get(self, key): - ''' + """ Accepts key, which must be json serializable - ''' + """ self._do_auto_refresh() try: return self._data[key] @@ -63,10 +67,10 @@ class DeadSimpleKV: return None def put(self, key, value): - ''' + """ Value setter. Will automatically flush if auto_flush is True and file_path is not None Will return value error if either key or value are not JSON serializable - ''' + """ self._data[key] = value # Set the key # Check if key and value can be put in JSON @@ -78,16 +82,16 @@ class DeadSimpleKV: return (key, value) def delete(self, key): - ''' + """ Deletes value. Will automatically flush if auto_flush is True and file_path is not None - ''' + """ del self._data[key] self._do_auto_flush() def refresh(self): - ''' + """ Refresh data and then mark time read. Can be manually called - ''' + """ try: self._data = json.loads(DeadSimpleKV._read_in(self.file_path)) except FileNotFoundError: @@ -95,9 +99,9 @@ class DeadSimpleKV: self._last_refresh = DeadSimpleKV._get_epoch() def flush(self): - ''' - Write out then mark time flushed. Can be manually called - ''' + """ + Write out to file then mark time flushed. Can be manually called + """ DeadSimpleKV._write_out(self.file_path, json.dumps(self._data)) self._last_flush = DeadSimpleKV._get_epoch() @@ -119,12 +123,12 @@ class DeadSimpleKV: @staticmethod def _write_out(file_path, data): - '''Write out the raw data''' + """Write out the raw data""" with open(file_path, 'w') as write_file: write_file.write(data) @staticmethod def _read_in(file_path): - '''Read in raw data''' + """Read in raw data""" with open(file_path, 'r') as read_file: return read_file.read() \ No newline at end of file diff --git a/run_tests.sh b/run_tests.sh index 5a40bf9..12953da 100755 --- a/run_tests.sh +++ b/run_tests.sh @@ -10,4 +10,5 @@ for f in tests/*.py; do let "ran++" done echo "ran $ran test files successfully in $SECONDS seconds" -rm *.dat \ No newline at end of file +rm *.dat +rm -rf my_test_dir \ No newline at end of file diff --git a/setup.py b/setup.py index 40b5a4f..347b813 100644 --- a/setup.py +++ b/setup.py @@ -1,7 +1,7 @@ from distutils.core import setup setup(name='deadsimplekv', - version='0.1.1', + version='0.2.0', description='Very simple key-value store for Python', author='Kevin Froman', author_email='beardog@mailbox.org', diff --git a/tests/test_basic.py b/tests/test_basic.py index dd4725a..83c88a9 100644 --- a/tests/test_basic.py +++ b/tests/test_basic.py @@ -70,7 +70,7 @@ class TestInit(unittest.TestCase): self.assertIsNone(kv.get('my_key')) def test_invalid(self): - kv = deadsimplekv.DeadSimpleKV(get_test_id()) + kv = deadsimplekv.DeadSimpleKV(get_test_id(), flush_on_exit=False) try: kv.put('rekt', b'bits') except ValueError: @@ -96,6 +96,19 @@ class TestInit(unittest.TestCase): self.assertEqual(test_data['my_key'], 'test') + def test_flush_no_path(self): + # assert data gets flushed when the path is not made yet + + test_id = "my_test_dir/my_second_dir/" + get_test_id() + + kv = deadsimplekv.DeadSimpleKV(test_id) + kv.put('my_key', 'test2') + + with open(test_id, 'r') as test_file: + test_data = json.loads(test_file.read()) + + self.assertEqual(test_data['my_key'], 'test2') + def test_time(self): self.assertEqual(deadsimplekv.DeadSimpleKV._get_epoch(), math.floor(time.time()))