Beispiel #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)
Beispiel #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)
Beispiel #3
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)
Beispiel #4
0
 def sketch_dir(self):
     return SKETCHBOOK_DIR.joinpath(f'{self.sketch_name}')
Beispiel #5
0
 def create_file(self, file_name, content=''):
     with (SKETCHBOOK_DIR.joinpath(file_name).resolve()).open('w') as fd:
         fd.write(content)
Beispiel #6
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)