Skip to content

Commit ff0fcb7

Browse files
authored
Merge pull request #89 from StagPython/globconf
remove global `Config` instance and `scaling.dimensional`
2 parents a9d0a23 + 2454ca1 commit ff0fcb7

28 files changed

+425
-475
lines changed

Examples/annulus/par

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
&switches
2+
dimensional_units = .false.
23
/
34

45
&geometry

docs/conf.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
sys.path.insert(0, os.path.abspath(".."))
2222

23-
import stagpy
23+
from stagpy.config import Config
2424

2525
html_theme = "sphinx_rtd_theme"
2626

@@ -93,7 +93,8 @@
9393
"""
9494
)
9595
)
96-
for sec_fld in fields(stagpy.conf):
96+
stagpy_conf = Config.default_()
97+
for sec_fld in fields(stagpy_conf):
9798
sec_name = sec_fld.name
9899
fid.write(
99100
dedent(
@@ -107,7 +108,7 @@
107108
""".format(sec_name)
108109
)
109110
)
110-
section = getattr(stagpy.conf, sec_name)
111+
section = getattr(stagpy_conf, sec_name)
111112
for fld in fields(section):
112113
opt = fld.name
113114
entry = section.meta_(opt).entry

docs/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ Welcome to StagPy's documentation!
4343
sources/apiref/commands
4444
sources/apiref/config
4545
sources/apiref/datatypes
46+
sources/apiref/dimensions
4647
sources/apiref/error
4748
sources/apiref/field
4849
sources/apiref/parfile

docs/sources/apiref/dimensions.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
dimensions
2+
==========
3+
4+
.. automodule:: stagpy.dimensions
5+
:members:

docs/sources/apiref/phyvars.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,4 @@ phyvars
5656

5757
Dictionary mapping dimension strings (**dim** fields in ``Var*``) to
5858
functions which are themselves mapping from
59-
:class:`~stagpy.stagyydata.StagyyData` to the scale for that dimension.
59+
:class:`~stagpy.dimensions.Scales` to the scale for that dimension.

docs/sources/apiref/stagpy.rst

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,3 @@ stagpy
1212
commits since the last stable version and the last commit hash. If you
1313
made modifications to the code that are not committed yet, the date of
1414
the last modification appears in the ``'.dYYYYMMDD'`` segment.
15-
16-
.. data:: conf
17-
18-
Global :class:`loam.manager.ConfigurationManager` instance, holding
19-
configuration options.

docs/sources/apiref/stagyydata.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@ stagyydata
22
=============
33

44
.. automodule:: stagpy.stagyydata
5-
:members: _Scales, _Refstate, _Tseries, _RprofsAveraged, _Steps, _Snaps,
6-
_StepsView, StagyyData
5+
:members: StagyyData
6+
:private-members: _Refstate, _Tseries, _RprofsAveraged, _Steps, _Snaps, _StepsView

stagpy/__init__.py

Lines changed: 3 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -4,65 +4,12 @@
44
documentation at
55
https://stagpy.readthedocs.io/en/stable/
66
7-
When using the CLI interface, if the environment variable STAGPY_DEBUG is set
8-
to a truthy value, warnings are issued normally and StagpyError are raised.
9-
Otherwise, warnings are ignored and only a short form of encountered
10-
StagpyErrors is printed.
11-
12-
Truthy values for environment variables are 'true', 't', 'yes', 'y', 'on', '1',
13-
and uppercase versions of those.
7+
When using the CLI interface, warnings are ignored and only a short form of
8+
encountered StagpyErrors is printed. Set the environment variable STAGPY_DEBUG
9+
to issue warnings normally and raise StagpyError.
1410
"""
1511

16-
from __future__ import annotations
17-
18-
import os
19-
import signal
20-
import sys
21-
import typing
22-
23-
from . import config
24-
25-
if typing.TYPE_CHECKING:
26-
from typing import Any, NoReturn
27-
28-
29-
def _env(var: str) -> bool:
30-
"""Return whether var is set to True."""
31-
val = os.getenv(var, default="").lower()
32-
return val in ("true", "t", "yes", "y", "on", "1")
33-
34-
35-
DEBUG = _env("STAGPY_DEBUG")
36-
37-
38-
def sigint_handler(*_: Any) -> NoReturn:
39-
"""Handler of SIGINT signal.
40-
41-
It is set when you use StagPy as a command line tool to handle gracefully
42-
keyboard interruption.
43-
"""
44-
print("\nSo long, and thanks for all the fish.")
45-
sys.exit()
46-
47-
48-
if DEBUG:
49-
print(
50-
"StagPy runs in DEBUG mode because the environment variable",
51-
'STAGPY_DEBUG is set to "True"',
52-
sep="\n",
53-
end="\n\n",
54-
)
55-
else:
56-
_PREV_INT = signal.signal(signal.SIGINT, sigint_handler)
57-
5812
try:
5913
from ._version import version as __version__
6014
except ImportError:
6115
__version__ = "unknown"
62-
63-
conf = config.Config.default_()
64-
if config.CONFIG_LOCAL.is_file():
65-
conf.update_from_file_(config.CONFIG_LOCAL)
66-
67-
if not DEBUG:
68-
signal.signal(signal.SIGINT, _PREV_INT)

stagpy/__main__.py

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,46 @@
11
"""The stagpy module is callable."""
22

3+
from __future__ import annotations
4+
5+
import os
36
import signal
47
import sys
58
import warnings
9+
from typing import Any, NoReturn
10+
611

7-
from . import DEBUG, sigint_handler
12+
def sigint_handler(*_: Any) -> NoReturn:
13+
"""Handler of SIGINT signal.
14+
15+
It is set when you use StagPy as a command line tool to handle gracefully
16+
keyboard interruption.
17+
"""
18+
print("\nSo long, and thanks for all the fish.")
19+
sys.exit()
820

921

1022
def main() -> None:
1123
"""Implement StagPy entry point."""
12-
if not DEBUG:
24+
debug = os.getenv("STAGPY_DEBUG") is not None
25+
if debug:
26+
print(
27+
"env variable 'STAGPY_DEBUG' is set: StagPy runs in DEBUG mode",
28+
end="\n\n",
29+
)
30+
else:
1331
signal.signal(signal.SIGINT, sigint_handler)
1432
warnings.simplefilter("ignore")
15-
from . import args, error
33+
34+
from . import args, config, error
35+
36+
conf = config.Config.default_()
37+
if config.CONFIG_LOCAL.is_file():
38+
conf.update_from_file_(config.CONFIG_LOCAL)
1639

1740
try:
18-
args.parse_args()()
41+
args.parse_args(conf)(conf)
1942
except error.StagpyError as err:
20-
if DEBUG:
43+
if debug:
2144
raise
2245
errtype = type(err).__name__
2346
print(

stagpy/_helpers.py

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,24 @@
77

88
import matplotlib.pyplot as plt
99

10-
from . import conf
11-
1210
if typing.TYPE_CHECKING:
1311
from typing import Any, Optional
1412

1513
from matplotlib.figure import Figure
1614
from numpy import ndarray
1715

16+
from .config import Config
17+
from .stagyydata import StagyyData, _StepsView
18+
19+
20+
def walk(sdat: StagyyData, conf: Config) -> _StepsView:
21+
"""Return view on configured steps slice."""
22+
if conf.core.timesteps:
23+
return sdat.steps[conf.core.timesteps]
24+
return sdat.snaps[conf.core.snapshots]
25+
1826

19-
def out_name(stem: str, timestep: Optional[int] = None) -> str:
27+
def out_name(conf: Config, stem: str, timestep: Optional[int] = None) -> str:
2028
"""Return StagPy out file name.
2129
2230
Args:
@@ -25,9 +33,6 @@ def out_name(stem: str, timestep: Optional[int] = None) -> str:
2533
2634
Returns:
2735
the output file name.
28-
29-
Other Parameters:
30-
conf.core.outname: the generic name stem, defaults to ``'stagpy'``.
3136
"""
3237
if conf.core.shortname:
3338
return conf.core.outname
@@ -54,7 +59,11 @@ def scilabel(value: float, precision: int = 2) -> str:
5459

5560

5661
def saveplot(
57-
fig: Figure, *name_args: Any, close: bool = True, **name_kwargs: Any
62+
conf: Config,
63+
fig: Figure,
64+
stem: str,
65+
timestep: Optional[int] = None,
66+
close: bool = True,
5867
) -> None:
5968
"""Save matplotlib figure.
6069
@@ -63,11 +72,11 @@ def saveplot(
6372
6473
Args:
6574
fig: the :class:`matplotlib.figure.Figure` to save.
75+
stem: short description of file content.
76+
timestep: timestep if relevant.
6677
close: whether to close the figure.
67-
name_args: positional arguments passed on to :func:`out_name`.
68-
name_kwargs: keyword arguments passed on to :func:`out_name`.
6978
"""
70-
oname = out_name(*name_args, **name_kwargs)
79+
oname = out_name(conf, stem, timestep)
7180
fig.savefig(
7281
f"{oname}.{conf.plot.format}", format=conf.plot.format, bbox_inches="tight"
7382
)

0 commit comments

Comments
 (0)