lib: Convert DataFiles to using pathlib Paths

Signed-off-by: Anna Schumaker <Anna@NoWheyCreamery.com>
This commit is contained in:
Anna Schumaker 2020-10-18 17:15:24 -04:00
parent 08a37fef87
commit cc2b74eaa3
2 changed files with 32 additions and 32 deletions

View File

@ -1,5 +1,6 @@
# Copyright 2019 (c) Anna Schumaker.
import os
import pathlib
import pickle
import xdg.BaseDirectory
@ -10,42 +11,41 @@ if os.environ.get("EMMENTAL_TESTING"):
READ = 'rb'
WRITE = 'wb'
emmental_data = xdg.BaseDirectory.save_data_path(__resource)
emmental_data = pathlib.Path(xdg.BaseDirectory.save_data_path(__resource))
class DataFile:
def __init__(self, path, mode):
self.path = os.path.join(emmental_data, path)
self.temp = os.path.join(emmental_data, f".{path}.tmp")
self.path = emmental_data / path
self.temp = emmental_data / f".{path}.tmp"
self.mode = mode
self.file = None
def __enter__(self):
if self.mode == WRITE:
self.file = open(self.temp, self.mode)
elif self.mode == READ and self.exists():
self.file = open(self.path, self.mode)
self.file = self.temp.open(self.mode)
elif self.mode == READ and self.path.exists():
self.file = self.path.open(self.mode)
return self
def __exit__(self, exp_type, exp_value, traceback):
if self.file:
self.file.flush()
self.file.close()
self.file = None
if self.mode == WRITE:
os.rename(self.temp, self.path)
self.temp.replace(self.path)
self.file = None
return True
def exists(self):
return os.path.exists(self.path)
return self.path.exists()
def pickle(self, obj):
if self.file:
pickle.dump(obj, self.file, pickle.HIGHEST_PROTOCOL)
def remove(self):
if not self.file and self.exists():
os.remove(self.path)
return True
return False
if self.file == None:
self.path.unlink(missing_ok = True)
def unpickle(self):
if self.file:

View File

@ -1,22 +1,23 @@
# Copyright 2019 (c) Anna Schumaker.
from . import data
import os
import pathlib
import unittest
import xdg.BaseDirectory
xdg_data_home = xdg.BaseDirectory.xdg_data_home
testing_data = os.path.join(xdg_data_home, "emmental-testing")
testing_file = os.path.join(testing_data, "test.file")
testing_temp = os.path.join(testing_data, ".test.file.tmp")
xdg_data_home = pathlib.Path(xdg.BaseDirectory.xdg_data_home)
testing_data = xdg_data_home / "emmental-testing"
testing_file = testing_data / "test.file"
testing_temp = testing_data / ".test.file.tmp"
class TestDataModule(unittest.TestCase):
def setUp(self):
if os.path.exists(testing_file): os.remove(testing_file)
def tearDown(self):
testing_file.unlink(missing_ok=True)
def test_dir(self):
self.assertEqual(data.emmental_data, testing_data)
self.assertTrue(os.path.exists(testing_data))
self.assertTrue(os.path.isdir(testing_data))
self.assertTrue(testing_data.exists())
self.assertTrue(testing_data.is_dir())
self.assertEqual(data.READ, 'rb')
self.assertEqual(data.WRITE, 'wb')
@ -40,31 +41,30 @@ class TestDataModule(unittest.TestCase):
self.assertIsNone(f.file)
f.pickle(test)
self.assertIsNone(f.unpickle())
self.assertFalse(f.remove())
self.assertTrue(f.exists())
f.remove()
self.assertFalse(f.exists())
with data.DataFile("test.file", data.WRITE) as f:
self.assertIsNotNone(f.file)
self.assertEqual(f.file.name, testing_temp)
self.assertEqual(pathlib.Path(f.file.name), testing_temp)
self.assertFalse(f.exists())
self.assertTrue(os.path.exists(testing_temp))
self.assertTrue(testing_temp.exists())
f.pickle(test)
self.assertFalse(f.remove())
self.assertTrue(f.exists())
f.remove()
self.assertFalse(f.exists())
self.assertIsNone(f.file)
self.assertFalse(os.path.exists(testing_temp))
self.assertFalse(testing_temp.exists())
self.assertTrue(f.exists())
with data.DataFile("test.file", data.READ) as f:
self.assertIsNotNone(f.file)
self.assertEqual(f.file.name, testing_file)
self.assertEqual(pathlib.Path(f.file.name), testing_file)
lst = f.unpickle()
self.assertEqual(test, lst)
self.assertFalse(f.remove())
f.remove()
self.assertTrue(f.exists())
f = data.DataFile("test.file", data.READ)
self.assertTrue(f.remove())
f.remove()
self.assertFalse(f.exists())
self.assertFalse(f.remove())