Skip to content

Commit

Permalink
Removes global_objects and adds template environment
Browse files Browse the repository at this point in the history
The global_objects was a bad solution and prone to breaking. The better
way is to just import what you need from your own app.

We also add in a reference to the template environment on the app
instance, which lets it be modified if needed. (e.g. adding functions or
variables to the globals.)
  • Loading branch information
Felix Ingram committed May 11, 2022
1 parent 6876f31 commit 9d4c85e
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/darkstar/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
A web framework for using htmx with Starlette
"""

__version__ = "0.1.4"
__version__ = "0.1.5"
16 changes: 5 additions & 11 deletions src/darkstar/applications.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ def __init__(
debug: bool = False,
routes: typing.Sequence[BaseRoute] = [],
static_directory: str = "static",
global_objects=[],
middleware: typing.Sequence[Middleware] = None,
exception_handlers: typing.Mapping[
typing.Any,
Expand All @@ -92,11 +91,11 @@ def __init__(
lifespan: typing.Callable[["Starlette"], typing.AsyncContextManager] = None,
) -> None:

self.global_objects = global_objects

global dark_star_templates
dark_star_templates = Jinja2Templates(routes_path)

self.template_env = dark_star_templates.env

path_routes = self._collect_routes(routes_path)

if not any(
Expand Down Expand Up @@ -127,24 +126,19 @@ def _collect_routes(self, routes_path) -> typing.Sequence[BaseRoute]:
)
)

new_globals = {x.__name__: x for x in self.global_objects}

compiled_function = exec(
compile(modded_function, f"{path}", "exec"),
new_globals.update(globals()),
)
exec(compile(modded_function, f"{path}", "exec"), globals())

route_options = get_options(python)

if path.relative_to(routes_path) == Path("index.py"):
if "name" not in route_options:
route_options["name"] = "index"
routes.append(Route("/", locals()[function_name], **route_options))
routes.append(Route("/", globals()[function_name], **route_options))
else:
routes.append(
Route(
f"/{path.relative_to(routes_path).with_suffix('')}/",
locals()[function_name],
globals()[function_name],
**route_options,
)
)
Expand Down

0 comments on commit 9d4c85e

Please sign in to comment.