diff options
| author | Pepper <pepper@scannerjammer.com> | 2016-03-02 18:08:32 -0500 |
|---|---|---|
| committer | Pepper <pepper@scannerjammer.com> | 2016-03-02 18:08:32 -0500 |
| commit | 0e49b5974203c62bcd1edde1a83e73515358920c (patch) | |
| tree | f75589a7cdba859e7d1c6fd7b7162e9b21c8a484 | |
| parent | 8b961caa2031c672973645565bd71d120024539c (diff) | |
| parent | c952f69853241c8ee7d8fde282571f39cb7199b7 (diff) | |
merging
Merge branch 'newfile'
| -rw-r--r-- | photoblaster/_file.py | 166 | ||||
| -rw-r--r-- | photoblaster/config.py | 2 | ||||
| -rw-r--r-- | photoblaster/modules/.ropeproject/globalnames | bin | 476 -> 482 bytes | |||
| -rw-r--r-- | photoblaster/modules/base.py | 176 | ||||
| -rwxr-xr-x | photoblaster/modules/pbbreaker.py | 73 | ||||
| -rwxr-xr-x | photoblaster/modules/pbgenerate.py | 20 | ||||
| -rwxr-xr-x | photoblaster/modules/pbgradient.py | 165 | ||||
| -rwxr-xr-x | photoblaster/modules/pbgrid.py | 158 | ||||
| -rwxr-xr-x | photoblaster/modules/pblandscape/__init__.py | 17 | ||||
| -rwxr-xr-x | photoblaster/modules/pbpattern.py | 83 | ||||
| -rw-r--r-- | photoblaster/param/img_url.py | 55 | ||||
| -rw-r--r-- | photoblaster/params.py | 18 | ||||
| -rw-r--r-- | photoblaster/server.py | 8 | ||||
| -rw-r--r-- | run_module_examples.py | 4 |
14 files changed, 569 insertions, 376 deletions
diff --git a/photoblaster/_file.py b/photoblaster/_file.py new file mode 100644 index 0000000..4e4a382 --- /dev/null +++ b/photoblaster/_file.py @@ -0,0 +1,166 @@ +import os +import re +import random +import sha +import sys +import time +from photoblaster.s3.cli import S3Cli +from subprocess import Popen, PIPE, call +from photoblaster.config import WORKING_DIR, BIN_IDENTIFY, DEFAULT_WIDTH, \ + DEFAULT_HEIGHT, BIN_CONVERT, LOCAL, BASE_URL, DEFAULT_FINALFORMAT + +_MAX_FILENAME_LENGTH = 20 + + +class File(object): + def __init__( + self, + namepart="", + username="", + is_temp=False, + extension=DEFAULT_FINALFORMAT, + directory=WORKING_DIR, + classname="pb" + ): + self._classname = classname + self._is_temp = is_temp + self.extension = extension + self._directory = directory + self._filename = None + self._creation_time = str(int(time.time())) + self.set_filename( + namepart=namepart, + username=username + ) + self._hashdir = sha.new( + self.get_filepath() + ).hexdigest()[:2] + self._storage = "local" + + @classmethod + def from_url(cls, url, **kwargs): + """creates the filename from a url""" + _basename = os.path.basename(url) + namepart = re.split(r'\.', _basename)[0] + namepart = cls.url_sanitize(namepart)[0:_MAX_FILENAME_LENGTH] + kwargs["namepart"] = namepart + return cls(**kwargs) + + def set_filename( + self, + namepart="", + username="", + filename=None + ): + if filename: + self._filename = filename + return + name = "" + if self._is_temp: + namepart = "temp" + name += "%s_%s" % (self._classname, self.get_creation_time()) + if namepart: + name += "_%s" % namepart + if username: + name += "_%s" % username + if self.extension: + name += ".%s" % self.extension + self._filename = name + + def set_filepath(self, filepath, module=None): + """sets the filepath""" + if os.path.exists(self.get_filepath()): + cmd = ['mv', self.get_filepath(), filepath] + if module: + module.commands.append(" ".join(cmd)) + self.set_filename(filename=os.path.basename(filepath)) + self._directory = os.path.dirname(filepath) + + def get_filepath(self): + return os.path.join(self._directory, self.get_filename()) + + def get_filename(self): + return self._filename + + @staticmethod + def url_sanitize(s): + return re.sub(r'\W+', '', s) + + def get_dimensions(self): + try: + ident = (Popen( + [BIN_IDENTIFY, self.get_filepath()], + stdout=PIPE).communicate()[0]).split(" ") + return ident[2].split("x") + except Exception as e: + sys.stderr.write("Unable to get file dimensions:\n") + sys.stderr.write("%s \n" % e) + + def get_raw_data(self): + f = open(self.get_filepath(), 'r') + data = f.read() + f.close() + return data + + @staticmethod + 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: + sys.stderr.write("couldn't get gif frames\n") + raise e + + def choose_gif_frame(self, module=None): + _gif_frames = self.gif_frames(self.get_filepath()) + frame = random.choice(_gif_frames) + cmd = [BIN_CONVERT, frame, self.get_filepath()] + call(cmd) + if module: + module.commands.append(" ".join(cmd)) + + def as_dict(self): + url = "%s/im/%s/%s" % (BASE_URL, self._hashdir, self.get_filename()) + if self.get_storage() == "local": + url = "/im/cache/%s" % self.get_filename() + dimensions = self.get_dimensions() + return { + 'url': url, + 'size': self.get_size_in_bytes(), + 'width': "%spx" % dimensions[0], + 'height': "%spx" % dimensions[1], + } + + def s3move(self): + s3cli = S3Cli() + s3cli.s3move( + self.get_filepath(), "im/{}/{}".format( + self._hashdir, self.get_filename()) + ) + self._storage = "s3" + + def delete(self): + os.remove(self.get_filepath()) + self._storage = None + + def get_storage(self): + return self._storage + + def get_hashdir(self): + return self._hashdir + + def get_creation_time(self): + return self._creation_time + + def get_size_in_bytes(self): + try: + return os.stat(self.get_filepath())[6] + except Exception as e: + sys.stderr.write( + "Couldn't determine filesize of %s\n" % self.get_filepath()) + sys.stderr.write("%s\n" % e) diff --git a/photoblaster/config.py b/photoblaster/config.py index adaae8e..9e0bc4f 100644 --- a/photoblaster/config.py +++ b/photoblaster/config.py @@ -60,5 +60,5 @@ DB_PASSWORD = "gTYgT&M6q" DB_NAME = "asdfus" if LOCAL: DB_HOST = "localhost" - +BASE_URL = "http://i.asdf.us" diff --git a/photoblaster/modules/.ropeproject/globalnames b/photoblaster/modules/.ropeproject/globalnames Binary files differindex 6a68327..795a9f6 100644 --- a/photoblaster/modules/.ropeproject/globalnames +++ b/photoblaster/modules/.ropeproject/globalnames diff --git a/photoblaster/modules/base.py b/photoblaster/modules/base.py index ada2504..cae728e 100644 --- a/photoblaster/modules/base.py +++ b/photoblaster/modules/base.py @@ -2,22 +2,11 @@ contains only the Pb class which is used by the Pb.* modules for inheritance """ -import re -from photoblaster.config import WORKING_DIR, BIN_CONVERT, BIN_IDENTIFY,\ - DEFAULT_HEIGHT, DEFAULT_WIDTH, LOCAL -import time import sys -import os -import random -from subprocess import Popen, PIPE, call +from subprocess import call from photoblaster.params import Params -import sha import simplejson as json -from photoblaster.s3.cli import S3Cli from photoblaster.db.models.imcmd import ImCmd -BASE_URL = "http://i.asdf.us" - -_MAX_FILENAME_LENGTH = 20 class PbProcessError(Exception): @@ -28,65 +17,18 @@ class Pb(object): """Base Pb class. USED ONLY FOR INHERITANCE""" def __init__(self, **kwargs): self._input_kwargs = kwargs - self._now = str(int(time.time())) - self.params = Params(classname=self.__class__.__name__, now=self._now) + self.params = Params( + classname=self.__class__.__name__ + ) self._files_created = [] self.commands = [] - self._working_dir = WORKING_DIR self.tag = self.__class__.__name__ - self._hashdir = None self._db_url_param = None - # FIXME move to separate class - self.file_size = None + self._output_file = None + self.width = None self.height = None - self.filename = None - self.filepath = None - self.file_height = None - self.file_width = None - - def _filename_create(self, url=None, namepart="", extension=""): - if url: - _basename = os.path.basename(url) - namepart = re.split(r'\.', _basename)[0] - namepart = self._url_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 - if extension: - name += ".%s" % extension - 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, - extension="" - ): - filename = self._filename_create( - url=url, namepart=namepart, extension=extension) - filepath = self._filepath_create(filename, directory=directory) - return filename, filepath - - def _tempfilepath_create( - self, namepart="temp", directory=WORKING_DIR, extension=""): - filename = self._filename_create( - namepart=namepart, extension=extension) - return self._filepath_create(filename, directory=directory) - - def _hashdir_create(self): - self._hashdir = sha.new(self.filename).hexdigest()[:2] - - def _url_sanitize(self, s): - return re.sub(r'\W+', '', s) def _call_cmd(self, cmd): try: @@ -96,55 +38,14 @@ class Pb(object): except Exception: raise Exception("Unable to call cmd {}".format(str(cmd))) - def _dimensions(self, filepath): - try: - ident = (Popen( - [BIN_IDENTIFY, filepath], - stdout=PIPE).communicate()[0]).split(" ") - return ident[2].split("x") - except Exception as e: - self.err_warn("Unable to get file dimensions:\n") - self.err_ward(str(e)) - - def _file_dimensions(self): - self.file_width, self.file_height = self._dimensions(self.filepath) - - def _width_and_height_set( - self, - filepath=None, - width=DEFAULT_WIDTH, - height=DEFAULT_HEIGHT - ): - if filepath: - self.width, self.height = self._dimensions(filepath) - return - self.width = width - self.height = height - - def _file_size_get(self): - try: - self.file_size = os.stat(self.filepath)[6] - except Exception as e: - self.err_warn("Couldn't determine filesize of %s\n" % self.filepath) - self.err_warn(str(e)) - - def _file_read(self, filepath): - f = open(filepath, 'r') - data = f.read() - f.close() - return data - def err_warn(self, s): sys.stderr.write("ERROR:{} - {}\n".format(self.__class__.__name__, s)) raise PbProcessError - def _cleanup(self): + def cleanup(self): if not self._files_created: return - map(lambda n: os.remove(n), self._files_created) - - def _file_clean_local(self): - os.remove(self.filepath) + map(lambda n: n.delete(), self._files_created) def err_fatal(self, s): sys.stderr.write("ERROR[FATAL]:%s - %s\n" % ( @@ -161,39 +62,26 @@ class Pb(object): b = cls(**example_params) b.create() if verbose: - sys.stderr.write("generated %s\n" % b.filepath) + sys.stderr.write( + "generated %s\n" % b.get_output_file().get_filepath()) sys.stderr.write("files created %s\n" % b._files_created) sys.stderr.write("commands:\n %s\n" % ";\n ".join(b.commands)) return b - @staticmethod - 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: - Pb.err_warn("couldn't get gif frames") - raise e - - def _choose_gif_frame(self, filepath): - _gif_frames = Pb.gif_frames(filepath) - frame = random.choice(_gif_frames) - self._call_cmd([BIN_CONVERT, frame, filepath]) - - def db_send(self, remote_addr=None, db_connection=None): + def db_send( + self, + remote_addr=None, + db_connection=None + ): try: _insert_data = { - 'date': self._now, + 'date': self.get_output_file().get_creation_time(), 'remote_addr': remote_addr, 'name': str(self.params.username), 'url': self._db_url_param, - 'dir': self._hashdir, + 'dir': self.get_output_file().get_hashdir(), 'oldfile': None, - 'newfile': self.filename, + 'newfile': self.get_output_file().get_filename(), 'dataobj': json.dumps(dict(self._input_kwargs)), 'cmd': "; ".join(self.commands), 'tag': self.tag, @@ -202,27 +90,11 @@ class Pb(object): except Exception as e: self.err_warn("Problem sending to database:\n %s" % str(e)) - def file_s3move(self): - self._hashdir_create() - s3cli = S3Cli() - s3cli.s3move( - self.filepath, "im/{}/{}".format(self._hashdir, self.filename) - ) - self._file_clean_local() + def create(self): + pass - def file_dict(self): - url = "%s/im/%s/%s" % (BASE_URL, self._hashdir, self.filename) - if LOCAL: - url = "/im/cache/%s" % self.filename - return { - 'url': url, - 'size': self.file_size, - 'width': "%spx" % self.file_width, - 'height': "%spx" % self.file_height, - } + def get_output_file(self): + return self._output_file - def create(self): - # base methods FIXME move into File class - self._file_dimensions() - self._file_size_get() - self._cleanup() + def set_output_file(self, output_file): + self._output_file = output_file diff --git a/photoblaster/modules/pbbreaker.py b/photoblaster/modules/pbbreaker.py index e4c4665..bddd07a 100755 --- a/photoblaster/modules/pbbreaker.py +++ b/photoblaster/modules/pbbreaker.py @@ -4,6 +4,7 @@ import random import re from photoblaster.config import BIN_CONVERT from photoblaster.modules import Pb +from photoblaster._file import File DEFAULT_FINALFORMAT = "png" @@ -60,7 +61,7 @@ class PbBreaker(Pb): self.params.definitions_import(_definitions, kwargs, classname=self.__class__.__name__) - self._files_created.append(self.params.url.path) + self._files_created.append(self.params.url.get_file()) self.params.breaktype.set_val(self._get_breaktype( str(self.params.breaktype))) @@ -72,14 +73,20 @@ class PbBreaker(Pb): self.params.breakmode.set_val("subtle") elif not self.params.finalformat: self.params.finalformat.set_val(DEFAULT_FINALFORMAT) - self._width_and_height_set(filepath=self.params.url.path) + self.width, self.height = self.params.url.get_file().get_dimensions() - self.filename, self.filepath = self._filename_filepath_create( - url=self.params.url.url, - extension=self.params.finalformat + self.set_output_file( + File.from_url( + self.params.url.url, + extension=self.params.finalformat, + classname=self.__class__.__name__, + username=self.params.username) ) - self._conversion_file = self._tempfilepath_create( + self._conversion_file = File( + is_temp=True, namepart="conversion", + classname=self.__class__.__name__, + username=self.params.username, extension=self.params.breaktype ) @@ -90,26 +97,28 @@ class PbBreaker(Pb): def _rotate(self): cmd = [ - BIN_CONVERT, self.params.url.path, + BIN_CONVERT, self.params.url.get_file().get_filepath(), "-rotate", self.params.breakangle, - "+repage", self.params.url.path + "+repage", self.params.url.get_file().get_filepath() ] 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.get_output_file().get_filepath(), + "-rotate", angle, "+repage", + self.get_output_file().get_filepath()] self._call_cmd(cmd) if not self.params.expanded: cmd = [BIN_CONVERT, - self.filepath, + self.get_output_file().get_filepath(), "-gravity", "Center", "-crop", "{}x{}+0+0".format(self.width, self.height), "+repage", - self.filepath] + self.get_output_file().get_filepath()] self._call_cmd(cmd) def _subtle_break(self): @@ -133,11 +142,17 @@ class PbBreaker(Pb): 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._tempfilepath_create(extension="jpg") - self._call_cmd([BIN_CONVERT, self.params.url.path, jpg_file]) + re.match( + r'jpe?g$', + self.params.url.mimetype, + re.IGNORECASE + ): + jpg_file = File( + is_temp=True, + extension="jpg", + ) + self._call_cmd( + [BIN_CONVERT, self.params.url.path, jpg_file.get_filepath()]) self._files_created.append(jpg_file) self._conversion_file = jpg_file @@ -146,19 +161,19 @@ class PbBreaker(Pb): self._conversion_file = self.params.url.path return self._call_cmd([BIN_CONVERT, - self.params.url.path, - self._conversion_file]) + self.params.url.get_file().get_filepath(), + self._conversion_file.get_filepath()]) self._files_created.append(self._conversion_file) def _prepare_filedata(self): if self.params.url.mimetype == "gif" and\ - self.params.breaktype not in ['mat', 'psd']: - self._choose_gif_frame(self.params.url.path) + self.params.breaktype not in ['mat', 'psd']: + self.params.url.get_file().choose_gif_frame(module=self) if self.params.breakangle: self._rotate() self._enforce_jpg() self._first_conversion() - self._file_data = self._file_read(self._conversion_file) + self._file_data = self._conversion_file.get_raw_data() if not self._file_data: self.err_warn("Unable to get file data") @@ -167,20 +182,26 @@ class PbBreaker(Pb): self._subtle_break() elif self.params.breakmode == "extreme": self._extreme_break() - f = open(self._conversion_file, 'w') + f = open(self._conversion_file.get_filepath(), 'w') f.write(self._file_data) f.close() def _final_conversion(self): - self._call_cmd([BIN_CONVERT, self._conversion_file, self.filepath]) + self._call_cmd([ + BIN_CONVERT, + self._conversion_file.get_filepath(), + self.get_output_file().get_filepath()]) def psd_psbfilepath(num): - return os.path.join(re.sub(r'\.', "-%s." % num, self.filepath)) + return os.path.join( + re.sub( + r'\.', "-%s." % num, self.get_output_file().get_filepath()) + ) if str(self.params.breaktype) == 'psd': - self._call_cmd(['mv', psd_psbfilepath(1), self.filepath]) + self.get_output_file().set_filepath(psd_psbfilepath(1), module=self) self._files_created.append(psd_psbfilepath(0)) if str(self.params.breaktype) == 'psb': - self._call_cmd(['mv', psd_psbfilepath(0), self.filepath]) + self.get_output_file().set_filepath(psd_psbfilepath(0), module=self) self._files_created.append(psd_psbfilepath(1)) if self.params.breakangle: self._rotate_back() diff --git a/photoblaster/modules/pbgenerate.py b/photoblaster/modules/pbgenerate.py index d857562..1dd264b 100755 --- a/photoblaster/modules/pbgenerate.py +++ b/photoblaster/modules/pbgenerate.py @@ -2,6 +2,7 @@ from photoblaster.config import BIN_CONVERT, OUTPUT_IMAGE_TYPES,\ DEFAULT_FINALFORMAT from photoblaster.modules import Pb +from photoblaster._file import File _GRAVITY_PARAMS = [ "NorthWest", "North", "NorthEast", "West", @@ -123,8 +124,13 @@ class PbGenerate(Pb): elif self.params.transparent: self.tag = "%s:%s" % (self.tag, "transparent") - self.filename, self.filepath = self._filename_filepath_create( - url=self.params.url['url'], extension=self.params.format + self.set_output_file( + File.from_url( + self.params.url['url'], + username=self.params.username, + classname=self.__class__.__name__, + extension=self.params.format + ) ) self._db_url_param = str(self.params.url['url']) @@ -132,18 +138,18 @@ class PbGenerate(Pb): def _composite(self): """Imagemagick composite command""" cmd = [ - BIN_CONVERT, self.params.background['path'], - "null:", self.filepath, "-matte", + BIN_CONVERT, self.params.background.get_file().get_filepath(), + "null:", self.get_output_file().get_filepath(), "-matte", "-dispose", self.params.dispose, "-gravity", self.params.gravity, "-compose", self.params.compose, "-layers", "composite", - self.filepath + self.get_output_file().get_filepath() ] self._call_cmd(cmd) def _convert(self): """Imagemagick convert command""" - cmd = [BIN_CONVERT, self.params.url['path']] + cmd = [BIN_CONVERT, self.params.url.get_file().get_filepath()] if self.params.rotate: cmd += ["-rotate", self.params.rotate] if self.params.flip: @@ -184,7 +190,7 @@ class PbGenerate(Pb): ) ] cmd.append("-coalesce") # why? FIXME - cmd += [self.filepath] + cmd += [self.get_output_file().get_filepath()] self._call_cmd(cmd) def create(self): diff --git a/photoblaster/modules/pbgradient.py b/photoblaster/modules/pbgradient.py index 9b8ecaf..fe885b5 100755 --- a/photoblaster/modules/pbgradient.py +++ b/photoblaster/modules/pbgradient.py @@ -4,6 +4,7 @@ from photoblaster.config import DEFAULT_WIDTH, DEFAULT_HEIGHT,\ DEFAULT_FINALFORMAT, \
BIN_CONVERT, BEVELBORDER, OUTPUT_IMAGE_TYPES
from photoblaster.modules import Pb
+from photoblaster._file import File
_DEFAULT_COLOR_1 = "white"
_DEFAULT_COLOR_2 = "black"
@@ -19,77 +20,82 @@ _halftone_values = { "flatstripes": "o2x2",
}
+
class PbGradient(Pb):
example_params = {
- "width" : "200",
- "color1" : "#ffdead",
- "color2" : "blue",
- "stripes" : "true",
- "stripenumber" : "20",
- "gradienttype" : "plasma",
- "stripeintensity" : "20",
- "halftone" : "checkeredfade",
- "percentbeveled" : "30",
- "flip" : "true",
- "bevel" : "flatinner",
- "rotate" : "20",
- "height" : "200",
- "filetype" : "jpg",
- "username" : "whatever"
+ "width": "200",
+ "color1": "#ffdead",
+ "color2": "blue",
+ "stripes": "true",
+ "stripenumber": "20",
+ "gradienttype": "plasma",
+ "stripeintensity": "20",
+ "halftone": "checkeredfade",
+ "percentbeveled": "30",
+ "flip": "true",
+ "bevel": "flatinner",
+ "rotate": "20",
+ "height": "200",
+ "filetype": "jpg",
+ "username": "whatever"
}
+
def __init__(self, **kwargs):
super(PbGradient, self).__init__(**kwargs)
_definitions = {
- 'width': {'type':'int', 'default': DEFAULT_WIDTH},
- 'height': {'type':'int', 'default' : DEFAULT_HEIGHT},
- 'color1': {'type':'color', 'default': _DEFAULT_COLOR_1},
- 'color2': {'type':'color', 'default': _DEFAULT_COLOR_2},
- 'stripes': {'type':'bool'},
- 'stripenumber': {'type':'int', 'default': 0},
- 'stripeintensity': {'type':'int', 'default': 0},
- 'blurriness': {'type':'int', 'default': 0},
- 'contrast': {'type':'int', 'default': 100},
- 'brightness': {'type':'int', 'default': 100},
- 'saturation': {'type':'int', 'default': 100},
- 'hue': {'type':'int', 'default': 100},
- 'halftone': {'type':'enum', 'enum_values' : [
+ 'width': {'type': 'int', 'default': DEFAULT_WIDTH},
+ 'height': {'type': 'int', 'default': DEFAULT_HEIGHT},
+ 'color1': {'type': 'color', 'default': _DEFAULT_COLOR_1},
+ 'color2': {'type': 'color', 'default': _DEFAULT_COLOR_2},
+ 'stripes': {'type': 'bool'},
+ 'stripenumber': {'type': 'int', 'default': 0},
+ 'stripeintensity': {'type': 'int', 'default': 0},
+ 'blurriness': {'type': 'int', 'default': 0},
+ 'contrast': {'type': 'int', 'default': 100},
+ 'brightness': {'type': 'int', 'default': 100},
+ 'saturation': {'type': 'int', 'default': 100},
+ 'hue': {'type': 'int', 'default': 100},
+ 'halftone': {'type': 'enum', 'enum_values': [
'checkeredfade', 'etchedtransition', 'bendaydots',
'smallerdots1', 'smallerdots2', 'flatstripes',
]},
- 'bevel': {'type':'enum', 'enum_values' : [
+ 'bevel': {'type': 'enum', 'enum_values': [
'flatout', 'flatinner', 'evenlyframed', 'biginner',
'bigouter', 'dramaticflatout', 'dramaticflatinner',
]},
- 'percentbeveled': {'type':'int', 'default': _DEFAULT_BEVEL_PERCENT},
- 'tilt': {'type':'int'},
- 'rotate': {'type':'int'},
- 'flip': {'type':'bool'},
- 'flop': {'type':'bool'},
+ 'percentbeveled': {
+ 'type': 'int', 'default': _DEFAULT_BEVEL_PERCENT},
+ 'tilt': {'type': 'int'},
+ 'rotate': {'type': 'int'},
+ 'flip': {'type': 'bool'},
+ 'flop': {'type': 'bool'},
'filetype': {
'type': 'enum',
'enum_values': OUTPUT_IMAGE_TYPES,
'default': DEFAULT_FINALFORMAT
},
- 'gradienttype': {'type':'enum', 'enum_values':[
+ 'gradienttype': {'type': 'enum', 'enum_values': [
'gradient', 'canvas', 'plasma', 'radial', 'colorspace',
'mirrored', 'plasmawash', 'gradientwash', 'noise'
], 'default': 'gradient'},
- 'username': {'type':'string'}
+ 'username': {'type': 'string'}
}
- self.params.definitions_import(_definitions, kwargs, classname=self.__class__.__name__)
+ self.params.definitions_import(
+ _definitions, kwargs, classname=self.__class__.__name__)
- self.filename, self.filepath = self._filename_filepath_create()
-
- def _filename_create(self, **kwargs):
- _base = "{}{}-{}_{}".format(
- self.__class__.__name__,
- str(self.params.color1).replace('#', '').replace('(', '-').replace(')', '-'),
- str(self.params.color2).replace('#', '').replace('(', '-').replace(')', '-'),
- self._now,
+ namepart = "%s-%s" % (
+ str(self.params.color1).replace(
+ '#', '').replace('(', '-').replace(')', '-'),
+ str(self.params.color2).replace(
+ '#', '').replace('(', '-').replace(')', '-')
+ )
+ self.set_output_file(
+ File(
+ namepart=namepart,
+ extension=self.params.filetype,
+ classname=self.__class__.__name__,
+ username=self.params.username)
)
- if self.params.username: _base += "_%s" % self.params.username
- return _base + ".%s" % self.params.filetype
-
def _build_cmd(self):
cmd = [BIN_CONVERT]
@@ -109,15 +115,18 @@ class PbGradient(Pb): if self.params.contrast:
cmd.extend(["-contrast-stretch", str(self.params.contrast)])
_gradients = {
- "gradient" : ["gradient:{}-{}".format(self.params.color1, self.params.color2)],
- "canvas" : ["canvas:{}".format(self.params.color1)],
- "radial" : [
- "radial-gradient:{}-{}".format(self.params.color1, self.params.color2)
+ "gradient": [
+ "gradient:{}-{}".format(
+ self.params.color1, self.params.color2)],
+ "canvas": ["canvas:{}".format(self.params.color1)],
+ "radial": [
+ "radial-gradient:{}-{}".format(
+ self.params.color1, self.params.color2)
],
- "plasma" : [
+ "plasma": [
"plasma:{}-{}".format(self.params.color1, self.params.color2)
],
- "colorspace" : [
+ "colorspace": [
"-colorspace",
"Gray",
"plasma:{}-{}".format(
@@ -125,7 +134,7 @@ class PbGradient(Pb): self.params.color2
)
],
- "mirrored" : [
+ "mirrored": [
"plasma:{}-{}".format(
self.params.color1,
self.params.color2
@@ -133,26 +142,27 @@ class PbGradient(Pb): "(", "+clone", "-flop", ")",
"+append"
],
- "plasmawash" : [
+ "plasmawash": [
"plasma:{}-{}".format(
self.params.color1,
self.params.color2
),
"-set", "colorspace", "HSB"
],
- "gradientwash" : [
+ "gradientwash": [
"gradient:{}-{}".format(
self.params.color1,
self.params.color2
),
"-set", "colorspace", "HSB"
],
- "noise" : ["xc:", "+noise", "Random", "-virtual-pixel", "tile"]
+ "noise": ["xc:", "+noise", "Random", "-virtual-pixel", "tile"]
}
cmd += _gradients[str(self.params.gradienttype)]
if self.params.blurriness:
- cmd.extend(["-blur", "0x{}".format(self.params.blurriness), "-auto-level"])
+ cmd.extend(
+ ["-blur", "0x{}".format(self.params.blurriness), "-auto-level"])
if self.params.stripes and self.params.stripenumber:
cmd.extend(["-function", "Sinusoid"])
@@ -177,32 +187,47 @@ class PbGradient(Pb): self.params.hue or "100"
)
]
- cmd.append(self.filepath)
- import sys
- sys.stderr.write("\n%s\n" % cmd)
+ cmd.append(self.get_output_file().get_filepath())
self._call_cmd(cmd)
- if self.params.bevel: self._make_bevel()
+ if self.params.bevel:
+ self._make_bevel()
def _get_bevelvalue(self):
w, h = map(int, (self.params.width, self.params.height))
if h >= w:
- bevpercentval = str(int(int(self.params.percentbeveled)*int(h))/500)
+ bevpercentval = str(
+ int(int(self.params.percentbeveled)*int(h))/500)
else:
- bevpercentval = str(int(int(self.params.percentbeveled)*int(w))/500)
+ bevpercentval = str(
+ int(int(self.params.percentbeveled)*int(w))/500)
return {
"flatout": ["-s", bevpercentval, "-m", "outer"],
"flatinner": ["-s", bevpercentval, "-m", "inner"],
"evenlyframed": ["-s", bevpercentval, "-m", "split"],
- "biginner": ["-s", bevpercentval, "-m", "outer", "-c", "50", "-b", "red", "-a", "25"],
- "bigouter": ["-s", bevpercentval, "-m", "split", "-c", "50", "-b", "red", "-a", "25"],
- "dramaticflatout": ["-s", bevpercentval, "-m", "outer", "-a", "25", "-b", "blue"],
- "dramaticflatinner": ["-s", bevpercentval, "-m", "outer", "-a", "25", "-b", "blue"],
+ "biginner": [
+ "-s",
+ bevpercentval,
+ "-m",
+ "outer",
+ "-c",
+ "50", "-b", "red", "-a", "25"],
+ "bigouter": [
+ "-s",
+ bevpercentval,
+ "-m",
+ "split", "-c", "50", "-b", "red", "-a", "25"],
+ "dramaticflatout": [
+ "-s", bevpercentval, "-m", "outer", "-a", "25", "-b", "blue"],
+ "dramaticflatinner": [
+ "-s", bevpercentval, "-m", "outer", "-a", "25", "-b", "blue"],
}[str(self.params.bevel)]
def _make_bevel(self):
cmd = [BEVELBORDER]
cmd += self._get_bevelvalue()
- cmd += [self.filepath, self.filepath]
+ cmd += [
+ self.get_output_file().get_filepath(),
+ self.get_output_file().get_filepath()]
self._call_cmd(cmd)
def create(self):
diff --git a/photoblaster/modules/pbgrid.py b/photoblaster/modules/pbgrid.py index 83950e6..c43c4b1 100755 --- a/photoblaster/modules/pbgrid.py +++ b/photoblaster/modules/pbgrid.py @@ -2,80 +2,99 @@ from photoblaster.config import DEFAULT_FINALFORMAT, DEFAULT_HEIGHT,\ DEFAULT_WIDTH, OUTPUT_IMAGE_TYPES,\ THREEDROTATE, GRID, BIN_CONVERT, BIN_COMPOSITE from photoblaster.modules import Pb +from photoblaster._file import File _DEFAULT_LINE_COLOR = "silver" + class PbGrid(Pb): """ Creates or overlays a grid on an image, and adds 3D perspective """ example_params = { - 'bgimage': 'http://i.asdf.us/im/1a/imBreak_1424909483_xx_abridged___.gif', + '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', + 'username': 'someuser', 'trim': 'true' } + def __init__(self, **kwargs): super(PbGrid, self).__init__(**kwargs) _definitions = { - 'width': {'type':'int'}, - 'height': {'type':'int'}, - 'linethickness': {'type':'int', 'default': 1}, - 'opacity': {'type':'float', "default": 1.0}, - 'linecolor': {'type':'color', 'default': 'whitesmoke'}, - 'spacing': {'type':'int', 'default': 10}, - 'vlines': {'type':'bool'}, - 'hlines': {'type':'bool'}, - 'shadow': {'type':'bool'}, - 'bgimage': {'type':'img_url'}, - 'bgcolor': {'type':'color', 'default': 'transparent'}, - 'imageinstead': {'type':'img_url'}, - 'planebgcolor': {'type':'color', 'default': 'transparent'}, - 'planebgimage': {'type':'img_url'}, - 'swing': {'type':'int'}, - 'tilt': {'type':'int'}, - 'roll': {'type':'int'}, - 'zoom': {'type':'float'}, - 'skycolor': {'type':'color', 'default': 'transparent'}, + 'width': {'type': 'int'}, + 'height': {'type': 'int'}, + 'linethickness': {'type': 'int', 'default': 1}, + 'opacity': {'type': 'float', "default": 1.0}, + 'linecolor': {'type': 'color', 'default': 'whitesmoke'}, + 'spacing': {'type': 'int', 'default': 10}, + 'vlines': {'type': 'bool'}, + 'hlines': {'type': 'bool'}, + 'shadow': {'type': 'bool'}, + 'bgimage': {'type': 'img_url'}, + 'bgcolor': {'type': 'color', 'default': 'transparent'}, + 'imageinstead': {'type': 'img_url'}, + 'planebgcolor': {'type': 'color', 'default': 'transparent'}, + 'planebgimage': {'type': 'img_url'}, + 'swing': {'type': 'int'}, + 'tilt': {'type': 'int'}, + 'roll': {'type': 'int'}, + 'zoom': {'type': 'float'}, + 'skycolor': {'type': 'color', 'default': 'transparent'}, 'transition': { - 'type':'enum', - 'enum_values' :[ + 'type': 'enum', + 'enum_values': [ 'background', 'dither', 'edge', 'mirror', 'random', 'tile' ], 'default': 'background' }, - 'trim': {'type':'bool'}, + 'trim': {'type': 'bool'}, 'finalformat': { - 'type':'enum', + 'type': 'enum', 'enum_values': OUTPUT_IMAGE_TYPES, 'default': DEFAULT_FINALFORMAT }, - 'username': {'type':'string'}, + 'username': {'type': 'string'}, } self.params.definitions_import( _definitions, kwargs, classname=self.__class__.__name__ ) if self.params.imageinstead: - self.filename, self.filepath = self._filename_filepath_create( - url=self.params.imageinstead['url'], extension=self.params.finalformat + self.set_output_file( + File.from_url( + self.params.imageinstead['url'], + extension=self.params.finalformat, + classname=self.__class__.__name__, + username=self.params.username) ) elif self.params.planebgimage: - self.filename, self.filepath = self._filename_filepath_create( - url=self.params.planebgimage['url'], extension=self.params.finalformat + self.set_output_file( + File.from_url( + self.params.planebgimage['url'], + extension=self.params.finalformat, + classname=self.__class__.__name__, + username=self.params.username) ) else: - self.filename, self.filepath = self._filename_filepath_create( - extension=self.params.finalformat + self.set_output_file( + File( + extension=self.params.finalformat, + classname=self.__class__.__name__, + username=self.params.username) ) self._db_url_param = str( filter( lambda n: n, [ - self.params.imageinstead, self.params.planebgimage, self.params.bgimage, "NULL" + self.params.imageinstead, + self.params.planebgimage, + self.params.bgimage, + "NULL" ] )[0] ) @@ -89,7 +108,10 @@ class PbGrid(Pb): if self.params.bgimage: return bgcolor = "xc:{}".format(self.params.bgcolor or 'transparent') - cmd = [BIN_CONVERT, "-size", dimensions, bgcolor, self.filepath] + cmd = [ + BIN_CONVERT, + "-size", dimensions, bgcolor, + self.get_output_file().get_filepath()] self._call_cmd(cmd) #2nd step-- run grid @@ -109,7 +131,9 @@ class PbGrid(Pb): cmd += ['-t', self.params.linethickness] if self.params.opacity: cmd += ['-o', self.params.opacity] - cmd += [self.filepath, self.filepath] + cmd += [ + self.get_output_file().get_filepath(), + self.get_output_file().get_filepath()] self._call_cmd(cmd) def _shadow_cmd(self): @@ -120,55 +144,69 @@ class PbGrid(Pb): """ cmd = [ BIN_CONVERT, - self.filepath, - "(", "+clone", "-background", "black", "-shadow", "100x2+20+10", ")", + self.get_output_file().get_filepath(), + "(", + "+clone", "-background", "black", "-shadow", "100x2+20+10", ")", "+swap", "-background", "none", "-layers", "merge", "+repage", - self.filepath + self.get_output_file().get_filepath() ] self._call_cmd(cmd) - def _threed_rotate_cmd(self): - #3rd step--run 3Drotate + #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.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 + 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] + if self.params.transition: + cmd += ["vp={}".format(self.params.transition)] + cmd += [ + self.get_output_file().get_filepath(), + self.get_output_file().get_filepath()] self._call_cmd(cmd) - def _trim_cmd(self): - cmd = [BIN_CONVERT, self.filepath, "-trim", "+repage", self.filepath] + cmd = [ + BIN_CONVERT, + self.get_output_file().get_filepath(), + "-trim", "+repage", self.get_output_file().get_filepath()] self._call_cmd(cmd) def _prepare_gridimage(self, image): if image['mimetype'] == 'gif': - _frame = self._choose_gif_frame(image['path']) + image.get_file().choose_gif_frame(module=self) if image['mimetype'] != 'png': - cmd = [BIN_CONVERT, image['path'], self.filepath] + cmd = [ + BIN_CONVERT, + image.get_file().get_filepath(), + self.get_output_file().get_filepath()] else: - cmd = ['cp', image['path'], self.filepath] + cmd = [ + 'cp', image.get_file().get_filepath(), + self.output_file.get_filepath()] self._call_cmd(cmd) - def _overlay_planebgimage(self): + import sys + sys.stderr.write("should be overlaying here!\n\n\n") cmd = [ BIN_COMPOSITE, "-compose", "Dst_Over", "-gravity", "center", self.params.planebgimage["path"], - self.filepath, - self.filepath + self.get_output_file().get_filepath(), + self.get_output_file().get_filepath() ] self._call_cmd(cmd) @@ -181,8 +219,16 @@ class PbGrid(Pb): else: self._make_canvas() self._grid_command() - if self.params.shadow: self._shadow_cmd() + 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() + if self.params.planebgimage: + import sys + sys.stderr.write("what the fuck!!!") + self._overlay_planebgimage() + else: + import sys + sys.stderr.write("UMMMMMMMMMMMMMMMMMM\n") + if self.params.trim: + self._trim_cmd() super(PbGrid, self).create() diff --git a/photoblaster/modules/pblandscape/__init__.py b/photoblaster/modules/pblandscape/__init__.py index b03dce0..3044b3b 100755 --- a/photoblaster/modules/pblandscape/__init__.py +++ b/photoblaster/modules/pblandscape/__init__.py @@ -2,7 +2,7 @@ import base64 from photoblaster.modules import Pb import urlparse import re - +from photoblaster._file import File class PbLandscape(Pb): try: @@ -28,11 +28,15 @@ class PbLandscape(Pb): _definitions, kwargs, classname=self.__class__.__name__) - _namepart = re.sub(r'https?:?/?/?', '', str(self.params.texture)) - self.filename, self.filepath = self._filename_filepath_create( - url=_namepart, extension="png" + namepart = re.sub(r'https?:?/?/?', '', str(self.params.texture)) + self.set_output_file( + File( + namepart=namepart, + classname=self.__class__.__name__, + extension="png", + username=self.params.username + ) ) - self._db_url_param = str(self.params.texture) def _saveImgData(self): @@ -43,7 +47,8 @@ class PbLandscape(Pb): # Do something smart with charset and b64 instead of assuming plaindata = base64.b64decode(data) - with open(self.filepath, 'wb') as f: + with open( + self.get_output_file().get_filepath(), 'wb') as f: f.write(plaindata) except Exception as e: self.err_warn(str(e)) diff --git a/photoblaster/modules/pbpattern.py b/photoblaster/modules/pbpattern.py index ccf6963..1e80647 100755 --- a/photoblaster/modules/pbpattern.py +++ b/photoblaster/modules/pbpattern.py @@ -1,39 +1,54 @@ from photoblaster.config import BIN_CONVERT, BIN_COMPOSITE from photoblaster.modules import Pb from PIL import Image +from photoblaster._file import File + +_FUSE_MODE = "Pin_Light" -_FUSE_MODE="Pin_Light" class PbPattern(Pb): example_params = { - "pattern_data" : '{"matrix":[["0","0","0","0","0","1","0","0","0","0"],["0","0","0","0","1","1","1","0","0","0"],["0","0","1","1","1","0","1","0","0","0"],["0","1","1","0","0","0","0","0","0","0"],["0","1","0","0","1","0","0","0","0","0"],["0","1","0","0","1","0","0","0","1","0"],["0","1","0","0","1","1","0","0","1","0"],["0","1","0","0","0","1","1","1","1","0"],["0","1","1","1","1","0","0","0","0","0"],["0","0","0","0","1","0","0","0","0","0"]],"width":"10","height":"10"}', - "image_url" : "http://i.asdf.us/im/be/PinkHijab_1425078647_reye.gif", -# "username" : "garfield", -# "pattern_url" : "http://asdf.us/impattern/patterns/1.png", + "pattern_data": '{"matrix":[["0","0","0","0","0","1","0","0","0","0"],["0","0","0","0","1","1","1","0","0","0"],["0","0","1","1","1","0","1","0","0","0"],["0","1","1","0","0","0","0","0","0","0"],["0","1","0","0","1","0","0","0","0","0"],["0","1","0","0","1","0","0","0","1","0"],["0","1","0","0","1","1","0","0","1","0"],["0","1","0","0","0","1","1","1","1","0"],["0","1","1","1","1","0","0","0","0","0"],["0","0","0","0","1","0","0","0","0","0"]],"width":"10","height":"10"}', + #"username": "garfield", + #"pattern_url": "http://asdf.us/impattern/patterns/1.png", + "image_url": "http://i.asdf.us/im/be/PinkHijab_1425078647_reye.gif", } + def __init__(self, **kwargs): super(PbPattern, self).__init__(**kwargs) _definitions = { - 'image_url': {'type':'img_url'}, - 'pattern_url': {'type':'img_url'}, - 'pattern_data': {'type':'json'}, - 'username': {'type':'string'}, + 'image_url': {'type': 'img_url'}, + 'pattern_url': {'type': 'img_url'}, + 'pattern_data': {'type': 'json'}, + 'username': {'type': 'string'}, } - self.params.definitions_import(_definitions, kwargs, classname=self.__class__.__name__) - self.filename, self.filepath = self._filename_filepath_create( - url=self.params.image_url['url'], extension=self.params.image_url['mimetype'] + self.params.definitions_import( + _definitions, kwargs, classname=self.__class__.__name__) + self.set_output_file( + File.from_url( + self.params.image_url['url'], + extension=self.params.image_url['mimetype'], + classname=self.__class__.__name__, + username=self.params.username + ) ) + self.pattern_file = None if self.params.pattern_data: - _pattern_filename, self._pattern_filepath = self._filename_filepath_create(namepart="pattern") + self.pattern_file = File( + namepart="pattern", + username=self.params.username, + classname=self.__class__.__name__, + extension="png" + ) self._from_pattern_data() elif not self.params.pattern_url: - self.err_warn("pattern must be supplied as json array or as a png url") + self.err_warn( + "pattern must be supplied as json array or as a png url") else: - self._pattern_filepath = self.params.pattern_url['path'] + self.pattern_file = self.params.pattern_url.get_file() self._db_url_param = str(self.params.image_url.url) - def _from_pattern_data(self): def boolToColor(boolean): if boolean: @@ -48,34 +63,48 @@ class PbPattern(Pb): for i in range(0, len(specs['matrix'])): for j in range(0, len(specs['matrix'][i])): pixels[j, i] = boolToColor(int(specs['matrix'][i][j])) - - img.save(self._pattern_filepath, "PNG") + img.save(self.pattern_file.get_filepath(), "PNG") #first step def _make_canvas(self): - _width, _height = self._dimensions(self.params.image_url['path']) # same here - cmd = [BIN_CONVERT, "-size", _width + "x" + _height, "canvas:transparent", self.filepath] + _width, _height = self.params.image_url.get_file().get_dimensions() + cmd = [ + BIN_CONVERT, "-size", _width + "x" + _height, + "canvas:transparent", + self.get_output_file().get_filepath()] self._call_cmd(cmd) #second step use the Canvas as a background def _make_mask(self): #tile the pattern pattern on the canvas - cmd = [BIN_COMPOSITE, "-tile", self._pattern_filepath, self.filepath, self.filepath] + cmd = [ + BIN_COMPOSITE, "-tile", self.pattern_file.get_filepath(), + self.get_output_file().get_filepath(), + self.get_output_file().get_filepath()] self._call_cmd(cmd) #fuse the tiled file to create a mask - #convert thebg.gif -compose Dst_In null: thefile.gif -matte -layers composite new.gif + #convert thebg.gif -compose Dst_In null: \ + #thefile.gif -matte -layers composite new.gif cmd = [ - BIN_CONVERT, self.filepath, "-compose", "Dst_In", "null:", - self.params.image_url['path'], "-matte", "-layers", "composite", self.filepath + BIN_CONVERT, + self.get_output_file().get_filepath(), + "-compose", "Dst_In", "null:", + self.params.image_url.get_file().get_filepath(), + "-matte", "-layers", "composite", + self.get_output_file().get_filepath() + ] self._call_cmd(cmd) #third step def _fuse_mask(self, fuse_mode=_FUSE_MODE): cmd = [ - BIN_CONVERT, "-dispose", "2", self.filepath, "null:", - self.params.image_url['path'], "-matte", "-compose", fuse_mode, "-layers", "composite", - self.filepath + BIN_CONVERT, "-dispose", "2", + self.get_output_file().get_filepath(), + "null:", + self.params.image_url.get_file().get_filepath(), + "-matte", "-compose", fuse_mode, "-layers", "composite", + self.get_output_file().get_filepath() ] self._call_cmd(cmd) diff --git a/photoblaster/param/img_url.py b/photoblaster/param/img_url.py index 27099eb..a6227e1 100644 --- a/photoblaster/param/img_url.py +++ b/photoblaster/param/img_url.py @@ -1,12 +1,12 @@ """Img_url param class definition lives here""" -import os from photoblaster.param import Param from photoblaster.config import MAX_SIZE, SPECIAL_DOWNLOADERS,\ SPECIAL_DOWNLOADERS_MAX_SIZE,\ BIN_IDENTIFY +from photoblaster._file import File import urllib2 from subprocess import Popen, PIPE -import sys + class Img_url(Param): def __init__(self, value, key="", classname=""): @@ -26,20 +26,19 @@ class Img_url(Param): """ super(Img_url, self).__init__(classname=classname) if value: - self.filename = self._filename_temporary(key) - - self.path = os.path.join(self._working_dir, self.filename) - self._image_download(value, self.path) - self.mimetype = self._image_mimetype(self.path) + self._file = File( + namepart=key, + classname=classname, + is_temp=True + ) + self._image_download(value) + self.mimetype = self._image_mimetype(self.get_filepath()) self.url = value - def _filename_temporary(self, s): - return "_tmp-{}-{}_{}".format(self._classname, self._now, s) - def __dict__(self): return { - 'filename' : self.filename, - 'path': self.path, + 'filename': self.get_filename(), + 'path': self.get_filepath(), 'url': self.url, 'mimetype': self.mimetype } @@ -51,17 +50,17 @@ class Img_url(Param): return str(self.__dict__()) def __nonzero__(self): - return True if self.path and self.mimetype else False + return True if self.get_file() and self.mimetype else False - def _image_download(self, url, path): + def _image_download(self, url): """downloads the image to the path specified in the local filesystem""" max_size = MAX_SIZE if self.username in SPECIAL_DOWNLOADERS: max_size = SPECIAL_DOWNLOADERS_MAX_SIZE try: - self._download(url, path, max_size=max_size) - except Exception as e: + self._download(url, self.get_filepath(), max_size=max_size) + except Exception: self.err_warn("Download failed") def _browser_request(self, url, data=None): @@ -75,7 +74,8 @@ class Img_url(Param): response = urllib2.urlopen(req) except IOError as e: if hasattr(e, 'code'): - self.err_warn('browser request error: %s - ERROR %s' % (url, e.code)) + self.err_warn( + 'browser request error: %s - ERROR %s' % (url, e.code)) raise IOError return response @@ -97,11 +97,28 @@ class Img_url(Param): f.close() def _image_mimetype(self, f): - """retrieves the image mimetype from the file header using imagemagick""" + """ + retrieves the image mimetype from the file header using imagemagick + """ try: mimetype = Popen( [BIN_IDENTIFY, f], stdout=PIPE ).communicate()[0].split(" ")[1].lower() return mimetype - except Exception as e: + except Exception: self.err_warn("Couldn't determine mimetype") + + def delete(self): + self._file.delete() + + def get_filename(self): + return self._file.get_filename() + + def get_filepath(self): + return self._file.get_filepath() + + def get_file_dimensions(self): + return self._file.get_dimensions() + + def get_file(self): + return self._file diff --git a/photoblaster/params.py b/photoblaster/params.py index 74570aa..275f030 100644 --- a/photoblaster/params.py +++ b/photoblaster/params.py @@ -3,9 +3,11 @@ import sys from photoblaster.param import Bool, Color, Enum, Float, Int,\ Img_url, Json, Raw, String + class BadParamError(Exception): pass + class Params(object): """ Params is a collection of Param instances, @@ -23,18 +25,19 @@ class Params(object): def _error_log(self, s, error=None, fatal=False): message = "ERROR - BAD PARAM" - if fatal: message += "- [FATAL] -" + if fatal: + message += "- [FATAL] -" sys.stderr.write("{}:{} - {}\n".format(message, self._classname, s)) if error: sys.stderr.write("PARAM ERROR: {}\n".format(str(error))) def err_warn(self, s, error=None): - self._error_log(s, error=error); + self._error_log(s, error=error) raise BadParamError("%s - %s" % (self._classname, s)) def __getattr__(self, key): try: - return self.__getattribute__(key); + return self.__getattribute__(key) except AttributeError: return None @@ -46,7 +49,8 @@ class Params(object): for key in def_dict.keys(): value = None if key in classkwargs: - value = classkwargs.get(key, None) or def_dict[key].get('default', None) + value = classkwargs.get(key, None) or \ + def_dict[key].get('default', None) elif 'default' in def_dict[key]: value = def_dict[key]['default'] if def_dict[key]['type'] == "bool": @@ -54,7 +58,10 @@ class Params(object): elif def_dict[key]['type'] == "color": instance = Color(value, classname=classname) elif def_dict[key]['type'] == "enum": - instance = Enum(value, enum_values=def_dict[key]['enum_values'], classname=classname) + instance = Enum( + value, + enum_values=def_dict[key]['enum_values'], + classname=classname) elif def_dict[key]['type'] == "float": instance = Float(value, classname=classname) elif def_dict[key]['type'] == "img_url": @@ -68,4 +75,3 @@ class Params(object): elif def_dict[key]['type'] == "string": instance = String(value, classname=classname) self.__setattr__(key, instance) - diff --git a/photoblaster/server.py b/photoblaster/server.py index 2ce1ebd..4a80fd0 100644 --- a/photoblaster/server.py +++ b/photoblaster/server.py @@ -81,7 +81,6 @@ class InvalidUsage(Exception): class Server(object): """Main server class""" def __init__(self): - # self.app = Flask(__name__) self.app = Flask(__name__, static_folder=STATIC_FOLDER) self._wsgi_server = None self._classname_aliases = _CLASSNAME_ALIASES @@ -228,15 +227,16 @@ class Server(object): raise InvalidUsage('No such api', status_code=410) def _response_post(self, pb_classname, request_form, remote_addr=None): + #load plugins here pb_class = self._find_class_by_name(pb_classname) - # classnames = map(lambda c: c.__name__, Pb.__subclasses__()) try: pb = pb_class(**request_form) pb.create() if not LOCAL: - pb.file_s3move() + pb.get_output_file().s3move() pb.db_send(remote_addr=remote_addr) - json_data = jsonify(pb.file_dict()) + json_data = jsonify(pb.get_output_file().as_dict()) + pb.cleanup() if pb.params.callback: # accounts for jsonp return "%s(%s)" % (pb.params.callback, json_data) return json_data diff --git a/run_module_examples.py b/run_module_examples.py index 82d35ea..8a22c10 100644 --- a/run_module_examples.py +++ b/run_module_examples.py @@ -5,6 +5,6 @@ for cls in Pb.__subclasses__(): print cls.__name__ if cls.__name__ == "PbGradient": instance = cls.example_run() - instance.file_s3move() - print instance.file_dict() + instance.get_output_file().s3move() + print instance.get_output_file().as_dict() instance.db_send() |
