summaryrefslogtreecommitdiff
path: root/lib/Param
diff options
context:
space:
mode:
Diffstat (limited to 'lib/Param')
-rw-r--r--lib/Param/Bool/.__init__.py0
-rw-r--r--lib/Param/Bool/__init__.py18
-rw-r--r--lib/Param/Color/.__init__.py0
-rw-r--r--lib/Param/Color/__init__.py17
-rw-r--r--lib/Param/Enum/.__init__.py0
-rw-r--r--lib/Param/Enum/__init__.py8
-rw-r--r--lib/Param/Float/.__init__.py0
-rw-r--r--lib/Param/Float/__init__.py19
-rw-r--r--lib/Param/Img_url/__init__.py98
-rw-r--r--lib/Param/Int/.__init__.py0
-rw-r--r--lib/Param/Int/__init__.py18
-rw-r--r--lib/Param/Json/__init__.py8
-rw-r--r--lib/Param/Raw/.__init__.py0
-rw-r--r--lib/Param/Raw/__init__.py6
-rw-r--r--lib/Param/String/.__init__.py0
-rw-r--r--lib/Param/String/__init__.py16
-rw-r--r--lib/Param/__init__.py65
17 files changed, 273 insertions, 0 deletions
diff --git a/lib/Param/Bool/.__init__.py b/lib/Param/Bool/.__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/lib/Param/Bool/.__init__.py
diff --git a/lib/Param/Bool/__init__.py b/lib/Param/Bool/__init__.py
new file mode 100644
index 0000000..8ccca00
--- /dev/null
+++ b/lib/Param/Bool/__init__.py
@@ -0,0 +1,18 @@
+from Param import Param
+import re
+class ParamBool(Param):
+ def __init__(self, value, classname=""):
+ super(ParamBool, 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:
+ 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))
diff --git a/lib/Param/Color/.__init__.py b/lib/Param/Color/.__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/lib/Param/Color/.__init__.py
diff --git a/lib/Param/Color/__init__.py b/lib/Param/Color/__init__.py
new file mode 100644
index 0000000..9163338
--- /dev/null
+++ b/lib/Param/Color/__init__.py
@@ -0,0 +1,17 @@
+from Param import Param
+import re
+class ParamColor(Param):
+ def __init__(self, value, classname=""):
+ super(ParamColor, 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/.__init__.py b/lib/Param/Enum/.__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/lib/Param/Enum/.__init__.py
diff --git a/lib/Param/Enum/__init__.py b/lib/Param/Enum/__init__.py
new file mode 100644
index 0000000..3923a64
--- /dev/null
+++ b/lib/Param/Enum/__init__.py
@@ -0,0 +1,8 @@
+from Param import Param
+import sys
+class ParamEnum(Param):
+ def __init__(self, value, enum_values=[], classname=""):
+ super(ParamEnum,self).__init__(classname=classname)
+ if value not in enum_values:
+ return self.err_warn("Value %s not in enum values" % str(value))
+ self.value = value
diff --git a/lib/Param/Float/.__init__.py b/lib/Param/Float/.__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/lib/Param/Float/.__init__.py
diff --git a/lib/Param/Float/__init__.py b/lib/Param/Float/__init__.py
new file mode 100644
index 0000000..0581815
--- /dev/null
+++ b/lib/Param/Float/__init__.py
@@ -0,0 +1,19 @@
+from Param import Param
+
+class ParamFloat(Param):
+ def __init__(self, value, classname=""):
+ self._classname = classname
+ super(ParamFloat, 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/Img_url/__init__.py b/lib/Param/Img_url/__init__.py
new file mode 100644
index 0000000..58ed2e0
--- /dev/null
+++ b/lib/Param/Img_url/__init__.py
@@ -0,0 +1,98 @@
+import os
+from Param import Param
+from Config import *
+import urllib, urllib2
+from subprocess import Popen, PIPE
+import sys
+Request = urllib2.Request
+urlencode = urllib.urlencode
+urlopen = urllib2.urlopen
+Request = urllib2.Request
+urlencode = urllib.urlencode
+urlopen = urllib2.urlopen
+
+import sys;
+class ParamImg_url(Param):
+ def __init__(self, value, key="", classname=""):
+ super(ParamImg_url, self).__init__(classname=classname)
+ if value:
+ try:
+ self.filename = self._filename_temporary(key)
+
+ self.path = os.path.join(self._working_dir, self.filename)
+ self._image_download(value, self.path)
+ self.mimetype = self._image_mimetype(self.path)
+ self.url = value
+ except Exception as e:
+ self.err_warn("Unable to download image: %s" % str(value))
+ self.err_warn(str(e))
+
+ def _filename_temporary(self, s):
+ return "_tmp-{}-{}_{}".format(self._classname, self._now, s)
+
+ def __dict__(self):
+ return {
+ 'filename' : self.filename,
+ 'path': self.path,
+ 'url': self.url,
+ 'mimetype': self.mimetype
+ }
+
+ def __getitem__(self, item):
+ return self.__dict__().__getitem__(item)
+
+ def __str__(self):
+ return str(self.__dict__())
+
+ def __nonzero__(self):
+ return True if self.path and self.mimetype else False
+
+ def _image_download(self, url, path):
+ max_size = MAX_SIZE
+ if self.username in SPECIAL_DOWNLOADERS:
+ max_size = SPECIAL_DOWNLOADERS_MAX_SIZE
+ try:
+ self._download(url, path, max_size=max_size)
+ except Exception as e:
+ self.err_warn("Download failed");
+
+ def _browser_request (self, url, data=None):
+ headers = {
+ 'User-Agent': 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)',
+ 'Accept': '*/*',
+ }
+ try:
+ req = Request(url, data, headers)
+ response = urlopen(req)
+ except IOError as e:
+ if hasattr(e, 'code'):
+ sys.stderr.write( 'browser request error: %s - ERROR %s' % (url, e.code) )
+ raise IOError
+ return response
+
+ def _download(self, url, destination, max_size=MAX_SIZE):
+ response = self._browser_request(url, None)
+
+ rawimg = response.read()
+ if len(rawimg) == 0:
+ self.err_warn("got zero-length file")
+ if len(rawimg) > max_size:
+ self.err_warn("file too big: max size {} KB / {} is {} KB".format(
+ str(MAX_SIZE/1024),
+ destination,
+ str(len(rawimg)/1024)
+ )
+ )
+ f = open(destination, "w")
+ f.write(rawimg)
+ f.close()
+
+ def _image_mimetype(self, f):
+ try:
+ mimetype = Popen(
+ [BIN_IDENTIFY, f], stdout=PIPE
+ ).communicate()[0].split(" ")[1].lower()
+ return mimetype
+ except Exception as e:
+ sys.stderr.write("couldn't determine mimetype\n")
+ raise e;
diff --git a/lib/Param/Int/.__init__.py b/lib/Param/Int/.__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/lib/Param/Int/.__init__.py
diff --git a/lib/Param/Int/__init__.py b/lib/Param/Int/__init__.py
new file mode 100644
index 0000000..0a73cc1
--- /dev/null
+++ b/lib/Param/Int/__init__.py
@@ -0,0 +1,18 @@
+from Param import Param
+
+class ParamInt(Param):
+ def __init__(self, value, classname=""):
+ super(ParamInt, self).__init__(classname=classname)
+ try:
+ if value:
+ self.value = int(value)
+ else:
+ self.value = 0
+ except Exception as e:
+ self.err_warn("Not an int: %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/__init__.py b/lib/Param/Json/__init__.py
new file mode 100644
index 0000000..28a7126
--- /dev/null
+++ b/lib/Param/Json/__init__.py
@@ -0,0 +1,8 @@
+from Param import Param
+import simplejson as json
+
+class ParamJson(Param):
+ def __init__(self, value, classname=""):
+ super(ParamJson, self).__init__(classname=classname)
+ self.value = json.parse(value)
+
diff --git a/lib/Param/Raw/.__init__.py b/lib/Param/Raw/.__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/lib/Param/Raw/.__init__.py
diff --git a/lib/Param/Raw/__init__.py b/lib/Param/Raw/__init__.py
new file mode 100644
index 0000000..f8adaab
--- /dev/null
+++ b/lib/Param/Raw/__init__.py
@@ -0,0 +1,6 @@
+from Param import Param
+
+class ParamRaw(Param):
+ def __init__(self, value, classname=""):
+ super(ParamRaw, self).__init__(classname=classname)
+ self.value = value
diff --git a/lib/Param/String/.__init__.py b/lib/Param/String/.__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/lib/Param/String/.__init__.py
diff --git a/lib/Param/String/__init__.py b/lib/Param/String/__init__.py
new file mode 100644
index 0000000..df65c9d
--- /dev/null
+++ b/lib/Param/String/__init__.py
@@ -0,0 +1,16 @@
+from Param import Param
+import re
+
+class ParamString(Param):
+ def __init__(self, value, classname=""):
+ super(ParamString, self).__init__(classname=classname)
+ if value:
+ try:
+ self.value = self.sanitize(value)
+ except Exception as e:
+ self.err_warn("Unable to sanitize: %s" % str(value))
+ self.err_warn(str(e))
+ else:
+ self.value = ""
+ def sanitize (self, s):
+ return re.sub(r'\W+', '', s)
diff --git a/lib/Param/__init__.py b/lib/Param/__init__.py
new file mode 100644
index 0000000..678b9dc
--- /dev/null
+++ b/lib/Param/__init__.py
@@ -0,0 +1,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