8000 Be less aggressive with source code detection · scijava/scyjava@7485091 · GitHub
[go: up one dir, main page]

Skip to content

Commit 7485091

Browse files
committed
Be less aggressive with source code detection
When invoking scyjava.inspect functions, they can optionally report the source URL at the top, before printing the members. But this only works if scijava-search is on the classpath. Let's let the source flag default to None, in which case it swallows source code URL detection failures gracefully, to make the common case of scijava-search not being available work without hassle. And let's have the various inspect functions accept static and source boolean flags, which get passed along to the internal _print_data routine, as was previously the case when they were partial functions.
1 parent b142738 commit 7485091

File tree

2 files changed

+26
-13
lines changed

2 files changed

+26
-13
lines changed

src/scyjava/inspect.py

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,38 +7,42 @@
77
from scyjava import _introspect
88

99

10-
def members(data, writer=None):
10+
def members(data, static: bool | None = None, source: bool | None = None, writer=None):
1111
"""
1212
Print all the members (constructors, fields, and methods)
1313
for a Java class, object, or class name.
1414
1515
:param data: The Java class, object, or fully qualified class name as string.
1616
:param writer: Function to which output will be sent, sys.stdout.write by default.
1717
"""
18-
_print_data(data, aspect="all", writer=writer)
18+
_print_data(data, aspect="all", static=static, source=source, writer=writer)
1919

2020

21-
def constructors(data, writer=None):
21+
def constructors(
22+
data, static: bool | None = None, source: bool | None = None, writer=None
23+
):
2224
"""
2325
Print the constructors for a Java class, object, or class name.
2426
2527
:param data: The Java class, object, or fully qualified class name as string.
2628
:param writer: Function to which output will be sent, sys.stdout.write by default.
2729
"""
28-
_print_data(data, aspect="constructors", writer=writer)
30+
_print_data(
31+
data, aspect="constructors", static=static, source=source, writer=writer
32+
)
2933

3034

31-
def fields(data, writer=None):
35+
def fields(data, static: bool | None = None, source: bool | None = None, writer=None):
3236
"""
3337
Print the fields for a Java class, object, or class name.
3438
3539
:param data: The Java class, object, or fully qualified class name as string.
3640
:param writer: Function to which output will be sent, sys.stdout.write by default.
3741
"""
38-
_print_data(data, aspect="fields", writer=writer)
42+
_print_data(data, aspect="fields", static=static, source=source, writer=writer)
3943

4044

41-
def methods(data, writer=None):
45+
def methods(data, static: bool | None = None, source: bool | None = None, writer=None):
4246
"""
4347
Print the methods for a Java class, object, or class name.
4448
@@ -115,7 +119,7 @@ def _pretty_string(entry, offset):
115119

116120

117121
def _print_data(
118-
data, aspect, static: bool | None = None, source: bool = True, writer=None
122+
data, aspect, static: bool | None = None, source: bool | None = None, writer=None
119123
):
120124
"""
121125
Write data to a printed table with inputs, static modifier,
@@ -125,7 +129,11 @@ def _print_data(
125129
:param static:
126130
Boolean filter on Static or Instance methods.
127131
Optional, default is None (prints all).
128-
:param source: Whether to print any available source code. Default True.
132+
:param source:
133+
Whether to discern and report a URL to the relevant source code.
134+
Requires org.scijava:scijava-search to be on the classpath.
135+
When set to None (the default), autodetects whether scijava-search
136+
is available, reporting source URL if so, or leaving it out if not.
129137
"""
130138
writer = writer or _stdout.write
131139
table = _introspect.jreflect(data, aspect)
@@ -136,9 +144,15 @@ def _print_data(
136144
# Print source code
137145
offset = max(list(map(lambda entry: len(entry["returns"] or "void"), table)))
138146
all_methods = ""
139-
if source:
140-
urlstring = _introspect.jsource(data)
141-
writer(f"Source code URL: {urlstring}\n")
147+
if source or source is None:
148+
try:
149+
urlstring = _introspect.jsource(data)
150+
writer(f"Source code URL: {urlstring}\n")
151+
except TypeError:
152+
if source:
153+
writer(
154+
"Classpath lacks scijava-search; no source code URL detection is available.\n"
155+
)
142156

143157
# Print methods
144158
for entry in table:

tests/test_inspect.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ def test_inspect_members(self):
1818
members = []
1919
inspect.members("java.lang.Iterable", writer=members.append)
2020
expected = [
21-
"Source code URL: java.lang.NullPointerException",
2221
" * indicates static modifier",
2322
"java.util.Iterator = iterator()",
2423
"java.util.Spliterator = spliterator()",

0 commit comments

Comments
 (0)
0