1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/WebStack/Repositories/Directory.py Tue Nov 15 15:09:50 2005 +0000
1.3 @@ -0,0 +1,112 @@
1.4 +#!/usr/bin/env python
1.5 +
1.6 +"""
1.7 +Directory repositories for WebStack.
1.8 +
1.9 +Copyright (C) 2005 Paul Boddie <paul@boddie.org.uk>
1.10 +
1.11 +This library is free software; you can redistribute it and/or
1.12 +modify it under the terms of the GNU Lesser General Public
1.13 +License as published by the Free Software Foundation; either
1.14 +version 2.1 of the License, or (at your option) any later version.
1.15 +
1.16 +This library is distributed in the hope that it will be useful,
1.17 +but WITHOUT ANY WARRANTY; without even the implied warranty of
1.18 +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1.19 +Lesser General Public License for more details.
1.20 +
1.21 +You should have received a copy of the GNU Lesser General Public
1.22 +License along with this library; if not, write to the Free Software
1.23 +Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
1.24 +"""
1.25 +
1.26 +import os
1.27 +
1.28 +class DirectoryRepository:
1.29 +
1.30 + "A directory repository providing session-like access to files."
1.31 +
1.32 + def __init__(self, path, fsencoding=None):
1.33 +
1.34 + """
1.35 + Initialise the repository using the given 'path' to indicate the
1.36 + location of the repository. If no such location exists in the filesystem
1.37 + an attempt will be made to create the directory.
1.38 +
1.39 + The optional 'fsencoding' parameter can be used to assert a particular
1.40 + character encoding used by the filesystem to represent filenames. By
1.41 + default, the default encoding is detected (or Unicode objects are used
1.42 + if appropriate).
1.43 + """
1.44 +
1.45 + if not os.path.exists(path):
1.46 + os.mkdir(path)
1.47 + self.path = path
1.48 + self.fsencoding = fsencoding
1.49 +
1.50 + # Guess the filesystem encoding.
1.51 +
1.52 + if fsencoding is None:
1.53 + if os.path.supports_unicode_filenames:
1.54 + self.fsencoding = None
1.55 + else:
1.56 + import locale
1.57 + self.fsencoding = locale.getdefaultlocale()[1]
1.58 +
1.59 + # Or override any guesses.
1.60 +
1.61 + else:
1.62 + self.fsencoding = fsencoding
1.63 +
1.64 + def _convert_name(self, name):
1.65 + if self.fsencoding:
1.66 + return name.encode(self.fsencoding)
1.67 + else:
1.68 + return name
1.69 +
1.70 + def _convert_fsname(self, name):
1.71 + if self.fsencoding:
1.72 + return unicode(name, self.fsencoding)
1.73 + else:
1.74 + return name
1.75 +
1.76 + def keys(self):
1.77 + return map(self._convert_fsname, os.listdir(self.path))
1.78 +
1.79 + def full_path(self, key):
1.80 + return os.path.join(self.path, self._convert_name(key))
1.81 +
1.82 + # NOTE: Methods very similar to Helpers.Session.Wrapper.
1.83 +
1.84 + def items(self):
1.85 + results = []
1.86 + for key in self.keys():
1.87 + results.append((key, self[key]))
1.88 + return results
1.89 +
1.90 + def values(self):
1.91 + results = []
1.92 + for key in self.keys():
1.93 + results.append(self[key])
1.94 + return results
1.95 +
1.96 + def __getitem__(self, key):
1.97 + f = open(os.path.join(self.path, self._convert_name(key)), "rb")
1.98 + s = ""
1.99 + try:
1.100 + s = f.read()
1.101 + finally:
1.102 + f.close()
1.103 + return s
1.104 +
1.105 + def __delitem__(self, key):
1.106 + os.remove(os.path.join(self.path, self._convert_name(key)))
1.107 +
1.108 + def __setitem__(self, key, value):
1.109 + f = open(os.path.join(self.path, self._convert_name(key)), "wb")
1.110 + try:
1.111 + f.write(value)
1.112 + finally:
1.113 + f.close()
1.114 +
1.115 +# vim: tabstop=4 expandtab shiftwidth=4
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
2.2 +++ b/WebStack/Repositories/__init__.py Tue Nov 15 15:09:50 2005 +0000
2.3 @@ -0,0 +1,24 @@
2.4 +#!/usr/bin/env python
2.5 +
2.6 +"""
2.7 +Repositories for WebStack, providing session-like interfaces to places where
2.8 +information is stored.
2.9 +
2.10 +Copyright (C) 2005 Paul Boddie <paul@boddie.org.uk>
2.11 +
2.12 +This library is free software; you can redistribute it and/or
2.13 +modify it under the terms of the GNU Lesser General Public
2.14 +License as published by the Free Software Foundation; either
2.15 +version 2.1 of the License, or (at your option) any later version.
2.16 +
2.17 +This library is distributed in the hope that it will be useful,
2.18 +but WITHOUT ANY WARRANTY; without even the implied warranty of
2.19 +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
2.20 +Lesser General Public License for more details.
2.21 +
2.22 +You should have received a copy of the GNU Lesser General Public
2.23 +License along with this library; if not, write to the Free Software
2.24 +Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
2.25 +"""
2.26 +
2.27 +# vim: tabstop=4 expandtab shiftwidth=4