diff options
30 files changed, 760 insertions, 0 deletions
@@ -1,2 +1,3 @@ *.sw* *.pyc +*.ropeproject* diff --git a/example.py b/example.py new file mode 100755 index 0000000..4fd6965 --- /dev/null +++ b/example.py @@ -0,0 +1,37 @@ +#!/usr/bin/python2.7 +import sys +from ricky.impattern import ImPattern +from ricky.imgrid import ImGrid +from ricky.imbreak import ImBreak +from ricky.imgradient import ImGradient +import pprint + +#api = ImPattern() +#params = api.params_init() +#params.randomize() +#print params.as_hash() +#req = params.execute() +#print req + +#api = ImGrid() +#params = api.params_init() +#params.randomize() +#print params.as_hash() +#req = params.execute() +#print req + + + +#api = ImBreak() +#params = api.params_init() +#params.randomize() +#print params.as_hash() +#req = params.execute() +#print req + +api = ImGradient() +params = api.params_init() +params.randomize() +print params.as_hash() +req = params.execute() +print req diff --git a/ricky/__init__.py b/ricky/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/ricky/__init__.py diff --git a/ricky/api.py b/ricky/api.py new file mode 100644 index 0000000..93fa301 --- /dev/null +++ b/ricky/api.py @@ -0,0 +1,29 @@ +import urllib +import urllib2 +import sys +import random +import simplejson as json +import urllib2 + +class Api: + 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", + "Accept": "text/plain" + } + try: + req = urllib2.Request(url, params, headers) + response = urllib2.urlopen(req) + return response.read() + except Exception as e: + sys.stderr.write(str(e)) + raise + def call(self, params): + if not(params.is_ready()): + raise Exception("Api Params Not Ready") + return json.loads(self.post_request(self.url, params.as_hash())) diff --git a/ricky/config.py b/ricky/config.py new file mode 100644 index 0000000..d4ebb23 --- /dev/null +++ b/ricky/config.py @@ -0,0 +1,7 @@ +USERNAME = "RICHARD_GIOVANNI" +TEST_URL = "http://i.asdf.us/im/fc/imBreak5qI6DN2_1425433157_.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" +IMGRADIENT_URL = "http://asdf.us/im/api/imgradient" +IMBREAK_URL = "http://asdf.us/im/api/imbreak" diff --git a/ricky/imbreak/__init__.py b/ricky/imbreak/__init__.py new file mode 100644 index 0000000..752d977 --- /dev/null +++ b/ricky/imbreak/__init__.py @@ -0,0 +1 @@ +from ricky.imbreak.imbreak import ImBreak diff --git a/ricky/imbreak/imbreak.py b/ricky/imbreak/imbreak.py new file mode 100644 index 0000000..a9a4c95 --- /dev/null +++ b/ricky/imbreak/imbreak.py @@ -0,0 +1,12 @@ +from ricky.api import Api +from ricky.imbreak.params import ImBreakParams +from ricky.config import IMBREAK_URL + +class ImBreak(Api): + def __init__(self): + self.url = IMBREAK_URL + def params_init(self): + new_params = ImBreakParams() + #new_params = self.get_from_server() + new_params.api = self + return new_params diff --git a/ricky/imbreak/params.py b/ricky/imbreak/params.py new file mode 100644 index 0000000..e5de5c7 --- /dev/null +++ b/ricky/imbreak/params.py @@ -0,0 +1,57 @@ +import re, random +from ricky.params import Params +from ricky.param.option import Option +from ricky.param.options import Options +from ricky.param.username import Username +from ricky.param.imageurl import ImageUrl +from ricky.param.multiselect import MultiSelect +from ricky.param.numberrange import NumberRange +from ricky.param.color import Color +from ricky.config import PATTERN_BASE_URL + + +breaktype_options = Options.from_dict( + {"value":"CLASSIC", "weight": 1}, + {"value":"REDUX", "weight": 1}, + {"value":"BLURRY_BREAK", "weight": 1}, + {"value":"BLURRY_BREAK_2", "weight": 1}, + {"value":"SWIPE", "weight": 1}, + {"value":"RGB_WASH", "weight": 1}, + {"value":"RGB_WASH_2", "weight": 1}, + {"value":"NOISY_BREAK", "weight": 1}, + {"value":"BROKEN_VIGNETTE", "weight": 1}, + {"value":"FAX_MACHINE", "weight": 1}, + {"value":"STRIPES", "weight": 1}, + {"value":"PHOTOCOPY", "weight": 1}, +) +breakmode_options = Options.from_dict( + {"value":"extreme", "weight": 1}, + {"value":"subtle", "weight": 1}, +) +finalformat_options = Options.from_dict( + {"value":"png", "weight": 5}, + {"value":"jpg", "weight": 2}, + {"value":"gif", "weight": 2}, +) +breakangle_options = Options.from_dict( + {"value":0, "weight": 9}, + {"value":90, "weight": 2}, + {"value":-180, "weight": 2}, + {"value":180, "weight": 2}, +) +expanded_options = Options.from_dict( + {"value": "" , "weight": 11 }, + {"value": 1, "weight": 2} +) + +class ImBreakParams(Params): + def __init__(self): + self.params = [ + Username(name="username", required=0), + ImageUrl(name="url", required=1), + MultiSelect(name="finalformat", required=0, options=finalformat_options), + MultiSelect(name="breaktype", required=1, options=breaktype_options), + NumberRange(name="breakangle", required=0, options=breakangle_options, min=-180, max=180), + MultiSelect(name="breakmode", required=1, options=breakmode_options), + MultiSelect(name="expanded", required=0, options=expanded_options), + ] diff --git a/ricky/imgradient/__init__.py b/ricky/imgradient/__init__.py new file mode 100644 index 0000000..72efca1 --- /dev/null +++ b/ricky/imgradient/__init__.py @@ -0,0 +1 @@ +from ricky.imgradient.imgradient import ImGradient diff --git a/ricky/imgradient/imgradient.py b/ricky/imgradient/imgradient.py new file mode 100644 index 0000000..c7be83d --- /dev/null +++ b/ricky/imgradient/imgradient.py @@ -0,0 +1,12 @@ +from ricky.api import Api +from ricky.imgradient.params import ImGradientParams +from ricky.config import IMGRADIENT_URL + +class ImGradient(Api): + def __init__(self): + self.url = IMGRADIENT_URL + def params_init(self): + new_params = ImGradientParams() + #new_params = self.get_from_server() + new_params.api = self + return new_params diff --git a/ricky/imgradient/options.py b/ricky/imgradient/options.py new file mode 100644 index 0000000..29091ce --- /dev/null +++ b/ricky/imgradient/options.py @@ -0,0 +1,83 @@ +from ricky.param.options import Options +width_options = Options.from_dict( + { "value" : 400, "weight" : 1 }, + { "value" : 600, "weight" : 1 }, +) +height_options = Options.from_dict( + { "value" : 400, "weight" : 1 }, + { "value" : 600, "weight" : 1 }, +) +color1_options = Options.from_dict( + { "value" : "black", "weight" : 1 }, + { "value" : "white", "weight" : 2 }, +) +color2_options = Options.from_dict( + { "value" : "black", "weight" : 2 }, + { "value" : "white", "weight" : 1 }, +) +stripes_options = Options.from_dict( + {"value":"true", "weight": 1}, + {"value":"false", "weight": 1}, +) +stripenumber_options = Options.from_dict( + {"value":1, "weight": 50}, + {"value":2, "weight": 50}, +) +# contrast_options = \ +stripeintensity_options = \ + brightness_options = \ + saturation_options = \ + hue_options = \ + Options.from_dict( + {"value": "", "weight": 300}, +) +halftone_options = Options.from_dict( + { "value" : "checkeredfade", "weight": 1 }, + { "value" : "etchedtransition", "weight": 1 }, + { "value" : "bendaydots", "weight": 1 }, + { "value" : "smallerdots1", "weight": 1 }, + { "value" : "smallerdots2", "weight": 1 }, + { "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 }, +) + +blurriness_options = \ + percentbeveled_options = Options.from_dict( + { "value" : "", "weight": 4 }, +) +rotate_options = \ + tilt_options = Options.from_dict( + {"value":"", "weight": 9}, + {"value":90, "weight": 2}, + {"value":180, "weight": 2}, + {"value":270, "weight": 2}, +) +flop_options = flip_options = Options.from_dict( + {"value":"", "weight": 1}, + {"value":"true", "weight": 1}, +) + +filetype_options = Options.from_dict( + {"value":"png", "weight": 5}, + {"value":"jpg", "weight": 2}, + {"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 }, +) diff --git a/ricky/imgradient/params.py b/ricky/imgradient/params.py new file mode 100644 index 0000000..88b7d46 --- /dev/null +++ b/ricky/imgradient/params.py @@ -0,0 +1,45 @@ +import re, random +from ricky.params import Params +from ricky.param import Param +from ricky.param.option import Option +from ricky.param.options import Options +from ricky.param.username import Username +from ricky.param.imageurl import ImageUrl +from ricky.param.multiselect import MultiSelect +from ricky.param.numberrange import NumberRange +from ricky.param.color import Color + +from ricky.imgradient.options import * + + + +class ImGradientParams(Params): + def __init__(self): + self.params = [ + Username(name="username", required=0), + NumberRange(name="width", required=1, options=width_options, min=100, max=800), + NumberRange(name="height", required=1, options=height_options, min=100, max=800), + Color(name="color1", required=1, options=color1_options), + Color(name="color2", required=1, options=color2_options), + MultiSelect(name="filetype", required=0, options=filetype_options), + MultiSelect(name="gradienttype", required=1, options=gradienttype_options), + MultiSelect(name="halftone", required=0, options=halftone_options), + MultiSelect(name="bevel", required=0, options=bevel_options), + + NumberRange(name="stripenumber", required=0, options=stripenumber_options, min=0, max=400), + NumberRange(name="stripeintensity", required=0, options=stripeintensity_options, min=0, max=5000), + + NumberRange(name="blurriness", required=0, options=blurriness_options, min=0, max=200), +# NumberRange(name="contrast", required=0, options=contrast_options, min=0, max=200), + NumberRange(name="brightness", required=0, options=brightness_options, min=0, max=200), + NumberRange(name="saturation", required=0, options=saturation_options, min=0, max=200), + NumberRange(name="hue", required=0, options=hue_options, min=0, max=200), + + NumberRange(name="percentbeveled", required=0, options=percentbeveled_options, min=0, max=100), + NumberRange(name="rotate", required=0, options=rotate_options, min=0, max=360), + NumberRange(name="tilt", required=0, options=tilt_options, min=0, max=360), + + + MultiSelect(name="flop", required=0, options=flop_options), + MultiSelect(name="flip", required=0, options=flip_options), + ] diff --git a/ricky/imgrid/__init__.py b/ricky/imgrid/__init__.py new file mode 100644 index 0000000..1ff6788 --- /dev/null +++ b/ricky/imgrid/__init__.py @@ -0,0 +1 @@ +from imgrid import ImGrid diff --git a/ricky/imgrid/imgrid.py b/ricky/imgrid/imgrid.py new file mode 100644 index 0000000..6872537 --- /dev/null +++ b/ricky/imgrid/imgrid.py @@ -0,0 +1,12 @@ +from ricky.api import Api +from ricky.imgrid.params import ImGridParams +from ricky.config import IMGRID_URL + +class ImGrid(Api): + def __init__(self): + self.url = IMGRID_URL + def params_init(self): + new_params = ImGridParams() + #new_params = self.get_from_server() + new_params.api = self + return new_params diff --git a/ricky/imgrid/options.py b/ricky/imgrid/options.py new file mode 100755 index 0000000..8ada475 --- /dev/null +++ b/ricky/imgrid/options.py @@ -0,0 +1,64 @@ +from ricky.param.options import Options +format_options = Options.from_dict( + { 'weight': 20, 'value': 'png' }, + { 'weight': 0, 'value': 'gif' }, + { 'weight': 0, 'value': 'jpg' }, +) +transition_options = Options.from_dict( + { "value" : "background", "weight": 1 }, + { "value" : "dither", "weight": 1 }, + { "value" : "random", "weight": 1 }, + { "value" : "tile", "weight": 1 }, + { "value" : "edge", "weight": 1 }, +) +skycolor_colors = \ + bgcolor_colors = planebgcolor_colors = Options.from_dict( + { "value" : "white", "weight" : 1 }, + { "value" : "silver", "weight" : 1 }, + { "value" : None, "weight" : 10 }, +) + +linecolor_colors = Options.from_dict( + { "value" : "black", "weight" : 1 }, + { "value" : "white", "weight" : 1 }, + { "value" : "silver", "weight" : 1 }, +) +swing_options = tilt_options = roll_options = Options.from_dict( + {"value": "", "weight": 2}, + {"value": 30, "weight": 1}, + {"value": -30, "weight": 1}, +) +width_options = height_options = Options.from_dict( + { "value" : 400, "weight" : 1 }, + { "value" : 600, "weight" : 1 }, +) +linethickness_options = Options.from_dict( + {"value":1, "weight": 2}, + {"value":2, "weight": 1}, +) +opacity_options = Options.from_dict( + {"value":1, "weight": 2}, + {"value":0.5, "weight": 1}, +) +spacing_options = Options.from_dict( + {"value":10, "weight": 1}, + {"value":15, "weight": 1}, +) +vlines_options = hlines_options = Options.from_dict( + {"value":"", "weight": 2}, + {"value":"true", "weight": 1}, +) +shadow_options = Options.from_dict( + {"value":"", "weight": 1}, + {"value":"true", "weight": 1}, +) +zoom_options = Options.from_dict( + {"value": 0, "weight": 3}, + {"value": 1.2, "weight": 1}, + {"value": -1.2, "weight": 1}, +) +trim_options = Options.from_dict( + {"value":"", "weight": 1}, + {"value":"true", "weight": 1}, +) + diff --git a/ricky/imgrid/params.py b/ricky/imgrid/params.py new file mode 100644 index 0000000..8b63759 --- /dev/null +++ b/ricky/imgrid/params.py @@ -0,0 +1,111 @@ +import re, random +from ricky.params import Params +from ricky.param import Param +from ricky.param.option import Option +from ricky.param.options import Options +from ricky.param.username import Username +from ricky.param.imageurl import ImageUrl +from ricky.param.multiselect import MultiSelect +from ricky.param.numberrange import NumberRange +from ricky.param.color import Color + +from ricky.imgrid.options import * + +class Param_Zoom(NumberRange): + def __init__(self, **kwargs): + super(Param_Zoom, self).__init__(**kwargs) + self.exclusion_range = kwargs['exclusion_range'] + def test_value(self): + return not ((self.value > self.exclusion_range[0]) and (self.value < self.exclusion_range[1])) + def randomize(self): + weights_total = sum(map(lambda x: x["weight"], self.options())) + 10 + choice = random.randint(0, weights_total) + position = 0 + for elem in self.options(): + position += elem["weight"] + if position >= choice: + self.value = elem["value"] + return + max_tries = 10000 + while(max_tries): + self.value = round(random.uniform(self.range_max, self.range_min), 2) + if self.test_value: return + max_tries -= 1; + raise ValueError + + @property + def value(self): + return super(MultiSelect, self).get_value() + @value.setter + def value(self, value): + self._value = value + if not self._value is None: + self.is_ready = 1 + self.set_by_user = 1 + +class Param_Opacity(NumberRange): + def __init__(self, **kwargs): + super(Param_Opacity, self).__init__(**kwargs) + def randomize(self): + weights_total = sum(map(lambda x: x["weight"], self.options()) + self.range_max - self.range_min) + choice = random.randint(0, weights_total) + position = 0 + for elem in self.options(): + position += elem["weight"] + if position >= choice: + self.value = elem["value"] + return + self.value = round(random.uniform(self.range_min,range_max), 2), + +class ImGridParams(Params): + def __init__(self): + self.params = [ + Username(name="username", required=0), + ImageUrl(name="bgimage", required=0), + ImageUrl(name="imageinstead", required=0), + ImageUrl(name="planebgimage", required=0), + MultiSelect(name="format", required=0, options=format_options), + MultiSelect(name="transition", required=1, options=transition_options), + Color(name="skycolor", required=0, options=skycolor_colors), + Color(name="planebgcolor", required=0, options=planebgcolor_colors), + Color(name="bgcolor", required=0, options=bgcolor_colors), + Color(name="linecolor", required=0, options=linecolor_colors), + NumberRange(name="swing", required=0, options=swing_options, min=-170, max=170), + NumberRange(name="tilt", required=0, options=tilt_options, min=-170, max=170), + NumberRange(name="roll", required=0, options=roll_options, min=-170, max=170), + NumberRange(name="width", required=0, options=width_options, min=100, max=800), + NumberRange(name="height", required=0, options=height_options, min=100, max=800), + NumberRange(name="linethickness", required=0, options=linethickness_options, min=1, max=30), + NumberRange(name="opacity", required=0, options=opacity_options, min=0, max=1), + NumberRange(name="spacing", required=0, options=spacing_options, min=2, max=100), + MultiSelect(name="vlines", required=0, options=vlines_options), + MultiSelect(name="hlines", required=0, options=hlines_options), + MultiSelect(name="trim", required=0, options=trim_options), + MultiSelect(name="shadow", required=0, options=shadow_options), + Param_Zoom(name="zoom", required=0, options=zoom_options, min=-12, max=12, exclusion_range=[-1.1, 1.1]), + ] + def test_values(self): + p = self.params + return not any([ + (self.param('spacing').value > self.param('width').value), + (self.param('spacing').value > self.param('height').value), + (self.param('linethickness').value > self.param('width').value), + (self.param('linethickness').value > self.param('height').value), + ]) + def randomize(self): + p = self.params + for el in p: + if el in ['spacing', 'linethickness']: + continue + if el.set_by_user: + continue + el.randomize() + for name in ['spacing', 'linethickness']: + max_tries = 10000 + while(max_tries): + self.param(name).randomize() + if self.test_values(): + return + max_tries -= 1 + raise ValueError + diff --git a/ricky/impattern/__init__.py b/ricky/impattern/__init__.py new file mode 100755 index 0000000..63a540e --- /dev/null +++ b/ricky/impattern/__init__.py @@ -0,0 +1 @@ +from impattern import ImPattern diff --git a/ricky/impattern/impattern.py b/ricky/impattern/impattern.py new file mode 100755 index 0000000..847ae84 --- /dev/null +++ b/ricky/impattern/impattern.py @@ -0,0 +1,12 @@ +from ricky.api import Api +from ricky.impattern.params import ImPatternParams +from ricky.config import IMPATTERN_URL + +class ImPattern(Api): + def __init__(self): + self.url = IMPATTERN_URL + def params_init(self): + new_params = ImPatternParams() + #new_params = self.get_from_server() + new_params.api = self + return new_params diff --git a/ricky/impattern/params.py b/ricky/impattern/params.py new file mode 100644 index 0000000..34d70bf --- /dev/null +++ b/ricky/impattern/params.py @@ -0,0 +1,33 @@ +import re +from ricky.params import Params +from ricky.param import Param +from ricky.param.option import Option +from ricky.param.options import Options +from ricky.param.username import Username +from ricky.param.imageurl import ImageUrl +from ricky.param.multiselect import MultiSelect + +from ricky.config import PATTERN_BASE_URL + +class Pattern_UrlOption(Option): + def __init__(self, **kwargs): + super(Pattern_UrlOption, self).__init__(**kwargs) + @classmethod + def from_name(cls, **kwargs): + formatted = "{}/{}.png".format(PATTERN_BASE_URL, kwargs["value"]) + return cls(weight=kwargs["weight"], value=formatted ) + +class ImPatternParams(Params): + def __init__(self): + self.params = [ + Username(name="username", required=0), + ImageUrl(name="imageurl", required=1), + MultiSelect(name="pattern_url", required=1, options=pattern_url_options) + ] + +pattern_url_options = Options(*[ + Pattern_UrlOption.from_name(weight=0, value=i) for i in range(1,100) ] + [ + Pattern_UrlOption.from_name(weight=0, value="a{}".format(i)) for i in range(0, 42) +]) + +pattern_url_options.search("a10").weight = 20; diff --git a/ricky/param/__init__.py b/ricky/param/__init__.py new file mode 100644 index 0000000..5149889 --- /dev/null +++ b/ricky/param/__init__.py @@ -0,0 +1 @@ +from ricky.param.param import Param diff --git a/ricky/param/color.py b/ricky/param/color.py new file mode 100644 index 0000000..8980236 --- /dev/null +++ b/ricky/param/color.py @@ -0,0 +1,30 @@ +from ricky.param.multiselect import MultiSelect +import random +class Color(MultiSelect): + def __init__(self, **kwargs): + super(Color, self).__init__(**kwargs) + + @classmethod + def from_rgb(cls, r,g,b): + return cls(value="rgb({},{},{})".format(r,g,b)) + + @property + def value(self): + return super(MultiSelect, self).get_value() + @value.setter + def value(self, value): + self._value = value + if not self._value is None: + self.is_ready = 1 + self.set_by_user = 1 + + def randomize(self): + weights_total = sum(map(lambda x: x["weight"], self.options())) + (255 * 255 * 255) + choice = random.randint(0, weights_total) + position = 0 + for elem in self.options(): + position += elem["weight"] + if position >= choice: + self.value = elem["value"] + return + self.value = "rgb({},{},{})".format( random.randint(0,255), random.randint(0,255), random.randint(0,255)) diff --git a/ricky/param/imageurl.py b/ricky/param/imageurl.py new file mode 100644 index 0000000..8c26e8f --- /dev/null +++ b/ricky/param/imageurl.py @@ -0,0 +1,7 @@ +from ricky.config import TEST_URL +from ricky.param.string import String + +class ImageUrl(String): + def __init__(self, *args, **kwargs): + super(ImageUrl, self).__init__(*args, **kwargs) + self.default(TEST_URL) diff --git a/ricky/param/multiselect.py b/ricky/param/multiselect.py new file mode 100644 index 0000000..d7db80f --- /dev/null +++ b/ricky/param/multiselect.py @@ -0,0 +1,56 @@ +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()) + + + def options(self): + return self._options + def _validate_options(self): + try: + int(self._options[0]['weight']) + self._options[0]['value'] + except Exception as e: + raise ValueError + + 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 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 + + 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() + def heaviest(self): + self.value = self._choose_heaviest() diff --git a/ricky/param/numberrange.py b/ricky/param/numberrange.py new file mode 100644 index 0000000..7e01a81 --- /dev/null +++ b/ricky/param/numberrange.py @@ -0,0 +1,27 @@ +import sys +from ricky.param.multiselect import MultiSelect +import random +class NumberRange(MultiSelect): + def __init__(self, **kwargs): + super(NumberRange, self).__init__(**kwargs) + self.range_min = kwargs['min'] + self.range_max = kwargs['max'] + def randomize(self): + weights_total = sum(map(lambda x: x["weight"], self.options())) + self.range_max - self.range_min + choice = random.randint(0, weights_total) + position = 0 + for elem in self.options(): + position += elem["weight"] + if position >= choice: + self.value = elem["value"] + return + self.value = random.randint(self.range_min,self.range_max) + @property + def value(self): + return super(MultiSelect, self).get_value() + @value.setter + def value(self, value): + self._value = value + if not self._value is None: + self.is_ready = 1 + self.set_by_user = 1 diff --git a/ricky/param/option.py b/ricky/param/option.py new file mode 100644 index 0000000..89bd5db --- /dev/null +++ b/ricky/param/option.py @@ -0,0 +1,9 @@ +class Option(dict): + def __init__(self, **kwargs): + super(Option, self).__init__(**kwargs) + self.value = kwargs["value"] + self.weight = kwargs["weight"] + def __getattr__(self, attr): + return self.get(attr) + __setattr__= dict.__setitem__ + __delattr__= dict.__delitem__ diff --git a/ricky/param/options.py b/ricky/param/options.py new file mode 100644 index 0000000..2a21cce --- /dev/null +++ b/ricky/param/options.py @@ -0,0 +1,21 @@ +from ricky.param.option import Option +import sys +class Options: + def __init__(self, *args): + self._values = args + def __iter__(self): + return iter(self._values) + def __len__(self): + return len(self._values) + def __str__(self): + return str(self._values) + def __getitem__(self, i): + return self._values[i] + def search(self, s): + for i in self: + if str(s) in i.value: + return i + @classmethod + def from_dict(cls, *args): + options = map(lambda x: Option(**x), args); + return cls(*options); diff --git a/ricky/param/param.py b/ricky/param/param.py new file mode 100644 index 0000000..05388f8 --- /dev/null +++ b/ricky/param/param.py @@ -0,0 +1,38 @@ +import pprint + +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 __str__(self): + return pprint.pformat(vars(self)) + + 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) + + def default(self, value): + self._value_default = value + + @property + def is_ready(self): + return self._is_ready or not self.required + @is_ready.setter + def is_ready(self, n): + self._is_ready = n + + + def randomize(self): + pass diff --git a/ricky/param/string.py b/ricky/param/string.py new file mode 100644 index 0000000..10e289a --- /dev/null +++ b/ricky/param/string.py @@ -0,0 +1,4 @@ +from ricky.param import Param +class String(Param): + def __init__(self, **kwargs): + super(String, self).__init__(**kwargs) diff --git a/ricky/param/username.py b/ricky/param/username.py new file mode 100644 index 0000000..e5023ec --- /dev/null +++ b/ricky/param/username.py @@ -0,0 +1,7 @@ +from ricky.config import USERNAME +from ricky.param.string import String + +class Username(String): + def __init__(self, **kwargs): + super(Username, self).__init__(**kwargs) + self.default(USERNAME) diff --git a/ricky/params.py b/ricky/params.py new file mode 100644 index 0000000..178643a --- /dev/null +++ b/ricky/params.py @@ -0,0 +1,41 @@ +import sys +import pprint +class Params(object): + def __init__(self): + self._api = None + def param(self, name): + for p in self.params: + if p.name == name: + return p + return None + def __str__(self): + return pprint.pformat({ "params": map(lambda x: vars(x), self.params) }) + + + def randomize(self): + for el in self.params: + if el.set_by_user: + continue + el.randomize() + + @property + def api(self): + return self._api + @api.setter + def api(self, cls): + self._api = cls + + def execute(self): + return self.api.call(self) + + 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 as_hash(self): + result = {} + for p in self.params: + result[p.name] = p.value + return result |
