summaryrefslogtreecommitdiff
path: root/ricky
diff options
context:
space:
mode:
Diffstat (limited to 'ricky')
-rw-r--r--ricky/config.py6
-rw-r--r--ricky/im.py13
-rw-r--r--ricky/imgradient/options.py49
-rw-r--r--ricky/imgrid/params.py2
-rw-r--r--ricky/param/color.py2
-rw-r--r--ricky/param/multiselect.py86
-rw-r--r--ricky/param/numberrange.py2
-rw-r--r--ricky/param/param.py58
-rw-r--r--ricky/params.py89
9 files changed, 174 insertions, 133 deletions
diff --git a/ricky/config.py b/ricky/config.py
index 3aeba81..d68543c 100644
--- a/ricky/config.py
+++ b/ricky/config.py
@@ -1,5 +1,9 @@
+"""ALL GLOBAL CONSTANTS GO HERE"""
USERNAME = "RICHARD_GIOVANNI"
-TEST_URL = "http://i.asdf.us/im/65/imBreak5qI6DN2_14254-PbPattern_1444004782_pepper.png"
+TEST_URL = (
+ "http://i.asdf.us/im/"
+ "65/imBreak5qI6DN2_14254-PbPattern_1444004782_pepper.png"
+)
PATTERN_BASE_URL = "http://asdf.us/impattern/patterns"
IMPATTERN_URL = "http://asdf.us/im/api/impattern"
IMGRID_URL = "http://asdf.us/im/api/imgrid"
diff --git a/ricky/im.py b/ricky/im.py
index 715d825..159aa89 100644
--- a/ricky/im.py
+++ b/ricky/im.py
@@ -1,19 +1,23 @@
import urllib
import urllib2
import sys
-import random
import simplejson as json
-import urllib2
+
class Im:
def __init__(self):
self._required_keys = []
self.url = ""
+
def post_request(self, url, params):
params = urllib.urlencode(params)
headers = {
"Content-type": "application/x-www-form-urlencoded",
- "User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.94 Safari/537.36",
+ "User-Agent":(
+ "Mozilla/5.0 (X11; Linux x86_64) "
+ "AppleWebKit/537.36 (KHTML, like Gecko) "
+ "Chrome/40.0.2214.94 Safari/537.36"
+ ),
"Accept": "text/plain"
}
try:
@@ -23,7 +27,8 @@ class Im:
except Exception as e:
sys.stderr.write(str(e))
raise
+
def call(self, params):
if not(params.is_ready()):
- raise Exception("Im Params Not Ready")
+ raise Exception("Im Params Not Ready")
return json.loads(self.post_request(self.url, params.as_dict()))
diff --git a/ricky/imgradient/options.py b/ricky/imgradient/options.py
index 29091ce..0873c2c 100644
--- a/ricky/imgradient/options.py
+++ b/ricky/imgradient/options.py
@@ -1,19 +1,20 @@
from ricky.param.options import Options
+
width_options = Options.from_dict(
- { "value" : 400, "weight" : 1 },
- { "value" : 600, "weight" : 1 },
+ { "value" : 400, "weight" : 1 },
+ { "value" : 600, "weight" : 1 },
)
height_options = Options.from_dict(
- { "value" : 400, "weight" : 1 },
- { "value" : 600, "weight" : 1 },
+ { "value" : 400, "weight" : 1 },
+ { "value" : 600, "weight" : 1 },
)
color1_options = Options.from_dict(
- { "value" : "black", "weight" : 1 },
- { "value" : "white", "weight" : 2 },
+ { "value" : "black", "weight" : 1 },
+ { "value" : "white", "weight" : 2 },
)
color2_options = Options.from_dict(
- { "value" : "black", "weight" : 2 },
- { "value" : "white", "weight" : 1 },
+ { "value" : "black", "weight" : 2 },
+ { "value" : "white", "weight" : 1 },
)
stripes_options = Options.from_dict(
{"value":"true", "weight": 1},
@@ -40,14 +41,14 @@ halftone_options = Options.from_dict(
{ "value" : "flatstripes", "weight": 1 },
)
bevel_options = Options.from_dict(
- { "value" : "", "weight" : 4 },
- { "value" : "flatout", "weight" : 1 },
- { "value" : "flatinner", "weight" : 1 },
- { "value" : "evenlyframed", "weight" : 1 },
- { "value" : "biginner", "weight" : 1 },
- { "value" : "bigouter", "weight" : 1 },
- { "value" : "dramaticflatout", "weight" : 1 },
- { "value" : "dramaticflatinner", "weight" : 1 },
+ { "value" : "", "weight" : 4 },
+ { "value" : "flatout", "weight" : 1 },
+ { "value" : "flatinner", "weight" : 1 },
+ { "value" : "evenlyframed", "weight" : 1 },
+ { "value" : "biginner", "weight" : 1 },
+ { "value" : "bigouter", "weight" : 1 },
+ { "value" : "dramaticflatout", "weight" : 1 },
+ { "value" : "dramaticflatinner", "weight" : 1 },
)
blurriness_options = \
@@ -72,12 +73,12 @@ filetype_options = Options.from_dict(
{"value":"gif", "weight": 2},
)
gradienttype_options = Options.from_dict(
- { "value" : "canvas", "weight" : 1 },
- { "value" : "gradient", "weight" : 3 },
- { "value" : "radial", "weight" : 1 },
- { "value" : "colorspace", "weight" : 1 },
- { "value" : "plasmawash", "weight" : 1 },
- { "value" : "gradientwash", "weight" : 1 },
- { "value" : "mirrored", "weight" : 1 },
- { "value" : "noise", "weight" : 1 },
+ { "value" : "canvas", "weight" : 1 },
+ { "value" : "gradient", "weight" : 3 },
+ { "value" : "radial", "weight" : 1 },
+ { "value" : "colorspace", "weight" : 1 },
+ { "value" : "plasmawash", "weight" : 1 },
+ { "value" : "gradientwash", "weight" : 1 },
+ { "value" : "mirrored", "weight" : 1 },
+ { "value" : "noise", "weight" : 1 },
)
diff --git a/ricky/imgrid/params.py b/ricky/imgrid/params.py
index 0d78c08..2bcb367 100644
--- a/ricky/imgrid/params.py
+++ b/ricky/imgrid/params.py
@@ -35,7 +35,7 @@ class Param_Zoom(NumberRange):
@property
def value(self):
- return super(MultiSelect, self).get_value()
+ return super(MultiSelect, self).value_get()
@value.setter
def value(self, value):
self._value = value
diff --git a/ricky/param/color.py b/ricky/param/color.py
index 8980236..b331fb9 100644
--- a/ricky/param/color.py
+++ b/ricky/param/color.py
@@ -10,7 +10,7 @@ class Color(MultiSelect):
@property
def value(self):
- return super(MultiSelect, self).get_value()
+ return super(MultiSelect, self).value_get()
@value.setter
def value(self, value):
self._value = value
diff --git a/ricky/param/multiselect.py b/ricky/param/multiselect.py
index d7db80f..1db22f3 100644
--- a/ricky/param/multiselect.py
+++ b/ricky/param/multiselect.py
@@ -1,56 +1,60 @@
import random
from ricky.param import Param
+
class MultiSelect(Param):
def __init__(self, **kwargs):
- self._options = []
- if 'options' in kwargs: self._options = kwargs['options'] or []
- super(MultiSelect, self).__init__(**kwargs)
- if len(self._options):
- self._validate_options()
- self.default(self._choose_heaviest())
-
+ self._options = kwargs.get('options') or []
+ super(MultiSelect, self).__init__(**kwargs)
+ if len(self._options):
+ self._validate_options()
+ """default value is the option with the heaviest weight"""
+ self.default(self._choose_heaviest())
def options(self):
- return self._options
+ return self._options
+
def _validate_options(self):
- try:
- int(self._options[0]['weight'])
- self._options[0]['value']
- except Exception as e:
- raise ValueError
+ try:
+ int(self._options[0]['weight'])
+ self._options[0]['value']
+ except Exception:
+ raise ValueError('Unable to validate %s\n:' % self.name)
- def get_value(self):
- return super(MultiSelect, self).get_value()
- def set_value(self, value):
- if not any([ value == i['value'] for i in self._options ]) and value != None:
- raise ValueError
- super(MultiSelect, self).set_value(value)
- value = property(get_value, set_value)
+ def value_get(self):
+ return super(MultiSelect, self).value_get()
+ def value_set(self, value):
+ if not any([value == i['value'] for i in self._options]) and \
+ value is not None:
+ raise ValueError
+ super(MultiSelect, self).value_set(value)
+
+ value = property(value_get, value_set)
def randomize(self):
- weights_total = sum(map(lambda x: x["weight"], self.options()))
- choice = random.randint(0, weights_total)
- position = 0
- for elem in self.options():
- position += elem["weight"]
- if position >= choice:
- self.value = elem["value"]
- break
+ weights_total = sum(map(lambda x: x["weight"], self.options()))
+ choice = random.randint(0, weights_total)
+ position = 0
+ for elem in self.options():
+ position += elem["weight"]
+ if position >= choice:
+ self.value = elem["value"]
+ break
def _choose_heaviest(self):
- heaviest_idx = 0
- heaviest_weight = 0
- idx = 0
- if (len(self.options())):
- for elem in self.options():
- if elem["weight"] > heaviest_weight:
- heaviest_weight = elem["weight"]
- heaviest_idx = idx;
- idx += 1
- return self.options()[heaviest_idx]["value"]
- else:
- self.randomize()
+ heaviest_idx = 0
+ heaviest_weight = 0
+ idx = 0
+ if (len(self.options())):
+ for elem in self.options():
+ if elem["weight"] > heaviest_weight:
+ heaviest_weight = elem["weight"]
+ heaviest_idx = idx
+ idx += 1
+ return self.options()[heaviest_idx]["value"]
+ else:
+ self.randomize()
+
def heaviest(self):
- self.value = self._choose_heaviest()
+ self.value = self._choose_heaviest()
diff --git a/ricky/param/numberrange.py b/ricky/param/numberrange.py
index 7e01a81..37b85e3 100644
--- a/ricky/param/numberrange.py
+++ b/ricky/param/numberrange.py
@@ -18,7 +18,7 @@ class NumberRange(MultiSelect):
self.value = random.randint(self.range_min,self.range_max)
@property
def value(self):
- return super(MultiSelect, self).get_value()
+ return super(MultiSelect, self).value_get()
@value.setter
def value(self, value):
self._value = value
diff --git a/ricky/param/param.py b/ricky/param/param.py
index 05388f8..838da6b 100644
--- a/ricky/param/param.py
+++ b/ricky/param/param.py
@@ -1,38 +1,48 @@
import pprint
+import sys
class Param(object):
-# def __init__(self, **kwargs):
- def __init__(self, required=0, set_by_user=0, value=None, name=None, **kwargs):
- self._value_default = None
- self.name = name
- self.required = required
- self.is_ready = 0
- self.value = value
- self.set_by_user = set_by_user
+ def __init__(
+ self,
+ required=0,
+ set_by_user=0,
+ value=None,
+ name=None,
+ **kwargs):
+ self._value_default = None
+ self.name = name
+ self.required = required
+ self.is_ready = 0
+ self._value = value
+ self.set_by_user = set_by_user
+
def __str__(self):
- return pprint.pformat(vars(self))
+ return pprint.pformat(vars(self))
+
+ def value_get(self):
+ if self.set_by_user == 1:
+ return self._value
+ return self._value_default
+
+ def value_set(self, value):
+ self._value = value
+ sys.stderr.write("trying to set %s: %s \n" % (self.name, value))
+ if self._value:
+ self.is_ready = 1
+ self.set_by_user = 1
- def get_value(self):
- if self.set_by_user == 1:
- return self._value
- return self._value_default
- def set_value(self, value):
- self._value = value
- if not self._value is None:
- self.is_ready = 1
- self.set_by_user = 1
- value = property(get_value, set_value)
+ value = property(value_get, value_set)
def default(self, value):
- self._value_default = value
+ self._value_default = value
@property
def is_ready(self):
- return self._is_ready or not self.required
+ return self._is_ready or not self.required
+
@is_ready.setter
def is_ready(self, n):
- self._is_ready = n
-
+ self._is_ready = n
def randomize(self):
- pass
+ pass
diff --git a/ricky/params.py b/ricky/params.py
index 304ad2d..2b5e6ee 100644
--- a/ricky/params.py
+++ b/ricky/params.py
@@ -1,49 +1,66 @@
+"""base class for all params"""
import sys
import pprint
+
+
class Params(object):
- def __init__(self):
- self._api = None
- self._params = []
+ def __init__(self):
+ self._api = None
+ self._params = []
+
+ def param(self, name):
+ """getter for the param by name"""
+ for param in self._params:
+ if param.name == name:
+ return param
+ return None
- def param(self, name):
- for p in self._params:
- if p.name == name:
- return p
- return None
+ def __str__(self):
+ """string representation"""
+ return pprint.pformat({"params": map(lambda x: vars(x), self._params)})
- def __str__(self):
- return pprint.pformat({ "params": map(lambda x: vars(x), self._params) })
+ def randomize(self):
+ """assign random values to all params, taking into account weight"""
+ for param in self._params:
+ if param.set_by_user:
+ continue
+ param.randomize()
- def randomize(self):
- for el in self._params:
- if el.set_by_user:
- continue
- el.randomize()
- @property
- def api(self):
- return self._api
+ @property
+ def api(self):
+ """property setter for im api"""
+ return self._api
- @api.setter
- def api(self, cls):
- self._api = cls
+ @api.setter
+ def api(self, cls):
+ """property getter for im api"""
+ self._api = cls
+ def execute(self):
+ """calls the associated api"""
+ return self.api.call(self)
- def execute(self):
- return self.api.call(self)
+ def is_ready(self):
+ """test to see if any params with required values are missing"""
+ for param in self._params:
+ if not param.is_ready and not param.default:
+ sys.stderr.write("param not ready: %s\n" % param)
+ return 0
+ return 1
- def is_ready(self):
- for p in self._params:
- if not p.is_ready and not p.default:
- sys.stderr.write("param not ready: {}".format(p))
- return 0
- return 1
+ def __dict__(self):
+ result = {}
+ for param in self._params:
+ result[param.name] = param.value
+ return result
- def __dict__(self):
- result = {}
- for p in self._params:
- result[p.name] = p.value
- return result
+ def as_dict(self):
+ """alias for __dict__"""
+ return self.__dict__()
- def as_dict(self):
- return self.__dict__()
+ def from_dict(self, params_dict):
+ """set param values manually from a dictionary"""
+ for param in self._params:
+ if param.name in params_dict.keys():
+ param.value_set(params_dict[param.name])