35de0ee153ed57187f83a7d7ebf48b7ebee5f51a
[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:
15     """Base class for all cumulus storage backends."""
16
17     def list(self, type):
18         raise NotImplementedException
19
20     def get(self, type, name):
21         raise NotImplementedException
22
23     def put(self, type, name, fp):
24         raise NotImplementedException
25
26     def delete(self, type, name):
27         raise NotImplementedException
28
29     def stat(self, type, name):
30         raise NotImplementedException
31
32     def scan(self):
33         """Cache file information stored in this backend.
34
35         This might make subsequent list or stat calls more efficient, but this
36         function is intended purely as a performance optimization."""
37
38         pass
39
40 def open(url):
41     (scheme, netloc, path, params, query, fragment) \
42         = urlparse.urlparse(url)
43
44     if scheme == "file":
45         import cumulus.store.file
46         return cumulus.store.file.FileStore(path)
47     elif scheme == "s3":
48         import cumulus.store.s3
49         while path.startswith("/"): path = path[1:]
50         (bucket, path) = path.split("/", 1)
51         return cumulus.store.s3.S3Store(bucket, path)
52     else:
53         raise NotImplementedException