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
|
import re
import os
import sys
from Param import *
class BadParamError(Exception):
pass
class Params(object):
def __init__(self, **kwargs):
for key, value in kwargs.items():
setattr(self, key, value)
def __iter__(self):
for key, value in vars(self).iteritems():
yield key, value
def _error_log(self, s, error=None, fatal=False):
message = "ERROR - BAD PARAM"
if fatal: message += "- [FATAL] -"
sys.stderr.write("{}:{} - {}\n".format(message, self._classname, s))
if error:
sys.stderr.write("PARAM ERROR: {}\n".format(str(error)))
def err_warn(self, s, error=None):
self._error_log(s, error=error);
raise BadParamError("%s - %s" % (self._classname, s))
def __getattr__(self, key):
try:
return self.__getattribute__(key);
except AttributeError:
return None
def definitions_import(self, d_hash, classname=""):
value = None
for key in d_hash.keys():
try:
if d_hash[key]['type'] == "bool":
instance = ParamBool(d_hash[key], classname=classname)
elif d_hash[key]['type'] == "color":
instance = ParamColor(d_hash[key], classname=classname)
elif d_hash[key]['type'] == "enum":
instance = ParamEnum(d_hash[key], enum_values=d_hash[key]['enum_values'], classname=classname)
elif d_hash[key]['type'] == "float":
instance = ParamFloat(d_hash[key], classname=classname)
elif d_hash[key]['type'] == "img_url":
instance = ParamImg_url(d_hash[key], classname=classname)
elif d_hash[key]['type'] == "int":
instance = ParamInt(d_hash[key], classname=classname)
elif d_hash[key]['type'] == "json":
instance = ParamJson(d_hash[key], classname=classname)
elif d_hash[key]['type'] == "raw":
instance = ParamRaw(d_hash[key], classname=classname)
elif d_hash[key]['type'] == "string":
instance = ParamString(d_hash[key], classname=classname)
self.__setattr__(key, instance.value)
except Exception as e:
self.err_warn("key: %s value: %s" % (key, str(d_hash[key])), error=str(e))
|