Skip to content

Commit

Permalink
feat: support .funixignore in recursive mode
Browse files Browse the repository at this point in the history
  • Loading branch information
Yazawazi committed Oct 29, 2023
1 parent 361acbf commit 49167d7
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 10 deletions.
34 changes: 25 additions & 9 deletions backend/funix/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
from inspect import isfunction
from ipaddress import ip_address
from os import chdir, getcwd, listdir
from os.path import dirname, exists, isdir, join, normpath, sep
from os.path import dirname, exists, isdir, join, normpath, sep, abspath
from sys import exit, path
from typing import Generator, Optional
from urllib.parse import quote

from flask import Flask
from gitignore_parser import parse_gitignore

import funix.decorator as decorator
import funix.hint as hint
Expand Down Expand Up @@ -140,7 +141,7 @@ def __prep(


def get_python_files_in_dir(
base_dir: str, add_to_sys_path: bool, need_full_path: bool
base_dir: str, add_to_sys_path: bool, need_full_path: bool, is_dir: bool
) -> Generator[str, None, None]:
"""
Get all the Python files in a directory.
Expand All @@ -149,23 +150,32 @@ def get_python_files_in_dir(
base_dir (str): The path.
add_to_sys_path (bool): If the path should be added to sys.path.
need_full_path (bool): If the full path is needed.
is_dir (bool): If mode is dir mode.
Returns:
Generator[str, None, None]: The Python files.
"""
if add_to_sys_path:
path.append(base_dir)
ignore_file = join(base_dir, ".funixignore")
matches = None
if exists(ignore_file):
matches = parse_gitignore(abspath(ignore_file))
files = listdir(base_dir)
for file in files:
if isdir(join(base_dir, file)):
yield from get_python_files_in_dir(
join(base_dir, file), add_to_sys_path, need_full_path
join(base_dir, file), add_to_sys_path, need_full_path, is_dir
)
if file.endswith(".py") and file != "__init__.py":
if need_full_path:
yield join(base_dir, file)
else:
yield file[:-3]
full_path = join(base_dir, file)
abs_full_path = abspath(full_path)
if matches:
if not matches(abs_full_path):
if need_full_path:
yield join(base_dir, file)
else:
yield file[:-3]


def import_from_config(
Expand Down Expand Up @@ -230,7 +240,10 @@ def import_from_config(
if exists(file_or_module_name) and isdir(file_or_module_name):
base_dir = file_or_module_name
for single_file in get_python_files_in_dir(
base_dir=base_dir, add_to_sys_path=False, need_full_path=True
base_dir=base_dir,
add_to_sys_path=False,
need_full_path=True,
is_dir=True,
):
__prep(
module_or_file=single_file,
Expand All @@ -254,7 +267,10 @@ def import_from_config(
f"`__init__.py` is not found inside module path: {module_path}!"
)
for module in get_python_files_in_dir(
base_dir=dirname(module_path), add_to_sys_path=True, need_full_path=False
base_dir=dirname(module_path),
add_to_sys_path=True,
need_full_path=False,
is_dir=False,
):
__prep(
module_or_file=module,
Expand Down
1 change: 1 addition & 0 deletions backend/funix/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ functions-framework==3.*
requests>=2.28.1
mpld3>=0.5.8
plac>=1.3.5
gitignore_parser>=0.1.9
2 changes: 2 additions & 0 deletions examples/.funixignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Same syntax as .gitignore
archive
21 changes: 21 additions & 0 deletions examples/archive/memory.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from decorator import TexteaExport


# @TexteaExport(path='/calc', type=['add', 'minus'])
def calc(a: int, b: int, type: str):
if type == "add":
return a + b
elif type == "minus":
return a - b
else:
raise "invalid parameter type"


@TexteaExport(path="/test", username={"possible": "turx"}, pi={"example": 3.1415926535})
def test(username: str, pi: float, d: dict, arr: list):
s = ""
s += ("{}, {}\n").format(username, type(username))
s += ("{}, {}\n").format(pi, type(pi))
s += ("{}, {}\n").format(d, type(d))
s += ("{}, {}\n").format(arr, type(arr))
return s
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ dependencies = [
"flask>=2.2.2",
"functions-framework==3.*",
"requests>=2.28.1",
"plac>=1.3.5"
"plac>=1.3.5",
"gitignore_parser>=0.1.9",
]

[project.optional-dependencies]
Expand Down

0 comments on commit 49167d7

Please sign in to comment.