Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ignoring files prefix and use only files implemented #557

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions eel/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Empty file added tests/__init__.py
Empty file.
22 changes: 22 additions & 0 deletions tests/data/init_test/ignore_test.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<!-- Below is from: https://github.com/samuelhwilliams/Eel/blob/master/examples/05%20-%20input/web/main.html -->

<!DOCTYPE html>
<html>

<head>
<script type="text/javascript" src="/eel.js"></script>
<script type="text/javascript">
$(function () {
eel.expose(ignore_test); // Expose this function to Python
function ignore_test(x) {
console.log("Ignore test.");
}
});
</script>
</head>

<body>
<h1>Testing if some js file can be ignored from parsing.</h1>
</body>

</html>
15 changes: 9 additions & 6 deletions tests/unit/test_eel.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}"