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@520 | 29 | new_filename = "__new__" |
paulb@520 | 30 | |
paulb@511 | 31 | def __init__(self, path, fsencoding=None, delay=1): |
paulb@498 | 32 | |
paulb@498 | 33 | """ |
paulb@498 | 34 | Initialise the repository using the given 'path' to indicate the |
paulb@498 | 35 | location of the repository. If no such location exists in the filesystem |
paulb@498 | 36 | an attempt will be made to create the directory. |
paulb@498 | 37 | |
paulb@498 | 38 | The optional 'fsencoding' parameter can be used to assert a particular |
paulb@498 | 39 | character encoding used by the filesystem to represent filenames. By |
paulb@498 | 40 | default, the default encoding is detected (or Unicode objects are used |
paulb@498 | 41 | if appropriate). |
paulb@511 | 42 | |
paulb@511 | 43 | The optional 'delay' argument specifies the time in seconds between each |
paulb@511 | 44 | poll of an opened repository file when that file is found to be locked |
paulb@511 | 45 | for editing. |
paulb@498 | 46 | """ |
paulb@498 | 47 | |
paulb@520 | 48 | # Convert the path to an absolute path. |
paulb@520 | 49 | |
paulb@520 | 50 | self.path = os.path.abspath(path) |
paulb@520 | 51 | self.fsencoding = fsencoding |
paulb@520 | 52 | self.delay = delay |
paulb@520 | 53 | |
paulb@520 | 54 | # Create a directory and initialise it with a special file. |
paulb@520 | 55 | |
paulb@498 | 56 | if not os.path.exists(path): |
paulb@498 | 57 | os.mkdir(path) |
paulb@520 | 58 | f = open(self.full_path(self.new_filename), "wb") |
paulb@520 | 59 | f.close() |
paulb@498 | 60 | |
paulb@498 | 61 | # Guess the filesystem encoding. |
paulb@498 | 62 | |
paulb@498 | 63 | if fsencoding is None: |
paulb@498 | 64 | if os.path.supports_unicode_filenames: |
paulb@498 | 65 | self.fsencoding = None |
paulb@498 | 66 | else: |
paulb@498 | 67 | import locale |
paulb@498 | 68 | self.fsencoding = locale.getdefaultlocale()[1] |
paulb@498 | 69 | |
paulb@498 | 70 | # Or override any guesses. |
paulb@498 | 71 | |
paulb@498 | 72 | else: |
paulb@498 | 73 | self.fsencoding = fsencoding |
paulb@498 | 74 | |
paulb@498 | 75 | def _convert_name(self, name): |
paulb@520 | 76 | |
paulb@520 | 77 | "Convert the given 'name' to a plain string in the filesystem encoding." |
paulb@520 | 78 | |
paulb@498 | 79 | if self.fsencoding: |
paulb@498 | 80 | return name.encode(self.fsencoding) |
paulb@498 | 81 | else: |
paulb@498 | 82 | return name |
paulb@498 | 83 | |
paulb@498 | 84 | def _convert_fsname(self, name): |
paulb@520 | 85 | |
paulb@520 | 86 | """ |
paulb@520 | 87 | Convert the given 'name' as a plain string in the filesystem encoding to |
paulb@520 | 88 | a Unicode object. |
paulb@520 | 89 | """ |
paulb@520 | 90 | |
paulb@498 | 91 | if self.fsencoding: |
paulb@498 | 92 | return unicode(name, self.fsencoding) |
paulb@498 | 93 | else: |
paulb@498 | 94 | return name |
paulb@498 | 95 | |
paulb@498 | 96 | def keys(self): |
paulb@520 | 97 | |
paulb@520 | 98 | "Return the names of the files stored in the repository." |
paulb@520 | 99 | |
paulb@511 | 100 | # NOTE: Special names converted using a simple rule. |
paulb@511 | 101 | l = [] |
paulb@511 | 102 | for name in os.listdir(self.path): |
paulb@511 | 103 | if name.endswith(".edit"): |
paulb@511 | 104 | l.append(name[:-5]) |
paulb@523 | 105 | elif name == self.new_filename: |
paulb@523 | 106 | pass |
paulb@511 | 107 | else: |
paulb@511 | 108 | l.append(name) |
paulb@511 | 109 | return map(self._convert_fsname, l) |
paulb@498 | 110 | |
paulb@520 | 111 | def full_path(self, key, edit=0): |
paulb@520 | 112 | |
paulb@520 | 113 | """ |
paulb@520 | 114 | Return the full path to the 'key' in the filesystem. If the optional |
paulb@520 | 115 | 'edit' parameter is set to a true value, the returned path will refer to |
paulb@520 | 116 | the editable version of the file. |
paulb@520 | 117 | """ |
paulb@520 | 118 | |
paulb@520 | 119 | # NOTE: Special names converted using a simple rule. |
paulb@513 | 120 | path = os.path.abspath(os.path.join(self.path, self._convert_name(key))) |
paulb@520 | 121 | if edit: |
paulb@520 | 122 | path = path + ".edit" |
paulb@513 | 123 | if not path.startswith(self.path): |
paulb@513 | 124 | raise ValueError, key |
paulb@513 | 125 | else: |
paulb@513 | 126 | return path |
paulb@498 | 127 | |
paulb@520 | 128 | def edit_path(self, key): |
paulb@520 | 129 | |
paulb@520 | 130 | """ |
paulb@520 | 131 | Return the full path to the 'key' in the filesystem provided that the |
paulb@520 | 132 | file associated with the 'key' is locked for editing. |
paulb@520 | 133 | """ |
paulb@520 | 134 | |
paulb@520 | 135 | return self.full_path(key, edit=1) |
paulb@520 | 136 | |
paulb@503 | 137 | def has_key(self, key): |
paulb@520 | 138 | |
paulb@520 | 139 | """ |
paulb@520 | 140 | Return whether a file with the name specified by 'key is stored in the |
paulb@520 | 141 | repository. |
paulb@520 | 142 | """ |
paulb@520 | 143 | |
paulb@503 | 144 | return key in self.keys() |
paulb@503 | 145 | |
paulb@498 | 146 | # NOTE: Methods very similar to Helpers.Session.Wrapper. |
paulb@498 | 147 | |
paulb@498 | 148 | def items(self): |
paulb@520 | 149 | |
paulb@520 | 150 | """ |
paulb@520 | 151 | Return a list of (name, value) tuples for the files stored in the |
paulb@520 | 152 | repository. |
paulb@520 | 153 | """ |
paulb@520 | 154 | |
paulb@498 | 155 | results = [] |
paulb@498 | 156 | for key in self.keys(): |
paulb@498 | 157 | results.append((key, self[key])) |
paulb@498 | 158 | return results |
paulb@498 | 159 | |
paulb@498 | 160 | def values(self): |
paulb@520 | 161 | |
paulb@520 | 162 | "Return the contents of the files stored in the repository." |
paulb@520 | 163 | |
paulb@498 | 164 | results = [] |
paulb@498 | 165 | for key in self.keys(): |
paulb@498 | 166 | results.append(self[key]) |
paulb@498 | 167 | return results |
paulb@498 | 168 | |
paulb@520 | 169 | def lock(self, key, create=0, opener=None): |
paulb@513 | 170 | |
paulb@513 | 171 | """ |
paulb@513 | 172 | Lock the file associated with the given 'key'. If the optional 'create' |
paulb@513 | 173 | parameter is set to a true value (unlike the default), the file will be |
paulb@513 | 174 | created if it did not already exist; otherwise, a KeyError will be |
paulb@513 | 175 | raised. |
paulb@513 | 176 | |
paulb@520 | 177 | If the optional 'opener' parameter is specified, it will be used to |
paulb@520 | 178 | create any new file in the case where 'create' is set to a true value; |
paulb@520 | 179 | otherwise, the standard 'open' function will be used to create the file. |
paulb@520 | 180 | |
paulb@520 | 181 | Return the full path to the editable file. |
paulb@513 | 182 | """ |
paulb@513 | 183 | |
paulb@520 | 184 | path = self.full_path(key) |
paulb@520 | 185 | edit_path = self.edit_path(key) |
paulb@520 | 186 | |
paulb@520 | 187 | # Attempt to lock the file by renaming it. |
paulb@520 | 188 | # NOTE: This assumes that renaming is an atomic operation. |
paulb@520 | 189 | |
paulb@520 | 190 | if os.path.exists(path) or os.path.exists(edit_path): |
paulb@511 | 191 | while 1: |
paulb@511 | 192 | try: |
paulb@520 | 193 | os.rename(path, edit_path) |
paulb@511 | 194 | except OSError: |
paulb@511 | 195 | time.sleep(self.delay) |
paulb@511 | 196 | else: |
paulb@511 | 197 | break |
paulb@520 | 198 | |
paulb@520 | 199 | # Where a file does not exist, attempt to create a new file. |
paulb@520 | 200 | # Since file creation is probably not atomic, we use the renaming of a |
paulb@520 | 201 | # special file in an attempt to impose some kind of atomic "bottleneck". |
paulb@520 | 202 | |
paulb@513 | 203 | elif create: |
paulb@523 | 204 | |
paulb@523 | 205 | # NOTE: Avoid failure case where no __new__ file exists for some |
paulb@523 | 206 | # NOTE: reason. |
paulb@523 | 207 | |
paulb@523 | 208 | try: |
paulb@523 | 209 | self.lock(self.new_filename) |
paulb@523 | 210 | except KeyError: |
paulb@523 | 211 | f = open(self.edit_path(self.new_filename), "wb") |
paulb@523 | 212 | f.close() |
paulb@523 | 213 | |
paulb@520 | 214 | try: |
paulb@520 | 215 | if opener is None: |
paulb@520 | 216 | f = open(edit_path, "wb") |
paulb@520 | 217 | f.close() |
paulb@520 | 218 | else: |
paulb@520 | 219 | f = opener(edit_path) |
paulb@520 | 220 | f.close() |
paulb@520 | 221 | finally: |
paulb@520 | 222 | self.unlock(self.new_filename) |
paulb@523 | 223 | |
paulb@523 | 224 | # Where no creation is requested, raise an exception. |
paulb@523 | 225 | |
paulb@511 | 226 | else: |
paulb@513 | 227 | raise KeyError, key |
paulb@520 | 228 | |
paulb@520 | 229 | return edit_path |
paulb@513 | 230 | |
paulb@513 | 231 | def unlock(self, key): |
paulb@513 | 232 | |
paulb@513 | 233 | """ |
paulb@513 | 234 | Unlock the file associated with the given 'key'. |
paulb@513 | 235 | |
paulb@513 | 236 | Important note: this method should be used in a finally clause in order |
paulb@513 | 237 | to avoid files being locked and never being unlocked by the same process |
paulb@513 | 238 | because an unhandled exception was raised. |
paulb@513 | 239 | """ |
paulb@513 | 240 | |
paulb@520 | 241 | path = self.full_path(key) |
paulb@520 | 242 | edit_path = self.edit_path(key) |
paulb@520 | 243 | os.rename(edit_path, path) |
paulb@498 | 244 | |
paulb@498 | 245 | def __delitem__(self, key): |
paulb@520 | 246 | |
paulb@520 | 247 | "Remove the file associated with the given 'key'." |
paulb@520 | 248 | |
paulb@520 | 249 | path = self.full_path(key) |
paulb@520 | 250 | edit_path = self.edit_path(key) |
paulb@520 | 251 | if os.path.exists(path) or os.path.exists(edit_path): |
paulb@511 | 252 | while 1: |
paulb@511 | 253 | try: |
paulb@520 | 254 | os.remove(path) |
paulb@511 | 255 | except OSError: |
paulb@511 | 256 | time.sleep(self.delay) |
paulb@511 | 257 | else: |
paulb@511 | 258 | break |
paulb@511 | 259 | else: |
paulb@511 | 260 | raise KeyError, key |
paulb@511 | 261 | |
paulb@511 | 262 | def __getitem__(self, key): |
paulb@520 | 263 | |
paulb@520 | 264 | "Return the contents of the file associated with the given 'key'." |
paulb@520 | 265 | |
paulb@520 | 266 | edit_path = self.lock(key, create=0) |
paulb@511 | 267 | try: |
paulb@520 | 268 | f = open(edit_path, "rb") |
paulb@511 | 269 | s = "" |
paulb@511 | 270 | try: |
paulb@511 | 271 | s = f.read() |
paulb@511 | 272 | finally: |
paulb@511 | 273 | f.close() |
paulb@511 | 274 | finally: |
paulb@513 | 275 | self.unlock(key) |
paulb@511 | 276 | |
paulb@511 | 277 | return s |
paulb@498 | 278 | |
paulb@498 | 279 | def __setitem__(self, key, value): |
paulb@520 | 280 | |
paulb@520 | 281 | """ |
paulb@520 | 282 | Set the contents of the file associated with the given 'key' using the |
paulb@520 | 283 | given 'value'. |
paulb@520 | 284 | """ |
paulb@520 | 285 | |
paulb@520 | 286 | edit_path = self.lock(key, create=1) |
paulb@498 | 287 | try: |
paulb@520 | 288 | f = open(edit_path, "wb") |
paulb@511 | 289 | try: |
paulb@511 | 290 | f.write(value) |
paulb@511 | 291 | finally: |
paulb@511 | 292 | f.close() |
paulb@498 | 293 | finally: |
paulb@513 | 294 | self.unlock(key) |
paulb@498 | 295 | |
paulb@498 | 296 | # vim: tabstop=4 expandtab shiftwidth=4 |