Skip to content

Commit bfeecb2

Browse files
authored
Merge pull request #17 from volfpeter/fix/ignore-hidden-folders
Just log module import errors, skip folders whose name starts with a dot
2 parents e482c67 + 181aabc commit bfeecb2

File tree

3 files changed

+16
-6
lines changed

3 files changed

+16
-6
lines changed

holm/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
__version__ = "0.3.0"
1+
__version__ = "0.3.1"
22

33
from .app import App as App
44
from .fastapi import FastAPIDependency as FastAPIDependency

holm/_model.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import annotations
22

33
import inspect
4+
import logging
45
from collections.abc import Callable
56
from dataclasses import dataclass, field
67
from importlib import import_module
@@ -137,7 +138,13 @@ def import_module(
137138

138139
# Support applications that are not wrapped in a Python package.
139140
# In that case `self.package_name` is ".".
140-
module = import_module(name if self.package_name == "." else f"{self.package_name}.{name}")
141+
import_name = name if self.package_name == "." else f"{self.package_name}.{name}"
142+
try:
143+
module = import_module(import_name)
144+
except Exception:
145+
# Handle potential misconfigurations with a simple warning, instead of an exception.
146+
logging.getLogger("holm").warning(f"Failed to import module {name} at: {import_name}")
147+
return None
141148

142149
if not validate(module):
143150
raise ValueError(f"Invalid module: {name}")

holm/app.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,18 +137,21 @@ def _discover_app_packages(config: AppConfig) -> set[PackageInfo]:
137137
@lru_cache()
138138
def is_excluded(path: Path) -> bool:
139139
"""Returns whether the given file or package path should be excluded from the application."""
140-
rel_path = path.relative_to(config.root_dir)
141140
return any(
142141
# Exclude if a path segment starts with an underscore but does not end with one.
143142
# Path segments that both start and end with an underscore represent path parameters!
144-
p.startswith("_") and not p.endswith("_")
145-
for p in rel_path.parts
143+
(p.startswith("_") and not p.endswith("_"))
144+
# Also exclude paths that start with a dot (virtual env, git, etc.)
145+
or p.startswith(".")
146+
for p in path.parts
146147
)
147148

148149
return {
149150
PackageInfo.from_marker_file(f, config=config)
150151
for f in config.app_dir.rglob("*.py")
151-
if f.stem in module_names and not is_excluded(f.parent) # Pass the parent to make use of caching
152+
if f.stem in module_names
153+
# Pass the relative path of the parent to make the best use of caching
154+
and not is_excluded(f.parent.relative_to(config.root_dir))
152155
}
153156

154157

0 commit comments

Comments
 (0)