From fd5016331351ef8a75e29422e595df66d7c5506c Mon Sep 17 00:00:00 2001 From: Bernardo Fontes Date: Sat, 7 Nov 2020 14:25:57 -0300 Subject: [PATCH 01/10] Unit test refactoring Signed-off-by: Bernardo Fontes --- pyp5js/tests/test_fs.py | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/pyp5js/tests/test_fs.py b/pyp5js/tests/test_fs.py index be2806fe..34896f91 100644 --- a/pyp5js/tests/test_fs.py +++ b/pyp5js/tests/test_fs.py @@ -58,18 +58,21 @@ def tearDown(self): if self.base_dir.exists(): shutil.rmtree(self.base_dir) + def get_expected_path(self, *args): + return self.base_dir.joinpath(self.sketch_name, *args) + def test_sketch_dirs(self): - assert self.base_dir.joinpath(self.sketch_name) == self.files.sketch_dir - assert self.base_dir.joinpath(self.sketch_name, 'static') == self.files.static_dir - assert self.base_dir.joinpath(self.sketch_name, 'target') == self.files.target_dir + assert self.get_expected_path() == self.files.sketch_dir + assert self.get_expected_path('static') == self.files.static_dir + assert self.get_expected_path('target') == self.files.target_dir assert self.files.TARGET_NAME == 'target' def test_sketch_files(self): self.files.check_sketch_dir = False - assert self.base_dir.joinpath(self.sketch_name, 'index.html') == self.files.index_html - assert self.base_dir.joinpath(self.sketch_name, 'static', 'p5.js') == self.files.p5js - assert self.base_dir.joinpath(self.sketch_name, 'foo.py') == self.files.sketch_py - assert self.base_dir.joinpath(self.sketch_name, 'target_sketch.py') == self.files.target_sketch + assert self.get_expected_path('index.html') == self.files.index_html + assert self.get_expected_path('static', 'p5.js') == self.files.p5js + assert self.get_expected_path('foo.py') == self.files.sketch_py + assert self.get_expected_path('target_sketch.py') == self.files.target_sketch def test_sketch_files_holds_reference_to_lib_files(self): lib_files = LibFiles() From 708fcfda1c4e0d414526ea9cf5b873c91ce70a2a Mon Sep 17 00:00:00 2001 From: Bernardo Fontes Date: Sat, 7 Nov 2020 15:17:11 -0300 Subject: [PATCH 02/10] Move config to its own module --- pyp5js/config.py | 5 +++++ pyp5js/config/__init__.py | 13 +++++++++++++ 2 files changed, 18 insertions(+) create mode 100644 pyp5js/config/__init__.py diff --git a/pyp5js/config.py b/pyp5js/config.py index 5d094262..56db5943 100644 --- a/pyp5js/config.py +++ b/pyp5js/config.py @@ -5,3 +5,8 @@ if not SKETCHBOOK_DIR.exists(): SKETCHBOOK_DIR.mkdir() + +TRANSCRYPT_INTERPRETER = 'transcrypt' +PYODIDE_INTERPRETER = 'pyodide' + +AVAILABLE_INTERPRETERS = [TRANSCRYPT_INTERPRETER, PYODIDE_INTERPRETER] diff --git a/pyp5js/config/__init__.py b/pyp5js/config/__init__.py new file mode 100644 index 00000000..cd6050a8 --- /dev/null +++ b/pyp5js/config/__init__.py @@ -0,0 +1,13 @@ +from . import sketch +from decouple import config +from pathlib import Path + +SKETCHBOOK_DIR = config("SKETCHBOOK_DIR", cast=Path, default=Path.home().joinpath('sketchbook-pyp5js')) + +if not SKETCHBOOK_DIR.exists(): + SKETCHBOOK_DIR.mkdir() + +TRANSCRYPT_INTERPRETER = 'transcrypt' +PYODIDE_INTERPRETER = 'pyodide' + +AVAILABLE_INTERPRETERS = [TRANSCRYPT_INTERPRETER, PYODIDE_INTERPRETER] From 7e076c859466b9db11cb3cc8769c51b3493c527f Mon Sep 17 00:00:00 2001 From: Bernardo Fontes Date: Sat, 7 Nov 2020 15:17:41 -0300 Subject: [PATCH 03/10] Use indirection to get sketch index html file --- pyp5js/config/sketch.py | 9 +++++++++ pyp5js/fs.py | 1 + pyp5js/templates_renderers.py | 3 ++- 3 files changed, 12 insertions(+), 1 deletion(-) create mode 100644 pyp5js/config/sketch.py diff --git a/pyp5js/config/sketch.py b/pyp5js/config/sketch.py new file mode 100644 index 00000000..6c08b492 --- /dev/null +++ b/pyp5js/config/sketch.py @@ -0,0 +1,9 @@ +class SketchConfig: + + def __init__(self, interpreter): + self.interpreter = interpreter + + def get_index_template(self): + from pyp5js.fs import LibFiles + pyp5js_files = LibFiles() + return pyp5js_files.index_html diff --git a/pyp5js/fs.py b/pyp5js/fs.py index de4e2754..16b5c3df 100644 --- a/pyp5js/fs.py +++ b/pyp5js/fs.py @@ -19,6 +19,7 @@ class SketchFiles(): def __init__(self, sketch_name): self.sketch_name = sketch_name self.from_lib = LibFiles() + self.config = config.sketch.SketchConfig(config.TRANSCRYPT_INTERPRETER) def validate_name(self): does_not_start_with_letter_or_underscore = r'^[^a-zA-Z_]' diff --git a/pyp5js/templates_renderers.py b/pyp5js/templates_renderers.py index 1964122e..220d8bf6 100644 --- a/pyp5js/templates_renderers.py +++ b/pyp5js/templates_renderers.py @@ -15,7 +15,8 @@ def get_sketch_index_content(sketch_files): "p5_js_url": sketch_files.urls.p5_js_url, "sketch_js_url": sketch_files.urls.sketch_js_url, } - index_template = templates.get_template(pyp5js_files.index_html.name) + template_file = sketch_files.config.get_index_template() + index_template = templates.get_template(template_file.name) return index_template.render(context) From b580d4137f9dcafc095fa95f8721fd306d58125c Mon Sep 17 00:00:00 2001 From: Bernardo Fontes Date: Sat, 7 Nov 2020 15:19:27 -0300 Subject: [PATCH 04/10] Rename test directory --- pyp5js/tests/{http => test_http}/assets/alien.png | Bin pyp5js/tests/{http => test_http}/test_web_app.py | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename pyp5js/tests/{http => test_http}/assets/alien.png (100%) rename pyp5js/tests/{http => test_http}/test_web_app.py (100%) diff --git a/pyp5js/tests/http/assets/alien.png b/pyp5js/tests/test_http/assets/alien.png similarity index 100% rename from pyp5js/tests/http/assets/alien.png rename to pyp5js/tests/test_http/assets/alien.png diff --git a/pyp5js/tests/http/test_web_app.py b/pyp5js/tests/test_http/test_web_app.py similarity index 100% rename from pyp5js/tests/http/test_web_app.py rename to pyp5js/tests/test_http/test_web_app.py From 8c0871a02e7687756b6531ef29103c2d9de4a8a3 Mon Sep 17 00:00:00 2001 From: Bernardo Fontes Date: Sat, 7 Nov 2020 16:05:13 -0300 Subject: [PATCH 05/10] Write/read sketch config as json file --- pyp5js/config/sketch.py | 14 ++++++++ pyp5js/tests/test_config/test_sketch.py | 48 +++++++++++++++++++++++++ 2 files changed, 62 insertions(+) create mode 100644 pyp5js/tests/test_config/test_sketch.py diff --git a/pyp5js/config/sketch.py b/pyp5js/config/sketch.py index 6c08b492..8507b0a7 100644 --- a/pyp5js/config/sketch.py +++ b/pyp5js/config/sketch.py @@ -1,5 +1,14 @@ +import json + + class SketchConfig: + @classmethod + def from_json(cls, json_file_path): + with open(json_file_path) as fd: + config_data = json.load(fd) + return cls(**config_data) + def __init__(self, interpreter): self.interpreter = interpreter @@ -7,3 +16,8 @@ def get_index_template(self): from pyp5js.fs import LibFiles pyp5js_files = LibFiles() return pyp5js_files.index_html + + def write(self, fname): + with open(fname, "w") as fd: + data = {"interpreter": self.interpreter} + json.dump(data, fd) diff --git a/pyp5js/tests/test_config/test_sketch.py b/pyp5js/tests/test_config/test_sketch.py new file mode 100644 index 00000000..59ce7f65 --- /dev/null +++ b/pyp5js/tests/test_config/test_sketch.py @@ -0,0 +1,48 @@ +import json +import os +from pytest import fixture +from pathlib import Path +from tempfile import NamedTemporaryFile + +from pyp5js.config import TRANSCRYPT_INTERPRETER, PYODIDE_INTERPRETER +from pyp5js.config.sketch import SketchConfig + + +@fixture +def transcrypt_json_file(): + with NamedTemporaryFile(mode='w') as fd: + data = {"interpreter": "transcrypt"} + json.dump(data, fd) + fd.seek(0) + yield fd + + +@fixture +def pyodide_json_file(): + with NamedTemporaryFile(mode='w') as fd: + data = {"interpreter": "pyodide"} + json.dump(data, fd) + fd.seek(0) + yield fd + + +def test_init_transcrypt_sketch_config_from_json(transcrypt_json_file): + config = SketchConfig.from_json(transcrypt_json_file.name) + assert config.interpreter == TRANSCRYPT_INTERPRETER + + +def test_init_pyodide_sketch_config_from_json(pyodide_json_file): + config = SketchConfig.from_json(pyodide_json_file.name) + assert config.interpreter == PYODIDE_INTERPRETER + + +def test_write_sketch_interpreter_config(): + config = SketchConfig(interpreter=TRANSCRYPT_INTERPRETER) + fd = NamedTemporaryFile(mode="w", delete=False) + config.write(fd.name) + fd.close() + with open(fd.name) as fd: + data = json.load(fd) + + assert data["interpreter"] == TRANSCRYPT_INTERPRETER + os.remove(fd.name) From cc8b5561e2535c269dc63491e9c1cbaf59f4bb85 Mon Sep 17 00:00:00 2001 From: Bernardo Fontes Date: Sat, 7 Nov 2020 17:02:09 -0300 Subject: [PATCH 06/10] Create index html exclusive for pyodide --- pyp5js/fs.py | 8 +++++-- pyp5js/templates/pyodide_index.html | 22 +++++++++++++++++++ .../{index.html => transcrypt_index.html} | 0 pyp5js/tests/test_fs.py | 7 ++++-- pyp5js/tests/test_templates_renderers.py | 2 +- 5 files changed, 34 insertions(+), 5 deletions(-) create mode 100644 pyp5js/templates/pyodide_index.html rename pyp5js/templates/{index.html => transcrypt_index.html} (100%) diff --git a/pyp5js/fs.py b/pyp5js/fs.py index 16b5c3df..947568b5 100644 --- a/pyp5js/fs.py +++ b/pyp5js/fs.py @@ -118,8 +118,12 @@ def target_sketch_template(self): return self.templates_dir.joinpath('target_sketch.py.template') @property - def index_html(self): - return self.templates_dir.joinpath('index.html') + def transcrypt_index_html(self): + return self.templates_dir.joinpath('transcrypt_index.html') + + @property + def pyodide_index_html(self): + return self.templates_dir.joinpath('pyodide_index.html') @property def p5js(self): diff --git a/pyp5js/templates/pyodide_index.html b/pyp5js/templates/pyodide_index.html new file mode 100644 index 00000000..ca9b2829 --- /dev/null +++ b/pyp5js/templates/pyodide_index.html @@ -0,0 +1,22 @@ + + + + + + + + + {{ sketch_name }} - pyp5js (using pyodide) + + + + + + + + +
+ +
+ + diff --git a/pyp5js/templates/index.html b/pyp5js/templates/transcrypt_index.html similarity index 100% rename from pyp5js/templates/index.html rename to pyp5js/templates/transcrypt_index.html diff --git a/pyp5js/tests/test_fs.py b/pyp5js/tests/test_fs.py index 34896f91..493eb79b 100644 --- a/pyp5js/tests/test_fs.py +++ b/pyp5js/tests/test_fs.py @@ -37,8 +37,11 @@ def test_files_properties(lib_files): assert lib_files.target_sketch_template == pyp5_dir.joinpath('templates', 'target_sketch.py.template') assert lib_files.target_sketch_template.exists() - assert lib_files.index_html == pyp5_dir.joinpath('templates', 'index.html') - assert lib_files.index_html.exists() + assert lib_files.transcrypt_index_html == pyp5_dir.joinpath('templates', 'transcrypt_index.html') + assert lib_files.transcrypt_index_html.exists() + + assert lib_files.pyodide_index_html == pyp5_dir.joinpath('templates', 'pyodide_index.html') + assert lib_files.pyodide_index_html.exists() assert lib_files.p5js == pyp5_dir.joinpath('static', 'p5', 'p5.min.js') assert lib_files.p5js.exists() diff --git a/pyp5js/tests/test_templates_renderers.py b/pyp5js/tests/test_templates_renderers.py index 5dccdc26..80cbe29c 100644 --- a/pyp5js/tests/test_templates_renderers.py +++ b/pyp5js/tests/test_templates_renderers.py @@ -5,7 +5,7 @@ def test_get_sketch_index_content(): sketch_files = SketchFiles('foo') - expected_template = renderers.templates.get_template(sketch_files.from_lib.index_html.name) + expected_template = renderers.templates.get_template(sketch_files.from_lib.transcrypt_index_html.name) expected_content = expected_template.render({ 'sketch_name': sketch_files.sketch_name, "p5_js_url": sketch_files.STATIC_NAME + "/p5.js", From 369e5197ac2d2a77f8f40e8981e8569dbb3c7d39 Mon Sep 17 00:00:00 2001 From: Bernardo Fontes Date: Sat, 7 Nov 2020 17:03:35 -0300 Subject: [PATCH 07/10] Get index html from sketch config file --- pyp5js/config.py | 12 ------------ pyp5js/config/__init__.py | 4 +--- pyp5js/config/sketch.py | 8 +++++++- pyp5js/tests/test_config/test_sketch.py | 25 +++++++++++++++++++++++-- 4 files changed, 31 insertions(+), 18 deletions(-) delete mode 100644 pyp5js/config.py diff --git a/pyp5js/config.py b/pyp5js/config.py deleted file mode 100644 index 56db5943..00000000 --- a/pyp5js/config.py +++ /dev/null @@ -1,12 +0,0 @@ -from decouple import config -from pathlib import Path - -SKETCHBOOK_DIR = config("SKETCHBOOK_DIR", cast=Path, default=Path.home().joinpath('sketchbook-pyp5js')) - -if not SKETCHBOOK_DIR.exists(): - SKETCHBOOK_DIR.mkdir() - -TRANSCRYPT_INTERPRETER = 'transcrypt' -PYODIDE_INTERPRETER = 'pyodide' - -AVAILABLE_INTERPRETERS = [TRANSCRYPT_INTERPRETER, PYODIDE_INTERPRETER] diff --git a/pyp5js/config/__init__.py b/pyp5js/config/__init__.py index cd6050a8..c942fe6a 100644 --- a/pyp5js/config/__init__.py +++ b/pyp5js/config/__init__.py @@ -1,4 +1,5 @@ from . import sketch +from .sketch import TRANSCRYPT_INTERPRETER, PYODIDE_INTERPRETER from decouple import config from pathlib import Path @@ -7,7 +8,4 @@ if not SKETCHBOOK_DIR.exists(): SKETCHBOOK_DIR.mkdir() -TRANSCRYPT_INTERPRETER = 'transcrypt' -PYODIDE_INTERPRETER = 'pyodide' - AVAILABLE_INTERPRETERS = [TRANSCRYPT_INTERPRETER, PYODIDE_INTERPRETER] diff --git a/pyp5js/config/sketch.py b/pyp5js/config/sketch.py index 8507b0a7..7aa4f599 100644 --- a/pyp5js/config/sketch.py +++ b/pyp5js/config/sketch.py @@ -1,5 +1,7 @@ import json +TRANSCRYPT_INTERPRETER = 'transcrypt' +PYODIDE_INTERPRETER = 'pyodide' class SketchConfig: @@ -15,7 +17,11 @@ def __init__(self, interpreter): def get_index_template(self): from pyp5js.fs import LibFiles pyp5js_files = LibFiles() - return pyp5js_files.index_html + index_map = { + TRANSCRYPT_INTERPRETER: pyp5js_files.transcrypt_index_html, + PYODIDE_INTERPRETER: pyp5js_files.pyodide_index_html, + } + return index_map[self.interpreter] def write(self, fname): with open(fname, "w") as fd: diff --git a/pyp5js/tests/test_config/test_sketch.py b/pyp5js/tests/test_config/test_sketch.py index 59ce7f65..ef0e7e3e 100644 --- a/pyp5js/tests/test_config/test_sketch.py +++ b/pyp5js/tests/test_config/test_sketch.py @@ -4,6 +4,7 @@ from pathlib import Path from tempfile import NamedTemporaryFile +from pyp5js.fs import LibFiles from pyp5js.config import TRANSCRYPT_INTERPRETER, PYODIDE_INTERPRETER from pyp5js.config.sketch import SketchConfig @@ -25,6 +26,14 @@ def pyodide_json_file(): fd.seek(0) yield fd +@fixture +def transcrypt_config(): + return SketchConfig(interpreter=TRANSCRYPT_INTERPRETER) + +@fixture +def pyodide_config(): + return SketchConfig(interpreter=PYODIDE_INTERPRETER) + def test_init_transcrypt_sketch_config_from_json(transcrypt_json_file): config = SketchConfig.from_json(transcrypt_json_file.name) @@ -36,8 +45,8 @@ def test_init_pyodide_sketch_config_from_json(pyodide_json_file): assert config.interpreter == PYODIDE_INTERPRETER -def test_write_sketch_interpreter_config(): - config = SketchConfig(interpreter=TRANSCRYPT_INTERPRETER) +def test_write_sketch_interpreter_config(transcrypt_config): + config = transcrypt_config fd = NamedTemporaryFile(mode="w", delete=False) config.write(fd.name) fd.close() @@ -46,3 +55,15 @@ def test_write_sketch_interpreter_config(): assert data["interpreter"] == TRANSCRYPT_INTERPRETER os.remove(fd.name) + +def test_get_transcrypt_index_template(transcrypt_config): + template = transcrypt_config.get_index_template() + pyp5js_files = LibFiles() + assert pyp5js_files.transcrypt_index_html == template + assert template.exists() + +def test_get_pyodide_index_template(pyodide_config): + template = pyodide_config.get_index_template() + pyp5js_files = LibFiles() + assert pyp5js_files.pyodide_index_html == template + assert template.exists() From 01bbeeb04138467a2ba229b9c803ac9a8eb2edd3 Mon Sep 17 00:00:00 2001 From: Bernardo Fontes Date: Sat, 7 Nov 2020 17:04:06 -0300 Subject: [PATCH 08/10] Sketch files with sketch config --- pyp5js/fs.py | 16 ++++++++++++++-- pyp5js/tests/test_fs.py | 14 +++++++++++++- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/pyp5js/fs.py b/pyp5js/fs.py index 947568b5..634ac923 100644 --- a/pyp5js/fs.py +++ b/pyp5js/fs.py @@ -6,6 +6,7 @@ from collections import namedtuple from pyp5js import config +from pyp5js.config.sketch import SketchConfig from pyp5js.exceptions import SketchDirAlreadyExistException, InvalidName @@ -13,13 +14,19 @@ class SketchFiles(): + # TODO now that we have the SketchConfig object, this class name is not good enough + # A better name would be Sketch TARGET_NAME = 'target' STATIC_NAME = 'static' - def __init__(self, sketch_name): + def __init__(self, sketch_name, interpreter=config.TRANSCRYPT_INTERPRETER, **cfg): self.sketch_name = sketch_name self.from_lib = LibFiles() - self.config = config.sketch.SketchConfig(config.TRANSCRYPT_INTERPRETER) + if self.config_file.exists(): + # TODO add warning to let the user know pyp5js is ignoring cfg + self.config = SketchConfig.from_json(self.config_file) + else: + self.config = SketchConfig(interpreter=interpreter, **cfg) def validate_name(self): does_not_start_with_letter_or_underscore = r'^[^a-zA-Z_]' @@ -37,6 +44,7 @@ def create_sketch_dir(self): os.makedirs(self.sketch_dir) self.static_dir.mkdir() self.target_dir.mkdir() + self.config.write(self.config_file) @property def sketch_exists(self): @@ -73,6 +81,10 @@ def target_sketch(self): def sketch_py(self): return self.sketch_dir.joinpath(f'{self.sketch_name}.py') + @property + def config_file(self): + return self.sketch_dir.joinpath('properties.json') + @property def target_dir(self): return self.sketch_dir.joinpath(self.TARGET_NAME) diff --git a/pyp5js/tests/test_fs.py b/pyp5js/tests/test_fs.py index 493eb79b..c9e7826b 100644 --- a/pyp5js/tests/test_fs.py +++ b/pyp5js/tests/test_fs.py @@ -3,7 +3,7 @@ from pathlib import Path from unittest import TestCase -from pyp5js.config import SKETCHBOOK_DIR +from pyp5js.config import SKETCHBOOK_DIR, PYODIDE_INTERPRETER from pyp5js.exceptions import SketchDirAlreadyExistException from pyp5js.fs import LibFiles, SketchFiles from pyp5js.exceptions import InvalidName @@ -76,6 +76,7 @@ def test_sketch_files(self): assert self.get_expected_path('static', 'p5.js') == self.files.p5js assert self.get_expected_path('foo.py') == self.files.sketch_py assert self.get_expected_path('target_sketch.py') == self.files.target_sketch + assert self.get_expected_path('properties.json') == self.files.config_file def test_sketch_files_holds_reference_to_lib_files(self): lib_files = LibFiles() @@ -86,12 +87,14 @@ def test_create_dirs(self): assert self.files.sketch_dir.exists() is False assert self.files.static_dir.exists() is False assert self.files.target_dir.exists() is False + assert self.files.config_file.exists() is False self.files.create_sketch_dir() assert self.files.sketch_dir.exists() is True assert self.files.static_dir.exists() is True assert self.files.target_dir.exists() is True + assert self.files.config_file.exists() is True with pytest.raises(SketchDirAlreadyExistException): self.files.create_sketch_dir() @@ -118,3 +121,12 @@ def test_name_should_accept_underscore_in_the_beginning(self): def test_name_should_accept_underscore_in_the_middle(self): file = SketchFiles('na_me') assert file.sketch_name == 'na_me' + + def test_loads_config_from_config_file(self): + files = SketchFiles('bar', interpreter=PYODIDE_INTERPRETER) + files.create_sketch_dir() # writes config file json + + same_files = SketchFiles('bar') + + assert same_files.config_file == files.config_file + assert same_files.config.interpreter == PYODIDE_INTERPRETER From 66907c73a34d271b27e986fa89b71936fd6dccc2 Mon Sep 17 00:00:00 2001 From: Bernardo Fontes Date: Sat, 7 Nov 2020 17:12:23 -0300 Subject: [PATCH 09/10] Add new flag --interpreter to command line --- pyp5js/cli.py | 11 ++++++----- pyp5js/commands.py | 6 ++++-- pyp5js/tests/test_commands.py | 14 +++++++++++++- 3 files changed, 23 insertions(+), 8 deletions(-) diff --git a/pyp5js/cli.py b/pyp5js/cli.py index 7b5866f1..115ffb78 100755 --- a/pyp5js/cli.py +++ b/pyp5js/cli.py @@ -5,7 +5,7 @@ import click from pyp5js import commands -from pyp5js.config import SKETCHBOOK_DIR +from pyp5js.config import SKETCHBOOK_DIR, AVAILABLE_INTERPRETERS, PYODIDE_INTERPRETER, TRANSCRYPT_INTERPRETER @click.group() @@ -19,7 +19,8 @@ def command_line_entrypoint(): @command_line_entrypoint.command('new') @click.argument('sketch_name') @click.option('--monitor', '-m', is_flag=True) -def configure_new_sketch(sketch_name, monitor): +@click.option('--interpreter', '-i', type=click.Choice(AVAILABLE_INTERPRETERS), default=TRANSCRYPT_INTERPRETER) +def configure_new_sketch(sketch_name, monitor, interpreter): """ Create dir and configure boilerplate @@ -32,13 +33,14 @@ def configure_new_sketch(sketch_name, monitor): Example: $ pyp5js new my_sketch """ - files = commands.new_sketch(sketch_name) + files = commands.new_sketch(sketch_name, interpreter) cprint.ok(f"Your sketch was created!") + compiler = "transcrypt" if interpreter == TRANSCRYPT_INTERPRETER else "pyodide" if not monitor: cprint.ok(f"Please, open and edit the file {files.sketch_py} to draw. When you're ready to see your results, just run:") - cmd = f"\t pyp5js transcrypt {sketch_name}" + cmd = f"\t pyp5js {compiler} {sketch_name}" cprint.ok(cmd) cprint.ok(f"And open file://{files.index_html.absolute()} on your browser to see yor results!") else: @@ -47,7 +49,6 @@ def configure_new_sketch(sketch_name, monitor): commands.monitor_sketch(sketch_name) - @command_line_entrypoint.command("transcrypt") @click.argument("sketch_name") def transcrypt_sketch(sketch_name): diff --git a/pyp5js/commands.py b/pyp5js/commands.py index 6d01b888..f51132ec 100644 --- a/pyp5js/commands.py +++ b/pyp5js/commands.py @@ -10,9 +10,11 @@ from pyp5js.http import pyp5js_web_app from pyp5js.monitor import monitor_sketch as monitor_sketch_service from pyp5js.templates_renderers import get_sketch_index_content +from pyp5js.config import TRANSCRYPT_INTERPRETER -def new_sketch(sketch_name): +# TODO precisa aceitar um parĂ¢metro de escolha de compilador +def new_sketch(sketch_name, interpreter=TRANSCRYPT_INTERPRETER): """ Creates a new sketch with the required assets and a index.html file, based on pyp5js's templates @@ -21,7 +23,7 @@ def new_sketch(sketch_name): :return: file names :rtype: list of strings """ - sketch_files = SketchFiles(sketch_name) + sketch_files = SketchFiles(sketch_name, interpreter=interpreter) sketch_files.create_sketch_dir() templates_files = [ diff --git a/pyp5js/tests/test_commands.py b/pyp5js/tests/test_commands.py index d278b561..a5dacffb 100644 --- a/pyp5js/tests/test_commands.py +++ b/pyp5js/tests/test_commands.py @@ -4,7 +4,7 @@ from unittest.mock import Mock, patch from pyp5js import commands -from pyp5js.config import SKETCHBOOK_DIR +from pyp5js.config import SKETCHBOOK_DIR, TRANSCRYPT_INTERPRETER, PYODIDE_INTERPRETER from pyp5js.exceptions import PythonSketchDoesNotExist, SketchDirAlreadyExistException, InvalidName from pyp5js.fs import SketchFiles @@ -78,6 +78,18 @@ def test_create_new_sketch_with_all_required_files(self): assert self.sketch_files.index_html.exists() assert self.sketch_files.sketch_py.exists() assert self.sketch_files.p5js.exists() + assert self.sketch_files.config_file.exists() + assert self.sketch_files.config.interpreter == TRANSCRYPT_INTERPRETER + + def test_create_pyodide_sketch(self): + commands.new_sketch(self.sketch_name, interpreter=PYODIDE_INTERPRETER) + self.sketch_files = SketchFiles(self.sketch_name) # read config after init + + assert self.sketch_files.index_html.exists() + assert self.sketch_files.sketch_py.exists() + assert self.sketch_files.p5js.exists() + assert self.sketch_files.config_file.exists() + assert self.sketch_files.config.interpreter == PYODIDE_INTERPRETER def test_raise_exception_if_dir_already_exist(self): self.sketch_files.create_sketch_dir() From eeee187d7d0ae4d26de3ab7f22416f4afc263545 Mon Sep 17 00:00:00 2001 From: Flavio Amieiro Date: Sat, 7 Nov 2020 21:39:59 +0100 Subject: [PATCH 10/10] Fixes SketchConfig tests for Windows (Hopefully) On Windows, a NamedTemporaryFile [cannot be opened][1] again if it's already open. Now the fixture creates the file, closes it, and removes the file after the tests run. [1]: https://docs.python.org/2/library/tempfile.html#tempfile.NamedTemporaryFile --- pyp5js/tests/test_config/test_sketch.py | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/pyp5js/tests/test_config/test_sketch.py b/pyp5js/tests/test_config/test_sketch.py index ef0e7e3e..806ca965 100644 --- a/pyp5js/tests/test_config/test_sketch.py +++ b/pyp5js/tests/test_config/test_sketch.py @@ -11,20 +11,29 @@ @fixture def transcrypt_json_file(): - with NamedTemporaryFile(mode='w') as fd: + try: + fd = NamedTemporaryFile(mode='w', delete=False) data = {"interpreter": "transcrypt"} json.dump(data, fd) + filename = fd.name fd.seek(0) - yield fd - + fd.close() + yield filename + finally: + os.remove(filename) @fixture def pyodide_json_file(): - with NamedTemporaryFile(mode='w') as fd: + try: + fd = NamedTemporaryFile(mode='w', delete=False) data = {"interpreter": "pyodide"} json.dump(data, fd) + filename = fd.name fd.seek(0) - yield fd + fd.close() + yield filename + finally: + os.remove(filename) @fixture def transcrypt_config(): @@ -36,12 +45,12 @@ def pyodide_config(): def test_init_transcrypt_sketch_config_from_json(transcrypt_json_file): - config = SketchConfig.from_json(transcrypt_json_file.name) + config = SketchConfig.from_json(transcrypt_json_file) assert config.interpreter == TRANSCRYPT_INTERPRETER def test_init_pyodide_sketch_config_from_json(pyodide_json_file): - config = SketchConfig.from_json(pyodide_json_file.name) + config = SketchConfig.from_json(pyodide_json_file) assert config.interpreter == PYODIDE_INTERPRETER