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))