diff options
| -rw-r--r-- | photoblaster/_file.py | 2 | ||||
| -rw-r--r-- | photoblaster/modules/__init__.py | 107 | ||||
| -rw-r--r-- | photoblaster/modules/base.py | 100 | ||||
| -rwxr-xr-x | photoblaster/modules/pbbreaker/__init__.py (renamed from photoblaster/modules/pbbreaker.py) | 8 | ||||
| -rwxr-xr-x | photoblaster/modules/pbgenerate/__init__.py (renamed from photoblaster/modules/pbgenerate.py) | 8 | ||||
| -rwxr-xr-x | photoblaster/modules/pbgradient/__init__.py (renamed from photoblaster/modules/pbgradient.py) | 8 | ||||
| -rwxr-xr-x | photoblaster/modules/pbgrid/__init__.py (renamed from photoblaster/modules/pbgrid.py) | 9 | ||||
| -rwxr-xr-x | photoblaster/modules/pblandscape/__init__.py | 4 | ||||
| -rwxr-xr-x | photoblaster/modules/pbpattern/__init__.py (renamed from photoblaster/modules/pbpattern.py) | 8 | ||||
| -rw-r--r-- | pluginloader.py | 19 | ||||
| -rw-r--r-- | run_module_examples.py | 12 | ||||
| -rw-r--r-- | share/install/requirements.txt | 1 |
12 files changed, 162 insertions, 124 deletions
diff --git a/photoblaster/_file.py b/photoblaster/_file.py index 4e4a382..5b51acc 100644 --- a/photoblaster/_file.py +++ b/photoblaster/_file.py @@ -4,7 +4,6 @@ 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 @@ -137,6 +136,7 @@ class File(object): } def s3move(self): + from photoblaster.s3.cli import S3Cli s3cli = S3Cli() s3cli.s3move( self.get_filepath(), "im/{}/{}".format( diff --git a/photoblaster/modules/__init__.py b/photoblaster/modules/__init__.py index abd4380..b71f67b 100644 --- a/photoblaster/modules/__init__.py +++ b/photoblaster/modules/__init__.py @@ -1,7 +1,100 @@ -from photoblaster.modules.base import Pb, PbProcessError -from photoblaster.modules.pbgrid import PbGrid -from photoblaster.modules.pbbreaker import PbBreaker -from photoblaster.modules.pbpattern import PbPattern -from photoblaster.modules.pbgenerate import PbGenerate -from photoblaster.modules.pblandscape import PbLandscape -from photoblaster.modules.pbgradient import PbGradient +""" +contains only the Pb class which is used by the Pb.* modules for inheritance +""" + +import sys +from subprocess import call +from photoblaster.params import Params +import simplejson as json +from photoblaster.db.models.imcmd import ImCmd + + +class PbProcessError(Exception): + pass + + +class ModuleBase(object): + """Base Pb class. USED ONLY FOR INHERITANCE""" + def __init__(self, **kwargs): + self._input_kwargs = kwargs + self.params = Params( + classname=self.__class__.__name__ + ) + self._files_created = [] + self.commands = [] + self.tag = self.__class__.__name__ + self._db_url_param = None + + self._output_file = None + + self.width = None + self.height = None + + def _call_cmd(self, cmd): + try: + cmd = map(lambda i: str(i), cmd) + call(cmd) + self.commands.append(" ".join(cmd)) + except Exception: + raise Exception("Unable to call cmd {}".format(str(cmd))) + + def err_warn(self, s): + sys.stderr.write("ERROR:{} - {}\n".format(self.__class__.__name__, s)) + raise PbProcessError + + def cleanup(self): + if not self._files_created: + return + map(lambda n: n.delete(), self._files_created) + + def err_fatal(self, s): + sys.stderr.write("ERROR[FATAL]:%s - %s\n" % ( + self.__class__.__name__, s)) + sys.exit(1) + + @classmethod + def example_run(cls, params=None, verbose=True): + example_params = params or cls.example_params + if not example_params: + raise AttributeError( + "Must supply test params to test %s" % cls.__name__ + ) + b = cls(**example_params) + b.create() + if verbose: + 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 + + def db_send( + self, + remote_addr=None, + db_connection=None + ): + try: + _insert_data = { + 'date': self.get_output_file().get_creation_time(), + 'remote_addr': remote_addr, + 'name': str(self.params.username), + 'url': self._db_url_param, + 'dir': self.get_output_file().get_hashdir(), + 'oldfile': None, + 'newfile': self.get_output_file().get_filename(), + 'dataobj': json.dumps(dict(self._input_kwargs)), + 'cmd': "; ".join(self.commands), + 'tag': self.tag, + } + ImCmd.create(**_insert_data) + except Exception as e: + self.err_warn("Problem sending to database:\n %s" % str(e)) + + def create(self): + pass + + def get_output_file(self): + return self._output_file + + def set_output_file(self, output_file): + self._output_file = output_file diff --git a/photoblaster/modules/base.py b/photoblaster/modules/base.py deleted file mode 100644 index cae728e..0000000 --- a/photoblaster/modules/base.py +++ /dev/null @@ -1,100 +0,0 @@ -""" -contains only the Pb class which is used by the Pb.* modules for inheritance -""" - -import sys -from subprocess import call -from photoblaster.params import Params -import simplejson as json -from photoblaster.db.models.imcmd import ImCmd - - -class PbProcessError(Exception): - pass - - -class Pb(object): - """Base Pb class. USED ONLY FOR INHERITANCE""" - def __init__(self, **kwargs): - self._input_kwargs = kwargs - self.params = Params( - classname=self.__class__.__name__ - ) - self._files_created = [] - self.commands = [] - self.tag = self.__class__.__name__ - self._db_url_param = None - - self._output_file = None - - self.width = None - self.height = None - - def _call_cmd(self, cmd): - try: - cmd = map(lambda i: str(i), cmd) - call(cmd) - self.commands.append(" ".join(cmd)) - except Exception: - raise Exception("Unable to call cmd {}".format(str(cmd))) - - def err_warn(self, s): - sys.stderr.write("ERROR:{} - {}\n".format(self.__class__.__name__, s)) - raise PbProcessError - - def cleanup(self): - if not self._files_created: - return - map(lambda n: n.delete(), self._files_created) - - def err_fatal(self, s): - sys.stderr.write("ERROR[FATAL]:%s - %s\n" % ( - self.__class__.__name__, s)) - sys.exit(1) - - @classmethod - def example_run(cls, params=None, verbose=True): - example_params = params or cls.example_params - if not example_params: - raise AttributeError( - "Must supply test params to test %s" % cls.__name__ - ) - b = cls(**example_params) - b.create() - if verbose: - 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 - - def db_send( - self, - remote_addr=None, - db_connection=None - ): - try: - _insert_data = { - 'date': self.get_output_file().get_creation_time(), - 'remote_addr': remote_addr, - 'name': str(self.params.username), - 'url': self._db_url_param, - 'dir': self.get_output_file().get_hashdir(), - 'oldfile': None, - 'newfile': self.get_output_file().get_filename(), - 'dataobj': json.dumps(dict(self._input_kwargs)), - 'cmd': "; ".join(self.commands), - 'tag': self.tag, - } - ImCmd.create(**_insert_data) - except Exception as e: - self.err_warn("Problem sending to database:\n %s" % str(e)) - - def create(self): - pass - - def get_output_file(self): - return self._output_file - - def set_output_file(self, output_file): - self._output_file = output_file diff --git a/photoblaster/modules/pbbreaker.py b/photoblaster/modules/pbbreaker/__init__.py index bddd07a..7e6c92c 100755 --- a/photoblaster/modules/pbbreaker.py +++ b/photoblaster/modules/pbbreaker/__init__.py @@ -3,7 +3,7 @@ import os import random import re from photoblaster.config import BIN_CONVERT -from photoblaster.modules import Pb +from photoblaster.modules_src import ModuleBase from photoblaster._file import File DEFAULT_FINALFORMAT = "png" @@ -31,7 +31,7 @@ _BREAKTYPE_TRANSLATE = { } -class PbBreaker(Pb): +class PbBreaker(ModuleBase): example_params = { "url": "http://i.asdf.us/im/de/HolyMountain2_1322275112_seamonkey.gif", "breaktype": "RGB_WASH", @@ -211,3 +211,7 @@ class PbBreaker(Pb): self._add_false_data() self._final_conversion() super(PbBreaker, self).create() + + +def get_class(): + return PbBreaker diff --git a/photoblaster/modules/pbgenerate.py b/photoblaster/modules/pbgenerate/__init__.py index 1dd264b..360a3ea 100755 --- a/photoblaster/modules/pbgenerate.py +++ b/photoblaster/modules/pbgenerate/__init__.py @@ -1,7 +1,7 @@ #!/usr/bin/python2.7 from photoblaster.config import BIN_CONVERT, OUTPUT_IMAGE_TYPES,\ DEFAULT_FINALFORMAT -from photoblaster.modules import Pb +from photoblaster.modules_src import ModuleBase from photoblaster._file import File _GRAVITY_PARAMS = [ @@ -20,7 +20,7 @@ _DISPOSE_DEFAULT = "None" _HUE_DEFUALT = '100' -class PbGenerate(Pb): +class PbGenerate(ModuleBase): example_params = { """Example params. Used with the classmethod Pb.run_example""" 'nearest': 'true', @@ -198,3 +198,7 @@ class PbGenerate(Pb): if self.params.background: self._composite() super(PbGenerate, self).create() + + +def get_class(): + return PbGenerate diff --git a/photoblaster/modules/pbgradient.py b/photoblaster/modules/pbgradient/__init__.py index fe885b5..343c35a 100755 --- a/photoblaster/modules/pbgradient.py +++ b/photoblaster/modules/pbgradient/__init__.py @@ -3,7 +3,7 @@ from photoblaster.config import DEFAULT_WIDTH, DEFAULT_HEIGHT,\
DEFAULT_FINALFORMAT, \
BIN_CONVERT, BEVELBORDER, OUTPUT_IMAGE_TYPES
-from photoblaster.modules import Pb
+from photoblaster.modules_src import ModuleBase
from photoblaster._file import File
_DEFAULT_COLOR_1 = "white"
@@ -21,7 +21,7 @@ _halftone_values = { }
-class PbGradient(Pb):
+class PbGradient(ModuleBase):
example_params = {
"width": "200",
"color1": "#ffdead",
@@ -233,3 +233,7 @@ class PbGradient(Pb): def create(self):
self._build_cmd()
super(PbGradient, self).create()
+
+
+def get_class():
+ return PbGradient
diff --git a/photoblaster/modules/pbgrid.py b/photoblaster/modules/pbgrid/__init__.py index c43c4b1..bff0ad6 100755 --- a/photoblaster/modules/pbgrid.py +++ b/photoblaster/modules/pbgrid/__init__.py @@ -1,13 +1,13 @@ 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.modules_src import ModuleBase from photoblaster._file import File _DEFAULT_LINE_COLOR = "silver" -class PbGrid(Pb): +class PbGrid(ModuleBase): """ Creates or overlays a grid on an image, and adds 3D perspective """ @@ -232,3 +232,8 @@ class PbGrid(Pb): if self.params.trim: self._trim_cmd() super(PbGrid, self).create() + + +def get_class(): + return PbGrid + diff --git a/photoblaster/modules/pblandscape/__init__.py b/photoblaster/modules/pblandscape/__init__.py index 3044b3b..651f2e9 100755 --- a/photoblaster/modules/pblandscape/__init__.py +++ b/photoblaster/modules/pblandscape/__init__.py @@ -1,10 +1,10 @@ import base64 -from photoblaster.modules import Pb +from photoblaster.modules_src import ModuleBase import urlparse import re from photoblaster._file import File -class PbLandscape(Pb): +class PbLandscape(ModuleBase): try: example_params = { 'imgdata': open( diff --git a/photoblaster/modules/pbpattern.py b/photoblaster/modules/pbpattern/__init__.py index 1e80647..355b30f 100755 --- a/photoblaster/modules/pbpattern.py +++ b/photoblaster/modules/pbpattern/__init__.py @@ -1,12 +1,12 @@ from photoblaster.config import BIN_CONVERT, BIN_COMPOSITE -from photoblaster.modules import Pb +from photoblaster.modules_src import ModuleBase from PIL import Image from photoblaster._file import File _FUSE_MODE = "Pin_Light" -class PbPattern(Pb): +class PbPattern(ModuleBase): 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"}', #"username": "garfield", @@ -113,3 +113,7 @@ class PbPattern(Pb): self._make_mask() self._fuse_mask() super(PbPattern, self).create() + + +def get_class(): + return PbPattern diff --git a/pluginloader.py b/pluginloader.py new file mode 100644 index 0000000..1f568d4 --- /dev/null +++ b/pluginloader.py @@ -0,0 +1,19 @@ +import imp +import os + +PluginFolder = "./photoblaster/modules_src" +MainModule = "__init__" + +def getPlugins(): + plugins = [] + possibleplugins = os.listdir(PluginFolder) + for i in possibleplugins: + location = os.path.join(PluginFolder, i) + if not os.path.isdir(location) or not MainModule + ".py" in os.listdir(location): + continue + info = imp.find_module(MainModule, [location]) + plugins.append({"name": i, "info": info}) + return plugins + +def loadPlugin(plugin): + return imp.load_module(MainModule, *plugin["info"]) diff --git a/run_module_examples.py b/run_module_examples.py index 8a22c10..c6559c8 100644 --- a/run_module_examples.py +++ b/run_module_examples.py @@ -1,9 +1,13 @@ #!/usr/bin/python2.7 """calls the example_run method on all modules""" -from photoblaster.modules import Pb -for cls in Pb.__subclasses__(): - print cls.__name__ - if cls.__name__ == "PbGradient": +import pluginbase +from photoblaster.modules import Modules + + +modules = Modules() +for module_name in modules.list_modules(): + if module_name == 'pbgradient': + cls = modules.get_module(module_name) instance = cls.example_run() instance.get_output_file().s3move() print instance.get_output_file().as_dict() diff --git a/share/install/requirements.txt b/share/install/requirements.txt index 85daaed..4de82f8 100644 --- a/share/install/requirements.txt +++ b/share/install/requirements.txt @@ -2,3 +2,4 @@ Flask-SQLAlchemy Flask SQLAlchemy +pluginbase |
