Implement FTP backend and other code cleanups.
[cumulus.git] / python / cumulus / store / __init__.py
1 import exceptions, re, urlparse
2
3 type_patterns = {
4     'checksums': re.compile(r"^snapshot-(.*)\.(\w+)sums$"),
5     '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+)?$"),
6     'snapshots': re.compile(r"^snapshot-(.*)\.lbs$")
7 }
8
9 class NotFoundError(exceptions.KeyError):
10     """Exception thrown when a file is not found in a repository."""
11
12     pass
13
14 class Store (object):
15     """Base class for all cumulus storage backends."""
16
17     def __new__ (cls, url, **kw):
18         """ Return the correct sub-class depending on url,
19         pass parsed url parameters to object
20         """
21         if cls != Store:
22             return super(Store, cls).__new__(cls, url, **kw)
23         (scheme, netloc, path, params, query, fragment) \
24             = urlparse.urlparse(url)
25
26         try:
27             cumulus = __import__('cumulus.store.%s' % scheme, globals())
28             subcls = getattr (cumulus.store, scheme).Store
29             obj = super(Store, cls).__new__(subcls, url, **kw)
30             obj.scheme = scheme
31             obj.netloc = netloc
32             obj.path = path
33             obj.params = params
34             obj.query = query
35             obj.fragment = fragment
36             return obj
37         except ImportError:
38             raise NotImplementedError, "Scheme %s not implemented" % scheme
39
40     def list(self, type):
41         raise NotImplementedError
42
43     def get(self, type, name):
44         raise NotImplementedError
45
46     def put(self, type, name, fp):
47         raise NotImplementedError
48
49     def delete(self, type, name):
50         raise NotImplementedError
51
52     def stat(self, type, name):
53         raise NotImplementedError
54
55     def scan(self):
56         """Cache file information stored in this backend.
57
58         This might make subsequent list or stat calls more efficient, but this
59         function is intended purely as a performance optimization."""
60
61         pass
62
63 def open(url):
64     return Store(url)