Esempio n. 1
0
def test_compile_sketch(sketch):
    sketch.sketch_py.touch()
    with patch('pyp5js.commands.compile_sketch_js') as compiler:
        output = commands.compile_sketch('foo')

        assert output == sketch
        compiler.assert_called_once_with(sketch)
Esempio n. 2
0
File: cli.py Progetto: jvfe/pyp5js
def compile_sketch(sketch_name, refresh, template):
    """
    Command to update your sketch files (index, js codes etc)
    \nExample:
    $ pyp5js compile my_sketch
    """
    files = commands.compile_sketch(sketch_name.replace("/", ""), refresh,
                                    template)
    cprint.ok(
        f"Your sketch is ready and available at file://{files.index_html.absolute()}"
    )
Esempio n. 3
0
def render_sketch_view(sketch_name, static_path):
    sketch = Sketch(sketch_name)

    error = ''
    if static_path:
        return _serve_static(sketch.sketch_dir, static_path)

    elif request.method == 'POST':
        py_code = request.form.get('py_code', '')
        if not py_code.strip():
            error = 'You have to input the Python code.'
        elif 'def setup():' not in py_code:
            error = 'You have to define a setup function.'
        elif 'def draw():' not in py_code:
            error = 'You have to define a draw function.'
        else:
            try:
                ast.parse(py_code, sketch.sketch_py.name)
                sketch.sketch_py.write_text(py_code)
            except SyntaxError as exc:
                error = f'SyntaxError: {exc}'

    if not error:
        try:
            # web editor must always use local install of JS dependencies
            commands.compile_sketch(sketch_name, force_local=True)
        except PythonSketchDoesNotExist:
            return f"There's no sketch in {sketch.sketch_dir.resolve()}", 404

    context = {
        'p5_js_url': sketch.urls.p5_js_url,
        'sketch_js_url': sketch.urls.sketch_js_url,
        'sketch_name': sketch.sketch_name,
        'py_code': sketch.sketch_py.read_text(),
        'error': error,
        'js_as_module': sketch.config.is_transcrypt,
        'live_run': sketch.config.is_pyodide,
    }
    return render_template('view_sketch.html', **context)
Esempio n. 4
0
def test_compile_sketch_error_if_invalid_sketch(sketch):
    with patch('pyp5js.commands.compile_sketch_js') as compiler:
        with pytest.raises(InvalidName):
            commands.compile_sketch('123foo')
        assert not compiler.called
Esempio n. 5
0
def test_compile_sketch_error_if_sketch_does_not_exist(sketch):
    with patch('pyp5js.commands.compile_sketch_js') as compiler:
        with pytest.raises(PythonSketchDoesNotExist):
            commands.compile_sketch('foo')
        assert not compiler.called
Esempio n. 6
0
 def create_sketch_with_static_files(self, name, use_cdn=True):
     sketch = commands.new_sketch(name, use_cdn=use_cdn)
     commands.compile_sketch(name)
     return sketch.sketch_dir
Esempio n. 7
0
def compile_sketch(sketch_name):
    files = commands.compile_sketch(sketch_name)
    cprint.ok(f"Your sketch is ready and available at file://{files.index_html.absolute()}")