summaryrefslogtreecommitdiff
path: root/Param
diff options
context:
space:
mode:
authoryo mama <pepper@scannerjammer.com>2015-09-15 19:39:59 -0700
committeryo mama <pepper@scannerjammer.com>2015-09-15 19:39:59 -0700
commitacc164568a215c3c5e2363855f712291b2a459e1 (patch)
treef91454aca1de19dadc61cd892a5b4fa72c7ac32d /Param
parent7d0116ba389e061bd609cc999f1c993b7344cf55 (diff)
more oop
Diffstat (limited to 'Param')
-rw-r--r--Param/Bool/.__init__.py0
-rw-r--r--Param/Bool/__init__.py16
-rw-r--r--Param/Color/.__init__.py0
-rw-r--r--Param/Color/__init__.py18
-rw-r--r--Param/Enum/.__init__.py0
-rw-r--r--Param/Enum/__init__.py9
-rw-r--r--Param/Float/.__init__.py0
-rw-r--r--Param/Float/__init__.py11
-rw-r--r--Param/Img_url/__init__.py81
-rw-r--r--Param/Int/.__init__.py0
-rw-r--r--Param/Int/__init__.py11
-rw-r--r--Param/Json/__init__.py8
-rw-r--r--Param/Raw/.__init__.py0
-rw-r--r--Param/Raw/__init__.py7
-rw-r--r--Param/String/.__init__.py0
-rw-r--r--Param/String/__init__.py13
-rw-r--r--Param/__init__.py59
17 files changed, 233 insertions, 0 deletions
diff --git a/Param/Bool/.__init__.py b/Param/Bool/.__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/Param/Bool/.__init__.py
diff --git a/Param/Bool/__init__.py b/Param/Bool/__init__.py
new file mode 100644
index 0000000..38fa7fa
--- /dev/null
+++ b/Param/Bool/__init__.py
@@ -0,0 +1,16 @@
+from Param import Param
+
+def ParamBool(Param):
+ def __init__(self, value, classname=""):
+ self._classname = classname
+ super(ParamBool).__init__()
+ self.value = self._bool_correct(value)
+ 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/Param/Color/.__init__.py b/Param/Color/.__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/Param/Color/.__init__.py
diff --git a/Param/Color/__init__.py b/Param/Color/__init__.py
new file mode 100644
index 0000000..ea7895c
--- /dev/null
+++ b/Param/Color/__init__.py
@@ -0,0 +1,18 @@
+from Param import Param
+
+def ParamColor(Param):
+ def __init__(self, value, classname=""):
+ self._classname = classname
+ super(ParamColor).__init__()
+ 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/Param/Enum/.__init__.py b/Param/Enum/.__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/Param/Enum/.__init__.py
diff --git a/Param/Enum/__init__.py b/Param/Enum/__init__.py
new file mode 100644
index 0000000..f5aad73
--- /dev/null
+++ b/Param/Enum/__init__.py
@@ -0,0 +1,9 @@
+from Param import Param
+
+def ParamEnum(Param):
+ def __init__(self, value, enum_values=[], classname=""):
+ self._classname = classname
+ super(ParamEnum).__init__()
+ if value not in enum_values:
+ return err_warn("Value %s not in enum values" % str(value))
+ self.value = value
diff --git a/Param/Float/.__init__.py b/Param/Float/.__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/Param/Float/.__init__.py
diff --git a/Param/Float/__init__.py b/Param/Float/__init__.py
new file mode 100644
index 0000000..e18fab4
--- /dev/null
+++ b/Param/Float/__init__.py
@@ -0,0 +1,11 @@
+from Param import Param
+
+def ParamFloat(Param):
+ def __init__(self, value, classname=""):
+ self._classname = classname
+ super(ParamFloat).__init__()
+ try:
+ self.value = float(value)
+ except Exception as e:
+ self.err_warn("Not a float: %s" % str(value))
+ self.err_warn(str(e))
diff --git a/Param/Img_url/__init__.py b/Param/Img_url/__init__.py
new file mode 100644
index 0000000..176888b
--- /dev/null
+++ b/Param/Img_url/__init__.py
@@ -0,0 +1,81 @@
+from Param import Param
+from Config import *
+import urllib, urllib2
+from subprocess import Popen, PIPE
+Request = urllib2.Request
+urlencode = urllib.urlencode
+urlopen = urllib2.urlopen
+Request = urllib2.Request
+urlencode = urllib.urlencode
+urlopen = urllib2.urlopen
+
+
+def ParamImg_url(Param):
+ def __init__(self, value, key="", classname=""):
+ self._classname = classname
+ super(ParamImg_url).__init__()
+
+ if value:
+ try:
+ self.filename = self._filename_temporary(key)
+ self.path = os.path.join(self._working_dir, _filename)
+ self._image_download(value, self._path)
+ self.mimetype = self._image_mimetype(_path)
+ self.value = self.path
+ 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 _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/Param/Int/.__init__.py b/Param/Int/.__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/Param/Int/.__init__.py
diff --git a/Param/Int/__init__.py b/Param/Int/__init__.py
new file mode 100644
index 0000000..b7a5d32
--- /dev/null
+++ b/Param/Int/__init__.py
@@ -0,0 +1,11 @@
+from Param import Param
+
+def ParamInt(Param):
+ def __init__(self, value, classname=""):
+ self._classname = classname
+ super(ParamInt).__init__()
+ try:
+ self.value = int(value)
+ except Exception as e:
+ self.err_warn("Not an int: %s" % str(value))
+ self.err_warn(str(e))
diff --git a/Param/Json/__init__.py b/Param/Json/__init__.py
new file mode 100644
index 0000000..5e3bbc2
--- /dev/null
+++ b/Param/Json/__init__.py
@@ -0,0 +1,8 @@
+from Param import Param
+import simplejson as json
+
+def ParamJson(Param):
+ def __init__(self, value, classname=""):
+ self._classname = classname
+ super(ParamJson).__init__()
+ self.value = json.parse(value)
diff --git a/Param/Raw/.__init__.py b/Param/Raw/.__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/Param/Raw/.__init__.py
diff --git a/Param/Raw/__init__.py b/Param/Raw/__init__.py
new file mode 100644
index 0000000..7eb1a4d
--- /dev/null
+++ b/Param/Raw/__init__.py
@@ -0,0 +1,7 @@
+from Param import Param
+
+def ParamRaw(Param):
+ def __init__(self, value, classname=""):
+ self._classname = classname
+ super(ParamRaw).__init__()
+ self.value = value
diff --git a/Param/String/.__init__.py b/Param/String/.__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/Param/String/.__init__.py
diff --git a/Param/String/__init__.py b/Param/String/__init__.py
new file mode 100644
index 0000000..fe6b6c1
--- /dev/null
+++ b/Param/String/__init__.py
@@ -0,0 +1,13 @@
+from Param import Param
+
+def ParamString(Param):
+ def __init__(self, value, classname=""):
+ self._classname = classname
+ super(ParamString).__init__()
+ try:
+ self.value = self.sanitize(value)
+ except Exception as e:
+ self.err_warn("Unable to sanitize: %s" % str(value))
+ self.err_warn(str(e))
+ def sanitize (self, s):
+ return re.sub(r'\W+', '', s)
diff --git a/Param/__init__.py b/Param/__init__.py
new file mode 100644
index 0000000..4152587
--- /dev/null
+++ b/Param/__init__.py
@@ -0,0 +1,59 @@
+import re
+import os
+import time
+import sys
+import urllib, urllib2
+from subprocess import Popen, PIPE
+Request = urllib2.Request
+urlencode = urllib.urlencode
+urlopen = urllib2.urlopen
+
+from Config import WORKING_DIR, MAX_SIZE, \
+ SPECIAL_DOWNLOADERS, SPECIAL_DOWNLOADERS_MAX_SIZE, \
+ BIN_IDENTIFY
+
+class BadParamError(Exception):
+ pass
+
+class Params(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 __iter__(self):
+ for key, value in vars(self).iteritems():
+ yield key, 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 set_val(self, key, value, value_type=None, enum_values=None):
+ try:
+ self.__setattr__(key, value)
+ except Exception as e:
+ self.err_warn("key: %s value: %s" % (key, value), error=str(e))
+
+
+ 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)))
+
+
+