diff options
author | Lars Wirzenius <liw@liw.fi> | 2015-04-12 17:58:25 +0300 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2015-04-12 17:58:25 +0300 |
commit | f6b60eb6c9fd9f8475b11314594436c5e78a1e15 (patch) | |
tree | c40fd6474a17f79c80b82ab5988186dd994c8fec | |
parent | 03a90f0a87b584e25dd6483225f59a1e1fb31679 (diff) | |
download | obnam-f6b60eb6c9fd9f8475b11314594436c5e78a1e15.tar.gz |
Add RepositoryFS class
-rw-r--r-- | obnamlib/__init__.py | 1 | ||||
-rw-r--r-- | obnamlib/repo_fs.py | 85 | ||||
-rw-r--r-- | without-tests | 1 |
3 files changed, 87 insertions, 0 deletions
diff --git a/obnamlib/__init__.py b/obnamlib/__init__.py index a7de08dd..41d7c39a 100644 --- a/obnamlib/__init__.py +++ b/obnamlib/__init__.py @@ -97,6 +97,7 @@ from vfs import ( NEW_FILE_MODE) from vfs_local import LocalFS from fsck_work_item import WorkItem +from repo_fs import RepositoryFS from lockmgr import LockManager from forget_policy import ForgetPolicy from app import App, ObnamIOError, ObnamSystemError diff --git a/obnamlib/repo_fs.py b/obnamlib/repo_fs.py new file mode 100644 index 00000000..053496c0 --- /dev/null +++ b/obnamlib/repo_fs.py @@ -0,0 +1,85 @@ +# 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 os + +import tracing + + +class RepositoryFS(object): + + '''A wrapper around a VFS object, with calls to hooks. + + This is exactly like a full VFS implementation, but it only wraps + another instance and provides read/write hooks. The hooks are + intended for filtering data read from or written to the + repository, which is then used to implement things like + compression and encryption. + + FIXME: Some day this might offer only the subset of the full VFS + that is necessary for repository access, to allow easier + implementation of new repository storage methods. + + ''' + + def __init__(self, repo, fs, hooks): + self.repo = repo + self.fs = fs + self.hooks = hooks + + def __getattr__(self, name): + return getattr(self.fs, name) + + def _get_toplevel(self, filename): + parts = filename.split(os.sep) + if len(parts) >= 1: + return parts[0] + else: # pragma: no cover + raise ToplevelIsFileError(filename=filename) + + def cat(self, filename, runfilters=True): + data = self.fs.cat(filename) + if not runfilters: # pragma: no cover + return data + toplevel = self._get_toplevel(filename) + return self.hooks.filter_read('repository-data', data, + repo=self.repo, toplevel=toplevel) + + def lock(self, filename, data): + self.fs.lock(filename, data) + + def create_and_init_toplevel(self, filename): + tracing.trace('filename=%s', filename) + toplevel = self._get_toplevel(filename) + if not self.fs.exists(toplevel): + self.fs.mkdir(toplevel) + self.hooks.call('repository-toplevel-init', self.repo, toplevel) + + def write_file(self, filename, data, runfilters=True): + toplevel = self._get_toplevel(filename) + if runfilters: + data = self.hooks.filter_write('repository-data', data, + repo=self.repo, toplevel=toplevel) + self.fs.write_file(filename, data) + + def overwrite_file(self, filename, data, runfilters=True): + toplevel = self._get_toplevel(filename) + if runfilters: + data = self.hooks.filter_write('repository-data', data, + repo=self.repo, toplevel=toplevel) + self.fs.overwrite_file(filename, data) diff --git a/without-tests b/without-tests index 9f171a82..98c28d66 100644 --- a/without-tests +++ b/without-tests @@ -5,6 +5,7 @@ obnamlib/humanise.py obnamlib/fsck_work_item.py obnamlib/repo_interface.py obnamlib/vfs.py +obnamlib/repo_fs.py obnamlib/fmt_6/__init__.py obnamlib/fmt_6/repo_tree.py obnamlib/plugins/__init__.py |