6b16fb65115f4b5900ba033a0f672f290cb2ab49
[cumulus.git] / python / cumulus / store / file.py
1 import os, sys, tempfile
2
3 import cumulus.store
4
5 type_patterns = cumulus.store.type_patterns
6
7 class FileStore(cumulus.store.Store):
8     def __init__(self, prefix):
9         while prefix.endswith("/") and prefix != "/": prefix = prefix[:-1]
10         self.prefix = prefix
11
12     def _get_path(self, type, name):
13         return "%s/%s" % (self.prefix, name)
14
15     def list(self, type):
16         files = os.listdir(self.prefix)
17         return (f for f in files if type_patterns[type].match(f))
18
19     def get(self, type, name):
20         k = self._get_path(type, name)
21         return open(k, 'rb')
22
23     def put(self, type, name, fp):
24         k = self._get_path(type, name)
25         out = open(k, 'wb')
26         buf = fp.read(4096)
27         while len(buf) > 0:
28             out.write(buf)
29             buf = fp.read(4096)
30
31     def delete(self, type, name):
32         k = self._get_path(type, name)
33         os.unlink(k)
34
35     def stat(self, type, name):
36         try:
37             stat = os.stat(self._get_path(type, name))
38             return {'size': stat.st_size}
39         except OSError:
40             raise cumulus.store.NotFoundError