7c43b695b232bf4eb4e87dbfa27a26713f08c0bc
[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 open(url):
30     (scheme, netloc, path, params, query, fragment) \
31         = urlparse.urlparse(url)
32
33     if scheme == "file":
34         import cumulus.store.file
35         return cumulus.store.file.FileStore(path)
36     elif scheme == "s3":
37         import cumulus.store.s3
38         while path.startswith("/"): path = path[1:]
39         (bucket, path) = path.split("/", 1)
40         return cumulus.store.s3.S3Store(bucket, path)
41     else:
42         raise NotImplementedException