~cr3/launchpad-results/trunk

« back to all changes in this revision

Viewing changes to lib/lpx/disk/store.py

  • Committer: Marc Tardif
  • Date: 2011-04-23 21:55:09 UTC
  • Revision ID: marc.tardif@canonical.com-20110423215509-lr5lve64vl0fin7b
Added disk package, attachment modules and corresponding infrastructure.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright 2010 Canonical Ltd.  This software is licensed under the
 
2
# GNU Affero General Public License version 3 (see the file LICENSE).
 
3
 
 
4
__metaclass__ = type
 
5
 
 
6
__all__ = [
 
7
    "Store",
 
8
    ]
 
9
 
 
10
from lpx.disk.directory import Directory
 
11
from lpx.disk.exceptions import ClientError
 
12
from lpx.disk.validators import valid_name
 
13
 
 
14
 
 
15
class Store:
 
16
    """The Disk Store.
 
17
 
 
18
    This is the highest-level interface to a backend in order to manage
 
19
    directories.
 
20
 
 
21
    Note that Store objects are not threadsafe. You should create one
 
22
    Store per thread in your application, passing them the same backend
 
23
    object.
 
24
    """
 
25
 
 
26
    directory_factory = Directory
 
27
 
 
28
    def __init__(self, backend):
 
29
        self._backend = backend
 
30
        self._connection = backend.connect()
 
31
 
 
32
    def __getitem__(self, name):
 
33
        """Get a Directory from this Store."""
 
34
        if not valid_name(name):
 
35
            raise ClientError("Invalid directory name: '%s'" % name)
 
36
 
 
37
        return self.directory_factory(self, name)
 
38
 
 
39
    def __delitem__(self, name):
 
40
        """Delete a Directory from this Store."""
 
41
        if not valid_name(name):
 
42
            raise ClientError("Invalid directory name: '%s'" % name)
 
43
 
 
44
        return self._connection.deleteDirectory(name)
 
45
 
 
46
    def getBackend(self):
 
47
        """Return this Store's Backend object."""
 
48
        return self._backend