Update copyright notices to use a central AUTHORS file.
[cumulus.git] / python / cumulus / store / __init__.py
1 # Cumulus: Efficient Filesystem Backup to the Cloud
2 # Copyright (C) 2008-2010 The Cumulus Developers
3 # See the AUTHORS file for a list of contributors.
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License along
16 # with this program; if not, write to the Free Software Foundation, Inc.,
17 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18
19 import exceptions, re, urlparse
20
21 type_patterns = {
22     'checksums': re.compile(r"^snapshot-(.*)\.(\w+)sums$"),
23     'segments': re.compile(r"^([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})(\.\S+)?$"),
24     'snapshots': re.compile(r"^snapshot-(.*)\.lbs$")
25 }
26
27 class NotFoundError(exceptions.KeyError):
28     """Exception thrown when a file is not found in a repository."""
29
30     pass
31
32 class Store (object):
33     """Base class for all cumulus storage backends."""
34
35     def __new__ (cls, url, **kw):
36         """ Return the correct sub-class depending on url,
37         pass parsed url parameters to object
38         """
39         if cls != Store:
40             return super(Store, cls).__new__(cls, url, **kw)
41         (scheme, netloc, path, params, query, fragment) \
42             = urlparse.urlparse(url)
43
44         try:
45             cumulus = __import__('cumulus.store.%s' % scheme, globals())
46             subcls = getattr (cumulus.store, scheme).Store
47             obj = super(Store, cls).__new__(subcls, url, **kw)
48             obj.scheme = scheme
49             obj.netloc = netloc
50             obj.path = path
51             obj.params = params
52             obj.query = query
53             obj.fragment = fragment
54             return obj
55         except ImportError:
56             raise NotImplementedError, "Scheme %s not implemented" % scheme
57
58     def list(self, type):
59         raise NotImplementedError
60
61     def get(self, type, name):
62         raise NotImplementedError
63
64     def put(self, type, name, fp):
65         raise NotImplementedError
66
67     def delete(self, type, name):
68         raise NotImplementedError
69
70     def stat(self, type, name):
71         raise NotImplementedError
72
73     def scan(self):
74         """Cache file information stored in this backend.
75
76         This might make subsequent list or stat calls more efficient, but this
77         function is intended purely as a performance optimization."""
78
79         pass
80
81     def close(self):
82         """Tear down the connection explicitly if needed
83
84         Currently needed for sftp to be able to end the program."""
85
86         pass
87
88     def __del__(self):
89         self.close()
90
91 def open(url):
92     return Store(url)