Skip to content

Commit 91c5da0

Browse files
committed
chore: lazy imports
1 parent 629cfa4 commit 91c5da0

File tree

10 files changed

+26
-17
lines changed

10 files changed

+26
-17
lines changed

src/jinjarope/codetree.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313
import inspect
1414
import os
1515

16-
import upath
17-
1816

1917
class NodeType(Enum):
2018
"""Types of nodes found in Python code structure analysis.
@@ -134,6 +132,8 @@ def parse_object(obj: os.PathLike[str] | str | type) -> Node:
134132
print(f"Found {len(root.children)} top-level definitions")
135133
```
136134
"""
135+
import upath
136+
137137
if isinstance(obj, str | os.PathLike):
138138
path = upath.UPath(obj)
139139
content = path.read_text("utf-8")

src/jinjarope/decorators.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
from functools import wraps
44
from typing import TYPE_CHECKING, Any, ParamSpec, TypeVar
55

6-
import upath
7-
86

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

7169

7270
if __name__ == "__main__":
71+
import upath
7372

7473
@cache_with_transforms(arg_transformers={0: lambda p: upath.UPath(p).resolve()})
7574
def read_file_content(filepath: str | upath.UPath) -> str:

src/jinjarope/envtests.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99
import sys
1010
from typing import TYPE_CHECKING, Any, TypeVar
1111

12-
import upath
13-
1412
from jinjarope import utils
1513

1614

@@ -173,6 +171,8 @@ def contains_files(directory: str | os.PathLike[str]) -> bool:
173171
Args:
174172
directory: The directoy to check
175173
"""
174+
import upath
175+
176176
path = upath.UPath(directory)
177177
return path.exists() and any(path.iterdir())
178178

src/jinjarope/filetree.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
from typing import TYPE_CHECKING, Any
99

1010
from jinja2 import filters
11-
import upath
1211

1312
from jinjarope import iconfilters, textfilters
1413

@@ -96,6 +95,8 @@ def __init__(
9695
root_path: Root path of the directory tree.
9796
options: Options for directory tree printing.
9897
"""
98+
import upath
99+
99100
self.root_path = upath.UPath(root_path)
100101
self.options = options or TreeOptions()
101102

src/jinjarope/fsspecloaders.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
import pathlib
44
from typing import TYPE_CHECKING, Any
55

6-
import fsspec
7-
import fsspec.core
86
import jinja2
97

108
from jinjarope import envglobals, loaders as loaders_, utils
@@ -13,6 +11,8 @@
1311
if TYPE_CHECKING:
1412
from collections.abc import Callable
1513

14+
import fsspec
15+
1616

1717
class FsSpecProtocolPathLoader(loaders_.LoaderMixin, jinja2.BaseLoader):
1818
"""A jinja loader for fsspec filesystems.
@@ -102,6 +102,9 @@ def __init__(self, fs: fsspec.AbstractFileSystem | str, **kwargs: Any):
102102
Also supports "::dir" prefix to set the root path.
103103
kwargs: Optional storage options for the filesystem.
104104
"""
105+
import fsspec
106+
import fsspec.core
107+
105108
super().__init__()
106109
match fs:
107110
case str() if "://" in fs:

src/jinjarope/htmlfilters.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
import re
99
from typing import TYPE_CHECKING, Any, Literal, TypeVar
1010

11-
import requests
12-
1311

1412
if TYPE_CHECKING:
1513
from collections.abc import Mapping
@@ -430,6 +428,8 @@ def url_to_b64(image_url: str) -> str | None:
430428
Raises:
431429
requests.RequestException: If there's an error downloading the image.
432430
"""
431+
import requests
432+
433433
# Download the image
434434
response = requests.get(image_url)
435435
response.raise_for_status()

src/jinjarope/iconfilters.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
from enum import StrEnum
44
from typing import TYPE_CHECKING, Final, Literal, TypedDict
55

6-
import upath
7-
86
from jinjarope import icons
97

108

@@ -424,6 +422,8 @@ def get_path_icon(path: str | os.PathLike[str]) -> str:
424422
Returns:
425423
iconify icon slug
426424
"""
425+
import upath
426+
427427
path_obj = upath.UPath(path)
428428

429429
# Handle directories
@@ -579,6 +579,8 @@ def get_path_ascii_icon(path: str | os.PathLike[str]) -> str:
579579
Returns:
580580
ASCII icon representing the file type
581581
"""
582+
import upath
583+
582584
path_obj = upath.UPath(path)
583585

584586
# Handle symbolic links

src/jinjarope/jinjaloaderfilesystem.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import fsspec
99
from fsspec.implementations import memory
1010
import jinja2
11-
from upath import UPath
1211

1312

1413
logger = logging.getLogger(__name__)
@@ -39,6 +38,8 @@ def isdir(self, path: str) -> bool:
3938
Returns:
4039
True if path is a directory
4140
"""
41+
from upath import UPath
42+
4243
if not self.env.loader:
4344
return False
4445

@@ -139,6 +140,8 @@ def ls(
139140
Raises:
140141
FileNotFoundError: If path doesn't exist or env has no loader
141142
"""
143+
from upath import UPath
144+
142145
if not self.env.loader:
143146
msg = "Environment has no loader"
144147
raise FileNotFoundError(msg)

src/jinjarope/loaders.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
from typing import TYPE_CHECKING
66

77
import jinja2
8-
import upath
98

109
from jinjarope import inspectfilters, utils
1110

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

3534
def __contains__(self, path: str):
3635
"""Check whether given path is loadable by this loader."""
36+
import upath
37+
3738
return upath.UPath(path).as_posix() in self.list_templates()
3839

3940
def __rtruediv__(self, path: str):

src/jinjarope/regexfilters.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
import re
44
from typing import Any, Literal
55

6-
from jinja2.exceptions import FilterArgumentError
7-
86

97
def re_replace(
108
value: str = "",
@@ -80,6 +78,8 @@ def re_search(
8078
ignorecase: Whether char casing should be ignored
8179
multiline: Whether to perform a multi-line search
8280
"""
81+
from jinja2.exceptions import FilterArgumentError
82+
8383
groups = list()
8484
for arg in args:
8585
if arg.startswith("\\g"):

0 commit comments

Comments
 (0)