Implement FTP backend and other code cleanups.
[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, url, **kw):
9         # if constructor isn't called via factory interpret url as filename
10         if not hasattr (self, 'path'):
11             self.path = url
12         self.prefix = self.path.rstrip("/")
13
14     def _get_path(self, type, name):
15         return "%s/%s" % (self.prefix, name)
16
17     def list(self, type):
18         files = os.listdir(self.prefix)
19         return (f for f in files if type_patterns[type].match(f))
20
21     def get(self, type, name):
22         k = self._get_path(type, name)
23         return open(k, 'rb')
24
25     def put(self, type, name, fp):
26         k = self._get_path(type, name)
27         out = open(k, 'wb')
28         buf = fp.read(4096)
29         while len(buf) > 0:
30             out.write(buf)
31             buf = fp.read(4096)
32
33     def delete(self, type, name):
34         k = self._get_path(type, name)
35         os.unlink(k)
36
37     def stat(self, type, name):
38         try:
39             stat = os.stat(self._get_path(type, name))
40             return {'size': stat.st_size}
41         except OSError:
42             raise cumulus.store.NotFoundError, (type, name)
43
44 Store = FileStore