Skip to content

Commit c171e75

Browse files
committed
Add type hints and docstrings to scyjava.config
1 parent f83edfa commit c171e75

File tree

1 file changed

+100
-29
lines changed

1 file changed

+100
-29
lines changed

src/scyjava/config.py

Lines changed: 100 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,22 @@
11
import enum as _enum
22
import logging as _logging
33
import os as _os
4-
import pathlib as _pathlib
4+
from pathlib import Path
5+
from typing import Sequence
56

67
import jpype as _jpype
78
from jgo import maven_scijava_repository as _scijava_public
89

910

1011
_logger = _logging.getLogger(__name__)
1112

12-
endpoints = []
13+
endpoints: list[str] = []
1314

1415
_repositories = {"scijava.public": _scijava_public()}
1516
_verbose = 0
1617
_manage_deps = True
17-
_cache_dir = _pathlib.Path.home() / ".jgo"
18-
_m2_repo = _pathlib.Path.home() / ".m2" / "repository"
18+
_cache_dir = Path.home() / ".jgo"
19+
_m2_repo = Path.home() / ".m2" / "repository"
1920
_options = []
2021
_shortcuts = {}
2122

@@ -60,7 +61,11 @@ def get_endpoints():
6061
return endpoints
6162

6263

63-
def add_repositories(*args, **kwargs):
64+
def add_repositories(*args, **kwargs) -> None:
65+
"""
66+
Add one or more Maven repositories to be used by jgo for downloading dependencies.
67+
See the jgo documentation for details.
68+
"""
6469
global _repositories
6570
for arg in args:
6671
_logger.debug("Adding repositories %s to %s", arg, _repositories)
@@ -69,57 +74,92 @@ def add_repositories(*args, **kwargs):
6974
_repositories.update(kwargs)
7075

7176

72-
def get_repositories():
77+
def get_repositories() -> dict[str, str]:
78+
"""
79+
Gets the Maven repositories jgo will use for downloading dependencies.
80+
See the jgo documentation for details.
81+
"""
7382
global _repositories
7483
return _repositories
7584

7685

77-
def set_verbose(level):
86+
def set_verbose(level: int) -> None:
87+
"""
88+
Set the level of verbosity for logging environment construction details.
89+
90+
:param level:
91+
0 for quiet (default), 1 for verbose, 2 for extra verbose.
92+
"""
7893
global _verbose
7994
_logger.debug("Setting verbose level to %d (was %d)", level, _verbose)
8095
_verbose = level
8196

8297

83-
def get_verbose():
98+
def get_verbose() -> int:
99+
"""
100+
Get the level of verbosity for logging environment construction details.
101+
"""
84102
global _verbose
85103
_logger.debug("Getting verbose level: %d", _verbose)
86104
return _verbose
87105

88106

89-
def set_manage_deps(manage):
107+
def set_manage_deps(manage: bool) -> None:
108+
"""
109+
Set whether jgo will resolve dependencies in managed mode.
110+
See the jgo documentation for details.
111+
"""
90112
global _manage_deps
91113
_logger.debug("Setting manage deps to %d (was %d)", manage, _manage_deps)
92114
_manage_deps = manage
93115

94116

95-
def get_manage_deps():
117+
def get_manage_deps() -> bool:
118+
"""
119+
Get whether jgo will resolve dependencies in managed mode.
120+
See the jgo documentation for details.
121+
"""
96122
global _manage_deps
97123
return _manage_deps
98124

99125

100-
def set_cache_dir(dir):
126+
def set_cache_dir(cache_dir: Path | str) -> None:
127+
"""
128+
Set the location to use for the jgo environment cache.
129+
See the jgo documentation for details.
130+
"""
101131
global _cache_dir
102-
_logger.debug("Setting cache dir to %s (was %s)", dir, _cache_dir)
103-
_cache_dir = dir
132+
_logger.debug("Setting cache dir to %s (was %s)", cache_dir, _cache_dir)
133+
_cache_dir = cache_dir
104134

105135

106-
def get_cache_dir():
136+
def get_cache_dir() -> Path:
137+
"""
138+
Get the location to use for the jgo environment cache.
139+
See the jgo documentation for details.
140+
"""
107141
global _cache_dir
108142
return _cache_dir
109143

110144

111-
def set_m2_repo(dir):
145+
def set_m2_repo(repo_dir : Path | str) -> None:
146+
"""
147+
Set the location to use for the local Maven repository cache.
148+
"""
112149
global _m2_repo
113-
_logger.debug("Setting m2 repo dir to %s (was %s)", dir, _m2_repo)
114-
_m2_repo = dir
150+
_logger.debug("Setting m2 repo dir to %s (was %s)", repo_dir, _m2_repo)
151+
_m2_repo = repo_dir
115152

116153

117-
def get_m2_repo():
154+
def get_m2_repo() -> Path:
155+
"""
156+
Get the location to use for the local Maven repository cache.
157+
"""
118158
global _m2_repo
119159
return _m2_repo
120160

121161

122-
def add_classpath(*path):
162+
def add_classpath(*path) -> None:
123163
"""
124164
Add elements to the Java class path.
125165
@@ -148,7 +188,7 @@ def add_classpath(*path):
148188
_jpype.addClassPath(p)
149189

150190

151-
def find_jars(directory):
191+
def find_jars(directory: Path | str) -> list[str]:
152192
"""
153193
Find .jar files beneath a given directory.
154194
@@ -164,11 +204,14 @@ def find_jars(directory):
164204
return jars
165205

166206

167-
def get_classpath():
207+
def get_classpath() -> str:
208+
"""
209+
Get the classpath to be passed to the JVM at startup.
210+
"""
168211
return _jpype.getClassPath()
169212

170213

171-
def set_heap_min(mb: int = None, gb: int = None):
214+
def set_heap_min(mb: int = None, gb: int = None) -> None:
172215
"""
173216
Set the initial amount of memory to allocate to the Java heap.
174217
@@ -185,7 +228,7 @@ def set_heap_min(mb: int = None, gb: int = None):
185228
add_option(f"-Xms{_mem_value(mb, gb)}")
186229

187230

188-
def set_heap_max(mb: int = None, gb: int = None):
231+
def set_heap_max(mb: int = None, gb: int = None) -> None:
189232
"""
190233
Shortcut for passing -Xmx###m or -Xmx###g to Java.
191234
@@ -208,7 +251,7 @@ def _mem_value(mb: int = None, gb: int = None) -> str:
208251
raise ValueError("Exactly one of mb or gb must be given.")
209252

210253

211-
def enable_headless_mode():
254+
def enable_headless_mode() -> None:
212255
"""
213256
Enable headless mode, for running Java without a display.
214257
This mode prevents any graphical elements from popping up.
@@ -237,29 +280,57 @@ def enable_remote_debugging(port: int = 8000, suspend: bool = False):
237280
add_option(f"-agentlib:jdwp={arg_string}")
238281

239282

240-
def add_option(option):
283+
def add_option(option: str) -> None:
284+
"""
285+
Add an option to pass at JVM startup. Examples:
286+
287+
-Djava.awt.headless=true
288+
-Xmx10g
289+
--add-opens=java.base/java.lang=ALL-UNNAMED
290+
-XX:+UnlockExperimentalVMOptions
291+
292+
:param option:
293+
The option to add.
294+
"""
241295
global _options
242296
_options.append(option)
243297

244298

245-
def add_options(options):
299+
def add_options(options: str | Sequence) -> None:
300+
"""
301+
Add one or more options to pass at JVM startup.
302+
303+
:param options:
304+
Sequence of options to add, or single string to pass as an individual option.
305+
"""
246306
global _options
247307
if isinstance(options, str):
248308
_options.append(options)
249309
else:
250310
_options.extend(options)
251311

252312

253-
def get_options():
313+
def get_options() -> list[str]:
314+
"""
315+
Get the list of options to be passed at JVM startup.
316+
"""
254317
global _options
255318
return _options
256319

257320

258-
def add_shortcut(k, v):
321+
def add_shortcut(k: str, v: str):
322+
"""
323+
Add a shortcut key/value to be used by jgo for evaluating endpoints.
324+
See the jgo documentation for details.
325+
"""
259326
global _shortcuts
260327
_shortcuts[k] = v
261328

262329

263-
def get_shortcuts():
330+
def get_shortcuts() -> dict[str, str]:
331+
"""
332+
Get the dictionary of shorts that jgo will use for evaluating endpoints.
333+
See the jgo documentation for details.
334+
"""
264335
global _shortcuts
265336
return _shortcuts

0 commit comments

Comments
 (0)