EMANE  1.2.1
build/lib/emane/shell/manifest.py
Go to the documentation of this file.
1 #
2 # Copyright (c) 2013,2017 - Adjacent Link LLC, Bridgewater, New Jersey
3 # All rights reserved.
4 #
5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions
7 # are met:
8 #
9 # * Redistributions of source code must retain the above copyright
10 # notice, this list of conditions and the following disclaimer.
11 # * Redistributions in binary form must reproduce the above copyright
12 # notice, this list of conditions and the following disclaimer in
13 # the documentation and/or other materials provided with the
14 # distribution.
15 # * Neither the name of Adjacent Link LLC nor the names of its
16 # contributors may be used to endorse or promote products derived
17 # from this software without specific prior written permission.
18 #
19 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 # POSSIBILITY OF SUCH DAMAGE.
31 #
32 
33 from pkg_resources import resource_filename
34 from lxml import etree
35 from . import ManifestException
36 
37 def toBool(value):
38 
39  if value == "yes" or \
40  value == "true":
41  return True
42  else:
43  return False
44 
45 class Manifest:
46  def __init__(self,filename):
47  self._statistics = {}
48  self._tables = {}
49  self._configuration = {}
50 
51  tree = etree.parse(filename)
52  root = tree.getroot()
53 
54  schemaDoc = etree.parse(resource_filename('emane.shell',
55  'schema/manifest.xsd'))
56 
57  schema = etree.XMLSchema(etree=schemaDoc,attribute_defaults=True)
58 
59  if not schema(root):
60  message = ""
61  for entry in schema.error_log:
62  message += "%d: %s" % (entry.line,entry.message)
63  raise ManifestException(message)
64 
65  self._name = root.xpath('/manifest/plugin/@name')[0]
66 
67  for parameter in root.xpath('/manifest/plugin/configuration/parameter'):
68  entry = {}
69 
70  values = parameter.xpath('.//values')[0]
71 
72  description = ""
73 
74  descriptions = parameter.xpath('.//description')
75 
76  if descriptions:
77  description = descriptions[0].text
78 
79  regex = ""
80 
81  regexes = parameter.xpath('.//regex')
82 
83  if regexes:
84  regex = regexes[0].text
85 
86  entry = { 'default' : toBool(parameter.get('default')),
87  'required' : toBool(parameter.get('required')),
88  'modifiable' : toBool(parameter.get('modifiable')),
89  'description' : description,
90  'regex' : regex,
91  'minOccurs' : int(values.get("minOccurs")),
92  'maxOccurs' : int(values.get("maxOccurs")),
93  'values' : [x.text for x in values.xpath('value')]}
94 
95  numeric = parameter.xpath('numeric')
96 
97  if numeric:
98  numeric = numeric[0]
99 
100  entry['numeric'] = { 'type' : numeric.get('type'),
101  'minValue' : numeric.get('minValue'),
102  'maxValue' : numeric.get('maxValue') }
103  else:
104  nonnumeric = parameter.xpath('nonnumeric')[0]
105  entry['nonnumeric'] = { 'type' : nonnumeric.get('type')}
106 
107  self._configuration[parameter.get('name')] = entry
108 
109 
110  for element in root.xpath('/manifest/plugin/statistics/element'):
111  description = ""
112 
113  descriptions = element.xpath('description')
114 
115  if descriptions:
116  description = descriptions[0].text
117 
118  self._statistics[element.get('name')] = {'type' : element.get('type'),
119  'clearable': toBool(element.get('clearable')),
120  'description' : description}
121 
122  for table in root.xpath('/manifest/plugin/statistictables/table'):
123  description = ""
124 
125  descriptions = table.xpath('description')
126 
127  if descriptions:
128  description = descriptions[0].text
129 
130  self._tables[table.get('name')] = {'description' : description,
131  'clearable': toBool(table.get('clearable'))}
132 
133 
134  def getName(self):
135  return self._name
136 
138  return list(self._configuration.keys())
139 
141  return [name for name,value in list(self._configuration.items())
142  if value['modifiable']]
143 
144  def getAllStatistics(self):
145  return list(self._statistics.keys())
146 
148  return [name for name,value in list(self._statistics.items())
149  if value['clearable']]
150 
152  return [name for name,value in list(self._tables.items())
153  if value['clearable']]
154 
155  def getAllTables(self):
156  return list(self._tables.keys())
157 
158  def getConfigurationInfo(self,name):
159  if name in self._configuration:
160  return self._configuration[name]
161  else:
162  raise ManifestException("unknown configuration parameter: %s" % name)
163 
164  def getStatisticInfo(self,name):
165  if name in self._statistics:
166  return self._statistics[name]
167  else:
168  raise ManifestException("unknown statistic element: %s" % name)
169 
170  def getTableInfo(self,name):
171  if name in self._tables:
172  return self._tables[name]
173  else:
174  raise ManifestException("unknown table: %s" % name)