# 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)
            width = width * MAX_THUMB_HEIGHT / height
        img.convert('RGB')
        img.thumbnail((width, height), Image.ANTIALIAS)
        img.save(self.current_effect['gui']['thumbnail'])
        self.next_effect()

    def crop(self, img):
        # first find latest non-transparent pixel in both width and height
        min_x = WIDTH
        min_y = HEIGHT
        max_x = 0
        max_y = 0
        for i, px in enumerate(img.getdata()):
            if px[3] > 0:
                width = i % img.size[0]
                height = i / img.size[0]
                min_x = min(min_x, width)
                min_y = min(min_y, height)
                max_x = max(max_x, width)
                max_y = max(max_y, height)
        # now crop
        return img.crop([int(i) for i in (min_x, min_y, max_x, max_y)])

def run():
    parser = argparse.ArgumentParser(description='Generates screenshot of all effects inside a bundle')
    parser.add_argument('bundles', help="The bundle path (a directory containing manifest.ttl file", type=str, nargs='+')

    args = parser.parse_args()
    bundles = []


# 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)

            width = width * MAX_THUMB_HEIGHT / height
        img.convert('RGB')
        img.thumbnail((width, height), Image.ANTIALIAS)
        img.save(self.current_effect['gui']['thumbnail'])
        self.next_effect()

    def crop(self, img):
        # first find latest non-transparent pixel in both width and height
        min_x = WIDTH
        min_y = HEIGHT
        max_x = 0
        max_y = 0
        for i, px in enumerate(img.getdata()):
            if px[3] > 0:
                width = i % img.size[0]
                height = i / img.size[0]
                min_x = min(min_x, width)
                min_y = min(min_y, height)
                max_x = max(max_x, width)
                max_y = max(max_y, height)
        # now crop
        return img.crop([int(i) for i in (min_x, min_y, max_x, max_y)])

def run():
    parser = argparse.ArgumentParser(description='Generates screenshot of all effects inside a bundle')
    parser.add_argument('bundles', help="The bundle path (a directory containing manifest.ttl file", type=str, nargs='+')

    args = parser.parse_args()
    bundles = []


    for bundle in args.bundles: