summaryrefslogtreecommitdiff
path: root/lib/param
diff options
context:
space:
mode:
Diffstat (limited to 'lib/param')
-rw-r--r--lib/param/__init__.py56
-rw-r--r--lib/param/bool_.py37
-rw-r--r--lib/param/color.py34
-rw-r--r--lib/param/enum.py19
-rw-r--r--lib/param/float_.py39
-rw-r--r--lib/param/json.py14
6 files changed, 115 insertions, 84 deletions
diff --git a/lib/param/__init__.py b/lib/param/__init__.py
index 2c05726..10ea7a6 100644
--- a/lib/param/__init__.py
+++ b/lib/param/__init__.py
@@ -4,55 +4,55 @@ import sys
from config import WORKING_DIR
class BadParamError(Exception):
- pass
+ 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)
+ 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
+ return True if self.value else False
def __str__(self):
- return str(self.value)
+ return str(self.value)
def __eq__(self, other):
- return self.value == other
+ return self.value == other
def __ne__(self, other):
- return self.value != 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))
+ 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))
-
+ 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
+ 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);
+ 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)))
+ 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 Int
from param.raw import Raw
diff --git a/lib/param/bool_.py b/lib/param/bool_.py
index acce969..394dea6 100644
--- a/lib/param/bool_.py
+++ b/lib/param/bool_.py
@@ -1,19 +1,24 @@
+"""Defines the bool param type"""
from param import Param
import re
-import sys
class Bool(Param):
- def __init__(self, value, classname=""):
- super(Bool, self).__init__(classname=classname)
- if value:
- self.value = self._bool_correct(value)
- else:
- self.value = False
- def _bool_correct(self, b):
- if type(b) == str or type(b) == unicode:
- if re.match(r'true', b, re.IGNORECASE):
- return True
- elif re.match(r'false', b, re.IGNORECASE):
- return False
- elif type(b) == bool:
- return b
- self.err_warn("Not a bool: %s" % str(b))
+ """Defines the bool param type
+ Args:
+ value: the value of the bool (string or python bool)
+ classname: the name of the class to which the param belongs
+ """
+ def __init__(self, value, classname=""):
+ super(Bool, self).__init__(classname=classname)
+ if value:
+ self.value = self._bool_correct(value)
+ else:
+ self.value = False
+ def _bool_correct(self, b):
+ if isinstance(b, str) or isinstance(b, unicode):
+ if re.match(r'true', b, re.IGNORECASE):
+ return True
+ elif re.match(r'false', b, re.IGNORECASE):
+ return False
+ elif isinstance(b, bool):
+ return b
+ self.err_warn("Not a bool: %s" % str(b))
diff --git a/lib/param/color.py b/lib/param/color.py
index a339b7f..1c62955 100644
--- a/lib/param/color.py
+++ b/lib/param/color.py
@@ -1,17 +1,23 @@
+"""Defines the color param type"""
from param import Param
import re
class Color(Param):
- def __init__(self, value, classname=""):
- super(Color, self).__init__(classname=classname)
- try:
- self.value = self._color_sanitize(value)
- except Exception as e:
- self.err_warn("Unable to sanitize the color: %s" % str(value))
- self.err_warn(str(e))
- def _color_sanitize(self, s):
- if s == "":
- return "transparent"
- if re.match('(rgba?\([0-9]+,[0-9]+,[0-9]+\))|([a-zA-Z]+)|(\#[A-Ha-h0-9]+)', s):
- return s.replace(' ', '');
- else:
- self.err_warn("Not a color: {}\n".format(s))
+ """Defines the color param type
+ Args:
+ value: the value of the color (string)
+ classname: the name of the class to which the param belongs
+ """
+ def __init__(self, value, classname=""):
+ super(Color, self).__init__(classname=classname)
+ try:
+ self.value = self._color_sanitize(value)
+ except Exception as e:
+ self.err_warn("Unable to sanitize the color: %s" % str(value))
+ self.err_warn(str(e))
+ def _color_sanitize(self, s):
+ if s == "":
+ return "transparent"
+ if re.match('(rgba?\([0-9]+,[0-9]+,[0-9]+\))|([a-zA-Z]+)|(\#[A-Ha-h0-9]+)', s):
+ return s.replace(' ', '')
+ else:
+ self.err_warn("Not a color: {}\n".format(s))
diff --git a/lib/param/enum.py b/lib/param/enum.py
index 49e11f6..c68adc9 100644
--- a/lib/param/enum.py
+++ b/lib/param/enum.py
@@ -1,8 +1,15 @@
+"""Defines the enum param type"""
+
from param import Param
-import sys
class Enum(Param):
- def __init__(self, value, enum_values=[], classname=""):
- super(Enum,self).__init__(classname=classname)
- if value and value not in enum_values:
- return self.err_warn("Value %s not in enum values" % str(value))
- self.value = value
+ """Defines the enum param type
+ Args:
+ value: the value of the param
+ enum_values: an array of possible value types
+ classname: name of the class that the param belongs to
+ """
+ def __init__(self, value, enum_values=[], classname=""):
+ super(Enum, self).__init__(classname=classname)
+ if value and value not in enum_values:
+ self.err_warn("Value %s not in enum values" % str(value))
+ self.value = value
diff --git a/lib/param/float_.py b/lib/param/float_.py
index f5d1816..88ed066 100644
--- a/lib/param/float_.py
+++ b/lib/param/float_.py
@@ -1,19 +1,24 @@
+"""Defines the float param type"""
from param import Param
-
class Float(Param):
- def __init__(self, value, classname=""):
- self._classname = classname
- super(Float, self).__init__(classname=classname)
- try:
- if value:
- self.value = float(value)
- else:
- self.value = 0.0
- except Exception as e:
- self.err_warn("Not a float: %s" % str(value))
- self.err_warn(str(e))
- def __int__(self):
- return int(self.value)
-
- def __float__(self):
- return float(self.value)
+ """Defines the float param type
+ Args:
+ value: the value of the Float
+ classname: the name of the class to which the param belongs
+ """
+ def __init__(self, value, classname=""):
+ self._classname = classname
+ super(Float, self).__init__(classname=classname)
+ try:
+ if value:
+ self.value = float(value)
+ else:
+ self.value = 0.0
+ except Exception as e:
+ self.err_warn("Not a float: %s" % str(value))
+ self.err_warn(str(e))
+ def __int__(self):
+ return int(self.value)
+
+ def __float__(self):
+ return float(self.value)
diff --git a/lib/param/json.py b/lib/param/json.py
index af9b775..08db3e9 100644
--- a/lib/param/json.py
+++ b/lib/param/json.py
@@ -1,7 +1,15 @@
+"""Defines the json param type"""
from param import Param
import simplejson as json
class Json(Param):
- def __init__(self, value, classname=""):
- super(Json, self).__init__(classname=classname)
- self.value = json.loads(value)
+ """Defines the json param type. Loads in a
+ json value and parses it.
+
+ Args:
+ value: a json string
+ classname: name of the class to which the param belongs
+ """
+ def __init__(self, value, classname=""):
+ super(Json, self).__init__(classname=classname)
+ self.value = json.loads(value)