summaryrefslogtreecommitdiff
path: root/lib/Param/__init__.py
blob: 678b9dc0ca048090d395785b1c05487bfb7a5322 (plain)
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
import time
import sys

from Config import WORKING_DIR

class BadParamError(Exception):
  pass


class Param(object):
    def __init__(self, classname="", **kwargs):
      self._working_dir = WORKING_DIR
      self._now = kwargs.get("now", str(int(time.time())));
      self._classname = classname 
      for key, value in kwargs.items():
        setattr(self, key, value)
    
    def __nonzero__(self):
      return True if self.value else False 

    def __str__(self):
      return str(self.value)

    def __eq__(self, other):
      return self.value == other

    def __ne__(self, other):
      return self.value != other

    def set_val(self, value):
      try:
        self.value = value
      except Exception as e:
        self.err_warn("Unable to set value {}".format(value))

    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 err_fatal(self, s, error=None):
      self._log(s, error, fatal=True);
      sys.exit(1);

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

from Param.Int import ParamInt
from Param.Raw import ParamRaw
from Param.Bool import ParamBool
from Param.Enum import ParamEnum
from Param.Json import ParamJson
from Param.Color import ParamColor
from Param.Float import ParamFloat
from Param.Img_url import ParamImg_url
from Param.String import ParamString