forked from H3C/hdm-redfish-script
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.py
176 lines (154 loc) · 6.63 KB
/
config.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
###
# Copyright 2021 New H3C Technologies Co., Ltd.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
###
# -*- coding: utf-8 -*-
import argparse
import importlib
import os
import xml.etree.ElementTree as ET
from utils import globalvar
from exception.ToolException import ConfigException
class XmlConfig(object):
tool_set = {"hREST"}
def __init__(self):
self._args = None
self._model_class = None
self._model_method = None
self._view_class = None
self._view_method = None
self._format = None
self._type = None
self.path = "./config_hrest.xml"
@property
def args(self):
return self._args
@property
def model_class(self):
return self._model_class
@property
def model_method(self):
return self._model_method
@property
def view_class(self):
return self._view_class
@property
def view_method(self):
return self._view_method
@property
def format(self):
return self._format
@property
def type(self):
return self._type
def config_parser(self):
try:
xml_path = self.path
config_path = os.path.join(
os.path.dirname(os.path.realpath(__file__)) + xml_path)
tree = ET.ElementTree(file=config_path)
except FileNotFoundError as err:
raise ConfigException(err)
root = tree.getroot()
type_ = root.get("type")
if type_ not in XmlConfig.tool_set:
err = ("Failure: Unrecognized profile type_ : "
"{}! Please check your configure file".format(type_))
raise ConfigException(err)
self._type = type_
version = root.get("version")
globalvar.IS_ADAPT_B01 = (
True if root.get("is_adapt_b01") == "True" else False)
parser = argparse.ArgumentParser(prog=type_,
add_help=True)
parser.add_argument('-V', '--version', action='version',
version="%s version %s" % (type_, version))
propertys = tree.find("propertys")
if propertys is not None:
propertys = propertys.findall("property")
for property_ in propertys:
parser.add_argument(property_.get("name"),
dest=property_.get("dest"),
required=(property_.get("required") ==
"True"),
type={"str": str,
"int": int,
"float": float}
.get(property_.get("type"), None),
help=property_.get("help"))
subparsers = parser.add_subparsers(title="sub commands",
dest="sub_command",
help="sub-command help",
metavar="sub command")
mappers = tree.findall("mapper")
for elem in mappers:
sub_parser = subparsers.add_parser(elem.get("id"),
help=elem.get("help"))
propertys = elem.find("propertys")
if propertys is not None:
propertys = propertys.findall("property")
for property_ in propertys:
choices = property_.get("choices")
if choices:
choices = choices.split()
if "int" == property_.get("type"):
try:
choices = [int(i) for i in choices]
except ValueError:
err = ("Failure: Unrecognized choices: "
"{%s}!" % property_.get("choices"))
raise ConfigException(err)
sub_parser.add_argument(property_.get("name"),
dest=property_.get("dest"),
required=(property_.get("required")
== "True"),
type={"str": str,
"int": int,
"float": float}
.get(property_.get("type"), None),
help=property_.get("help"),
choices=choices)
self._args = parser.parse_args()
sub_command = self._args.sub_command
path = "mapper[@id='" + sub_command + "']"
module_config = tree.find(path)
self._format = module_config.find("result")
file = module_config.get("file")
if not file:
raise ConfigException(
"Failure: File is not exist. File: %s" %
file)
filename = file.split("/")
class_name = module_config.get("class")
method_name = module_config.get("method")
module = importlib.import_module(".".join(filename))
module_class = getattr(module, class_name)
self._model_method = getattr(module_class, method_name)
self._model_class = module_class
def get_all_cmd(self):
try:
tree = ET.ElementTree(file=self.path)
xml_path = tree.getroot().findall("filename")[-1].get("path")
config_path = os.path.join(
os.path.dirname(os.path.realpath(__file__)) + xml_path)
tree = ET.ElementTree(file=config_path)
except FileNotFoundError as err:
raise ConfigException(err)
else:
cmd_list = []
mappers = tree.findall("mapper")
for elem in mappers:
cmd = elem.get("id")
cmd_list.append(cmd)
return cmd_list