Skip to content

Commit 0f48293

Browse files
committed
Add type hints and docstrings to scyjava.config
1 parent 67a6193 commit 0f48293

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
@@ -3,21 +3,22 @@
33
import enum as _enum
44
import logging as _logging
55
import os as _os
6-
import pathlib as _pathlib
6+
from pathlib import Path
7+
from typing import Sequence
78

89
import jpype as _jpype
910
from jgo import maven_scijava_repository as _scijava_public
1011

1112

1213
_logger = _logging.getLogger(__name__)
1314

14-
endpoints = []
15+
endpoints: list[str] = []
1516

1617
_repositories = {"scijava.public": _scijava_public()}
1718
_verbose = 0
1819
_manage_deps = True
19-
_cache_dir = _pathlib.Path.home() / ".jgo"
20-
_m2_repo = _pathlib.Path.home() / ".m2" / "repository"
20+
_cache_dir = Path.home() / ".jgo"
21+
_m2_repo = Path.home() / ".m2" / "repository"
2122
_options = []
2223
_shortcuts = {}
2324

@@ -62,7 +63,11 @@ def get_endpoints():
6263
return endpoints
6364

6465

65-
def add_repositories(*args, **kwargs):
66+
def add_repositories(*args, **kwargs) -> None:
67+
"""
68+
Add one or more Maven repositories to be used by jgo for downloading dependencies.
69+
See the jgo documentation for details.
70+
"""
6671
global _repositories
6772
for arg in args:
6873
_logger.debug("Adding repositories %s to %s", arg, _repositories)
@@ -71,57 +76,92 @@ def add_repositories(*args, **kwargs):
7176
_repositories.update(kwargs)
7277

7378

74-
def get_repositories():
79+
def get_repositories() -> dict[str, str]:
80+
"""
81+
Gets the Maven repositories jgo will use for downloading dependencies.
82+
See the jgo documentation for details.
83+
"""
7584
global _repositories
7685
return _repositories
7786

7887

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

8499

85-
def get_verbose():
100+
def get_verbose() -> int:
101+
"""
102+
Get the level of verbosity for logging environment construction details.
103+
"""
86104
global _verbose
87105
_logger.debug("Getting verbose level: %d", _verbose)
88106
return _verbose
89107

90108

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

96118

97-
def get_manage_deps():
119+
def get_manage_deps() -> bool:
120+
"""
121+
Get whether jgo will resolve dependencies in managed mode.
122+
See the jgo documentation for details.
123+
"""
98124
global _manage_deps
99125
return _manage_deps
100126

101127

102-
def set_cache_dir(dir):
128+
def set_cache_dir(cache_dir: Path | str) -> None:
129+
"""
130+
Set the location to use for the jgo environment cache.
131+
See the jgo documentation for details.
132+
"""
103133
global _cache_dir
104-
_logger.debug("Setting cache dir to %s (was %s)", dir, _cache_dir)
105-
_cache_dir = dir
134+
_logger.debug("Setting cache dir to %s (was %s)", cache_dir, _cache_dir)
135+
_cache_dir = cache_dir
106136

107137

108-
def get_cache_dir():
138+
def get_cache_dir() -> Path:
139+
"""
140+
Get the location to use for the jgo environment cache.
141+
See the jgo documentation for details.
142+
"""
109143
global _cache_dir
110144
return _cache_dir
111145

112146

113-
def set_m2_repo(dir):
147+
def set_m2_repo(repo_dir : Path | str) -> None:
148+
"""
149+
Set the location to use for the local Maven repository cache.
150+
"""
114151
global _m2_repo
115-
_logger.debug("Setting m2 repo dir to %s (was %s)", dir, _m2_repo)
116-
_m2_repo = dir
152+
_logger.debug("Setting m2 repo dir to %s (was %s)", repo_dir, _m2_repo)
153+
_m2_repo = repo_dir
117154

118155

119-
def get_m2_repo():
156+
def get_m2_repo() -> Path:
157+
"""
158+
Get the location to use for the local Maven repository cache.
159+
"""
120160
global _m2_repo
121161
return _m2_repo
122162

123163

124-
def add_classpath(*path):
164+
def add_classpath(*path) -> None:
125165
"""
126166
Add elements to the Java class path.
127167
@@ -150,7 +190,7 @@ def add_classpath(*path):
150190
_jpype.addClassPath(p)
151191

152192

153-
def find_jars(directory):
193+
def find_jars(directory: Path | str) -> list[str]:
154194
"""
155195
Find .jar files beneath a given directory.
156196
@@ -166,11 +206,14 @@ def find_jars(directory):
166206
return jars
167207

168208

169-
def get_classpath():
209+
def get_classpath() -> str:
210+
"""
211+
Get the classpath to be passed to the JVM at startup.
212+
"""
170213
return _jpype.getClassPath()
171214

172215

173-
def set_heap_min(mb: int = None, gb: int = None):
216+
def set_heap_min(mb: int = None, gb: int = None) -> None:
174217
"""
175218
Set the initial amount of memory to allocate to the Java heap.
176219
@@ -187,7 +230,7 @@ def set_heap_min(mb: int = None, gb: int = None):
187230
add_option(f"-Xms{_mem_value(mb, gb)}")
188231

189232

190-
def set_heap_max(mb: int = None, gb: int = None):
233+
def set_heap_max(mb: int = None, gb: int = None) -> None:
191234
"""
192235
Shortcut for passing -Xmx###m or -Xmx###g to Java.
193236
@@ -210,7 +253,7 @@ def _mem_value(mb: int = None, gb: int = None) -> str:
210253
raise ValueError("Exactly one of mb or gb must be given.")
211254

212255

213-
def enable_headless_mode():
256+
def enable_headless_mode() -> None:
214257
"""
215258
Enable headless mode, for running Java without a display.
216259
This mode prevents any graphical elements from popping up.
@@ -239,29 +282,57 @@ def enable_remote_debugging(port: int = 8000, suspend: bool = False):
239282
add_option(f"-agentlib:jdwp={arg_string}")
240283

241284

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

246300

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

254314

255-
def get_options():
315+
def get_options() -> list[str]:
316+
"""
317+
Get the list of options to be passed at JVM startup.
318+
"""
256319
global _options
257320
return _options
258321

259322

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

264331

265-
def get_shortcuts():
332+
def get_shortcuts() -> dict[str, str]:
333+
"""
334+
Get the dictionary of shorts that jgo will use for evaluating endpoints.
335+
See the jgo documentation for details.
336+
"""
266337
global _shortcuts
267338
return _shortcuts

0 commit comments

Comments
 (0)