def test_draw_image(self): buf = io.BytesIO() generate_image('svg', (75, 50), 2, ('red', 'green'), buf) self.assertEqual( buf.getvalue(), b'<?xml version="1.0" encoding="UTF-8"?>\n<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"\n width="75px" height="50px" viewBox="0 0 75 50" version="1.1">\n<g id="surface1">\n<rect x="0" y="0" width="38" height="33" style="fill:rgb(255,0,0)" />\n<rect x="0" y="33" width="38" height="17" style="fill:rgb(0,128,0)" />\n<rect x="38" y="0" width="37" height="50" style="fill:rgb(255,0,0)" />\n<line x1="38" y1="0" x2="38" y2="50" style="stroke:rgb(20,30,40);stroke-width:2" />\n<line x1="0" y1="33" x2="38" y2="33" style="stroke:rgb(20,30,40);stroke-width:2" />\n</g>\n</svg>\n' )
def handle(self, *args, **options): if not options["appsource"]: # Show valid appsources. print("Specify one of the following app sources plus an app name:") for appsrc in AppSource.objects.all(): if appsrc.spec["type"] == "local" and appsrc.spec.get("path"): print(appsrc.slug, "(path: " + appsrc.spec["path"] + ")") print("") print( "Or create a new AppSource using '--path path/to/apps appsource'." ) elif options["path"]: if options["appname"]: print("You cannot use --path together with an appname.") return # Make directory if it doesn't exist. os.makedirs(options["path"], exist_ok=True) # Create a new AppSource. appsrc = AppSource.objects.create(slug=options["appsource"], spec={ "type": "local", "path": options["path"] }) print("Created new AppSource", appsrc, "using local path", options["path"]) elif not options["appname"]: print( "You must specify an app name, which should be a valid directory name (i.e. no spaces), or or --path to create a new AppSource." ) else: # Ok do the real work. appsrc = AppSource.objects.get(slug=options["appsource"]) # Do we have a path? if not appsrc.spec.get("path"): print("AppSource does not have a local path specified!") return # What's the path to the app? path = os.path.join(appsrc.spec["path"], options["appname"]) # Does this app already exist? if os.path.exists(path): print("An app with that name already exists.") return # Copy stub files. guidedmodules_path = os.path.dirname( os.path.dirname(os.path.dirname(__file__))) shutil.copytree(os.path.join(guidedmodules_path, "stub_app"), path, copy_function=shutil.copy) # Edit the app title. with rtyaml.edit(os.path.join(path, "app.yaml")) as app: app['title'] = options["appname"] # Create a unique icon for the app and delete the existing app icon # svg file that we know is in the stub. from mondrianish import generate_image colors = ("#FFF8F0", "#FCAA67", "#7DB7C0", "#932b25", "#498B57") with open(os.path.join(path, "assets", "app.png"), "wb") as f: generate_image("png", (128, 128), 3, colors, f) os.unlink(os.path.join(path, "assets", "app.svg")) # Which AppSource is used? print("Created new app in AppSource", appsrc, "at", path)