Skip to content

Commit 5bc7904

Browse files
germa89akaszynski
andcommitted
Removing scooby dependency (#1092)
* fixing forgotten pyvista import * Added base class for reporting. So scooby is not enforced. * Removing self * Fixing code coverage * Fixing unit tests. * Improving coverage * Update src/ansys/mapdl/core/misc.py Co-authored-by: Alex Kaszynski <[email protected]> (cherry picked from commit cc11149)
1 parent 0af0be6 commit 5bc7904

File tree

2 files changed

+188
-8
lines changed

2 files changed

+188
-8
lines changed

src/ansys/mapdl/core/misc.py

Lines changed: 96 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,16 @@
1111
from threading import Thread
1212

1313
import numpy as np
14-
import scooby
14+
15+
from ansys.mapdl.core import _HAS_PYVISTA, LOG
16+
17+
try:
18+
import scooby
19+
20+
_HAS_SCOOBY = True
21+
except ModuleNotFoundError: # pragma: no cover
22+
LOG.debug("The module 'scooby' is not installed.")
23+
_HAS_SCOOBY = False
1524

1625
# path of this module
1726
MODULE_PATH = os.path.dirname(inspect.getfile(inspect.currentframe()))
@@ -33,7 +42,87 @@ def get_ansys_bin(rver):
3342
return mapdlbin
3443

3544

36-
class Report(scooby.Report):
45+
class Plain_Report:
46+
def __init__(self, core, optional=None, additional=None, **kwargs):
47+
"""
48+
Base class for a plain report.
49+
50+
51+
Based on `scooby <https://github.com/banesullivan/scooby>`_ package.
52+
53+
Parameters
54+
----------
55+
additional : iter[str]
56+
List of packages or package names to add to output information.
57+
core : iter[str]
58+
The core packages to list first.
59+
optional : iter[str]
60+
A list of packages to list if they are available. If not available,
61+
no warnings or error will be thrown.
62+
"""
63+
64+
self.additional = additional
65+
self.core = core
66+
self.optional = optional
67+
self.kwargs = kwargs
68+
69+
def get_version(self, package):
70+
from importlib.metadata import PackageNotFoundError
71+
from importlib.metadata import version as get_lib_version
72+
73+
package = package.replace(".", "-")
74+
75+
try:
76+
return get_lib_version(package)
77+
except PackageNotFoundError:
78+
return "Package not found"
79+
80+
def __repr__(self):
81+
header = ["\n", "Packages Requirements", "*********************"]
82+
83+
core = ["\nCore packages", "-------------"]
84+
core.extend(
85+
[
86+
f"{each.ljust(20)}: {self.get_version(each)}"
87+
for each in self.core
88+
if self.get_version(each)
89+
]
90+
)
91+
92+
if self.optional:
93+
optional = ["\nOptional packages", "-----------------"]
94+
optional.extend(
95+
[
96+
f"{each.ljust(20)}: {self.get_version(each)}"
97+
for each in self.optional
98+
if self.get_version(each)
99+
]
100+
)
101+
else:
102+
optional = [""]
103+
104+
if self.additional:
105+
additional = ["\nAdditional packages", "-----------------"]
106+
additional.extend(
107+
[
108+
f"{each.ljust(20)}: {self.get_version(each)}"
109+
for each in self.additional
110+
if self.get_version(each)
111+
]
112+
)
113+
else:
114+
additional = [""]
115+
116+
return "\n".join(header + core + optional + additional)
117+
118+
119+
if _HAS_SCOOBY:
120+
base_report_class = scooby.Report
121+
else: # pragma: no cover
122+
base_report_class = Plain_Report
123+
124+
125+
class Report(base_report_class):
37126
"""A class for custom scooby.Report."""
38127

39128
def __init__(self, additional=None, ncol=3, text_width=80, sort=False, gpu=True):
@@ -56,7 +145,7 @@ def __init__(self, additional=None, ncol=3, text_width=80, sort=False, gpu=True)
56145
57146
gpu : bool
58147
Gather information about the GPU. Defaults to ``True`` but if
59-
experiencing renderinng issues, pass ``False`` to safely generate
148+
experiencing rendering issues, pass ``False`` to safely generate
60149
a report.
61150
62151
"""
@@ -83,18 +172,17 @@ def __init__(self, additional=None, ncol=3, text_width=80, sort=False, gpu=True)
83172

84173
# Information about the GPU - bare except in case there is a rendering
85174
# bug that the user is trying to report.
86-
if gpu:
175+
if gpu and _HAS_PYVISTA:
87176
from pyvista.utilities.errors import GPUInfo
88177

89178
try:
90179
extra_meta = [(t[1], t[0]) for t in GPUInfo().get_info()]
91-
except:
92-
extra_meta = ("GPU Details", "error")
180+
except Exception as e: # pragma: no cover
181+
extra_meta = ("GPU Details", f"Error: {e.message}")
93182
else:
94183
extra_meta = ("GPU Details", "None")
95184

96-
scooby.Report.__init__(
97-
self,
185+
super().__init__(
98186
additional=additional,
99187
core=core,
100188
optional=optional,

tests/test_misc.py

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,95 @@ def test_check_valid_port(port):
6262
)
6363
def test_check_valid_start_instance(start_instance):
6464
check_valid_start_instance(start_instance)
65+
66+
67+
def test_get_ansys_bin(mapdl):
68+
rver = mapdl.__str__().splitlines()[1].split(":")[1].strip().replace(".", "")
69+
assert isinstance(get_ansys_bin(rver), str)
70+
71+
72+
def test_mapdl_info(mapdl, capfd):
73+
info = mapdl.info
74+
for attr, value in inspect.getmembers(info):
75+
if not attr.startswith("_") and attr not in ["title", "stitles"]:
76+
assert isinstance(value, str)
77+
78+
with pytest.raises(AttributeError):
79+
setattr(info, attr, "any_value")
80+
81+
assert "PyMAPDL" in mapdl.info.__repr__()
82+
out = info.__str__()
83+
84+
assert "ansys" in out.lower()
85+
assert "Product" in out
86+
assert "MAPDL Version" in out
87+
assert "UPDATE" in out
88+
89+
90+
def test_info_title(mapdl):
91+
title = "this is my title"
92+
mapdl.info.title = title
93+
assert title == mapdl.info.title
94+
95+
96+
def test_info_stitle(mapdl):
97+
info = mapdl.info
98+
99+
assert not info.stitles
100+
stitles = ["asfd", "qwer", "zxcv", "jkl"]
101+
info.stitles = "\n".join(stitles)
102+
103+
assert stitles == info.stitles
104+
105+
stitles = stitles[::-1]
106+
107+
info.stitles = stitles
108+
assert stitles == info.stitles
109+
110+
info.stitles = None
111+
assert not info.stitles
112+
113+
114+
def test_plain_report():
115+
from ansys.mapdl.core.misc import Plain_Report
116+
117+
core = ["numpy", "ansys.mapdl.reader"]
118+
optional = ["pyvista", "tqdm"]
119+
additional = ["scipy", "ger"]
120+
121+
report = Plain_Report(core=core, optional=optional, additional=additional)
122+
rep_str = report.__repr__()
123+
124+
for each in core + optional + additional:
125+
assert each in rep_str
126+
127+
# There should be only one package not found ("ger")
128+
assert "Package not found" in rep_str
129+
_rep_str = rep_str.replace("Package not found", "", 1)
130+
assert "Package not found" not in _rep_str
131+
132+
assert "\n" in rep_str
133+
assert len(rep_str.splitlines()) > 3
134+
135+
assert "Core packages" in rep_str
136+
assert "Optional packages" in rep_str
137+
assert "Additional packages" in rep_str
138+
139+
140+
def test_plain_report_no_options():
141+
from ansys.mapdl.core.misc import Plain_Report
142+
143+
core = ["numpy", "ansys.mapdl.reader"]
144+
145+
report = Plain_Report(core=core)
146+
rep_str = report.__repr__()
147+
148+
for each in core:
149+
assert each in rep_str
150+
151+
assert "\n" in rep_str
152+
assert len(rep_str.splitlines()) > 3
153+
154+
assert "Core packages" in rep_str
155+
assert "Optional packages" not in rep_str
156+
assert "Additional packages" not in rep_str

0 commit comments

Comments
 (0)