Prechádzať zdrojové kódy

Added basis set of functions for file collections to Cache object.

Taddeus Kroes 14 rokov pred
rodič
commit
ee28c5c86b
2 zmenil súbory, kde vykonal 134 pridanie a 2 odobranie
  1. 70 1
      src/cache.py
  2. 64 1
      tests/test_cache.py

+ 70 - 1
src/cache.py

@@ -1,2 +1,71 @@
+from os.path import getmtime, exists
+
+
+def to_dir(path):
+    """
+    Transform a path to a directory, that can be used as prefix before a
+    filename.
+    """
+    if not path:
+        return ''
+
+    if path[-1] != '/':
+        return path + '/'
+
+    return path
+
+
 class Cache:
-    pass
+    """
+    A Cache instance represents a single file in the cache directory, which is
+    a concatenation of the file list.
+    """
+
+    def __init__(self, root='', files=[], cached='cached'):
+        """
+        The constructor takes the following arguments (all are optional):
+        1. The directory in which the cached files are located (empty by
+           default).
+        2. An initial list of number of files to include in the cahce file.
+        3. The cached files directory (defaults to 'cached').
+        """
+        self.root = to_dir(root)
+        self.cached = to_dir(cached)
+        self.files = map(lambda f: self.root + f, files)
+        self.assert_files_exist()
+
+    def assert_files_exist(self):
+        for path in self.files:
+            if not exists(path):
+                raise IOError('File "%s" does not exist.' % path)
+
+    def add(self, path, absolute=False):
+        """
+        Add a file to the cache object. Requires the full path to the file,
+        relative to the root directory. If the second argument is True, the
+        root directory will not be prepended to the path.
+        """
+        if not absolute:
+            path = self.root + path
+
+        self.files.append(path)
+
+    def remove(self, path, absolute=False):
+        """
+        Remove a file from the cache object. Requires the full path to the
+        file, relative to the root directory. If the second argument is True,
+        the root directory will not be prepended to the path.
+        """
+        if not absolute:
+            path = self.root + path
+
+        self.files.remove(path)
+
+    def get_modification_dates(self):
+        """
+        Get the latest modification dates fo each file in the cache object.
+        """
+        self.modified = {}
+
+        for path in self.files:
+            self.modified[path] = getmtime(path)

+ 64 - 1
tests/test_cache.py

@@ -1,5 +1,68 @@
 import unittest
+import os
+from shutil import rmtree
+
+from src.cache import to_dir, Cache
 
 
 class TestCache(unittest.TestCase):
-    pass
+    def setUp(self):
+        os.mkdir('baz')
+
+        for name in ('foo', 'bar'):
+            f = open('baz/' + name, 'w')
+            f.write(name)
+            f.close()
+
+        self.c = Cache(files=['foo', 'bar'], root='baz')
+
+    def tearDown(self):
+        rmtree('baz')
+
+    def test_to_dir(self):
+        self.assertEqual(to_dir(''), '')
+        self.assertEqual(to_dir('foo'), 'foo/')
+        self.assertEqual(to_dir('foo/'), 'foo/')
+
+    def test___init__(self):
+        self.assertTrue(hasattr(self.c, 'root'))
+        self.assertEqual(self.c.root, 'baz/')
+        self.assertTrue(hasattr(self.c, 'files'))
+        self.assertEqual(self.c.files, ['baz/foo', 'baz/bar'])
+        self.assertTrue(hasattr(self.c, 'cached'))
+        self.assertEqual(self.c.cached, 'cached/')
+
+    def test___init___custom_root(self):
+        c = Cache(root='foo')
+        self.assertTrue(hasattr(c, 'root'))
+        self.assertEqual(c.root, 'foo/')
+
+    def test___init___custom_cached_dir(self):
+        c = Cache(cached='foo')
+        self.assertTrue(hasattr(c, 'cached'))
+        self.assertEqual(c.cached, 'foo/')
+
+    def test___init___non_existant_file(self):
+        self.assertRaises(IOError, Cache, files=['foo'])
+
+    def test_add(self):
+        self.c.add('baz')
+        self.assertIn('baz/baz', self.c.files)
+
+    def test_add_non_relative(self):
+        self.c.add('baz', absolute=True)
+        self.assertIn('baz', self.c.files)
+
+    def test_remove(self):
+        self.c.remove('foo')
+        self.assertNotIn('baz/foo', self.c.files)
+
+    def test_remove_non_relative(self):
+        self.c.remove('baz/foo', absolute=True)
+        self.assertNotIn('baz/foo', self.c.files)
+
+    def test_get_modification_dates(self):
+        self.c.get_modification_dates()
+        self.assertIn('baz/foo', self.c.modified)
+        self.assertEqual(self.c.modified['baz/foo'],
+                         os.path.getmtime('baz/foo'))