From 88719ade802786bdff6586fdaafc0f56d0d85ec4 Mon Sep 17 00:00:00 2001 From: Sanskar Jethi Date: Sat, 23 Dec 2023 13:46:58 +0000 Subject: [PATCH 1/3] fix: cli invocation for Robyn --- pyproject.toml | 1 + robyn/__main__.py | 119 +--------------------------------------------- robyn/cli.py | 118 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 121 insertions(+), 117 deletions(-) create mode 100644 robyn/cli.py diff --git a/pyproject.toml b/pyproject.toml index 2c6fc542d..702f7246b 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -82,6 +82,7 @@ nox = "2023.4.22" websocket-client = "1.5.0" [tool.poetry.scripts] +robyn = "robyn.cli:run" test_server = { callable = "integration_tests.base_routes:main" } [tool.ruff] diff --git a/robyn/__main__.py b/robyn/__main__.py index 4b688516f..b9781ffbb 100644 --- a/robyn/__main__.py +++ b/robyn/__main__.py @@ -1,119 +1,4 @@ -import os -import sys -from typing import Optional -import webbrowser -from InquirerPy import prompt -from InquirerPy.base.control import Choice -from .argument_parser import Config -from .reloader import setup_reloader -from robyn.robyn import get_version -from pathlib import Path -import shutil -import subprocess - - -SCAFFOLD_DIR = Path(__file__).parent / "scaffold" -CURRENT_WORKING_DIR = Path.cwd() - - -def create_robyn_app(): - questions = [ - { - "type": "input", - "message": "Directory Path:", - "name": "directory", - }, - { - "type": "list", - "message": "Need Docker? (Y/N)", - "choices": [ - Choice("Y", name="Y"), - Choice("N", name="N"), - ], - "default": Choice("N", name="N"), - "name": "docker", - }, - { - "type": "list", - "message": "Please select project type (Mongo/Postgres/Sqlalchemy/Prisma): ", - "choices": [ - Choice("no-db", name="No DB"), - Choice("sqlite", name="Sqlite"), - Choice("postgres", name="Postgres"), - Choice("mongo", name="MongoDB"), - Choice("sqlalchemy", name="SqlAlchemy"), - Choice("prisma", name="Prisma"), - ], - "default": Choice("no-db", name="No DB"), - "name": "project_type", - }, - ] - result = prompt(questions=questions) - project_dir_path = Path(result["directory"]).resolve() - docker = result["docker"] - project_type = result["project_type"] - - final_project_dir_path = (CURRENT_WORKING_DIR / project_dir_path).resolve() - - print(f"Creating a new Robyn project '{final_project_dir_path}'...") - - # Create a new directory for the project - os.makedirs(final_project_dir_path, exist_ok=True) - - selected_project_template = (SCAFFOLD_DIR / Path(project_type)).resolve() - shutil.copytree(str(selected_project_template), str(final_project_dir_path), dirs_exist_ok=True) - - # If docker is not needed, delete the docker file - if docker == "N": - os.remove(f"{final_project_dir_path}/Dockerfile") - - print(f"New Robyn project created in '{final_project_dir_path}' ") - - -def docs(): - print("Opening Robyn documentation... | Offline docs coming soon!") - webbrowser.open("https://robyn.tech") - - -def start_dev_server(file_path: Optional[str] = None): - if file_path is None: - return - - directory_path = Path.cwd() - absolute_file_path = (directory_path / file_path).resolve() - - if config.dev and not os.environ.get("IS_RELOADER_RUNNING", False): - setup_reloader(str(directory_path), str(absolute_file_path)) - return - - -def start_app_normally(config: Config): - # Parsing the known and unknown arguments - known_arguments, unknown_args = config.parser.parse_known_args() - - # Convert known arguments to a list of strings suitable for subprocess.run - known_args_list = [f"{key}={value}" for key, value in vars(known_arguments).items() if value is not None] - - # Combine the python executable, unknown arguments, and known arguments - command = [sys.executable, *unknown_args, *known_args_list] - - # Run the subprocess - subprocess.run(command, start_new_session=False) - +from robyn.cli import run if __name__ == "__main__": - config = Config() - if config.create: - create_robyn_app() - - elif config.version: - print(get_version()) - - elif config.docs: - docs() - - elif config.dev: - print("Starting dev server...") - start_dev_server(config.file_path) - else: - start_app_normally(config) + run() diff --git a/robyn/cli.py b/robyn/cli.py new file mode 100644 index 000000000..ffa6fd4bc --- /dev/null +++ b/robyn/cli.py @@ -0,0 +1,118 @@ +import os +import sys +from typing import Optional +import webbrowser +from InquirerPy import prompt +from InquirerPy.base.control import Choice +from .argument_parser import Config +from .reloader import setup_reloader +from robyn.robyn import get_version +from pathlib import Path +import shutil +import subprocess + + +SCAFFOLD_DIR = Path(__file__).parent / "scaffold" +CURRENT_WORKING_DIR = Path.cwd() + + +def create_robyn_app(): + questions = [ + { + "type": "input", + "message": "Directory Path:", + "name": "directory", + }, + { + "type": "list", + "message": "Need Docker? (Y/N)", + "choices": [ + Choice("Y", name="Y"), + Choice("N", name="N"), + ], + "default": Choice("N", name="N"), + "name": "docker", + }, + { + "type": "list", + "message": "Please select project type (Mongo/Postgres/Sqlalchemy/Prisma): ", + "choices": [ + Choice("no-db", name="No DB"), + Choice("sqlite", name="Sqlite"), + Choice("postgres", name="Postgres"), + Choice("mongo", name="MongoDB"), + Choice("sqlalchemy", name="SqlAlchemy"), + Choice("prisma", name="Prisma"), + ], + "default": Choice("no-db", name="No DB"), + "name": "project_type", + }, + ] + result = prompt(questions=questions) + project_dir_path = Path(result["directory"]).resolve() + docker = result["docker"] + project_type = result["project_type"] + + final_project_dir_path = (CURRENT_WORKING_DIR / project_dir_path).resolve() + + print(f"Creating a new Robyn project '{final_project_dir_path}'...") + + # Create a new directory for the project + os.makedirs(final_project_dir_path, exist_ok=True) + + selected_project_template = (SCAFFOLD_DIR / Path(project_type)).resolve() + shutil.copytree(str(selected_project_template), str(final_project_dir_path), dirs_exist_ok=True) + + # If docker is not needed, delete the docker file + if docker == "N": + os.remove(f"{final_project_dir_path}/Dockerfile") + + print(f"New Robyn project created in '{final_project_dir_path}' ") + + +def docs(): + print("Opening Robyn documentation... | Offline docs coming soon!") + webbrowser.open("https://robyn.tech") + + +def start_dev_server(config: Config, file_path: Optional[str] = None): + if file_path is None: + return + + directory_path = Path.cwd() + absolute_file_path = (directory_path / file_path).resolve() + + if config.dev and not os.environ.get("IS_RELOADER_RUNNING", False): + setup_reloader(str(directory_path), str(absolute_file_path)) + return + + +def start_app_normally(config: Config): + # Parsing the known and unknown arguments + known_arguments, unknown_args = config.parser.parse_known_args() + + # Convert known arguments to a list of strings suitable for subprocess.run + known_args_list = [f"{key}={value}" for key, value in vars(known_arguments).items() if value is not None] + + # Combine the python executable, unknown arguments, and known arguments + command = [sys.executable, *unknown_args, *known_args_list] + + # Run the subprocess + subprocess.run(command, start_new_session=False) + +def run(): + config = Config() + if config.create: + create_robyn_app() + + elif config.version: + print(get_version()) + + elif config.docs: + docs() + + elif config.dev: + print("Starting dev server...") + start_dev_server(config, config.file_path) + else: + start_app_normally(config) From 5c3a2d64d43d14bbc2208ceef379956670b7d690 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 24 Dec 2023 16:27:37 +0000 Subject: [PATCH 2/3] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- robyn/cli.py | 1 + 1 file changed, 1 insertion(+) diff --git a/robyn/cli.py b/robyn/cli.py index ffa6fd4bc..c64497767 100644 --- a/robyn/cli.py +++ b/robyn/cli.py @@ -100,6 +100,7 @@ def start_app_normally(config: Config): # Run the subprocess subprocess.run(command, start_new_session=False) + def run(): config = Config() if config.create: From 5b2e0eea413332da232eeee2bc18f10f6b233dcc Mon Sep 17 00:00:00 2001 From: Sanskar Jethi Date: Mon, 25 Dec 2023 14:06:59 +0000 Subject: [PATCH 3/3] fix clippy --- src/types/headers.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/types/headers.rs b/src/types/headers.rs index 5608940d4..965053373 100644 --- a/src/types/headers.rs +++ b/src/types/headers.rs @@ -102,7 +102,7 @@ impl Headers { if new_value.is_err() { let value = value.to_string(); - self.headers.entry(key).or_insert_with(Vec::new).push(value); + self.headers.entry(key).or_default().push(value); } else { let value: Vec = new_value.unwrap().iter().map(|x| x.to_string()).collect(); self.headers.entry(key).or_default().extend(value);