Skip to content

Commit

Permalink
chore: lazy imports
Browse files Browse the repository at this point in the history
  • Loading branch information
phil65 committed Feb 17, 2025
1 parent 629cfa4 commit 91c5da0
Show file tree
Hide file tree
Showing 10 changed files with 26 additions and 17 deletions.
4 changes: 2 additions & 2 deletions src/jinjarope/codetree.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
import inspect
import os

import upath


class NodeType(Enum):
"""Types of nodes found in Python code structure analysis.
Expand Down Expand Up @@ -134,6 +132,8 @@ def parse_object(obj: os.PathLike[str] | str | type) -> Node:
print(f"Found {len(root.children)} top-level definitions")
```
"""
import upath

if isinstance(obj, str | os.PathLike):
path = upath.UPath(obj)
content = path.read_text("utf-8")
Expand Down
3 changes: 1 addition & 2 deletions src/jinjarope/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
from functools import wraps
from typing import TYPE_CHECKING, Any, ParamSpec, TypeVar

import upath


if TYPE_CHECKING:
from collections.abc import Callable
Expand Down Expand Up @@ -70,6 +68,7 @@ def cache_info() -> dict[str, int]:


if __name__ == "__main__":
import upath

@cache_with_transforms(arg_transformers={0: lambda p: upath.UPath(p).resolve()})
def read_file_content(filepath: str | upath.UPath) -> str:
Expand Down
4 changes: 2 additions & 2 deletions src/jinjarope/envtests.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
import sys
from typing import TYPE_CHECKING, Any, TypeVar

import upath

from jinjarope import utils


Expand Down Expand Up @@ -173,6 +171,8 @@ def contains_files(directory: str | os.PathLike[str]) -> bool:
Args:
directory: The directoy to check
"""
import upath

path = upath.UPath(directory)
return path.exists() and any(path.iterdir())

Expand Down
3 changes: 2 additions & 1 deletion src/jinjarope/filetree.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
from typing import TYPE_CHECKING, Any

from jinja2 import filters
import upath

from jinjarope import iconfilters, textfilters

Expand Down Expand Up @@ -96,6 +95,8 @@ def __init__(
root_path: Root path of the directory tree.
options: Options for directory tree printing.
"""
import upath

self.root_path = upath.UPath(root_path)
self.options = options or TreeOptions()

Expand Down
7 changes: 5 additions & 2 deletions src/jinjarope/fsspecloaders.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
import pathlib
from typing import TYPE_CHECKING, Any

import fsspec
import fsspec.core
import jinja2

from jinjarope import envglobals, loaders as loaders_, utils
Expand All @@ -13,6 +11,8 @@
if TYPE_CHECKING:
from collections.abc import Callable

import fsspec


class FsSpecProtocolPathLoader(loaders_.LoaderMixin, jinja2.BaseLoader):
"""A jinja loader for fsspec filesystems.
Expand Down Expand Up @@ -102,6 +102,9 @@ def __init__(self, fs: fsspec.AbstractFileSystem | str, **kwargs: Any):
Also supports "::dir" prefix to set the root path.
kwargs: Optional storage options for the filesystem.
"""
import fsspec
import fsspec.core

super().__init__()
match fs:
case str() if "://" in fs:
Expand Down
4 changes: 2 additions & 2 deletions src/jinjarope/htmlfilters.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
import re
from typing import TYPE_CHECKING, Any, Literal, TypeVar

import requests


if TYPE_CHECKING:
from collections.abc import Mapping
Expand Down Expand Up @@ -430,6 +428,8 @@ def url_to_b64(image_url: str) -> str | None:
Raises:
requests.RequestException: If there's an error downloading the image.
"""
import requests

# Download the image
response = requests.get(image_url)
response.raise_for_status()
Expand Down
6 changes: 4 additions & 2 deletions src/jinjarope/iconfilters.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
from enum import StrEnum
from typing import TYPE_CHECKING, Final, Literal, TypedDict

import upath

from jinjarope import icons


Expand Down Expand Up @@ -424,6 +422,8 @@ def get_path_icon(path: str | os.PathLike[str]) -> str:
Returns:
iconify icon slug
"""
import upath

path_obj = upath.UPath(path)

# Handle directories
Expand Down Expand Up @@ -579,6 +579,8 @@ def get_path_ascii_icon(path: str | os.PathLike[str]) -> str:
Returns:
ASCII icon representing the file type
"""
import upath

path_obj = upath.UPath(path)

# Handle symbolic links
Expand Down
5 changes: 4 additions & 1 deletion src/jinjarope/jinjaloaderfilesystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import fsspec
from fsspec.implementations import memory
import jinja2
from upath import UPath


logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -39,6 +38,8 @@ def isdir(self, path: str) -> bool:
Returns:
True if path is a directory
"""
from upath import UPath

if not self.env.loader:
return False

Expand Down Expand Up @@ -139,6 +140,8 @@ def ls(
Raises:
FileNotFoundError: If path doesn't exist or env has no loader
"""
from upath import UPath

if not self.env.loader:
msg = "Environment has no loader"
raise FileNotFoundError(msg)
Expand Down
3 changes: 2 additions & 1 deletion src/jinjarope/loaders.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from typing import TYPE_CHECKING

import jinja2
import upath

from jinjarope import inspectfilters, utils

Expand Down Expand Up @@ -34,6 +33,8 @@ def __getitem__(self, val: str) -> jinja2.Template:

def __contains__(self, path: str):
"""Check whether given path is loadable by this loader."""
import upath

return upath.UPath(path).as_posix() in self.list_templates()

def __rtruediv__(self, path: str):
Expand Down
4 changes: 2 additions & 2 deletions src/jinjarope/regexfilters.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
import re
from typing import Any, Literal

from jinja2.exceptions import FilterArgumentError


def re_replace(
value: str = "",
Expand Down Expand Up @@ -80,6 +78,8 @@ def re_search(
ignorecase: Whether char casing should be ignored
multiline: Whether to perform a multi-line search
"""
from jinja2.exceptions import FilterArgumentError

groups = list()
for arg in args:
if arg.startswith("\\g"):
Expand Down

0 comments on commit 91c5da0

Please sign in to comment.