-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathicoolobject.py
272 lines (237 loc) · 9.26 KB
/
icoolobject.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
# -*- coding: utf-8 -*-
import sys
import icool_exceptions as ie
class ICoolObject(object):
"""Generic ICOOL object providing methods for"""
def __init__(self, kwargs):
if self.check_command_params_init(kwargs) is False:
sys.exit(0)
else:
self.setall(kwargs)
def __call__(self, kwargs):
if self.check_command_params_call(kwargs) is False:
sys.exit(0)
else:
self.setall(kwargs)
def __str__(self, return_str):
command_parameters_dict = self.command_params
for key in command_parameters_dict:
if hasattr(self, key):
return_str += '\n'
return_str += key
return_str += ': '
return_str += str(getattr(self, key))
return return_str
def __repr__(self):
return '[ICool Object]'
"""Checks whether all required command parameters specified in __init__ are provided are valid
for the command.
Valid means the parameters are recognized for the command, all required parameters are provided
and the parameters are the correct type."""
def __setattr__(self, name, value):
if self.check_command_param(name):
object.__setattr__(self, name, value)
else:
sys.exit(0)
def check_command_param(self, command_param):
"""
Checks whether a parameter specified for command is valid.
"""
command_parameters_dict = self.get_command_params()
# Check command parameters are all valid
try:
if command_param not in command_parameters_dict:
raise ie.InvalidCommandParameter(
command_param,
command_parameters_dict.keys())
except ie.InvalidCommandParameter as e:
print e
return False
except ie.InvalidType as e:
print e
return False
return True
def check_command_params_valid(
self,
command_params,
command_parameters_dict):
"""Returns True if command_params are valid (correspond to the command)
Otherwise raises an exception and returns False"""
# command_parameters_dict = self.get_command_params()
try:
for key in command_params:
if key not in command_parameters_dict:
raise ie.InvalidCommandParameter(
key,
command_parameters_dict)
except ie.InvalidCommandParameter as e:
print e
return False
return True
def check_all_required_command_params_specified(
self,
command_params,
command_parameters_dict):
"""Returns True if all required command parameters were specified
Otherwise raises an exception and returns False"""
# command_parameters_dict = self.get_command_params()
try:
for key in command_parameters_dict:
if self.is_required(key, command_parameters_dict):
if key not in command_params:
raise ie.MissingCommandParameter(key, command_params)
except ie.MissingCommandParameter as e:
print e
return False
return True
def check_command_params_type(self, command_params, command_params_dict):
"""Checks to see whether all required command parameters specified were of the correct type"""
# command_params_dict = self.get_command_params()
try:
for key in command_params:
if self.check_type(
command_params_dict[key]['type'],
command_params[key]) is False:
raise ie.InvalidType(
command_params_dict[key]['type'],
command_params[key].__class__.__name__)
except ie.InvalidType as e:
print e
return False
return True
def check_command_param_type(self, name, value):
"""Checks to see whether a particular command parameter of name with value is of the correct type"""
command_params_dict = self.get_command_params()
try:
if self.check_type(
command_params_dict[name]['type'],
value) is False:
raise ie.InvalidType(
command_params_dict[name]['type'],
value.__class__.__name__)
except ie.InvalidType as e:
print e
return False
return True
def check_command_params_init(self, command_params):
"""
Checks whether the parameters specified for command are valid, all required parameters are
specified and all parameters are of correct type. If not, raises an exception.
"""
command_parameters_dict = self.get_command_params()
check_params = not self.check_command_params_valid(
command_params,
command_parameters_dict) or not self.check_all_required_command_params_specified(
command_params,
command_parameters_dict) or not self.check_command_params_type(
command_params,
command_parameters_dict)
if check_params:
return False
else:
return True
def check_command_params_call(self, command_params):
"""
Checks whether the parameters specified for command are valid and all required parameters exist.
"""
command_parameters_dict = self.get_command_params()
return self.check_command_params_valid(command_params, command_parameters_dict) and\
self.check_command_params_type(
command_params,
command_parameters_dict)
def setall(self, command_params):
for key in command_params:
self.__setattr__(key, command_params[key])
def setdefault(self, command_params):
command_params_dict = self.get_command_params()
for key in command_params_dict:
if key not in command_params:
self.__setattr__(key, command_params_dict[key]['default'])
def check_type(self, icool_type, provided_type):
"""Takes provided python object and compares with required icool type name.
Returns True if the types match and False otherwise.
"""
provided_type_name = provided_type.__class__.__name__
print icool_type, provided_type_name
if icool_type == 'Real':
if provided_type_name == 'int' or provided_type_name == 'long' or provided_type_name == 'float':
return True
else:
return False
if icool_type == 'Integer':
if provided_type_name == 'int' or provided_type_name == 'long':
return True
else:
return False
if icool_type == 'Logical':
if provided_type_name == 'bool':
return True
else:
return False
if icool_type == 'Field':
if isinstance(provided_type, Field):
return True
else:
return False
if icool_type == 'Material':
if isinstance(provided_type, Material):
return True
else:
return False
if icool_type == 'SubRegion':
if isinstance(provided_type, SubRegion):
return True
else:
return False
if icool_type == 'Distribution':
if isinstance(provided_type, Distribution):
return True
else:
return False
if icool_type == 'Correlation':
if isinstance(provided_type, Correlation):
return True
else:
return False
if icool_type == 'ICoolComposite':
if isinstance(provided_type, ICoolComposite):
return True
else:
return False
def get_command_params(self):
return self.command_params
def is_required(self, command_param, command_parameters_dict):
# command_parameters_dict = self.get_command_params()
if 'req' not in command_parameters_dict[command_param]:
return True
else:
return command_parameters_dict[command_param]['req']
def gen_parm(self):
command_params = self.get_command_params()
#parm = [None] * len(command_params)
parm = [None] * self.num_params
for key in command_params:
pos = int(command_params[key]['pos']) - 1
val = getattr(self, key)
parm[pos] = val
print parm
return parm
def for001_str_gen(self, value):
if value.__class__.__name__ == 'bool':
if value is True:
return '.true.'
else:
return '.false.'
else:
return str(value)
def get_begtag(self):
return self.begtag
def get_endtag(self):
return self.endtag
def get_line_splits(self):
return self.for001_format['line_splits']
from field import Field
from material import Material
from distribution import Distribution
from correlation import Correlation
from subregion import SubRegion