def test_split_file_factory_raises_on_invalid_packages(tmp_path): import os dirname = path.join(tmp_path, "my_app") os.makedirs(dirname) with pytest.raises(SyntaxError): root, app = utils.split_file_factory(dirname)
def test_split_file_factory_with_py_file_existing(tmp_path): import os filename = os.path.join(tmp_path, "wsgi.py") with open(filename, "w") as fid: fid.write("") root, app = utils.split_file_factory(f"{filename[:-3]}:app") assert root == filename assert app == "app"
def test_split_file_factory_handles_packages(tmp_path): dirname = os.path.join(tmp_path, "my_app") os.makedirs(dirname) with open(f"{dirname}/__init__.py", "w") as fid: fid.write("") root, app = utils.split_file_factory(dirname) assert "my_app" in root
def attach(args): print("Attaching...") # TODO: Check that the provided blueprint exists, error if not filename, func = split_file_factory(args.to) key_lines, contents = _find_key_lines(filename, func) # TODO: remove the need for this check by enabling an existing static dir (see #3) # (https://github.com/apryor6/flaskerize/issues/3) indent = " " * 4 # TODO: dynamically determine indentation new_static = ", static_folder=None" # TODO: Verify that the flask line is greater than start_func or, more rigorously, # make sure that you are inserting from back to front so that the line numbers are # not perturbed as you go call_to_Flask = [ c.strip() for c in contents[key_lines["flask"]][:-1].split(",") ] # TODO: Clean up this messy logic that is simply checking if the static_folder you # want to add is already present # TODO: Support multi-line definitions where the Flask call is not only one line if not any("static_folder" in c for c in call_to_Flask): updated = ( ", ".join(i.strip() for i in call_to_Flask if "static_folder" not in i) + new_static) if updated.strip() != ", ".join(call_to_Flask).strip(): contents[key_lines["flask"]] = f"{indent}{updated})" register_line = f"{indent}app.register_blueprint(site, url_prefix='/')" if (register_line not in contents and register_line.replace("'", '"') not in contents): contents.insert(key_lines["flask"] + 1, register_line) import_bp_line = f"{indent}from {args.bp.replace('.py', '')} import site" if import_bp_line not in contents: contents.insert(key_lines["start_func"] + 1, import_bp_line) contents = "\n".join(contents) if args.dry_run: print("Dry run result: ") print(contents) else: with open(filename, "w") as fid: fid.write(contents)
def wsgi(args): from flaskerize.utils import split_file_factory filename, func = split_file_factory(args.source) filename = filename.replace(".py", "") CONTENTS = f"""{HEADER} from {filename} import {func} app = {func}() """ _generate( CONTENTS, output_name=args.output_name, filename=args.output_file, dry_run=args.dry_run, ) print("Successfully created new wsgi")
def test_a(): with pytest.raises(ValueError): utils.split_file_factory("oops:this:is:wrong:syntax!")
def test_split_file_factory(): root, app = utils.split_file_factory("wsgi:app") assert root == "wsgi" assert app == "app"
def test_split_file_factory_respects_explicity_path_over_a_default_path(): root, app = utils.split_file_factory("shake/and:bake", default_func_name="take") assert root == "shake/and" assert app == "bake"
def test_split_file_factory_with_a_default_path(): root, app = utils.split_file_factory("shake/and", default_func_name="bake") assert root == "shake/and" assert app == "bake"
def test_split_file_factory_with_path(): root, app = utils.split_file_factory("my/path/wsgi:app") assert root == "my/path/wsgi" assert app == "app"
def test_split_file_factory_with_other_delim(): root, app = utils.split_file_factory("wsgi::app", delim="::") assert root == "wsgi" assert app == "app"