diff options
author | Lars Wirzenius <liw@liw.fi> | 2015-07-05 12:45:41 +0300 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2015-07-05 13:13:16 +0300 |
commit | 9580621fcf8a34ca2068b87bcdd5b4f3d52a4e3d (patch) | |
tree | 54b06dc964362a0adef06a4a77bcaecc7ebdde01 | |
parent | da847ff41b775e38a209e82c1502e706bc93a9ba (diff) | |
download | obnam-9580621fcf8a34ca2068b87bcdd5b4f3d52a4e3d.tar.gz |
Add GADirectory
-rw-r--r-- | obnamlib/__init__.py | 2 | ||||
-rw-r--r-- | obnamlib/fmt_ga/__init__.py | 1 | ||||
-rw-r--r-- | obnamlib/fmt_ga/dirobj.py | 67 | ||||
-rw-r--r-- | obnamlib/fmt_ga/dirobj_tests.py | 92 |
4 files changed, 162 insertions, 0 deletions
diff --git a/obnamlib/__init__.py b/obnamlib/__init__.py index 4d2479c4..14930736 100644 --- a/obnamlib/__init__.py +++ b/obnamlib/__init__.py @@ -182,6 +182,8 @@ from fmt_ga import ( RepositoryFormatGA, GAClientList, GAClient, + GADirectory, + create_gadirectory_from_dict, GAChunkStore, GAChunkIndexes) diff --git a/obnamlib/fmt_ga/__init__.py b/obnamlib/fmt_ga/__init__.py index a32864a7..e9b96ecb 100644 --- a/obnamlib/fmt_ga/__init__.py +++ b/obnamlib/fmt_ga/__init__.py @@ -19,5 +19,6 @@ from .client_list import GAClientList from .chunk_store import GAChunkStore from .indexes import GAChunkIndexes +from .dirobj import GADirectory, create_gadirectory_from_dict from .client import GAClient from .format import RepositoryFormatGA diff --git a/obnamlib/fmt_ga/dirobj.py b/obnamlib/fmt_ga/dirobj.py new file mode 100644 index 00000000..bc1648f3 --- /dev/null +++ b/obnamlib/fmt_ga/dirobj.py @@ -0,0 +1,67 @@ +# Copyright 2015 Lars Wirzenius +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. +# +# =*= License: GPL-3+ =*= + + +class GADirectory(object): + + def __init__(self): + self._dict = { + 'metadata': {}, + 'subdirs': {}, + } + self._mutable = True + + def is_mutable(self): + return self._mutable + + def set_immutable(self): + self._mutable = False + + def as_dict(self): + return self._dict + + def add_file(self, basename): + self._dict['metadata'][basename] = {} + + def get_file_basenames(self): + return self._dict['metadata'].keys() + + def get_file_key(self, basename, key): + return self._dict['metadata'][basename].get(key) + + def set_file_key(self, basename, key, value): + self._dict['metadata'][basename][key] = value + + def get_subdir_basenames(self): + return self._dict['subdirs'].keys() + + def add_subdir(self, basename, obj_id): + self._dict['subdirs'][basename] = obj_id + + def get_subdir_object_id(self, basename): + return self._dict['subdirs'][basename] + + +def create_gadirectory_from_dict(a_dict): + dir_obj = GADirectory() + for basename in a_dict['metadata']: + dir_obj.add_file(basename) + for key, value in a_dict['metadata'][basename].items(): + dir_obj.set_file_key(basename, key, value) + for subdir, obj_id in a_dict['subdirs'].items(): + dir_obj.add_subdir(subdir, obj_id) + return dir_obj diff --git a/obnamlib/fmt_ga/dirobj_tests.py b/obnamlib/fmt_ga/dirobj_tests.py new file mode 100644 index 00000000..9efff755 --- /dev/null +++ b/obnamlib/fmt_ga/dirobj_tests.py @@ -0,0 +1,92 @@ +# Copyright 2015 Lars Wirzenius +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. +# +# =*= License: GPL-3+ =*= + + +import stat +import unittest + +import obnamlib + + +class GADirectoryTests(unittest.TestCase): + + def test_is_mutable_initially(self): + dir_obj = obnamlib.GADirectory() + self.assertTrue(dir_obj.is_mutable()) + + def test_can_be_made_immutable(self): + dir_obj = obnamlib.GADirectory() + dir_obj.set_immutable() + self.assertFalse(dir_obj.is_mutable()) + + def test_returns_itself_as_dictionary(self): + dir_obj = obnamlib.GADirectory() + self.assertEqual(type(dir_obj.as_dict()), dict) + + def test_has_no_files_initially(self): + dir_obj = obnamlib.GADirectory() + self.assertEqual(dir_obj.get_file_basenames(), []) + + def test_adds_file(self): + dir_obj = obnamlib.GADirectory() + dir_obj.add_file('README') + self.assertEqual(dir_obj.get_file_basenames(), ['README']) + + def test_gets_file_key_when_unset(self): + dir_obj = obnamlib.GADirectory() + dir_obj.add_file('README') + self.assertEqual( + dir_obj.get_file_key('README', obnamlib.REPO_FILE_MODE), + None) + + def test_sets_file_key(self): + dir_obj = obnamlib.GADirectory() + dir_obj.add_file('README') + dir_obj.set_file_key('README', obnamlib.REPO_FILE_MODE, 0123) + self.assertEqual( + dir_obj.get_file_key('README', obnamlib.REPO_FILE_MODE), + 0123) + + def test_has_no_subdirs_initially(self): + dir_obj = obnamlib.GADirectory() + self.assertEqual(dir_obj.get_subdir_basenames(), []) + + def test_adds_subdir(self): + dir_obj = obnamlib.GADirectory() + dir_obj.add_subdir('.git', 'obj-id') + self.assertEqual(dir_obj.get_subdir_basenames(), ['.git']) + + def test_returns_subdir_object_id(self): + dir_obj = obnamlib.GADirectory() + dir_obj.add_subdir('.git', 'obj-id') + self.assertEqual(dir_obj.get_subdir_object_id('.git'), 'obj-id') + + +class GADirectoryCreationTests(unittest.TestCase): + + def test_creates_GADirectory_from_dict(self): + orig = obnamlib.GADirectory() + orig.add_file('.') + orig.set_file_key( + '.', obnamlib.REPO_FILE_MODE, stat.S_IFDIR | 0755) + orig.add_file('README') + orig.set_file_key( + 'README', obnamlib.REPO_FILE_MODE, stat.S_IFREG | 0644) + orig.add_subdir('.git', 'git-dir-id') + + new = obnamlib.create_gadirectory_from_dict(orig.as_dict()) + self.assertEqual(new.as_dict(), orig.as_dict()) |