Manual 2to3 fixups.
[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 from __future__ import division, print_function, unicode_literals
20
21 import re
22 try:
23     from urllib.parse import urlparse
24 except ImportError:
25     from urlparse import urlparse
26
27 type_patterns = {
28     'checksums': re.compile(r"^snapshot-(.*)\.(\w+)sums$"),
29     '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+)?$"),
30     'snapshots': re.compile(r"^snapshot-(.*)\.(cumulus|lbs)$")
31 }
32
33 class NotFoundError(KeyError):
34     """Exception thrown when a file is not found in a repository."""
35
36     pass
37
38 class Store (object):
39     """Base class for all cumulus storage backends."""
40
41     def __new__ (cls, url, **kw):
42         """ Return the correct sub-class depending on url,
43         pass parsed url parameters to object
44         """
45         if cls != Store:
46             return super(Store, cls).__new__(cls, url, **kw)
47         (scheme, netloc, path, params, query, fragment) \
48             = urlparse(url)
49
50         try:
51             cumulus = __import__('cumulus.store.%s' % scheme, globals())
52             subcls = getattr (cumulus.store, scheme).Store
53             obj = super(Store, cls).__new__(subcls, url, **kw)
54             obj.scheme = scheme
55             obj.netloc = netloc
56             obj.path = path
57             obj.params = params
58             obj.query = query
59             obj.fragment = fragment
60             return obj
61         except ImportError:
62             raise NotImplementedError("Scheme %s not implemented" % scheme)
63
64     def list(self, path):
65         raise NotImplementedError
66
67     def get(self, path):
68         raise NotImplementedError
69
70     def put(self, path, fp):
71         raise NotImplementedError
72
73     def delete(self, path):
74         raise NotImplementedError
75
76     def stat(self, path):
77         raise NotImplementedError
78
79     def scan(self, path):
80         """Cache file information stored in this backend.
81
82         This might make subsequent list or stat calls more efficient, but this
83         function is intended purely as a performance optimization."""
84
85         pass
86
87     def close(self):
88         """Tear down the connection explicitly if needed
89
90         Currently needed for sftp to be able to end the program."""
91
92         pass
93
94     def __del__(self):
95         self.close()
96
97 def open(url):
98     return Store(url)