Skip to content

Commit

Permalink
feat: auto recursive mode
Browse files Browse the repository at this point in the history
  • Loading branch information
Yazawazi committed Oct 31, 2023
1 parent 53c9202 commit 1b0858b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 49 deletions.
70 changes: 31 additions & 39 deletions backend/funix/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,39 +254,30 @@ def import_from_config(
set_app_secret(app_secret)

if dir_mode:
if exists(file_or_module_name) and isdir(file_or_module_name):
base_dir = file_or_module_name
if default and ":" not in default:
raise ValueError(
"Default function name should be in the format of `file:func` in dir mode!"
)
ignore_file = join(base_dir, ".funixignore")
matches = None
if exists(ignore_file):
matches = parse_gitignore(
abspath(ignore_file), base_dir=abspath(base_dir)
)
for single_file in get_python_files_in_dir(
base_dir = file_or_module_name
if default and ":" not in default:
raise ValueError(
"Default function name should be in the format of `file:func` in dir mode!"
)
ignore_file = join(base_dir, ".funixignore")
matches = None
if exists(ignore_file):
matches = parse_gitignore(abspath(ignore_file), base_dir=abspath(base_dir))
for single_file in get_python_files_in_dir(
base_dir=base_dir,
add_to_sys_path=False,
need_full_path=True,
is_dir=True,
matches=matches,
):
__prep(
module_or_file=single_file,
lazy=lazy,
need_path=True,
is_module=False,
need_name=True,
base_dir=base_dir,
add_to_sys_path=False,
need_full_path=True,
is_dir=True,
matches=matches,
):
__prep(
module_or_file=single_file,
lazy=lazy,
need_path=True,
is_module=False,
need_name=True,
base_dir=base_dir,
default=default,
)
else:
raise RuntimeError(
"Directory not found or not a directory! "
"If you want to use package mode, please use --package/-P option, "
"if you want to use file mode, please use remove --recursive/-R option."
default=default,
)
elif package_mode:
if default or transform:
Expand Down Expand Up @@ -317,11 +308,6 @@ def import_from_config(
raise RuntimeError(
"File not found! If you want to use package mode, please use --package/-P option"
)
elif isdir(file_or_module_name):
raise RuntimeError(
"Oh this is a directory! If you want to use directory/recursive mode, "
"please use --recursive/-R option"
)
elif not file_or_module_name.endswith(".py"):
raise RuntimeError(
"This is not a Python file! You should change the file extension to `.py`."
Expand Down Expand Up @@ -421,7 +407,6 @@ def run(
no_frontend: Optional[bool] = False,
no_browser: Optional[bool] = False,
lazy: Optional[bool] = False,
dir_mode: Optional[bool] = False,
package_mode: Optional[bool] = False,
from_git: Optional[str] = None,
repo_dir: Optional[str] = None,
Expand All @@ -441,7 +426,6 @@ def run(
no_frontend (bool): If you want to disable the frontend, default is False
no_browser (bool): If you want to disable the browser opening, default is False
lazy (bool): If you want to enable lazy mode, default is False
dir_mode (bool): If you want to enable dir mode, default is False
package_mode (bool): If you want to enable package mode, default is False
from_git (str): If you want to run the app from a git repo, default is None
repo_dir (str): If you want to run the app from a git repo, you can specify the directory, default is None
Expand All @@ -453,6 +437,14 @@ def run(
Returns:
None
"""
dir_mode = exists(file_or_module_name) and isdir(file_or_module_name)

if dir_mode and package_mode:
print(
'Error: Cannot use both directory mode and package mode.\nPlease run "funix --help" for more information.'
)
sys.exit(1)

import_from_config(
file_or_module_name=file_or_module_name,
lazy=lazy,
Expand Down
10 changes: 0 additions & 10 deletions backend/funix/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
@plac.flg("no_frontend", "Disable frontend server", abbrev="F")
@plac.flg("no_browser", "Disable auto open browser", abbrev="B")
@plac.flg("lazy", "Load functions without decorator", abbrev="l")
@plac.flg("recursive", "Enable directory mode", abbrev="R")
@plac.flg("package", "Enable package mode", abbrev="P")
@plac.flg("dev", "Enable development mode", abbrev="d")
@plac.flg("transform", "Transform the globals to a session variables", abbrev="t")
Expand All @@ -33,7 +32,6 @@ def main(
no_frontend=False,
no_browser=False,
lazy=False,
recursive=False,
package=False,
dev=False,
transform=False,
Expand All @@ -58,19 +56,12 @@ def main(
)
sys.exit(1)

if recursive and package:
print(
'Error: Cannot use both directory mode and package mode.\nPlease run "funix --help" for more information.'
)
sys.exit(1)

sys.path.append(os.getcwd())
parsed_host: str = os.getenv("FUNIX_HOST", host)
parsed_port: int = int(os.getenv("FUNIX_PORT", port))
parsed_no_frontend: bool = os.getenv("FUNIX_NO_FRONTEND", no_frontend)
parsed_no_browser: bool = os.getenv("FUNIX_NO_BROWSER", no_browser)
parsed_lazy: bool = os.getenv("FUNIX_LAZY", lazy)
parsed_dir_mode: bool = os.getenv("FUNIX_DIR_MODE", recursive)
parsed_package_mode: bool = os.getenv("FUNIX_PACKAGE_MODE", package)
parsed_from_git: str = os.getenv("FUNIX_FROM_GIT", from_git)
parsed_repo_dir: str = os.getenv("FUNIX_REPO_DIR", repo_dir)
Expand All @@ -97,7 +88,6 @@ def main(
no_frontend=parsed_no_frontend,
no_browser=parsed_no_browser,
lazy=parsed_lazy,
dir_mode=parsed_dir_mode,
package_mode=parsed_package_mode,
from_git=parsed_from_git,
repo_dir=parsed_repo_dir,
Expand Down

0 comments on commit 1b0858b

Please sign in to comment.