# HG changeset patch # User paulb # Date 1132067390 0 # Node ID cbd835482601cc8fa0261d11da2d1cf88912c5af # Parent e6e70e39bc6cb78f04411bdeaa0712bedf97a3f2 [project @ 2005-11-15 15:09:50 by paulb] Added the WebStack.Repositories package. Implemented a simple directory repository. diff -r e6e70e39bc6c -r cbd835482601 WebStack/Repositories/Directory.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/WebStack/Repositories/Directory.py Tue Nov 15 15:09:50 2005 +0000 @@ -0,0 +1,112 @@ +#!/usr/bin/env python + +""" +Directory repositories for WebStack. + +Copyright (C) 2005 Paul Boddie + +This library is free software; you can redistribute it and/or +modify it under the terms of the GNU Lesser General Public +License as published by the Free Software Foundation; either +version 2.1 of the License, or (at your option) any later version. + +This library 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 +Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with this library; if not, write to the Free Software +Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA +""" + +import os + +class DirectoryRepository: + + "A directory repository providing session-like access to files." + + def __init__(self, path, fsencoding=None): + + """ + Initialise the repository using the given 'path' to indicate the + location of the repository. If no such location exists in the filesystem + an attempt will be made to create the directory. + + The optional 'fsencoding' parameter can be used to assert a particular + character encoding used by the filesystem to represent filenames. By + default, the default encoding is detected (or Unicode objects are used + if appropriate). + """ + + if not os.path.exists(path): + os.mkdir(path) + self.path = path + self.fsencoding = fsencoding + + # Guess the filesystem encoding. + + if fsencoding is None: + if os.path.supports_unicode_filenames: + self.fsencoding = None + else: + import locale + self.fsencoding = locale.getdefaultlocale()[1] + + # Or override any guesses. + + else: + self.fsencoding = fsencoding + + def _convert_name(self, name): + if self.fsencoding: + return name.encode(self.fsencoding) + else: + return name + + def _convert_fsname(self, name): + if self.fsencoding: + return unicode(name, self.fsencoding) + else: + return name + + def keys(self): + return map(self._convert_fsname, os.listdir(self.path)) + + def full_path(self, key): + return os.path.join(self.path, self._convert_name(key)) + + # NOTE: Methods very similar to Helpers.Session.Wrapper. + + def items(self): + results = [] + for key in self.keys(): + results.append((key, self[key])) + return results + + def values(self): + results = [] + for key in self.keys(): + results.append(self[key]) + return results + + def __getitem__(self, key): + f = open(os.path.join(self.path, self._convert_name(key)), "rb") + s = "" + try: + s = f.read() + finally: + f.close() + return s + + def __delitem__(self, key): + os.remove(os.path.join(self.path, self._convert_name(key))) + + def __setitem__(self, key, value): + f = open(os.path.join(self.path, self._convert_name(key)), "wb") + try: + f.write(value) + finally: + f.close() + +# vim: tabstop=4 expandtab shiftwidth=4 diff -r e6e70e39bc6c -r cbd835482601 WebStack/Repositories/__init__.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/WebStack/Repositories/__init__.py Tue Nov 15 15:09:50 2005 +0000 @@ -0,0 +1,24 @@ +#!/usr/bin/env python + +""" +Repositories for WebStack, providing session-like interfaces to places where +information is stored. + +Copyright (C) 2005 Paul Boddie + +This library is free software; you can redistribute it and/or +modify it under the terms of the GNU Lesser General Public +License as published by the Free Software Foundation; either +version 2.1 of the License, or (at your option) any later version. + +This library 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 +Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with this library; if not, write to the Free Software +Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA +""" + +# vim: tabstop=4 expandtab shiftwidth=4