diff --git a/eel/__init__.py b/eel/__init__.py index 1f9d8bf4..ac03ee21 100644 --- a/eel/__init__.py +++ b/eel/__init__.py @@ -101,17 +101,29 @@ def decorator(function): ) -def init(path, allowed_extensions=['.js', '.html', '.txt', '.htm', - '.xhtml', '.vue'], js_result_timeout=10000): +def init( + path, + allowed_extensions=[".js", ".html", ".txt", ".htm", ".xhtml", ".vue"], + js_result_timeout=10000, + exclude_file_prefixes=None, + use_only_files=None, +): global root_path, _js_functions, _js_result_timeout root_path = _get_real_path(path) js_functions = set() for root, _, files in os.walk(root_path): for name in files: + + if use_only_files and name not in use_only_files: + continue + if not any(name.endswith(ext) for ext in allowed_extensions): continue + if exclude_file_prefixes and any(name.startswith(exc) for exc in exclude_file_prefixes): + continue + try: with open(os.path.join(root, name), encoding='utf-8') as file: contents = file.read() diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/tests/data/init_test/ignore_test.html b/tests/data/init_test/ignore_test.html new file mode 100644 index 00000000..a0a85e3f --- /dev/null +++ b/tests/data/init_test/ignore_test.html @@ -0,0 +1,22 @@ + + + + + + + + + + + +

Testing if some js file can be ignored from parsing.

+ + + \ No newline at end of file diff --git a/tests/unit/test_eel.py b/tests/unit/test_eel.py index 5a5b3223..ef0d24ad 100644 --- a/tests/unit/test_eel.py +++ b/tests/unit/test_eel.py @@ -20,10 +20,13 @@ def test_exposed_js_functions(js_code, expected_matches): matches = eel.EXPOSED_JS_FUNCTIONS.parseString(js_code).asList() assert matches == expected_matches, f'Expected {expected_matches} (found: {matches}) in: {js_code}' - -def test_init(): +@pytest.mark.parametrize('kwargs, exposed_functions', [ + ({"path": INIT_DIR}, ["show_log", "js_random", "ignore_test", "show_log_alt", "say_hello_js"]), + ({"path": INIT_DIR, "exclude_file_prefixes": ["ignore"]}, ["show_log", "js_random", "show_log_alt", "say_hello_js"]), + ({"path": INIT_DIR, "use_only_files": ["hello.html"]}, ["js_random", "say_hello_js"]), +]) +def test_init_file_excluding(kwargs, exposed_functions): """Test eel.init() against a test directory and ensure that all JS functions are in the global _js_functions.""" - eel.init(path=INIT_DIR) - result = eel._js_functions.sort() - functions = ['show_log', 'js_random', 'show_log_alt', 'say_hello_js'].sort() - assert result == functions, f'Expected {functions} (found: {result}) in {INIT_DIR}' + eel.init(**kwargs) + result = eel._js_functions + assert set(result) == set(exposed_functions), f"Expected {exposed_functions} (found: {result}) in {INIT_DIR}"