paulb@498 | 1 | #!/usr/bin/env python |
paulb@498 | 2 | |
paulb@498 | 3 | """ |
paulb@498 | 4 | Directory repositories for WebStack. |
paulb@498 | 5 | |
paulb@498 | 6 | Copyright (C) 2005 Paul Boddie <paul@boddie.org.uk> |
paulb@498 | 7 | |
paulb@498 | 8 | This library is free software; you can redistribute it and/or |
paulb@498 | 9 | modify it under the terms of the GNU Lesser General Public |
paulb@498 | 10 | License as published by the Free Software Foundation; either |
paulb@498 | 11 | version 2.1 of the License, or (at your option) any later version. |
paulb@498 | 12 | |
paulb@498 | 13 | This library is distributed in the hope that it will be useful, |
paulb@498 | 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
paulb@498 | 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
paulb@498 | 16 | Lesser General Public License for more details. |
paulb@498 | 17 | |
paulb@498 | 18 | You should have received a copy of the GNU Lesser General Public |
paulb@498 | 19 | License along with this library; if not, write to the Free Software |
paulb@498 | 20 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA |
paulb@498 | 21 | """ |
paulb@498 | 22 | |
paulb@498 | 23 | import os |
paulb@498 | 24 | |
paulb@498 | 25 | class DirectoryRepository: |
paulb@498 | 26 | |
paulb@498 | 27 | "A directory repository providing session-like access to files." |
paulb@498 | 28 | |
paulb@498 | 29 | def __init__(self, path, fsencoding=None): |
paulb@498 | 30 | |
paulb@498 | 31 | """ |
paulb@498 | 32 | Initialise the repository using the given 'path' to indicate the |
paulb@498 | 33 | location of the repository. If no such location exists in the filesystem |
paulb@498 | 34 | an attempt will be made to create the directory. |
paulb@498 | 35 | |
paulb@498 | 36 | The optional 'fsencoding' parameter can be used to assert a particular |
paulb@498 | 37 | character encoding used by the filesystem to represent filenames. By |
paulb@498 | 38 | default, the default encoding is detected (or Unicode objects are used |
paulb@498 | 39 | if appropriate). |
paulb@498 | 40 | """ |
paulb@498 | 41 | |
paulb@498 | 42 | if not os.path.exists(path): |
paulb@498 | 43 | os.mkdir(path) |
paulb@498 | 44 | self.path = path |
paulb@498 | 45 | self.fsencoding = fsencoding |
paulb@498 | 46 | |
paulb@498 | 47 | # Guess the filesystem encoding. |
paulb@498 | 48 | |
paulb@498 | 49 | if fsencoding is None: |
paulb@498 | 50 | if os.path.supports_unicode_filenames: |
paulb@498 | 51 | self.fsencoding = None |
paulb@498 | 52 | else: |
paulb@498 | 53 | import locale |
paulb@498 | 54 | self.fsencoding = locale.getdefaultlocale()[1] |
paulb@498 | 55 | |
paulb@498 | 56 | # Or override any guesses. |
paulb@498 | 57 | |
paulb@498 | 58 | else: |
paulb@498 | 59 | self.fsencoding = fsencoding |
paulb@498 | 60 | |
paulb@498 | 61 | def _convert_name(self, name): |
paulb@498 | 62 | if self.fsencoding: |
paulb@498 | 63 | return name.encode(self.fsencoding) |
paulb@498 | 64 | else: |
paulb@498 | 65 | return name |
paulb@498 | 66 | |
paulb@498 | 67 | def _convert_fsname(self, name): |
paulb@498 | 68 | if self.fsencoding: |
paulb@498 | 69 | return unicode(name, self.fsencoding) |
paulb@498 | 70 | else: |
paulb@498 | 71 | return name |
paulb@498 | 72 | |
paulb@498 | 73 | def keys(self): |
paulb@498 | 74 | return map(self._convert_fsname, os.listdir(self.path)) |
paulb@498 | 75 | |
paulb@498 | 76 | def full_path(self, key): |
paulb@498 | 77 | return os.path.join(self.path, self._convert_name(key)) |
paulb@498 | 78 | |
paulb@498 | 79 | # NOTE: Methods very similar to Helpers.Session.Wrapper. |
paulb@498 | 80 | |
paulb@498 | 81 | def items(self): |
paulb@498 | 82 | results = [] |
paulb@498 | 83 | for key in self.keys(): |
paulb@498 | 84 | results.append((key, self[key])) |
paulb@498 | 85 | return results |
paulb@498 | 86 | |
paulb@498 | 87 | def values(self): |
paulb@498 | 88 | results = [] |
paulb@498 | 89 | for key in self.keys(): |
paulb@498 | 90 | results.append(self[key]) |
paulb@498 | 91 | return results |
paulb@498 | 92 | |
paulb@498 | 93 | def __getitem__(self, key): |
paulb@498 | 94 | f = open(os.path.join(self.path, self._convert_name(key)), "rb") |
paulb@498 | 95 | s = "" |
paulb@498 | 96 | try: |
paulb@498 | 97 | s = f.read() |
paulb@498 | 98 | finally: |
paulb@498 | 99 | f.close() |
paulb@498 | 100 | return s |
paulb@498 | 101 | |
paulb@498 | 102 | def __delitem__(self, key): |
paulb@498 | 103 | os.remove(os.path.join(self.path, self._convert_name(key))) |
paulb@498 | 104 | |
paulb@498 | 105 | def __setitem__(self, key, value): |
paulb@498 | 106 | f = open(os.path.join(self.path, self._convert_name(key)), "wb") |
paulb@498 | 107 | try: |
paulb@498 | 108 | f.write(value) |
paulb@498 | 109 | finally: |
paulb@498 | 110 | f.close() |
paulb@498 | 111 | |
paulb@498 | 112 | # vim: tabstop=4 expandtab shiftwidth=4 |