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)
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)
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())
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)
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)
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)
def sketch_dir(self): return SKETCHBOOK_DIR.joinpath(f'{self.sketch_name}')
def tearDown(self): if SKETCHBOOK_DIR.exists(): shutil.rmtree(SKETCHBOOK_DIR)
def create_file(self, file_name, content=''): with (SKETCHBOOK_DIR.joinpath(file_name).resolve()).open('w') as fd: fd.write(content)
def setUp(self): if not SKETCHBOOK_DIR.exists(): SKETCHBOOK_DIR.mkdir()
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)
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)