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 |
Configuration flattener
|
|
18 |
'''
|
|
19 |
|
|
20 |
import re
|
|
21 |
import os
|
|
22 |
import sys
|
|
23 |
import logging
|
|
24 |
import xml.parsers.expat
|
|
25 |
|
|
26 |
try:
|
|
27 |
from cElementTree import ElementTree
|
|
28 |
except ImportError:
|
|
29 |
try:
|
|
30 |
from elementtree import ElementTree
|
|
31 |
except ImportError:
|
|
32 |
try:
|
|
33 |
from xml.etree import cElementTree as ElementTree
|
|
34 |
except ImportError:
|
|
35 |
from xml.etree import ElementTree
|
|
36 |
|
|
37 |
import __init__
|
|
38 |
|
|
39 |
from cone.public import exceptions,plugin,utils,api
|
|
40 |
import copy
|
|
41 |
|
|
42 |
|
|
43 |
|
|
44 |
class ConfigurationFlattener():
|
|
45 |
"""
|
|
46 |
Configuration flattener
|
|
47 |
"""
|
|
48 |
|
|
49 |
def _init(self):
|
|
50 |
self.logger = logging.getLogger('cone.gcfml(%s)' % self.ref)
|
|
51 |
pass
|
|
52 |
|
|
53 |
|
|
54 |
def flat(self, conf_from_org, settings, to_config):
|
|
55 |
"""
|
|
56 |
Flats configuration to one element xml element
|
|
57 |
"""
|
|
58 |
"""
|
|
59 |
Get the default view
|
|
60 |
Create the new flat configuration
|
|
61 |
"""
|
|
62 |
dview_from = conf_from_org.get_default_view()
|
|
63 |
|
|
64 |
""" Go through the required settings """
|
|
65 |
for setting in settings:
|
|
66 |
setting_name = setting.replace('/', '.')
|
|
67 |
try:
|
|
68 |
for fea in dview_from.get_features(setting_name):
|
|
69 |
""" Add the given feature ref and its children """
|
|
70 |
newfea = copy.copy(fea._obj)
|
|
71 |
to_config.add_feature(newfea, fea.namespace)
|
|
72 |
for subfeaname in fea.list_features():
|
|
73 |
subfea = fea.get_feature(subfeaname)
|
|
74 |
newfea = copy.copy(subfea._obj)
|
|
75 |
to_config.add_feature(newfea, subfea.namespace)
|
|
76 |
except exceptions.NotFound, e:
|
|
77 |
logging.getLogger('cone.gcfml').warning('Failed to get feature: %s , %s %s' % (setting_name, type(e), e) )
|
|
78 |
except Exception, e:
|
|
79 |
logging.getLogger('cone.gcfml').warning('Failed to flat feature: %s , %s %s' % (setting_name, type(e), e) )
|
|
80 |
|
|
81 |
""" Copy all data values from the existing configuration to the new configuration """
|
|
82 |
toview = to_config.get_default_view()
|
|
83 |
for fea in toview.get_features('**'):
|
|
84 |
fromfea = dview_from.get_feature(fea.fqr)
|
3
|
85 |
# If the value is an empty list, don't add it.
|
|
86 |
# This is because an empty list means that we have
|
|
87 |
# a sequence setting or sequence sub-setting that doesn't have
|
|
88 |
# any contents, and calling set_value() would create a data
|
|
89 |
# element for it with the attribute empty="true".
|
|
90 |
val = fromfea.get_value()
|
|
91 |
if val not in (None, []):
|
|
92 |
fea.set_value(val)
|
0
|
93 |
return to_config
|
|
94 |
|
|
95 |
def create_configuration(self, conf_from_org, settings, path="tempfile.confml"):
|
|
96 |
"""
|
|
97 |
Flats configuration to one feature and data confml
|
|
98 |
"""
|
|
99 |
"""
|
|
100 |
Get the default view
|
|
101 |
Create the new flat configuration
|
|
102 |
"""
|
|
103 |
prj = conf_from_org.get_project()
|
|
104 |
flat = prj.create_configuration(path)
|
|
105 |
(root,ext) = os.path.splitext(path)
|
|
106 |
dataname = "%s_data%s" % (root,ext)
|
|
107 |
flat.create_configuration(dataname)
|
|
108 |
self.flat(conf_from_org, settings, flat)
|
|
109 |
flat.close()
|