From 5c982ed500b51f5b8d911e5a5adec10ac2d31d2e Mon Sep 17 00:00:00 2001 From: yo mama Date: Wed, 23 Sep 2015 23:27:27 -0700 Subject: ok good --- lib/Param/Bool.py | 19 +++++++++ lib/Param/Bool/__init__.py | 19 --------- lib/Param/Color.py | 17 ++++++++ lib/Param/Color/__init__.py | 17 -------- lib/Param/Enum.py | 8 ++++ lib/Param/Enum/__init__.py | 8 ---- lib/Param/Float.py | 19 +++++++++ lib/Param/Float/__init__.py | 19 --------- lib/Param/Img_url.py | 98 +++++++++++++++++++++++++++++++++++++++++++ lib/Param/Img_url/__init__.py | 98 ------------------------------------------- lib/Param/Int.py | 18 ++++++++ lib/Param/Int/__init__.py | 18 -------- lib/Param/Json.py | 8 ++++ lib/Param/Json/__init__.py | 8 ---- lib/Param/Raw.py | 6 +++ lib/Param/Raw/__init__.py | 6 --- lib/Param/String.py | 15 +++++++ lib/Param/String/__init__.py | 15 ------- 18 files changed, 208 insertions(+), 208 deletions(-) create mode 100644 lib/Param/Bool.py delete mode 100644 lib/Param/Bool/__init__.py create mode 100644 lib/Param/Color.py delete mode 100644 lib/Param/Color/__init__.py create mode 100644 lib/Param/Enum.py delete mode 100644 lib/Param/Enum/__init__.py create mode 100644 lib/Param/Float.py delete mode 100644 lib/Param/Float/__init__.py create mode 100644 lib/Param/Img_url.py delete mode 100644 lib/Param/Img_url/__init__.py create mode 100644 lib/Param/Int.py delete mode 100644 lib/Param/Int/__init__.py create mode 100644 lib/Param/Json.py delete mode 100644 lib/Param/Json/__init__.py create mode 100644 lib/Param/Raw.py delete mode 100644 lib/Param/Raw/__init__.py create mode 100644 lib/Param/String.py delete mode 100644 lib/Param/String/__init__.py (limited to 'lib/Param') diff --git a/lib/Param/Bool.py b/lib/Param/Bool.py new file mode 100644 index 0000000..7b779cb --- /dev/null +++ b/lib/Param/Bool.py @@ -0,0 +1,19 @@ +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/Bool/__init__.py b/lib/Param/Bool/__init__.py deleted file mode 100644 index 7b779cb..0000000 --- a/lib/Param/Bool/__init__.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 new file mode 100644 index 0000000..9163338 --- /dev/null +++ b/lib/Param/Color.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/Color/__init__.py b/lib/Param/Color/__init__.py deleted file mode 100644 index 9163338..0000000 --- a/lib/Param/Color/__init__.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 new file mode 100644 index 0000000..3923a64 --- /dev/null +++ b/lib/Param/Enum.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/Enum/__init__.py b/lib/Param/Enum/__init__.py deleted file mode 100644 index 3923a64..0000000 --- a/lib/Param/Enum/__init__.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 new file mode 100644 index 0000000..0581815 --- /dev/null +++ b/lib/Param/Float.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/Float/__init__.py b/lib/Param/Float/__init__.py deleted file mode 100644 index 0581815..0000000 --- a/lib/Param/Float/__init__.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 new file mode 100644 index 0000000..55bf353 --- /dev/null +++ b/lib/Param/Img_url.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/Img_url/__init__.py b/lib/Param/Img_url/__init__.py deleted file mode 100644 index 55bf353..0000000 --- a/lib/Param/Img_url/__init__.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 new file mode 100644 index 0000000..0a73cc1 --- /dev/null +++ b/lib/Param/Int.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/Int/__init__.py b/lib/Param/Int/__init__.py deleted file mode 100644 index 0a73cc1..0000000 --- a/lib/Param/Int/__init__.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 new file mode 100644 index 0000000..28a7126 --- /dev/null +++ b/lib/Param/Json.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/Json/__init__.py b/lib/Param/Json/__init__.py deleted file mode 100644 index 28a7126..0000000 --- a/lib/Param/Json/__init__.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 new file mode 100644 index 0000000..f8adaab --- /dev/null +++ b/lib/Param/Raw.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/Raw/__init__.py b/lib/Param/Raw/__init__.py deleted file mode 100644 index f8adaab..0000000 --- a/lib/Param/Raw/__init__.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 new file mode 100644 index 0000000..8f08e49 --- /dev/null +++ b/lib/Param/String.py @@ -0,0 +1,15 @@ +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/String/__init__.py b/lib/Param/String/__init__.py deleted file mode 100644 index 8f08e49..0000000 --- a/lib/Param/String/__init__.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) -- cgit v1.2.3-70-g09d2