# Path: modsdk/webserver.py
# def make_application(port=PORT, output_log=False):
#     application = web.Application([
#             (r"/bundles", BundleList),
#             (r"/effects", EffectList),
#             (r"/effect/get/", EffectGet),
#             (r"/effect/image/(screenshot|thumbnail).png", EffectImage),
#             (r"/effect/file/(.*)", EffectFile),
#             (r"/effect/save", EffectSave),
#             (r"/config/get", ConfigurationGet),
#             (r"/config/set", ConfigurationSet),
#             (r"/(icon.html)?", Index),
#             (r"/screenshot", Screenshot),
#             (r"/post/(.+)/?", BundlePost),
#             (r"/js/templates.js$", BulkTemplateLoader),
#             (r"/resources/(.*)", EffectResource),
#             (r"/monitor", BundleMonitorWebsocket),
#             (r"/(.*)", web.StaticFileHandler, {"path": HTML_DIR}),
#             ], debug=output_log)
# 
#     application.listen(port, address="0.0.0.0")
#     if output_log:
#         options.parse_command_line()
# 
#     return ioloop.IOLoop.instance()
# 
# def lv2_init():
#     global _W
#     _W = lilv.World()
#     _W.load_all()
#     lv2_init2()
# 
# def lv2_cleanup():
#     global _W
#     _W = None
#     lv2_cleanup2()
# 
# def get_bundle_plugins(bundle):
#     return [get_plugin_info(p['uri']) for p in get_bundle_plugins2(bundle)]
# 
# def get_plugin_info(uri):
#     global _W
#     ps = _W.get_all_plugins()
#     pn = _W.new_uri(uri)
#     pl = ps.get_by_uri(pn)
#     return get_plugin_info2(_W, pl, True)


import os, glob, subprocess, random, argparse
from PIL import Image
from modsdk.settings import (ROOT, PHANTOM_BINARY, SCREENSHOT_SCRIPT,
                             MAX_THUMB_WIDTH, MAX_THUMB_HEIGHT)
from modsdk.webserver import (make_application,
                              lv2_init, lv2_cleanup,
                              get_bundle_plugins,
                              get_plugin_info)
            return self.next_bundle()
        self.current_effect = self.effect_queue.pop(0)
        try:
            self.current_effect['gui']['screenshot']
            self.current_effect['gui']['thumbnail']
        except (TypeError, KeyError):
            return self.next_effect()

        fname = self.tmp_filename()
        proc = subprocess.Popen([ PHANTOM_BINARY,
                                  SCREENSHOT_SCRIPT,
                                  'http://localhost:%d/icon.html#%s' % (PORT, self.current_effect['uri']),
                                  fname,
                                  str(WIDTH),
                                  str(HEIGHT),
                                ],
                                stdout=subprocess.PIPE)

        def proc_callback(fileno, event):
            if proc.poll() is None:
                return
            self.webserver.remove_handler(fileno)
            fh = open(fname, 'rb')
            os.remove(fname)
            self.handle_image(fh)

        self.webserver.add_handler(proc.stdout.fileno(), proc_callback, 16)

    def handle_image(self, fh):
        img = Image.open(fh)

# Path: modsdk/webserver.py
# def make_application(port=PORT, output_log=False):
#     application = web.Application([
#             (r"/bundles", BundleList),
#             (r"/effects", EffectList),
#             (r"/effect/get/", EffectGet),
#             (r"/effect/image/(screenshot|thumbnail).png", EffectImage),
#             (r"/effect/file/(.*)", EffectFile),
#             (r"/effect/save", EffectSave),
#             (r"/config/get", ConfigurationGet),
#             (r"/config/set", ConfigurationSet),
#             (r"/(icon.html)?", Index),
#             (r"/screenshot", Screenshot),
#             (r"/post/(.+)/?", BundlePost),
#             (r"/js/templates.js$", BulkTemplateLoader),
#             (r"/resources/(.*)", EffectResource),
#             (r"/monitor", BundleMonitorWebsocket),
#             (r"/(.*)", web.StaticFileHandler, {"path": HTML_DIR}),
#             ], debug=output_log)
# 
#     application.listen(port, address="0.0.0.0")
#     if output_log:
#         options.parse_command_line()
# 
#     return ioloop.IOLoop.instance()
# 
# def lv2_init():
#     global _W
#     _W = lilv.World()
#     _W.load_all()
#     lv2_init2()
# 
# def lv2_cleanup():
#     global _W
#     _W = None
#     lv2_cleanup2()
# 
# def get_bundle_plugins(bundle):
#     return [get_plugin_info(p['uri']) for p in get_bundle_plugins2(bundle)]
# 
# def get_plugin_info(uri):
#     global _W
#     ps = _W.get_all_plugins()
#     pn = _W.new_uri(uri)
#     pl = ps.get_by_uri(pn)
#     return get_plugin_info2(_W, pl, True)


# Path: modsdk/screenshot.py
import os, glob, subprocess, random, argparse
from PIL import Image
from modsdk.settings import (ROOT, PHANTOM_BINARY, SCREENSHOT_SCRIPT,
                             MAX_THUMB_WIDTH, MAX_THUMB_HEIGHT)
from modsdk.webserver import (make_application,
                              lv2_init, lv2_cleanup,
                              get_bundle_plugins,
                              get_plugin_info)

            return self.next_bundle()
        self.current_effect = self.effect_queue.pop(0)
        try:
            self.current_effect['gui']['screenshot']
            self.current_effect['gui']['thumbnail']
        except (TypeError, KeyError):
            return self.next_effect()

        fname = self.tmp_filename()
        proc = subprocess.Popen([ PHANTOM_BINARY,
                                  SCREENSHOT_SCRIPT,
                                  'http://localhost:%d/icon.html#%s' % (PORT, self.current_effect['uri']),
                                  fname,
                                  str(WIDTH),
                                  str(HEIGHT),
                                ],
                                stdout=subprocess.PIPE)

        def proc_callback(fileno, event):
            if proc.poll() is None:
                return
            self.webserver.remove_handler(fileno)
            fh = open(fname, 'rb')
            os.remove(fname)
            self.handle_image(fh)

        self.webserver.add_handler(proc.stdout.fileno(), proc_callback, 16)

    def handle_image(self, fh):
        img = Image.open(fh)

        img = self.crop(img)