|
1 # |
|
2 # Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 # All rights reserved. |
|
4 # This component and the accompanying materials are made available |
|
5 # under the terms of "Eclipse Public License v1.0" |
|
6 # which accompanies this distribution, and is available |
|
7 # at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 # |
|
9 # Initial Contributors: |
|
10 # Nokia Corporation - initial contribution. |
|
11 # |
|
12 # Contributors: |
|
13 # |
|
14 # Description: |
|
15 # |
|
16 |
|
17 import logging |
|
18 import xml.parsers.expat |
|
19 |
|
20 from cone.public import api, utils |
|
21 from cone.storage import metadata |
|
22 |
|
23 class StorageBase(api.Storage): |
|
24 """ |
|
25 A general base class for all storage type classes |
|
26 """ |
|
27 METADATA_FILENAME = ".metadata" |
|
28 |
|
29 def __init__(self,path): |
|
30 super(StorageBase, self).__init__(path) |
|
31 self.meta = self.read_metadata() |
|
32 |
|
33 def get_active_configuration(self): |
|
34 """ |
|
35 Return the active configuration. |
|
36 If the storage holds only one configuration, it will be always active |
|
37 """ |
|
38 root_confmls = self.list_resources("/") |
|
39 root_confmls = utils.resourceref.filter_resources(root_confmls,"\.confml") |
|
40 if self.meta.get_root_file() == '' and len(root_confmls) == 1: |
|
41 return root_confmls[0] |
|
42 else: |
|
43 return self.meta.get_root_file() |
|
44 |
|
45 |
|
46 def set_active_configuration(self, ref): |
|
47 self.meta.set_root_file(ref) |
|
48 |
|
49 def read_metadata(self): |
|
50 meta = None |
|
51 if not self.is_resource(self.METADATA_FILENAME): |
|
52 logging.getLogger('cone').info("No metadata found for: %s" % (self.get_path())) |
|
53 # return empty metadata object |
|
54 meta = metadata.Metadata() |
|
55 else: |
|
56 res = self.open_resource(self.METADATA_FILENAME) |
|
57 try: |
|
58 try: |
|
59 meta = metadata.MetadataReader().fromstring(res.read()) |
|
60 except xml.parsers.expat.ExpatError: |
|
61 """ in case of xml parsing error return empty metadata """ |
|
62 meta = metadata.Metadata() |
|
63 finally: |
|
64 res.close() |
|
65 return meta |
|
66 |
|
67 def write_metadata(self): |
|
68 # Try to update the metadata, which might fail on ZipStorage |
|
69 |
|
70 try: |
|
71 if self.get_mode(self.mode) != api.Storage.MODE_READ: |
|
72 # update the active configuration |
|
73 self.set_active_configuration(self.get_active_configuration()) |
|
74 metares = self.open_resource(self.METADATA_FILENAME,"wb") |
|
75 metadata.MetadataWriter().toresource(self.meta,metares) |
|
76 metares.close() |
|
77 except Exception,e: |
|
78 logging.getLogger('cone').error("Could not save metadata. Exception %s" % e) |
|
79 return |
|
80 |
|
81 def close(self): |
|
82 if self.get_current_path() == "": |
|
83 self.write_metadata() |
|
84 super(StorageBase, self).close() |
|
85 |