diff options
Diffstat (limited to 'lib/Param')
| -rw-r--r-- | lib/Param/Bool.py | 19 | ||||
| -rw-r--r-- | lib/Param/Color.py | 17 | ||||
| -rw-r--r-- | lib/Param/Enum.py | 8 | ||||
| -rw-r--r-- | lib/Param/Float.py | 19 | ||||
| -rw-r--r-- | lib/Param/Img_url.py | 98 | ||||
| -rw-r--r-- | lib/Param/Int.py | 18 | ||||
| -rw-r--r-- | lib/Param/Json.py | 8 | ||||
| -rw-r--r-- | lib/Param/Raw.py | 6 | ||||
| -rw-r--r-- | lib/Param/String.py | 15 | ||||
| -rw-r--r-- | lib/Param/__init__.py | 65 |
10 files changed, 0 insertions, 273 deletions
diff --git a/lib/Param/Bool.py b/lib/Param/Bool.py deleted file mode 100644 index 7b779cb..0000000 --- a/lib/Param/Bool.py +++ /dev/null @@ -1,19 +0,0 @@ -from Param import Param -import re -import sys -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 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)) diff --git a/lib/Param/Color.py b/lib/Param/Color.py deleted file mode 100644 index 9163338..0000000 --- a/lib/Param/Color.py +++ /dev/null @@ -1,17 +0,0 @@ -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.py b/lib/Param/Enum.py deleted file mode 100644 index 3923a64..0000000 --- a/lib/Param/Enum.py +++ /dev/null @@ -1,8 +0,0 @@ -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.py b/lib/Param/Float.py deleted file mode 100644 index 0581815..0000000 --- a/lib/Param/Float.py +++ /dev/null @@ -1,19 +0,0 @@ -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.py b/lib/Param/Img_url.py deleted file mode 100644 index 55bf353..0000000 --- a/lib/Param/Img_url.py +++ /dev/null @@ -1,98 +0,0 @@ -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.py b/lib/Param/Int.py deleted file mode 100644 index 0a73cc1..0000000 --- a/lib/Param/Int.py +++ /dev/null @@ -1,18 +0,0 @@ -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.py b/lib/Param/Json.py deleted file mode 100644 index 28a7126..0000000 --- a/lib/Param/Json.py +++ /dev/null @@ -1,8 +0,0 @@ -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.py b/lib/Param/Raw.py deleted file mode 100644 index f8adaab..0000000 --- a/lib/Param/Raw.py +++ /dev/null @@ -1,6 +0,0 @@ -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.py b/lib/Param/String.py deleted file mode 100644 index 8f08e49..0000000 --- a/lib/Param/String.py +++ /dev/null @@ -1,15 +0,0 @@ -from Param import Param -import re -import sys -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\nreason:%s" % (str(value), 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 deleted file mode 100644 index c658c61..0000000 --- a/lib/Param/__init__.py +++ /dev/null @@ -1,65 +0,0 @@ -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 |
