Exemplo n.º 1
0
def add_new_sketch_view():
    template = 'new_sketch_form.html'
    context = {'sketches_dir': SKETCHBOOK_DIR.resolve()}

    if request.method == 'POST':
        sketch_name = slugify(request.form.get('sketch_name', '').strip(),
                              separator='_')
        if not sketch_name:
            context['error'] = "You have to input a sketch name to proceed."
        else:
            try:
                # web client only supports pyodide mode for now
                # TODO: improve post payload to accept a select
                files = commands.new_sketch(sketch_name,
                                            interpreter=PYODIDE_INTERPRETER)
                template = 'new_sketch_success.html'
                context.update({
                    'files': files,
                    'sketch_url': f'/sketch/{sketch_name}/',
                })
            except SketchDirAlreadyExistException:
                path = SKETCHBOOK_DIR.joinpath(sketch_name)
                context['error'] = f"The sketch {path} already exists."

    return render_template(template, **context)
Exemplo n.º 2
0
def add_new_sketch_view():
    template = 'new_sketch_form.html'
    context = {
        'sketches_dir': SKETCHBOOK_DIR.resolve(),
        'pyodide_interpreter': PYODIDE_INTERPRETER,
        'transcrypt_interpreter': TRANSCRYPT_INTERPRETER,
    }

    if request.method == 'POST':
        sketch_name = slugify(request.form.get('sketch_name', '').strip(),
                              separator='_')
        interpreter = request.form.get('interpreter', PYODIDE_INTERPRETER)
        if not sketch_name:
            context['error'] = "You have to input a sketch name to proceed."
        elif interpreter not in AVAILABLE_INTERPRETERS:
            context[
                'error'] = f"The interpreter {interpreter} is not valid. Please, select a valid one."
        else:
            try:
                files = commands.new_sketch(sketch_name,
                                            interpreter=interpreter)
                template = 'new_sketch_success.html'
                context.update({
                    'files': files,
                    'sketch_url': f'/sketch/{sketch_name}/',
                })
            except SketchDirAlreadyExistException:
                path = SKETCHBOOK_DIR.joinpath(sketch_name)
                context['error'] = f"The sketch {path} already exists."

    return render_template(template, **context)
Exemplo n.º 3
0
def sketches_list_view():
    sketches = []
    for sketch_dir in (p for p in SKETCHBOOK_DIR.iterdir() if p.is_dir()):
        name = sketch_dir.name
        sketch_files = SketchFiles(name)
        if sketch_files.has_all_files:
            sketches.append({'name': name, 'url': f'/sketch/{name}'})

    return render_template('index.html',
                           sketches=sketches,
                           sketches_dir=SKETCHBOOK_DIR.resolve())
Exemplo n.º 4
0
def serve_sketches(host, port, debug):
    """
    Run HTTP server to compile and serve sketches

    Opitionals:
    - host: http server host (defaults to 127.0.0.1)
    - port: listened by the server (defaults to 5000)

    Example:
    $ pyp5js serve
    """

    if not SKETCHBOOK_DIR.exists():
        SKETCHBOOK_DIR.mkdir()

    commands.serve_http(host, port, debug)
Exemplo n.º 5
0
Arquivo: cli.py Projeto: jvfe/pyp5js
def serve_sketches(host, port, debug):
    """
    Run HTTP server to compile and serve sketches

    Opitionals:
    - host:
    - port:

    Example:
    $ pyp5js serve
    """

    if not SKETCHBOOK_DIR.exists():
        SKETCHBOOK_DIR.mkdir()

    commands.serve_http(host, port, debug)
Exemplo n.º 6
0
def add_new_sketch_view():
    template = 'new_sketch_form.html'
    context = {'sketches_dir': SKETCHBOOK_DIR.resolve()}

    if request.method == 'POST':
        sketch_name = slugify(request.form.get('sketch_name', '').strip(),
                              separator='_')
        if not sketch_name:
            context['error'] = "You have to input a sketch name to proceed."
        try:
            files = commands.new_sketch(sketch_name)
            template = 'new_sketch_success.html'
            context.update({
                'files': files,
                'sketch_url': f'/sketch/{sketch_name}/',
            })
        except SketchDirAlreadyExistException:
            path = SKETCHBOOK_DIR.joinpath(sketch_name)
            context['error'] = f"The sketch {path} already exists."

    return render_template(template, **context)
Exemplo n.º 7
0
 def sketch_dir(self):
     return SKETCHBOOK_DIR.joinpath(f'{self.sketch_name}')
Exemplo n.º 8
0
 def tearDown(self):
     if SKETCHBOOK_DIR.exists():
         shutil.rmtree(SKETCHBOOK_DIR)
Exemplo n.º 9
0
 def create_file(self, file_name, content=''):
     with (SKETCHBOOK_DIR.joinpath(file_name).resolve()).open('w') as fd:
         fd.write(content)
Exemplo n.º 10
0
 def setUp(self):
     if not SKETCHBOOK_DIR.exists():
         SKETCHBOOK_DIR.mkdir()
Exemplo n.º 11
0
 def test_get_new_sketch_renders_new_sketch_form_template(self):
     self.client.get(self.route)
     self.assert_template_used('new_sketch_form.html')
     self.assert_context('sketches_dir', SKETCHBOOK_DIR.resolve())
     self.assert_context('pyodide_interpreter', PYODIDE_INTERPRETER)
     self.assert_context('transcrypt_interpreter', TRANSCRYPT_INTERPRETER)
Exemplo n.º 12
0
 def create_file(self, file_name, content=''):
     mode = 'w'
     if isinstance(content, bytes):
         mode += 'b'
     with (SKETCHBOOK_DIR.joinpath(file_name).resolve()).open(mode) as fd:
         fd.write(content)