diff options
Diffstat (limited to 'lib/param')
| -rw-r--r-- | lib/param/__init__.py | 11 | ||||
| -rw-r--r-- | lib/param/bool_.py | 24 | ||||
| -rw-r--r-- | lib/param/color.py | 26 | ||||
| -rw-r--r-- | lib/param/enum.py | 15 | ||||
| -rw-r--r-- | lib/param/float_.py | 24 | ||||
| -rw-r--r-- | lib/param/img_url.py | 106 | ||||
| -rw-r--r-- | lib/param/int_.py | 25 | ||||
| -rw-r--r-- | lib/param/json.py | 16 | ||||
| -rw-r--r-- | lib/param/param.py | 57 | ||||
| -rw-r--r-- | lib/param/raw.py | 14 | ||||
| -rw-r--r-- | lib/param/string.py | 21 |
11 files changed, 0 insertions, 339 deletions
diff --git a/lib/param/__init__.py b/lib/param/__init__.py deleted file mode 100644 index c7ea845..0000000 --- a/lib/param/__init__.py +++ /dev/null @@ -1,11 +0,0 @@ -"""imports for the param package""" -from .param import Param, BadParamError -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 deleted file mode 100644 index fb688e3..0000000 --- a/lib/param/bool_.py +++ /dev/null @@ -1,24 +0,0 @@ -"""Defines the bool param type""" -from .param import Param -import re -class Bool(Param): - """Defines the bool param type - Args: - value: the value of the bool (string or python bool) - classname: the name of the class to which the param belongs - """ - def __init__(self, value, classname=""): - super(Bool, self).__init__(classname=classname) - if value: - self.value = self._bool_correct(value) - else: - self.value = False - def _bool_correct(self, b): - if isinstance(b, str) or isinstance(b, unicode): - if re.match(r'true', b, re.IGNORECASE): - return True - elif re.match(r'false', b, re.IGNORECASE): - return False - elif isinstance(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 829a8f4..0000000 --- a/lib/param/color.py +++ /dev/null @@ -1,26 +0,0 @@ -"""Defines the color param type""" -from .param import Param -import re -class Color(Param): - """Defines the color param type - Args: - value: the value of the color (string) - classname: the name of the class to which the param belongs - """ - def __init__(self, value, classname=""): - super(Color, self).__init__(classname=classname) - if value: - 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 d28073d..0000000 --- a/lib/param/enum.py +++ /dev/null @@ -1,15 +0,0 @@ -"""Defines the enum param type""" - -from .param import Param -class Enum(Param): - """Defines the enum param type - Args: - value: the value of the param - enum_values: an array of possible value types - classname: name of the class that the param belongs to - """ - def __init__(self, value, enum_values=[], classname=""): - super(Enum, self).__init__(classname=classname) - if value and value not in enum_values: - 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 46b0b14..0000000 --- a/lib/param/float_.py +++ /dev/null @@ -1,24 +0,0 @@ -"""Defines the float param type""" -from .param import Param -class Float(Param): - """Defines the float param type - Args: - value: the value of the Float - classname: the name of the class to which the param belongs - """ - def __init__(self, value, classname=""): - self._classname = classname - super(Float, 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 84d6123..0000000 --- a/lib/param/img_url.py +++ /dev/null @@ -1,106 +0,0 @@ -"""Img_url param class definition lives here""" -import os -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 - -class Img_url(Param): - 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: - 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 - - 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): - """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 _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'): - self.err_warn('browser request error: %s - ERROR %s' % (url, e.code)) - raise IOError - return response - - def _download(self, url, destination, max_size=MAX_SIZE): - """generic download method, checks the size of the filedata""" - 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): - """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: - self.err_warn("Couldn't determine mimetype") diff --git a/lib/param/int_.py b/lib/param/int_.py deleted file mode 100644 index 9d48518..0000000 --- a/lib/param/int_.py +++ /dev/null @@ -1,25 +0,0 @@ -"""Int class definition lives here""" -from .param import Param - -class Int(Param): - 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 deleted file mode 100644 index c72cb11..0000000 --- a/lib/param/json.py +++ /dev/null @@ -1,16 +0,0 @@ -"""Defines the json param type""" -from .param import Param -import simplejson as json - -class Json(Param): - """Defines the json param type. Loads in a - json value and parses it. - - Args: - value: a json string - classname: name of the class to which the param belongs - """ - def __init__(self, value, classname=""): - super(Json, self).__init__(classname=classname) - if value: - self.value = json.loads(value) diff --git a/lib/param/param.py b/lib/param/param.py deleted file mode 100644 index 3268274..0000000 --- a/lib/param/param.py +++ /dev/null @@ -1,57 +0,0 @@ -"""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): - self._error_log(s) - raise BadParamError("%s - %s\n" % (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, fatal=False): - message = "ERROR - BAD PARAM" - if fatal: message += "- [FATAL] -" - sys.stderr.write("{}:{} - {}\n".format(message, self._classname, s)) - diff --git a/lib/param/raw.py b/lib/param/raw.py deleted file mode 100644 index 72d6e0d..0000000 --- a/lib/param/raw.py +++ /dev/null @@ -1,14 +0,0 @@ -"""Defines the raw param type""" -from .param import Param - -class Raw(Param): - """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 deleted file mode 100644 index 14e8e87..0000000 --- a/lib/param/string.py +++ /dev/null @@ -1,21 +0,0 @@ -"""String class definition lives here""" -from .param import Param -import re -class String(Param): - """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) |
