0
|
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 |
|
3
|
20 |
from cone.public.api import Storage
|
|
21 |
from cone.public import utils
|
0
|
22 |
from cone.storage import metadata
|
|
23 |
|
3
|
24 |
class StorageBase(Storage):
|
0
|
25 |
"""
|
|
26 |
A general base class for all storage type classes
|
|
27 |
"""
|
|
28 |
METADATA_FILENAME = ".metadata"
|
|
29 |
|
3
|
30 |
def __init__(self, path, mode):
|
|
31 |
super(StorageBase, self).__init__(path, mode)
|
0
|
32 |
self.meta = self.read_metadata()
|
3
|
33 |
|
0
|
34 |
|
|
35 |
def get_active_configuration(self):
|
|
36 |
"""
|
|
37 |
Return the active configuration.
|
|
38 |
If the storage holds only one configuration, it will be always active
|
|
39 |
"""
|
|
40 |
root_confmls = self.list_resources("/")
|
|
41 |
root_confmls = utils.resourceref.filter_resources(root_confmls,"\.confml")
|
3
|
42 |
if self.meta.get_root_file() == None and len(root_confmls) == 1:
|
0
|
43 |
return root_confmls[0]
|
|
44 |
else:
|
|
45 |
return self.meta.get_root_file()
|
|
46 |
|
|
47 |
|
|
48 |
def set_active_configuration(self, ref):
|
|
49 |
self.meta.set_root_file(ref)
|
|
50 |
|
|
51 |
def read_metadata(self):
|
|
52 |
meta = None
|
|
53 |
if not self.is_resource(self.METADATA_FILENAME):
|
|
54 |
logging.getLogger('cone').info("No metadata found for: %s" % (self.get_path()))
|
|
55 |
# return empty metadata object
|
|
56 |
meta = metadata.Metadata()
|
|
57 |
else:
|
|
58 |
res = self.open_resource(self.METADATA_FILENAME)
|
|
59 |
try:
|
|
60 |
try:
|
|
61 |
meta = metadata.MetadataReader().fromstring(res.read())
|
|
62 |
except xml.parsers.expat.ExpatError:
|
|
63 |
""" in case of xml parsing error return empty metadata """
|
|
64 |
meta = metadata.Metadata()
|
|
65 |
finally:
|
|
66 |
res.close()
|
|
67 |
return meta
|
|
68 |
|
|
69 |
def write_metadata(self):
|
|
70 |
# Try to update the metadata, which might fail on ZipStorage
|
|
71 |
|
|
72 |
try:
|
3
|
73 |
if self.get_mode(self.mode) != Storage.MODE_READ:
|
0
|
74 |
# update the active configuration
|
|
75 |
self.set_active_configuration(self.get_active_configuration())
|
|
76 |
metares = self.open_resource(self.METADATA_FILENAME,"wb")
|
|
77 |
metadata.MetadataWriter().toresource(self.meta,metares)
|
|
78 |
metares.close()
|
|
79 |
except Exception,e:
|
|
80 |
logging.getLogger('cone').error("Could not save metadata. Exception %s" % e)
|
|
81 |
return
|
|
82 |
|
|
83 |
def close(self):
|
|
84 |
if self.get_current_path() == "":
|
|
85 |
self.write_metadata()
|
|
86 |
super(StorageBase, self).close()
|
|
87 |
|