15 # |
15 # |
16 |
16 |
17 import logging |
17 import logging |
18 import xml.parsers.expat |
18 import xml.parsers.expat |
19 |
19 |
20 from cone.public import api, utils |
20 from cone.public.api import Storage |
|
21 from cone.public import utils |
21 from cone.storage import metadata |
22 from cone.storage import metadata |
22 |
23 |
23 class StorageBase(api.Storage): |
24 class StorageBase(Storage): |
24 """ |
25 """ |
25 A general base class for all storage type classes |
26 A general base class for all storage type classes |
26 """ |
27 """ |
27 METADATA_FILENAME = ".metadata" |
28 METADATA_FILENAME = ".metadata" |
28 |
29 |
29 def __init__(self,path): |
30 def __init__(self, path, mode): |
30 super(StorageBase, self).__init__(path) |
31 super(StorageBase, self).__init__(path, mode) |
31 self.meta = self.read_metadata() |
32 self.meta = self.read_metadata() |
|
33 |
32 |
34 |
33 def get_active_configuration(self): |
35 def get_active_configuration(self): |
34 """ |
36 """ |
35 Return the active configuration. |
37 Return the active configuration. |
36 If the storage holds only one configuration, it will be always active |
38 If the storage holds only one configuration, it will be always active |
37 """ |
39 """ |
38 root_confmls = self.list_resources("/") |
40 root_confmls = self.list_resources("/") |
39 root_confmls = utils.resourceref.filter_resources(root_confmls,"\.confml") |
41 root_confmls = utils.resourceref.filter_resources(root_confmls,"\.confml") |
40 if self.meta.get_root_file() == '' and len(root_confmls) == 1: |
42 if self.meta.get_root_file() == None and len(root_confmls) == 1: |
41 return root_confmls[0] |
43 return root_confmls[0] |
42 else: |
44 else: |
43 return self.meta.get_root_file() |
45 return self.meta.get_root_file() |
44 |
46 |
45 |
47 |
66 |
68 |
67 def write_metadata(self): |
69 def write_metadata(self): |
68 # Try to update the metadata, which might fail on ZipStorage |
70 # Try to update the metadata, which might fail on ZipStorage |
69 |
71 |
70 try: |
72 try: |
71 if self.get_mode(self.mode) != api.Storage.MODE_READ: |
73 if self.get_mode(self.mode) != Storage.MODE_READ: |
72 # update the active configuration |
74 # update the active configuration |
73 self.set_active_configuration(self.get_active_configuration()) |
75 self.set_active_configuration(self.get_active_configuration()) |
74 metares = self.open_resource(self.METADATA_FILENAME,"wb") |
76 metares = self.open_resource(self.METADATA_FILENAME,"wb") |
75 metadata.MetadataWriter().toresource(self.meta,metares) |
77 metadata.MetadataWriter().toresource(self.meta,metares) |
76 metares.close() |
78 metares.close() |