diff options
Diffstat (limited to 'lib/param')
| -rw-r--r-- | lib/param/__init__.py | 76 | ||||
| -rw-r--r-- | lib/param/bool_.py | 2 | ||||
| -rw-r--r-- | lib/param/color.py | 2 | ||||
| -rw-r--r-- | lib/param/enum.py | 2 | ||||
| -rw-r--r-- | lib/param/float_.py | 2 | ||||
| -rw-r--r-- | lib/param/img_url.py | 179 | ||||
| -rw-r--r-- | lib/param/int_.py | 39 | ||||
| -rw-r--r-- | lib/param/json.py | 2 | ||||
| -rw-r--r-- | lib/param/param.py | 59 | ||||
| -rw-r--r-- | lib/param/raw.py | 16 | ||||
| -rw-r--r-- | lib/param/string.py | 32 |
11 files changed, 225 insertions, 186 deletions
diff --git a/lib/param/__init__.py b/lib/param/__init__.py index 10ea7a6..4f3daba 100644 --- a/lib/param/__init__.py +++ b/lib/param/__init__.py @@ -1,65 +1,11 @@ -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 Int -from param.raw import Raw -from param.bool_ import Bool -from param.enum import Enum -from param.json import Json -from param.color import Color -from param.float_ import Float -from param.img_url import Img_url -from param.string import String +"""imports for the param package""" +from .param import Param +from .int_ import Int +from .raw import Raw +from .bool_ import Bool +from .enum import Enum +from .json import Json +from .color import Color +from .float_ import Float +from .img_url import Img_url +from .string import String diff --git a/lib/param/bool_.py b/lib/param/bool_.py index 394dea6..fb688e3 100644 --- a/lib/param/bool_.py +++ b/lib/param/bool_.py @@ -1,5 +1,5 @@ """Defines the bool param type""" -from param import Param +from .param import Param import re class Bool(Param): """Defines the bool param type diff --git a/lib/param/color.py b/lib/param/color.py index 1c62955..096a1e8 100644 --- a/lib/param/color.py +++ b/lib/param/color.py @@ -1,5 +1,5 @@ """Defines the color param type""" -from param import Param +from .param import Param import re class Color(Param): """Defines the color param type diff --git a/lib/param/enum.py b/lib/param/enum.py index c68adc9..d28073d 100644 --- a/lib/param/enum.py +++ b/lib/param/enum.py @@ -1,6 +1,6 @@ """Defines the enum param type""" -from param import Param +from .param import Param class Enum(Param): """Defines the enum param type Args: diff --git a/lib/param/float_.py b/lib/param/float_.py index 88ed066..46b0b14 100644 --- a/lib/param/float_.py +++ b/lib/param/float_.py @@ -1,5 +1,5 @@ """Defines the float param type""" -from param import Param +from .param import Param class Float(Param): """Defines the float param type Args: diff --git a/lib/param/img_url.py b/lib/param/img_url.py index 4ce7ce7..69e3d66 100644 --- a/lib/param/img_url.py +++ b/lib/param/img_url.py @@ -1,98 +1,111 @@ +"""Img_url param class definition lives here""" import os -from param import Param -from config import * -import urllib, urllib2 +from .param import Param +from config import MAX_SIZE, SPECIAL_DOWNLOADERS, SPECIAL_DOWNLOADERS_MAX_SIZE,\ + BIN_IDENTIFY +import 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 Img_url(Param): - def __init__(self, value, key="", classname=""): - super(Img_url, self).__init__(classname=classname) - if value: - try: - self.filename = self._filename_temporary(key) + def __init__(self, value, key="", classname=""): + """Defines the float param type. + Takes in a url, sends a get request to the url, writes the response + to a temporary filename, and checks the mimetype with imagemagick. + Img_url class is different from other params in that it has + the attributes: + url: the original url used to retrieve the image + filename: the filename created to store the image + filepath: complete path to the stored image file + mimetype: the mimetype of the image + Args: + value: the image url string + key: the intended name of the param instance + classname: the name of the class to which the param belongs + """ + super(Img_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)) + 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 _filename_temporary(self, s): + return "_tmp-{}-{}_{}".format(self._classname, self._now, s) - def __getitem__(self, item): - return self.__dict__().__getitem__(item) + def __dict__(self): + return { + 'filename' : self.filename, + 'path': self.path, + 'url': self.url, + 'mimetype': self.mimetype + } - def __str__(self): - return str(self.__dict__()) + def __getitem__(self, item): + return self.__dict__().__getitem__(item) - def __nonzero__(self): - return True if self.path and self.mimetype else False + def __str__(self): + return str(self.__dict__()) - 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 __nonzero__(self): + return True if self.path and self.mimetype else False - 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 _image_download(self, url, path): + """downloads the image to the path specified in the local + filesystem""" + 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 _download(self, url, destination, max_size=MAX_SIZE): - response = self._browser_request(url, None) + def _browser_request(self, url, data=None): + """sends a get request to the url using browser User-Agent headers""" + headers = { + 'User-Agent': 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)', + 'Accept': '*/*', + } + try: + req = urllib2.Request(url, data, headers) + response = urllib2.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 - 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 _download(self, url, destination, max_size=MAX_SIZE): + """generic download method, checks the size of the filedata""" + response = self._browser_request(url, None) - 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; + 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): + """retrieves the image mimetype from the file header using imagemagick""" + 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 index b33c6b5..9d48518 100644 --- a/lib/param/int_.py +++ b/lib/param/int_.py @@ -1,18 +1,25 @@ -from param import Param +"""Int class definition lives here""" +from .param import Param class Int(Param): - def __init__(self, value, classname=""): - super(Int, 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) + def __init__(self, value, classname=""): + """Defines the float param type + Args: + value: the value of the Int + classname: the name of the class to which the param belongs + """ + super(Int, 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 index 08db3e9..4fa4933 100644 --- a/lib/param/json.py +++ b/lib/param/json.py @@ -1,5 +1,5 @@ """Defines the json param type""" -from param import Param +from .param import Param import simplejson as json class Json(Param): diff --git a/lib/param/param.py b/lib/param/param.py new file mode 100644 index 0000000..d1e1446 --- /dev/null +++ b/lib/param/param.py @@ -0,0 +1,59 @@ +"""param base class lives here, used for inheritance only""" +import time +import sys + +from config import WORKING_DIR + +class BadParamError(Exception): + pass + + +class Param(object): + """Defines the param base class, this class is used for inheritance only""" + def __init__(self, classname="", **kwargs): + self.value = None + 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))) + diff --git a/lib/param/raw.py b/lib/param/raw.py index 3d2f93f..72d6e0d 100644 --- a/lib/param/raw.py +++ b/lib/param/raw.py @@ -1,6 +1,14 @@ -from param import Param +"""Defines the raw param type""" +from .param import Param class Raw(Param): - def __init__(self, value, classname=""): - super(Raw, self).__init__(classname=classname) - self.value = value or None + """Defines the raw param type. + Basically, this is a catchall class, any input can go here, + so it needs to be used carefully (for security reasons). + Args: + value: can be any value + classname: name of the class to which the param instance belongs + """ + def __init__(self, value, classname=""): + super(Raw, self).__init__(classname=classname) + self.value = value or None diff --git a/lib/param/string.py b/lib/param/string.py index bd3d6d8..14e8e87 100644 --- a/lib/param/string.py +++ b/lib/param/string.py @@ -1,15 +1,21 @@ -from param import Param +"""String class definition lives here""" +from .param import Param import re -import sys class String(Param): - def __init__(self, value, classname=""): - super(String, 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) + """String param class definition + Args: + value: a string + classname: name of the class to which the param instance will belong + """ + def __init__(self, value, classname=""): + super(String, 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): + """Removes non-word characters from the string for security reasons""" + return re.sub(r'\W+', '', s) |
