From f904b1b552134e6b8701d9d94c7fed321011ae94 Mon Sep 17 00:00:00 2001 From: yo mama Date: Fri, 11 Sep 2015 18:54:37 -0700 Subject: finished the params class, almost done with pb master class --- Pb/Break/.__init__.py.swl | Bin 0 -> 20480 bytes Pb/Break/.__init__.py.swm | Bin 0 -> 16384 bytes Pb/Break/__init__.py | 179 ++++++++++++++++++++++++++++++++ Pb/Break/__init__.py~ | 205 +++++++++++++++++++++++++++++++++++++ Pb/Breaker/__init__.py | 198 ----------------------------------- Pb/Generate/__init__.py | 2 +- Pb/Gradient/__init__.py | 2 +- Pb/Grid/__init__.py | 193 +++++++++++++++++++++++++++++++++++ Pb/Imgrid/__init__.py | 232 ------------------------------------------ Pb/Imlandscape/__init__.py | 87 ---------------- Pb/Imlandscape/landscape | 163 ----------------------------- Pb/Landscape/__init__.py | 87 ++++++++++++++++ Pb/Landscape/landscape | 163 +++++++++++++++++++++++++++++ Pb/Params/__init__.py | 10 -- Pb/Pattern/__init__.py | 6 +- Pb/__init__.py | 167 +++++++++--------------------- Pb/inspect_objects_example.py | 15 +++ 17 files changed, 897 insertions(+), 812 deletions(-) create mode 100644 Pb/Break/.__init__.py.swl create mode 100644 Pb/Break/.__init__.py.swm create mode 100755 Pb/Break/__init__.py create mode 100755 Pb/Break/__init__.py~ delete mode 100755 Pb/Breaker/__init__.py create mode 100755 Pb/Grid/__init__.py delete mode 100755 Pb/Imgrid/__init__.py delete mode 100755 Pb/Imlandscape/__init__.py delete mode 100755 Pb/Imlandscape/landscape create mode 100755 Pb/Landscape/__init__.py create mode 100755 Pb/Landscape/landscape delete mode 100644 Pb/Params/__init__.py create mode 100644 Pb/inspect_objects_example.py (limited to 'Pb') diff --git a/Pb/Break/.__init__.py.swl b/Pb/Break/.__init__.py.swl new file mode 100644 index 0000000..e2bd941 Binary files /dev/null and b/Pb/Break/.__init__.py.swl differ diff --git a/Pb/Break/.__init__.py.swm b/Pb/Break/.__init__.py.swm new file mode 100644 index 0000000..0fcc095 Binary files /dev/null and b/Pb/Break/.__init__.py.swm differ diff --git a/Pb/Break/__init__.py b/Pb/Break/__init__.py new file mode 100755 index 0000000..db9daaf --- /dev/null +++ b/Pb/Break/__init__.py @@ -0,0 +1,179 @@ +#!/usr/bin/python2.7 +import os +import sys +import random +import re +import urllib +import inspect +from Config import * +from Pb import Pb +from Params import Params + +DEFAULT_FINALFORMAT = "png"; + +_subtle_break_mark = 'pron' +_extreme_break_mark = 'sugar' + +_header_offset = 2000 + +class PbBreak(Pb): #FIXME in db gallery + def __init__(self, + url=None, + breaktype=None, + finalformat=DEFAULT_FINALFORMAT, + breakmode=None, + breakangle=None, + username=None, + expanded=None, + ): + + super(PbBreak,self).__init__(); + + self.params.set_val("breaktype", self._get_breaktype(breaktype), value_type="string"); + self.params.set_val("finalformat", finalformat, value_type="string") + self.params.set_val("breakmode", breakmode, value_type="enum", enum_values=["subtle", "extreme", "gradual"]) + self.params.set_val("breakangle", breakangle, value_type="float") + self.params.set_val("expanded", expanded, value_type="bool") + self.params.set_val("url", url, value_type="img_url"); + self._files_created.append(self.params.url['path']) + +# if not self.params.finalformat and self._gif_frames: +# self.params.finalformat = 'gif' +# if self.params.breaktype == 'miff': +# self.params.finalformat = 'jpg' +# self.params.breakmode = 'subtle' + #final filepath is stored in self.filepath + self.filename, self.filepath = self._filename_filepath_create(url=self.params.url['url']) + self._conversion_file = self._tempfilepath_create() + + def _get_breaktype(self, key): + #{{{ conversion table + breaktypeTranslate = { + 'CLASSIC':'jpg', + 'REDUX':'pcds', + 'BLURRY_BREAK':'viff', + 'BLURRY_BREAK_2':'mat', + 'SWIPE':'miff', + 'RGB_WASH':'psd', + 'RGB_WASH_2':'psb', + 'NOISY_BREAK':'palm', + 'NOISY_BREAK_2':'fig', + 'BROKEN_VIGNETTE':'pbm', + 'FAX_MACHINE':'cals', + 'STRIPES':'exr', + 'PHOTOCOPY':'art', + } + #}}} + return breaktypeTranslate[key] + +#{{{#########rotatefunctions####################################### + def _rotate(self): + cmd = [BIN_CONVERT,self.params.url['path'],"-rotate",self.params.breakangle,"+repage",self.params.url['path']] + self._call_cmd(cmd) + + def _rotate_back(self): + angle = str(360-int(self.params.breakangle)) + cmd = [BIN_CONVERT,self.filepath,"-rotate",angle,"+repage",self.filepath] + self._call_cmd(cmd) + if not self.params.expanded: + cmd = [BIN_CONVERT,self.filepath,"-gravity","Center","-crop","{}x{}+0+0".format( + self.width, self.height),"+repage",self.filepath] + self._call_cmd(cmd) +#}}} + + def _subtle_break(self): + #assume the header is no longer than _header_offset bytes + breakpoint = random.randint(_header_offset, len(self.file_data)) + newfile = self.file_data[0:breakpoint] \ + + _subtle_break_mark \ + + self.file_data[breakpoint:] + self.file_data = newfile[0:len(self.file_data)] + + def _extreme_break(self): + increment = len(self.file_data)/10; + i = 0 + newfile = ""; + for b in self.file_data: + if i > _header_offset and not (i % increment): + b += _extreme_break_mark + newfile += b + i += 1 + self.file_data = newfile[0:len(self.file_data)] + + def _choose_frame(self, filepath): + _gif_frames = PbBreak.gif_frames(filepath) + frame = random.choice(_gif_frames) + self._call_cmd([BIN_CONVERT, frame, filepath]) + + def _enforce_jpg(self): + if self.params.breaktype in [ "exr", "bmp", "miff" ] and not re.match(r'jpe?g$', self.params.url['mimetype'], re.IGNORECASE): + jpg_file = self.tempname_create(basename=self.basename, fmt="jpg") + self._call_cmd([BIN_CONVERT,self.params.url['path'], jpg_file]) + self._call_cmd(["rm",self.params.url['path']]) + #FIXME create logical separation between params and files created by the module + #FIXME add appropriate methods to the master class, to create temporary filenames, + #to create appropriate output names + #FIXME add flask methods when you can + self.params.url = { 'path': jpg_file, 'mimetype': "jpg" } + + def _first_conversion(self): + if self.params.url['mimetype'] == self.params.breaktype: + self.params.url['path'] = self._conversion_file + return + self._call_cmd([BIN_CONVERT, self.params.url['path'], self._conversion_file]) + self.files_created.append(self._conversion_file) + + def _prepare_filedata(self): + if self.params.url['mimetype'] == "gif": + self._choose_frame(self.params.url['path']) + if self.params.breakangle: + self._rotate() + self._enforce_jpg(); + self._first_conversion(); + self.file_data = self._file_read(self._conversion_file) + if not self.file_data: + self.err_warn("Unable to get file data"); + + def _add_false_data(self, breakmode): + if breakmode == "subtle": + self._subtle_break() + elif breakmode == "extreme": + self._extreme_break() + f = open(self._conversion_file, 'w') + f.write(self.file_data) + f.close(); + + def _final_conversion(self): + self._call_cmd( [BIN_CONVERT, self._conversion_file, self.filepath]) + def psd_psbfilepath(num): + return os.path.join(self._working_dir, "{}-{}.{}".format(self.basename, num, self.params.finalformat)) + if self.params.breaktype == 'psd': + self._call_cmd(['mv', psd_psbfilepath(1), self.filepath]) + self.files_created.append(psd_psbfilepath(0)) + if self.params.breaktype == 'psb': + self._call_cmd(['mv', psd_psbfilepath(0), self.filepath]) + self.files_created.append(psd_psbfilepath(1)) + if self.params.breakangle: + self._rotate_back() + + def create(self, breakmode=""): + if not breakmode: breakmode = self.params.breakmode + self._prepare_filedata(); + self._add_false_data(breakmode); + self._final_conversion() + self._cleanup() + + @classmethod + def test(cls): + TEST_PARAMS = { + "url" : "http://i.asdf.us/im/27/1424816234661dumpfmpfifferkinggr_1424816412_pfifferking.gif" , + "breaktype" : "RGB_WASH", + "finalformat" : "png", + "breakmode" : "extreme", + "breakangle" : "10", + "username" : "donkey", + "expanded" : "false" + } + b = cls(**TEST_PARAMS) + b.create(); + print b.filepath diff --git a/Pb/Break/__init__.py~ b/Pb/Break/__init__.py~ new file mode 100755 index 0000000..c1064e2 --- /dev/null +++ b/Pb/Break/__init__.py~ @@ -0,0 +1,205 @@ +#!/usr/bin/python2.7 +import os +import sys +import random +import re +import urllib +import inspect +from Config import * +from Pb import Pb +from Pb.Params import Params + +DEFAULT_FINALFORMAT = "png"; +SUBTLE_BREAK_MARK = 'pron' +EXTREME_BREAK_MARK = 'sugar' + +HEADER_OFFSET = 2000 + +class Breaker(Pb): + def __init__(self, url=None, breaktype=None, finalformat=DEFAULT_FINALFORMAT, + breakmode=None, breakangle=None, username=None, expanded=None, firsttime=None): + super(Breaker,self).__init__(); + + self.params.breaktype = self._get_breaktype(breaktype); + self.params.url = url + + _frame = inspect.currentframe(); + _args_vals = inspect.getargvalues(_frame); + for arg in _args_vals.args: + if arg == "self": + continue + sys.stderr.write(str(arg) + "\n") + try: + if arg not in ['breaktype', 'url']: + sys.stderr.write("Yoooo"); + sys.stderr.write(arg) + strarg = str(_args_vals.locals.get(arg)) + django self.bool_correct(strarg) + self.params.__setattr__("dingo", django) +# self.params.__setattr__(arg, self.bool_correct(_args_vals.locals.get(arg))) + except Exception as e: + sys.stderr.write("\n"+str(e)+"\n") + sys.exit(1); + self.params = Params(**self.params); + + self.tag = "imBreak" + self.commands = []; + self._now = self.now() + self.files_created = [] + + self.basename, self._first_format = self._get_filename_and_type_from_url(); + self._downloaded_file = self.tempname_create(basename=self.basename, fmt=self._first_format) + + try: + self.download(self.params.url, self._downloaded_file) + self.files_created.append(self._downloaded_file) + except Exception as e: + self.err_warn(str(e)) + + self._gif_frames = self.gif_frames(self._downloaded_file) + self._gif_frames = self._gif_frames if len(self._gif_frames) > 1 else False + self.width, self.height = self.dimensions(self._downloaded_file) # same here + + if not self.params.finalformat and self._gif_frames: + self.params.finalformat = 'gif' + if self.params.breaktype == 'miff': + self.params.finalformat = 'jpg' + self.params.breakmode = 'subtle' + #final filepath is stored in self.filepath + self.filename = "{}.{}".format(self.basename, self.params.finalformat) + self.filepath = os.path.join(self._working_dir, self.filename) + self._conversion_file = self.tempname_create(basename=self.basename, fmt=self.params.breaktype); + + def _call_cmd(self, cmd): + super(Breaker,self)._call_cmd(cmd, error) + self.commands.append(" ".join(cmd)); + + def _get_breaktype(self, key): + #{{{ conversion table + breaktypeTranslate = { + 'CLASSIC':'jpg', + 'REDUX':'pcds', + 'BLURRY_BREAK':'viff', + 'BLURRY_BREAK_2':'mat', + 'SWIPE':'miff', + 'RGB_WASH':'psd', + 'RGB_WASH_2':'psb', + 'NOISY_BREAK':'palm', + 'NOISY_BREAK_2':'fig', + 'BROKEN_VIGNETTE':'pbm', + 'FAX_MACHINE':'cals', + 'STRIPES':'exr', + 'PHOTOCOPY':'art', + } + #}}} + return breaktypeTranslate[key] + +#{{{#########rotatefunctions####################################### + def _rotate(self): + cmd = [BIN_CONVERT,self._downloaded_file,"-rotate",self.params.breakangle,"+repage",self._downloaded_file] + self._call_cmd(cmd) + + def _rotate_back(self): + angle = str(360-int(self.params.breakangle)) + cmd = [BIN_CONVERT,self.filepath,"-rotate",angle,"+repage",self.filepath] + self._call_cmd(cmd) + if not self.params.expanded: + cmd = [BIN_CONVERT,self.filepath,"-gravity","Center","-crop","{}x{}+0+0".format( + self.width, self.height),"+repage",self.filepath] + self._call_cmd(cmd) +#}}} + def _subtle_break(self): + #assume the header is no longer than HEADER_OFFSET bytes + breakpoint = random.randint(HEADER_OFFSET, len(self.file_data)) + newfile = self.file_data[0:breakpoint] \ + + SUBTLE_BREAK_MARK \ + + self.file_data[breakpoint:] + self.file_data = newfile[0:len(self.file_data)] + + def _extreme_break(self): + increment = len(self.file_data)/10; + i = 0 + newfile = ""; + for b in self.file_data: + if i > HEADER_OFFSET and not (i % increment): + b += EXTREME_BREAK_MARK + newfile += b + i += 1 + self.file_data = newfile[0:len(self.file_data)] + + def _choose_frame(self): + frame = random.choice(self._gif_frames) + self._call_cmd([BIN_CONVERT, frame, self._downloaded_file]) + + def _enforce_jpg(self): + if self.params.breaktype in [ "exr", "bmp", "miff" ] and not re.match(r'jpe?g$', self._first_format, re.IGNORECASE): + jpg_file = self.tempname_create(basename=self.basename, fmt="jpg") + self._call_cmd([BIN_CONVERT,self._downloaded_file,jpg_file]) + self._call_cmd(["rm",self._downloaded_file]) + self._downloaded_file = jpg_file + + def _first_conversion(self): + if self._first_format == self.params.breaktype: + self._downloaded_file = self._conversion_file + return + self._call_cmd([BIN_CONVERT, self._downloaded_file, self._conversion_file]) + self.files_created.append(self._conversion_file) + + def _prepare_filedata(self): + if self._gif_frames: + self._choose_frame() + if self.params.breakangle: + self._rotate() + self._enforce_jpg(); + self._first_conversion(); + self.file_data = self._file_read(self._conversion_file) + if not self.file_data: + self.err_warn("Unable to get file data"); + + def _add_false_data(self, breakmode): + if breakmode == "subtle": + self._subtle_break() + elif breakmode == "extreme": + self._extreme_break() + f = open(self._conversion_file, 'w') + f.write(self.file_data) + f.close(); + + def _final_conversion(self): + self._call_cmd( [BIN_CONVERT, self._conversion_file, self.filepath]) + def psd_psbfilepath(num): + return os.path.join(self._working_dir, "{}-{}.{}".format(self.basename, num, self.params.finalformat)) + if self.params.breaktype == 'psd': + self._call_cmd(['mv', psd_psbfilepath(1), self.filepath]) + self.files_created.append(psd_psbfilepath(0)) + if self.params.breaktype == 'psb': + self._call_cmd(['mv', psd_psbfilepath(0), self.filepath]) + self.files_created.append(psd_psbfilepath(1)) + if self.params.breakangle: + self._rotate_back() + + def _cleanup(self): + cmd = ["rm"]+self.files_created + self._call_cmd(cmd) + + def create(self, breakmode=""): + if not breakmode: breakmode = self.params.breakmode + self._prepare_filedata(); + self._add_false_data(breakmode); + self._final_conversion() + self._cleanup() + + @classmethod + def test(cls): + TEST_PARAMS = { + "url" : "http://i.asdf.us/im/27/1424816234661dumpfmpfifferkinggr_1424816412_pfifferking.gif" , + "breaktype" : "RGB_WASH", + "finalformat" : "png", + "breakmode" : "extreme", + "breakangle" : "10", + "username" : "donkey", + "expanded" : "false" + } + b = cls(**TEST_PARAMS) + b.create(); + print b.filepath diff --git a/Pb/Breaker/__init__.py b/Pb/Breaker/__init__.py deleted file mode 100755 index 2d2f573..0000000 --- a/Pb/Breaker/__init__.py +++ /dev/null @@ -1,198 +0,0 @@ -#!/usr/bin/python2.7 -import os -import sys -import random -import re -import urllib -import inspect -from Config import * -from Pb import Pb -from Pb.Params import Params - -DEFAULT_FINALFORMAT = "png"; -SUBTLE_BREAK_MARK = 'pron' -EXTREME_BREAK_MARK = 'sugar' - -HEADER_OFFSET = 2000 - -class Breaker(Pb): - def __init__(self, url=None, breaktype=None, finalformat=DEFAULT_FINALFORMAT, - breakmode=None, breakangle=None, username=None, expanded=None, firsttime=None): - super(Breaker,self).__init__(); - - self.params.breaktype = self._get_breaktype(breaktype); - self.params.url = url - - _frame = inspect.currentframe(); - _args_vals = inspect.getargvalues(_frame); - for arg in _args_vals.args: - if arg == "self": - continue - try: - if arg not in ['breaktype', 'url']: - self.params.__setattr__(arg, self.bool_correct(str(_args_vals.locals.get(arg)))) - except Exception as e: - sys.stderr.write("\n"+str(e)+"\n") - - self.tag = "imBreak" - self.commands = []; - self._now = self.now() - self.files_created = [] - - self.basename, self._first_format = self._get_filename_and_type_from_url(self.params.url); - self._downloaded_file = self.tempname_create(basename=self.basename, fmt=self._first_format) - - - try: - self.download(self.params.url, self._downloaded_file) - self.files_created.append(self._downloaded_file) - except Exception as e: - self.err_warn(str(e)) - - self._gif_frames = self.gif_frames(self._downloaded_file) - self._gif_frames = self._gif_frames if len(self._gif_frames) > 1 else False - self.width, self.height = self.dimensions(self._downloaded_file) # same here - - if not self.params.finalformat and self._gif_frames: - self.params.finalformat = 'gif' - if self.params.breaktype == 'miff': - self.params.finalformat = 'jpg' - self.params.breakmode = 'subtle' - #final filepath is stored in self.filepath - self.filename = "{}.{}".format(self.basename, self.params.finalformat) - self.filepath = os.path.join(self._working_dir, self.filename) - self._conversion_file = self.tempname_create(basename=self.basename, fmt=self.params.breaktype); - - def _call_cmd(self, cmd): - super(Breaker,self)._call_cmd(cmd) - self.commands.append(" ".join(cmd)); - - def _get_breaktype(self, key): - #{{{ conversion table - breaktypeTranslate = { - 'CLASSIC':'jpg', - 'REDUX':'pcds', - 'BLURRY_BREAK':'viff', - 'BLURRY_BREAK_2':'mat', - 'SWIPE':'miff', - 'RGB_WASH':'psd', - 'RGB_WASH_2':'psb', - 'NOISY_BREAK':'palm', - 'NOISY_BREAK_2':'fig', - 'BROKEN_VIGNETTE':'pbm', - 'FAX_MACHINE':'cals', - 'STRIPES':'exr', - 'PHOTOCOPY':'art', - } - #}}} - return breaktypeTranslate[key] - -#{{{#########rotatefunctions####################################### - def _rotate(self): - cmd = [BIN_CONVERT,self._downloaded_file,"-rotate",self.params.breakangle,"+repage",self._downloaded_file] - self._call_cmd(cmd) - - def _rotate_back(self): - angle = str(360-int(self.params.breakangle)) - cmd = [BIN_CONVERT,self.filepath,"-rotate",angle,"+repage",self.filepath] - self._call_cmd(cmd) - if not self.params.expanded: - cmd = [BIN_CONVERT,self.filepath,"-gravity","Center","-crop","{}x{}+0+0".format( - self.width, self.height),"+repage",self.filepath] - self._call_cmd(cmd) -#}}} - def _subtle_break(self): - #assume the header is no longer than HEADER_OFFSET bytes - breakpoint = random.randint(HEADER_OFFSET, len(self.file_data)) - newfile = self.file_data[0:breakpoint] \ - + SUBTLE_BREAK_MARK \ - + self.file_data[breakpoint:] - self.file_data = newfile[0:len(self.file_data)] - - def _extreme_break(self): - increment = len(self.file_data)/10; - i = 0 - newfile = ""; - for b in self.file_data: - if i > HEADER_OFFSET and not (i % increment): - b += EXTREME_BREAK_MARK - newfile += b - i += 1 - self.file_data = newfile[0:len(self.file_data)] - - def _choose_frame(self): - frame = random.choice(self._gif_frames) - self._call_cmd([BIN_CONVERT, frame, self._downloaded_file]) - - def _enforce_jpg(self): - if self.params.breaktype in [ "exr", "bmp", "miff" ] and not re.match(r'jpe?g$', self._first_format, re.IGNORECASE): - jpg_file = self.tempname_create(basename=self.basename, fmt="jpg") - self._call_cmd([BIN_CONVERT,self._downloaded_file,jpg_file]) - self._call_cmd(["rm",self._downloaded_file]) - self._downloaded_file = jpg_file - - def _first_conversion(self): - if self._first_format == self.params.breaktype: - self._downloaded_file = self._conversion_file - return - self._call_cmd([BIN_CONVERT, self._downloaded_file, self._conversion_file]) - self.files_created.append(self._conversion_file) - - def _prepare_filedata(self): - if self._gif_frames: - self._choose_frame() - if self.params.breakangle: - self._rotate() - self._enforce_jpg(); - self._first_conversion(); - self.file_data = self._file_read(self._conversion_file) - if not self.file_data: - self.err_warn("Unable to get file data"); - - def _add_false_data(self, breakmode): - if breakmode == "subtle": - self._subtle_break() - elif breakmode == "extreme": - self._extreme_break() - f = open(self._conversion_file, 'w') - f.write(self.file_data) - f.close(); - - def _final_conversion(self): - self._call_cmd( [BIN_CONVERT, self._conversion_file, self.filepath]) - def psd_psbfilepath(num): - return os.path.join(self._working_dir, "{}-{}.{}".format(self.basename, num, self.params.finalformat)) - if self.params.breaktype == 'psd': - self._call_cmd(['mv', psd_psbfilepath(1), self.filepath]) - self.files_created.append(psd_psbfilepath(0)) - if self.params.breaktype == 'psb': - self._call_cmd(['mv', psd_psbfilepath(0), self.filepath]) - self.files_created.append(psd_psbfilepath(1)) - if self.params.breakangle: - self._rotate_back() - - def _cleanup(self): - cmd = ["rm"]+self.files_created - self._call_cmd(cmd) - - def create(self, breakmode=""): - if not breakmode: breakmode = self.params.breakmode - self._prepare_filedata(); - self._add_false_data(breakmode); - self._final_conversion() - self._cleanup() - - @classmethod - def test(cls): - TEST_PARAMS = { - "url" : "http://i.asdf.us/im/27/1424816234661dumpfmpfifferkinggr_1424816412_pfifferking.gif" , - "breaktype" : "RGB_WASH", - "finalformat" : "png", - "breakmode" : "extreme", - "breakangle" : "10", - "username" : "donkey", - "expanded" : "false" - } - b = cls(**TEST_PARAMS) - b.create(); - print b.filepath diff --git a/Pb/Generate/__init__.py b/Pb/Generate/__init__.py index 385591e..191d600 100755 --- a/Pb/Generate/__init__.py +++ b/Pb/Generate/__init__.py @@ -22,7 +22,7 @@ DISPOSE_DEFAULT = "None" def debuglog(s): sys.stderr.write(str(s) + "\n"); -class Generate(Pb): +class Im(Pb): def __init__(self, **kwargs): self.params = {} self._now = self.now() diff --git a/Pb/Gradient/__init__.py b/Pb/Gradient/__init__.py index 5120c7d..c384df2 100755 --- a/Pb/Gradient/__init__.py +++ b/Pb/Gradient/__init__.py @@ -44,7 +44,7 @@ HALFTONEVALUES = { } -class Gradient(Pb): +class Imgradient(Pb): def __init__(self, **kwargs): self.tag = "imGradient" self.directory = WORKING_DIR diff --git a/Pb/Grid/__init__.py b/Pb/Grid/__init__.py new file mode 100755 index 0000000..09718bd --- /dev/null +++ b/Pb/Grid/__init__.py @@ -0,0 +1,193 @@ +#!/usr/bin/python2.7 +import sys +import re +import os +import simplejson as json +import random +from Config import DEFAULT_FINALFORMAT, DEFAULT_HEIGHT, DEFAULT_WIDTH, WORKING_DIR +from Config import THREEDROTATE, GRID +import tempfile +from Pb import Pb + +_default_line_color = "silver" + +class Imgrid(Pb): + def __init__(self, + width=None, + height=None, + linethickness=None, + opacity=None, + linecolor=None, + spacing=None, + vlines=None, + hlines=None, + shadow=None, + bgimage=None, + bgcolor=None, + imageinstead=None, + planebgcolor=None, + planebgimage=None, + swing=None, + tilt=None, + roll=None, + zoom=None, + skycolor=None, + transition=None, + trim=None, + finalformat=DEFAULT_FINALFORMAT, + username=None + ): + super(Imgrid,self).__init__(); + + _frame = inspect.currentframe(); + _args_vals = inspect.getargvalues(_frame); + for arg in _args_vals.args: + if arg == "self": + continue + argval = _args_vals.locals.get(arg) + try: + if arg in [ 'skycolor', 'bgcolor', 'planebgcolor','linecolor' ]: + self.params.set_val(arg, argval, value_type="color") + elif arg == 'opacity': + self.params.set_val(arg, argval, value_type="float") + elif arg == 'zoom': + self.params.set_val(arg, argval, value_type="int") + elif arg in [ 'bgimage', 'planebgimage', 'imageinstead' ]: + self.params.set_val(arg, argval, value_type="img_url") + if self.params.get_mimetype(arg) == 'gif': + self.params.gif_convert_to_still(arg) + self.files_created.append(self.params.get_filepath(arg)) + elif arg in ['finalformat', 'username' ] : + self.params.set_val(arg, argval, value_type="string") + else: + self.params.set_val(arg, argval, value_type="bool") + + except Exception as e: + self.log_err(str(e)) + + #FIXME these need to be bumped up to the parent class + self.basename = self._get_filename(); + self.filename = "{}.{}".format(self.basename, self.params.finalformat) + #final filepath is stored in self.filepath + self.filepath = os.path.join(WORKING_DIR, self.filename) + + + + #makes a canvas file...step 1 (if not bgimage) + def _make_canvas(self): + dimensions = "{}x{}".format( + self.params.width or DEFAULT_WIDTH, + self.params.height or DEFAULT_HEIGHT + ) + if self.params.bgimage: + return + bgcolor = "xc:{}".format(self.params.bgcolor or 'transparent') + cmd = [ BIN_CONVERT, "-size", dimensions, bgcolor, self.filepath ] + self._call_cmd(cmd) + + #2nd step-- run grid + def _grid_command(self): + cmd = [GRID] + if self.params.spacing: + if self.params.vlines: + width = 2 * int(self.params.width or DEFAULT_WIDTH) + cmd += ["-s","{},{}".format(self.params.spacing,width)] + elif self.params.hlines: + height = 2 * int(self.params.height or DEFAULT_HEIGHT) + cmd += ["-s", "{},{}".format(height,self.params.spacing)] + else: + cmd += ["-s",self.params.spacing] + cmd += [ "-c", self.params.linecolor or _default_line_color] + if self.params.linethickness: cmd += ['-t',self.params.linethickness] + if self.params.opacity: cmd += ['-o',self.params.opacity] + cmd += [ self.filepath, self.filepath ] + self._call_cmd(cmd) + + def _shadow_cmd(self): + #convert 1.png \( +clone -background black -shadow 110x1+9+9 \) +swap -background none -layers merge +repage 2.png + cmd = [ + BIN_CONVERT, + self.filepath, + "(","+clone","-background","black","-shadow","100x2+20+10",")", + "+swap","-background","none","-layers","merge","+repage" , + self.filepath + ] + self._call_cmd(cmd) + + + def _threed_rotate_cmd (self): + #3rd step--run 3Drotate + cmd = [THREEDROTATE] + if self.params.swing: cmd += ["pan={}".format(self.params.swing)] + if self.params.tilt: cmd += ["tilt={}".format(self.params.tilt)] + if self.params.roll: cmd += ["roll={}".format(self.params.roll)] + if self.params.zoom: + cmd += ["zoom={}".format(self.params.zoom)] + if cmd == [THREEDROTATE]: #if nothing has been added + return + if self.params.planebgcolor and not self.params.planebgimage: + cmd += ["bgcolor={}".format(self.params.planebgcolor)] + else: + cmd += ["bgcolor=none"] + cmd += ["skycolor={}".format(self.params.skycolor or 'none')] + if self.params.transition: cmd += ["vp={}".format(self.params.transition)] + cmd += [ self.filepath, self.filepath ] + self._call_cmd(cmd) + + + def _trim_cmd (self): + cmd = [BIN_CONVERT, self.filepath, "-trim", "+repage", self.filepath] + self._call_cmd(cmd) + + def _prepare_gridimage(self, image): + if image['mimetype'] != 'png': + cmd = [BIN_CONVERT, image['path'], self.filepath] + else: + cmd = ['cp', image['path'], self.filepath] + self._call_cmd(cmd) + + + def _overlay_planebgimage(self): + cmd = [ + BIN_COMPOSITE, + "-compose", "Dst_Over", "-gravity", "center", + self.params.planebgimage["path"], + self.filepath, + self.filepath + ] + self._call_cmd(cmd) + + def _cleanup(self): + if not len(self.files_created): + return + cmd = ["rm", "-f"] + self.files_created + self._call_cmd(cmd) + + def create(self): + if self.params.bgimage: + self._prepare_gridimage(self.params.bgimage) + self._grid_command() + elif self.params.imageinstead: + self._prepare_gridimage(self.params.imageinstead) + else: + self._make_canvas() + self._grid_command() + if self.params.shadow: self._shadow_cmd() + self._threed_rotate_cmd() + if self.params.planebgimage: self._overlay_planebgimage() + if self.params.trim: self._trim_cmd() + self._cleanup() + +if __name__ == "__main__": + g = Imgrid(**{ + 'bgimage' : 'http://i.asdf.us/im/1a/imBreak_1424909483_xx_abridged___.gif', + 'planebgimage' : 'http://i.imgur.com/FICZtph.png', + 'tilt' : '30', + 'spacing' : '30', + 'hlines' : 'true', + 'roll' : '30', + 'shadow' : 'true', + 'trim' : 'true' + }) + g.create() + print g.commands diff --git a/Pb/Imgrid/__init__.py b/Pb/Imgrid/__init__.py deleted file mode 100755 index 52cb748..0000000 --- a/Pb/Imgrid/__init__.py +++ /dev/null @@ -1,232 +0,0 @@ -#!/usr/bin/python2.7 -import sys -import re -import os -import simplejson as json -import random -from Config import * -import tempfile -from Pb import Pb - -DEFAULT_HEIGHT = 400 -DEFAULT_WIDTH = 600 -DEFAULT_LINE_COLOR = "silver" - -class Imgrid(Pb): - def __init__(self, **kwargs): - self.tag = "imGrid" - self.files_created = [] - self.commands = []; - self._required_keys = [ -#{{{ required_keys - "width", - "height", - "linethickness", - "opacity", - "linecolor", - "spacing", - "vlines", - "hlines", - "shadow", - "bgimage", - "bgcolor", - "imageinstead", - "planebgcolor", - "planebgimage", - "swing", - "tilt", - "roll", - "zoom", - "skycolor", - "transition", - "trim", - "format", - "username" -#}}} - ] - self._now = self.now() - - #Work out params ... - #note, tmpfile lib is pretty much useless here, given imagemagick's behavior with gifs (it splits them) etc... - #instead we're just making our own in /var/www/cache (tmpfs mounted there) - self.params = {} - for k in self._required_keys: - if k in kwargs: - if k in [ 'bgimage', 'planebgimage', 'imageinstead' ] and self.bool_correct(kwargs[k]): - self.params[k] = { - 'url' : kwargs[k], - 'filename' : self._make_tempname(k), - 'path' : os.path.join(WORKING_DIR, self._make_tempname(k)) , - } - try: - self.download(self.params[k]['url'], self.params[k]['path']) - self.files_created.append(self.params[k]['path']) - self.params[k]['mimetype'] = self.get_mimetype(self.params[k]['path']) - frames = self.gif_frames(self.params[k]['path']) - if len(frames) > 1: - self.params[k]['path'] = random.choice(frames) - - except Exception: - sys.stderr.write(str(e)) - raise Exception ("BAD PARAMS"); - elif k in [ 'skycolor', 'bgcolor', 'planebgcolor','linecolor' ]: - try: - self.params[k] = self.is_color(kwargs[k]) - except Exception: - sys.stderr.write(kwargs[k] + "\n") - raise Exception("Unable to process color for:\n{}".format(k)) - elif k == 'opacity': - self.params[k] = str(float(kwargs[k])) - elif k == 'zoom': - self.params[k] = int(float(kwargs[k])) - else: - self.params[k] = self.bool_correct(self.sanitize(kwargs[k])) - else: - self.params[k] = None; - - self.params = self.make_dotdict(self.params) - - self.basename = self._get_filename(); - - if not self.params.finalformat: - self.params.finalformat = DEFAULT_FINALFORMAT - self.filename = "{}.{}".format(self.basename, self.params.finalformat) - #final filepath is stored in self.filepath - self.filepath = os.path.join(WORKING_DIR, self.filename) - - def _get_filename(self): - return "{}_{}_{}".format( - self.tag, - self._now, - self.params.username or "" - ); - - def _call_cmd(self, cmd): - try: - self.call_cmd(cmd) - self.commands.append(" ".join(cmd)); - except Exception: - raise Exception("Unable to call cmd {}".format(str(cmd))) - - def _make_tempname(self, s): - return "IMGRIDTMP{}{}".format(self._now, s); - - - #makes a canvas file...step 1 (if not bgimage) - def _make_canvas(self): - dimensions = "{}x{}".format( - self.params.width or DEFAULT_WIDTH, - self.params.height or DEFAULT_HEIGHT - ) - if self.params.bgimage: - return - bgcolor = "xc:{}".format(self.params.bgcolor or 'transparent') - cmd = [ BIN_CONVERT, "-size", dimensions, bgcolor, self.filepath ] - self._call_cmd(cmd) - - #2nd step-- run grid - def _grid_command(self): - cmd = [GRID] - if self.params.spacing: - if self.params.vlines: - width = 2 * int(self.params.width or DEFAULT_WIDTH) - cmd += ["-s","{},{}".format(self.params.spacing,width)] - elif self.params.hlines: - height = 2 * int(self.params.height or DEFAULT_HEIGHT) - cmd += ["-s", "{},{}".format(height,self.params.spacing)] - else: - cmd += ["-s",self.params.spacing] - cmd += [ "-c", self.params.linecolor or DEFAULT_LINE_COLOR] - if self.params.linethickness: cmd += ['-t',self.params.linethickness] - if self.params.opacity: cmd += ['-o',self.params.opacity] - cmd += [ self.filepath, self.filepath ] - self._call_cmd(cmd) - - def _shadow_cmd(self): - #convert 1.png \( +clone -background black -shadow 110x1+9+9 \) +swap -background none -layers merge +repage 2.png - cmd = [ - BIN_CONVERT, - self.filepath, - "(","+clone","-background","black","-shadow","100x2+20+10",")", - "+swap","-background","none","-layers","merge","+repage" , - self.filepath - ] - self._call_cmd(cmd) - - - def _threed_rotate_cmd (self): - #3rd step--run 3Drotate - cmd = [THREEDROTATE] - if self.params.swing: cmd += ["pan={}".format(self.params.swing)] - if self.params.tilt: cmd += ["tilt={}".format(self.params.tilt)] - if self.params.roll: cmd += ["roll={}".format(self.params.roll)] - if self.params.zoom: - cmd += ["zoom={}".format(self.params.zoom)] - if cmd == [THREEDROTATE]: #if nothing has been added - return - if self.params.planebgcolor and not self.params.planebgimage: - cmd += ["bgcolor={}".format(self.params.planebgcolor)] - else: - cmd += ["bgcolor=none"] - cmd += ["skycolor={}".format(self.params.skycolor or 'none')] - if self.params.transition: cmd += ["vp={}".format(self.params.transition)] - cmd += [ self.filepath, self.filepath ] - self._call_cmd(cmd) - - - def _trim_cmd (self): - cmd = [BIN_CONVERT, self.filepath, "-trim", "+repage", self.filepath] - self._call_cmd(cmd) - - def _prepare_gridimage(self, image): - if image['mimetype'] != 'png': - cmd = [BIN_CONVERT, image['path'], self.filepath] - else: - cmd = ['cp', image['path'], self.filepath] - self._call_cmd(cmd) - - - def _overlay_planebgimage(self): - cmd = [ - BIN_COMPOSITE, - "-compose", "Dst_Over", "-gravity", "center", - self.params.planebgimage["path"], - self.filepath, - self.filepath - ] - self._call_cmd(cmd) - - def _cleanup(self): - if not len(self.files_created): - return - cmd = ["rm", "-f"] + self.files_created - self._call_cmd(cmd) - - def create(self): - if self.params.bgimage: - self._prepare_gridimage(self.params.bgimage) - self._grid_command() - elif self.params.imageinstead: - self._prepare_gridimage(self.params.imageinstead) - else: - self._make_canvas() - self._grid_command() - if self.params.shadow: self._shadow_cmd() - self._threed_rotate_cmd() - if self.params.planebgimage: self._overlay_planebgimage() - if self.params.trim: self._trim_cmd() - self._cleanup() - -if __name__ == "__main__": - g = Imgrid(**{ - 'bgimage' : 'http://i.asdf.us/im/1a/imBreak_1424909483_xx_abridged___.gif', - 'planebgimage' : 'http://i.imgur.com/FICZtph.png', - 'tilt' : '30', - 'spacing' : '30', - 'hlines' : 'true', - 'roll' : '30', - 'shadow' : 'true', - 'trim' : 'true' - }) - g.create() - print g.commands diff --git a/Pb/Imlandscape/__init__.py b/Pb/Imlandscape/__init__.py deleted file mode 100755 index 59212a6..0000000 --- a/Pb/Imlandscape/__init__.py +++ /dev/null @@ -1,87 +0,0 @@ -#!/usr/bin/python2.7 -import sys -import random -import re -import urllib -import urlparse -from Config import * -import Pb -import base64 -import time -import string -from subprocess import Popen, PIPE -import sha -import simplejson as json -from Pb import Pb - -import mimetypes - -class Imlandscape(Pb): - def __init__(self, **kwargs): - sys.stderr.write("inside init"); - sys.stderr.write(str(kwargs)) - return; - self.params = {} - self.tag = "imlandscape" - self.commands = []; - self._required_keys = [ - "heightmap", - "imgdata", - "texture", - "name", - ] - self._now = self.now() - self.files_created = [] - for k in self._required_keys: - if k in kwargs: - self.params[k] = kwargs[k] - else: - self.params[k] = self.bool_correct(self.sanitize(kwargs[k])) - - def _filename_from_url (self, url, name=""): - if "?" in url: - url = url.split("?")[0] - if "/" in url: - url = urllib.unquote(url).replace(" ","") - filename = url.split("/")[-1] - filetype = "png" - filename = sanitize(filename[:-4]) - else: - filename = "" - if name != "": - name = name+"_" - return "{}_{}{}_{}.{}".format("imlandscape", name, filename,self._now, "png") - - def _saveImgData(self, imgdata, filename): - try: -# up = urlparse.urlparse(url) -# head, data = imgdata.split(',', 1) -# bits = head.split(';') - parts = imgdata.split(';') - mime_type = parts[0] if parts[0] else 'text/plain' - data = parts[1] - charset, b64 = 'ASCII', False - for part in parts[1]: - if part.startswith('charset='): - charset = part[8:] - elif part == 'base64': - b64 = True - - # Do something smart with charset and b64 instead of assuming - if b64: - plaindata = base64.b64decode(data) - else: - plaindata = data - with open(filename, 'wb') as f: - f.write(plaindata) - except Exception as e: - sys.stderr.write("ERROR: {}\n".format(str(e))); - -# def _cleanup(self): -# cmd = ["rm"]+self.files_created -# self._call_cmd(cmd) - - def create(self, breakmode=""): - self.filepath = self._filename_from_url(self.params.get('texture',""), self.params.get('name',"")); - self._saveImgData(self.params.get('imgdata'), self.filepath); - diff --git a/Pb/Imlandscape/landscape b/Pb/Imlandscape/landscape deleted file mode 100755 index 10e8ede..0000000 --- a/Pb/Imlandscape/landscape +++ /dev/null @@ -1,163 +0,0 @@ -#!/usr/bin/python2.7 -import cgi -import sys -import os -import re -import time -import string -import urllib -from subprocess import Popen, PIPE -import sha -import simplejson as json - -import mimetypes -import s3 - -import db -DB = db.db () - -import base64 -import urlparse - - -AWS_ACCESS_KEY_ID = 'AKIAIR53VPBXKJMXZIBA' -AWS_SECRET_ACCESS_KEY = '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' -BUCKET_NAME = 'i.asdf.us' -BASE_PATH = "/var/www/asdf.us/httpdocs/imlandscape" -BASE_URL = "http://i.asdf.us/" -PARAM_LIST = "heightmap texture name imgdata filename" -BIN_IDENTIFY = "/usr/bin/identify" - -print "Content-type: text/plain" -print "" -def insert_cmd (dir, newfile, name, texture, dataobj): - if texture == "": - texture = "NULL" - try: - sql = "INSERT INTO im_cmd (date,remote_addr,name,url,dir,oldfile,newfile,cmd, dataobj, tag) VALUES(%s,%s,%s,%s,%s,%s,%s,%s,%s, %s)" - - args = (now(), "NULL", name, texture, dir, "NULL", newfile, "NULL", dataobj, "imlandscape") - DB.execute(sql, args) - except (): - return - -def hash_dir (s): - return sha.new(s).hexdigest()[:2] - -def bin_identify (filename): - ident = Popen([BIN_IDENTIFY, filename], stdout=PIPE).communicate()[0] - partz = ident.split(" ") - width,height = partz[2].split("x") - return width, height - -def get_params (paramlist): - paramkeys = paramlist.split() - form = cgi.FieldStorage() - params = {} - for key in paramkeys: - if key in form: - if key == 'heightmap': - params[key] = form[key].value - elif key == 'imgdata': - params[key] = form[key].value - elif key == 'texture': - params[key] = form[key].value - else: - params[key] = sanitize(form[key].value) - else: - params[key] = None - return params - -def error (e): - print "#@imlandscape" - print "ERROR\t"+e - sys.exit() - -def now (): - return int(time.mktime(time.localtime())) - -def sanitize (str): - return re.sub(r'\W+', '', str) - -def filename_from_url (url, name=""): - if "?" in url: - url = url.split("?")[0] - if "/" in url: - url = urllib.unquote(url).replace(" ","") - filename = url.split("/")[-1] - filetype = "png" - filename = sanitize(filename[:-4]) - else: - filename = "" - if name != "": - name = name+"_" - return "{}_{}{}_{}.{}".format("imlandscape", name, filename,now(), "png") - -def saveImgData(url, filename): - try: - up = urlparse.urlparse(url) - head, data = up.path.split(',', 1) - bits = head.split(';') - mime_type = bits[0] if bits[0] else 'text/plain' - charset, b64 = 'ASCII', False - for bit in bits[1]: - if bit.startswith('charset='): - charset = bit[8:] - elif bit == 'base64': - b64 = True - - # Do something smart with charset and b64 instead of assuming - plaindata = base64.b64decode(data) - - with open(filename, 'wb') as f: - f.write(plaindata) - except Exception as e: - error(str(e)); - -def file_size (file): - return os.stat(file)[6] - -def moveToS3(filename,objectname): - conn = s3.AWSAuthConnection(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY) - sys.stderr.write( "Uploading %s" % filename) - filedata = open(filename, 'rb').read() - content_type = mimetypes.guess_type(filename)[0] - if not content_type: - content_type = 'text/plain' - conn.put(BUCKET_NAME, objectname, s3.S3Object(filedata), - {'x-amz-acl': 'public-read', 'Content-Type': content_type, 'x-amz-storage-class': 'REDUCED_REDUNDANCY'}) - -param = get_params(PARAM_LIST) -if param['imgdata'] is None: - error("no imgdata") -url = param['imgdata'] -if param['texture'] is None: - param['texture'] = ""; -if param['heightmap'] is None: - param['heightmap'] = ""; -if param['name'] is None: - param['name'] = ""; - -dataobj = json.dumps({ - 'texture' : param['texture'], - 'heightmap' : param['heightmap'], - 'name' : param['name'] -}) - -dir = hash_dir(param['imgdata']); - -filename = filename_from_url(param['texture'], param['name']); - -tag = "imlandscape" -objectname = "im/"+dir+"/"+filename - -saveImgData(param['imgdata'], filename); - -print "#@imlandscape" -#print ", ".join([k+"="+str(v) for k,v in param.iteritems()]) -print file_size (filename) -print bin_identify (filename) -print BASE_URL+objectname -insert_cmd(dir, filename, param['name'], param['texture'], dataobj); -moveToS3(filename, objectname); -os.remove(filename); diff --git a/Pb/Landscape/__init__.py b/Pb/Landscape/__init__.py new file mode 100755 index 0000000..59212a6 --- /dev/null +++ b/Pb/Landscape/__init__.py @@ -0,0 +1,87 @@ +#!/usr/bin/python2.7 +import sys +import random +import re +import urllib +import urlparse +from Config import * +import Pb +import base64 +import time +import string +from subprocess import Popen, PIPE +import sha +import simplejson as json +from Pb import Pb + +import mimetypes + +class Imlandscape(Pb): + def __init__(self, **kwargs): + sys.stderr.write("inside init"); + sys.stderr.write(str(kwargs)) + return; + self.params = {} + self.tag = "imlandscape" + self.commands = []; + self._required_keys = [ + "heightmap", + "imgdata", + "texture", + "name", + ] + self._now = self.now() + self.files_created = [] + for k in self._required_keys: + if k in kwargs: + self.params[k] = kwargs[k] + else: + self.params[k] = self.bool_correct(self.sanitize(kwargs[k])) + + def _filename_from_url (self, url, name=""): + if "?" in url: + url = url.split("?")[0] + if "/" in url: + url = urllib.unquote(url).replace(" ","") + filename = url.split("/")[-1] + filetype = "png" + filename = sanitize(filename[:-4]) + else: + filename = "" + if name != "": + name = name+"_" + return "{}_{}{}_{}.{}".format("imlandscape", name, filename,self._now, "png") + + def _saveImgData(self, imgdata, filename): + try: +# up = urlparse.urlparse(url) +# head, data = imgdata.split(',', 1) +# bits = head.split(';') + parts = imgdata.split(';') + mime_type = parts[0] if parts[0] else 'text/plain' + data = parts[1] + charset, b64 = 'ASCII', False + for part in parts[1]: + if part.startswith('charset='): + charset = part[8:] + elif part == 'base64': + b64 = True + + # Do something smart with charset and b64 instead of assuming + if b64: + plaindata = base64.b64decode(data) + else: + plaindata = data + with open(filename, 'wb') as f: + f.write(plaindata) + except Exception as e: + sys.stderr.write("ERROR: {}\n".format(str(e))); + +# def _cleanup(self): +# cmd = ["rm"]+self.files_created +# self._call_cmd(cmd) + + def create(self, breakmode=""): + self.filepath = self._filename_from_url(self.params.get('texture',""), self.params.get('name',"")); + self._saveImgData(self.params.get('imgdata'), self.filepath); + diff --git a/Pb/Landscape/landscape b/Pb/Landscape/landscape new file mode 100755 index 0000000..10e8ede --- /dev/null +++ b/Pb/Landscape/landscape @@ -0,0 +1,163 @@ +#!/usr/bin/python2.7 +import cgi +import sys +import os +import re +import time +import string +import urllib +from subprocess import Popen, PIPE +import sha +import simplejson as json + +import mimetypes +import s3 + +import db +DB = db.db () + +import base64 +import urlparse + + +AWS_ACCESS_KEY_ID = 'AKIAIR53VPBXKJMXZIBA' +AWS_SECRET_ACCESS_KEY = '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' +BUCKET_NAME = 'i.asdf.us' +BASE_PATH = "/var/www/asdf.us/httpdocs/imlandscape" +BASE_URL = "http://i.asdf.us/" +PARAM_LIST = "heightmap texture name imgdata filename" +BIN_IDENTIFY = "/usr/bin/identify" + +print "Content-type: text/plain" +print "" +def insert_cmd (dir, newfile, name, texture, dataobj): + if texture == "": + texture = "NULL" + try: + sql = "INSERT INTO im_cmd (date,remote_addr,name,url,dir,oldfile,newfile,cmd, dataobj, tag) VALUES(%s,%s,%s,%s,%s,%s,%s,%s,%s, %s)" + + args = (now(), "NULL", name, texture, dir, "NULL", newfile, "NULL", dataobj, "imlandscape") + DB.execute(sql, args) + except (): + return + +def hash_dir (s): + return sha.new(s).hexdigest()[:2] + +def bin_identify (filename): + ident = Popen([BIN_IDENTIFY, filename], stdout=PIPE).communicate()[0] + partz = ident.split(" ") + width,height = partz[2].split("x") + return width, height + +def get_params (paramlist): + paramkeys = paramlist.split() + form = cgi.FieldStorage() + params = {} + for key in paramkeys: + if key in form: + if key == 'heightmap': + params[key] = form[key].value + elif key == 'imgdata': + params[key] = form[key].value + elif key == 'texture': + params[key] = form[key].value + else: + params[key] = sanitize(form[key].value) + else: + params[key] = None + return params + +def error (e): + print "#@imlandscape" + print "ERROR\t"+e + sys.exit() + +def now (): + return int(time.mktime(time.localtime())) + +def sanitize (str): + return re.sub(r'\W+', '', str) + +def filename_from_url (url, name=""): + if "?" in url: + url = url.split("?")[0] + if "/" in url: + url = urllib.unquote(url).replace(" ","") + filename = url.split("/")[-1] + filetype = "png" + filename = sanitize(filename[:-4]) + else: + filename = "" + if name != "": + name = name+"_" + return "{}_{}{}_{}.{}".format("imlandscape", name, filename,now(), "png") + +def saveImgData(url, filename): + try: + up = urlparse.urlparse(url) + head, data = up.path.split(',', 1) + bits = head.split(';') + mime_type = bits[0] if bits[0] else 'text/plain' + charset, b64 = 'ASCII', False + for bit in bits[1]: + if bit.startswith('charset='): + charset = bit[8:] + elif bit == 'base64': + b64 = True + + # Do something smart with charset and b64 instead of assuming + plaindata = base64.b64decode(data) + + with open(filename, 'wb') as f: + f.write(plaindata) + except Exception as e: + error(str(e)); + +def file_size (file): + return os.stat(file)[6] + +def moveToS3(filename,objectname): + conn = s3.AWSAuthConnection(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY) + sys.stderr.write( "Uploading %s" % filename) + filedata = open(filename, 'rb').read() + content_type = mimetypes.guess_type(filename)[0] + if not content_type: + content_type = 'text/plain' + conn.put(BUCKET_NAME, objectname, s3.S3Object(filedata), + {'x-amz-acl': 'public-read', 'Content-Type': content_type, 'x-amz-storage-class': 'REDUCED_REDUNDANCY'}) + +param = get_params(PARAM_LIST) +if param['imgdata'] is None: + error("no imgdata") +url = param['imgdata'] +if param['texture'] is None: + param['texture'] = ""; +if param['heightmap'] is None: + param['heightmap'] = ""; +if param['name'] is None: + param['name'] = ""; + +dataobj = json.dumps({ + 'texture' : param['texture'], + 'heightmap' : param['heightmap'], + 'name' : param['name'] +}) + +dir = hash_dir(param['imgdata']); + +filename = filename_from_url(param['texture'], param['name']); + +tag = "imlandscape" +objectname = "im/"+dir+"/"+filename + +saveImgData(param['imgdata'], filename); + +print "#@imlandscape" +#print ", ".join([k+"="+str(v) for k,v in param.iteritems()]) +print file_size (filename) +print bin_identify (filename) +print BASE_URL+objectname +insert_cmd(dir, filename, param['name'], param['texture'], dataobj); +moveToS3(filename, objectname); +os.remove(filename); diff --git a/Pb/Params/__init__.py b/Pb/Params/__init__.py deleted file mode 100644 index 2285e7a..0000000 --- a/Pb/Params/__init__.py +++ /dev/null @@ -1,10 +0,0 @@ -class Params(object): - def __init__(self, **kwargs): - for key, value in kwargs.items(): - setattr(self, key, value) - def __getattr__(self, attr): - return self.get(attr, None) - def __iter__(self): - for key, value in vars(self).iteritems(): - yield key, value - diff --git a/Pb/Pattern/__init__.py b/Pb/Pattern/__init__.py index 905ddf9..7c53746 100755 --- a/Pb/Pattern/__init__.py +++ b/Pb/Pattern/__init__.py @@ -11,9 +11,9 @@ import simplejson as json from PIL import Image import uuid -FUSE_MODE="Pin_Light" +_fuse_mode="Pin_Light" -class Pattern(Pb): +class Impattern(Pb): def __init__(self, **kwargs): self.params = {} self.tag = "imPattern"; @@ -149,7 +149,7 @@ class Pattern(Pb): self._call_cmd(cmd) #third step - def _fuse_mask(self, fuse_mode=FUSE_MODE): + def _fuse_mask(self, fuse_mode=_fuse_mode): cmd = [BIN_CONVERT, "-dispose", "2", self.filepath, "null:", self._downloaded_file, "-matte", "-compose", fuse_mode, "-layers", "composite", self.filepath] diff --git a/Pb/__init__.py b/Pb/__init__.py index 909f03b..0212235 100644 --- a/Pb/__init__.py +++ b/Pb/__init__.py @@ -7,33 +7,64 @@ import sys import os import random from subprocess import Popen,PIPE,call -from Pb.Params import Params +from Params import Params +import time Request = urllib2.Request urlencode = urllib.urlencode urlopen = urllib2.urlopen +_max_filename_length = 20; class Pb(object): def __init__(self): - self.params = Params(); + self._now = str(int(time.time())); + self.params = Params(classname=self.__class__.__name__, now=self._now); + self._files_created = [] + self.commands = []; self._working_dir = WORKING_DIR + def _filename_create(self, url=None, namepart=""): + if url: + _basename = os.path.basename(url) + namepart = re.split(r'\.')[0] + namepart = self.params.sanitize(_namepart)[0:_max_filename_length] + name = "" + if namepart: name += "%s-" % namepart + name += "%s_%s" % (self.__class__.__name__, self._now) + if self.params.username : name += "_%s" % self.params.username + return name + + def _filepath_create(self, filename, directory=WORKING_DIR): + return os.path.join(directory, filename) + + def _filename_filepath_create(self, url=None, namepart="", directory=WORKING_DIR): + _filename = self._filename_create(url=url, namepart=namepart); + _filepath = _filename_create(_filename, directory=directory); + return _filename, _filepath + + def _tempfilepath_create(self, namepart="temp", directory=WORKING_DIR): + _filename = self._filename_create(namepart=namepart) + return _filepath_create(_filename, directory=directory) + def _call_cmd(self, cmd): - try: - call(cmd) - except Exception as e: - self.err_warn("Could not call cmd: {}".format(str(cmd))) - self.err_warn(str(e)) + try: + self.call_cmd(cmd) + self.commands.append(" ".join(cmd)); + except Exception: + raise Exception("Unable to call cmd {}".format(str(cmd))) @staticmethod - def is_color(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: - sys.stderr.write("Not a color: {}\n".format(s)) - raise ValueError + def gif_frames(filepath): + try: + info = Popen([BIN_IDENTIFY,filepath], stdout=PIPE).communicate()[0] + frames = filter((lambda x: x), map( + (lambda x: x.split(" ")[0]), + (info).split('\n') + )) + return frames + except Exception as e: + self.err_warn("couldn't get gif frames") + raise e; @staticmethod def dimensions (filepath): @@ -41,77 +72,6 @@ class Pb(object): ident = (Popen([BIN_IDENTIFY, filepath], stdout=PIPE).communicate()[0]).split(" ") return ident[2].split("x") - @staticmethod - def is_number(s): - try: - return int(s) - except (ValueError, TypeError): - return False - - def bool_correct(self, s): - try: - if re.match(r'^false$', str(s), re.IGNORECASE): - return False - elif re.match(r'^true$', str(s), re.IGNORECASE): - return True - else: - return s - except Exception as e: - sys.stderr.write("WHAT THE FUCK") - - @staticmethod - def get_mimetype(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") - sys.stderr.write(str(e)) - raise; - - def sanitize (self, s): - return re.sub(r'\W+', '', s) - - @staticmethod - def now(): - return int(time.time()) - - @staticmethod - def browser_request (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, e: - if hasattr(e, 'code'): - sys.stderr.write( '%s - ERROR %s' % (url, e.code) ) - raise; - return None - else: - return response - - def download(self, url, destination, max_size=MAX_SIZE): - response = self.browser_request(url, None) - rawimg = response.read() - if len(rawimg) == 0: - sys.stderr.write("got zero-length file") - raise; - if len(rawimg) > max_size: - sys.stderr.write("file too big: max size {} KB / {} is {} KB".format( - str(MAX_SIZE/1024), - destination, - str(len(rawimg)/1024) - )) - raise; - f = open(destination, "w") - f.write(rawimg) - f.close() - @staticmethod def file_size (filepath): try: @@ -121,37 +81,6 @@ class Pb(object): sys.stderr.write(str(e)+"\n") raise; - def gif_frames(self, filepath): - try: - info = Popen([BIN_IDENTIFY,filepath], stdout=PIPE).communicate()[0] - frames = filter((lambda x: x), map( - (lambda x: x.split(" ")[0]), - (info).split('\n') - )) - return frames - except Exception as e: - sys.stderr.write("{} couldn't get gif frames".format(self.__class__.__name__)) - sys.stderr.write(str(e)) - raise; - - def tempname_create(self, basename=None, fmt="png"): - if not basename: basename = self.random_string_create() - return os.path.join(self._working_dir, "{}{}.{}".format(self.__class__.__name__, basename, fmt)) # this - - def _get_filename_and_type_from_url (self, url): - name = re.sub(r'(?:\s|\?.*|.*/)', '', urllib.unquote(url)); - try: - basename, ext = map(lambda s: self.sanitize(s), re.search(r'(^.*)\.(.*$)', name).groups()); - except Exception as e: - self.err_warn("Incompatible input file type") - if (len(basename) > 20): - basename = basename[:-20] - return "{}{}_{}_{}".format(self.tag, basename, self._now, self.params.username or ""), ext - - def random_string_create(self): - return "{}".format(random.uniform(0, 100000)); - - def _file_read(self, filepath): f = open(filepath, 'r'); data = f.read() @@ -161,6 +90,10 @@ class Pb(object): def err_warn(self, s): sys.stderr.write("ERROR:{} - {}\n".format(self.__class__.__name__, s)) + def _cleanup(self): + cmd = ["rm"]+self._files_created + self._call_cmd(cmd) + def err_fatal(self, s): sys.stderr.write("ERROR[FATAL]:{} - {}\n".format(self.__class__.__name__, s)) sys.exit(1); diff --git a/Pb/inspect_objects_example.py b/Pb/inspect_objects_example.py new file mode 100644 index 0000000..92e28b7 --- /dev/null +++ b/Pb/inspect_objects_example.py @@ -0,0 +1,15 @@ +class Test(object): + def __init__(self, one="la", two="di"): + self._test_args(inspect.getargvalues(inspect.currentframe())); + + def _test_args(self, _args_vals): + for arg in _args_vals.args: + if arg == "self": + continue + try: + sys.stdout.write("%s\n" % arg) + sys.stdout.write("\t%s\n" % _args_vals.locals.get(arg)) + except Exception as e: + sys.stderr.write("%s\n" % e ); + +t = Test(); -- cgit v1.2.3-70-g09d2