Skip to content

Commit f419e5c

Browse files
committed
args and cmds do not depend on global conf
1 parent e1b75f8 commit f419e5c

File tree

10 files changed

+58
-76
lines changed

10 files changed

+58
-76
lines changed

stagpy/__main__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ def main() -> None:
1212
if not DEBUG:
1313
signal.signal(signal.SIGINT, sigint_handler)
1414
warnings.simplefilter("ignore")
15-
from . import args, error
15+
from . import args, conf, error
1616

1717
try:
18-
args.parse_args()()
18+
args.parse_args(conf)(conf)
1919
except error.StagpyError as err:
2020
if DEBUG:
2121
raise

stagpy/args.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@
1616
from . import (
1717
_styles,
1818
commands,
19-
conf,
2019
field,
2120
plates,
2221
refstate,
2322
rprof,
2423
time_series,
2524
)
2625
from ._helpers import baredoc
26+
from .config import Config
2727

2828
if typing.TYPE_CHECKING:
2929
from typing import Any, Callable, List, Optional
@@ -35,13 +35,13 @@ def _sub(cmd: Any, *sections: str) -> Subcmd:
3535
return Subcmd(baredoc(cmd), *sections, func=cmd_func)
3636

3737

38-
def _bare_cmd() -> None:
38+
def _bare_cmd(conf: Config) -> None:
3939
"""Print help message when no arguments are given."""
4040
print(doc_module)
4141
print("Run `stagpy -h` for usage")
4242

4343

44-
def _load_mplstyle() -> None:
44+
def _load_mplstyle(conf: Config) -> None:
4545
"""Try to load conf.plot.mplstyle matplotlib style."""
4646
for style in conf.plot.mplstyle:
4747
style_fname = style + ".mplstyle"
@@ -71,11 +71,11 @@ def _load_mplstyle() -> None:
7171
)
7272

7373

74-
def parse_args(arglist: Optional[List[str]] = None) -> Callable[[], None]:
74+
def parse_args(
75+
conf: Config, arglist: Optional[List[str]] = None
76+
) -> Callable[[Config], None]:
7577
"""Parse cmd line arguments.
7678
77-
Update :attr:`stagpy.conf` accordingly.
78-
7979
Args:
8080
arglist: the list of cmd line arguments. If set to None, the arguments
8181
are taken from :attr:`sys.argv`.
@@ -84,7 +84,7 @@ def parse_args(arglist: Optional[List[str]] = None) -> Callable[[], None]:
8484
the function implementing the sub command to be executed.
8585
"""
8686

87-
def compl_cmd() -> None:
87+
def compl_cmd(conf: Config) -> None:
8888
if conf.completions.zsh:
8989
filepath = Path("_stagpy.sh")
9090
print(f"writing zsh completion file {filepath}")
@@ -111,6 +111,6 @@ def compl_cmd() -> None:
111111
if conf.common.config:
112112
commands.config_pp(climan.sections_list(sub_cmd), conf)
113113

114-
_load_mplstyle()
114+
_load_mplstyle(conf)
115115

116116
return cmd_args.func

stagpy/commands.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,12 @@
2121
from .datatypes import Varf, Varr, Vart
2222

2323

24-
def info_cmd() -> None:
24+
def info_cmd(conf: Config) -> None:
2525
"""Print basic information about StagYY run.
2626
2727
Other Parameters:
2828
conf.info
2929
"""
30-
from . import conf
31-
3230
sdat = stagyydata.StagyyData(conf.core.path)
3331
lsnap = sdat.snaps[-1]
3432
lstep = sdat.steps[-1]
@@ -114,14 +112,12 @@ def _layout(
114112
_pretty_print(desc, min_col_width=26)
115113

116114

117-
def var_cmd() -> None:
115+
def var_cmd(conf: Config) -> None:
118116
"""Print a list of available variables.
119117
120118
See :mod:`stagpy.phyvars` where the lists of variables organized by command
121119
are defined.
122120
"""
123-
from . import conf
124-
125121
print_all = not any(getattr(conf.var, fld.name) for fld in fields(conf.var))
126122
if print_all or conf.var.field:
127123
print("field:")
@@ -145,7 +141,7 @@ def var_cmd() -> None:
145141
print()
146142

147143

148-
def version_cmd() -> None:
144+
def version_cmd(conf: Config) -> None:
149145
"""Print StagPy version.
150146
151147
Use :data:`stagpy.__version__` to obtain the version in a script.
@@ -177,14 +173,12 @@ def config_pp(subs: Iterable[str], conf: Config) -> None:
177173
print()
178174

179175

180-
def config_cmd() -> None:
176+
def config_cmd(conf: Config) -> None:
181177
"""Configuration handling.
182178
183179
Other Parameters:
184180
conf.config
185181
"""
186-
from . import conf
187-
188182
if conf.config.create:
189183
conf.default_().to_file_(CONFIG_LOCAL)
190184
else:

stagpy/field.py

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -354,15 +354,8 @@ def _findminmax(
354354
return minmax
355355

356356

357-
def cmd() -> None:
358-
"""Implementation of field subcommand.
359-
360-
Other Parameters:
361-
conf.field
362-
conf.core
363-
"""
364-
from . import conf
365-
357+
def cmd(conf: Config) -> None:
358+
"""Implementation of field subcommand."""
366359
sdat = StagyyData(conf.core.path)
367360
view = _helpers.walk(sdat, conf)
368361
# no more than two fields in a subplot

stagpy/plates.py

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -394,17 +394,8 @@ def plot_scalar_field(
394394
saveplot(conf, fig, f"plates_zoom_{fieldname}", snap.isnap)
395395

396396

397-
def cmd() -> None:
398-
"""Implementation of plates subcommand.
399-
400-
Other Parameters:
401-
conf.plates
402-
conf.scaling
403-
conf.plot
404-
conf.core
405-
"""
406-
from . import conf
407-
397+
def cmd(conf: Config) -> None:
398+
"""Implementation of plates subcommand."""
408399
sdat = StagyyData(conf.core.path)
409400
view = _helpers.walk(sdat, conf)
410401

stagpy/refstate.py

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,8 @@ def plot_ref(sdat: StagyyData, var: str, conf: Optional[Config] = None) -> None:
4242
_helpers.saveplot(conf, fig, f"refstate_{var}")
4343

4444

45-
def cmd() -> None:
46-
"""Implementation of refstate subcommand.
47-
48-
Other Parameters:
49-
conf.core
50-
conf.plot
51-
"""
52-
from . import conf
53-
45+
def cmd(conf: Config) -> None:
46+
"""Implementation of refstate subcommand."""
5447
sdat = StagyyData(conf.core.path)
5548

5649
for var in conf.refstate.plot:

stagpy/rprof.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,15 +91,13 @@ def plot_grid(step: Step, conf: Optional[Config] = None) -> None:
9191
_helpers.saveplot(conf, fig, "grid", step.istep)
9292

9393

94-
def cmd() -> None:
94+
def cmd(conf: Config) -> None:
9595
"""Implementation of rprof subcommand.
9696
9797
Other Parameters:
9898
conf.rprof
9999
conf.core
100100
"""
101-
from . import conf
102-
103101
sdat = StagyyData(conf.core.path)
104102
view = _helpers.walk(sdat, conf)
105103

stagpy/time_series.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,15 +124,13 @@ def compstat(
124124
return stats
125125

126126

127-
def cmd() -> None:
127+
def cmd(conf: Config) -> None:
128128
"""Implementation of time subcommand.
129129
130130
Other Parameters:
131131
conf.time
132132
conf.core
133133
"""
134-
from . import conf
135-
136134
sdat = StagyyData(conf.core.path)
137135
if sdat.tseries is None:
138136
return

tests/test_args.py

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@
33
import pytest
44
from pytest import CaptureFixture
55

6-
import stagpy.args
6+
import stagpy
7+
from stagpy.args import parse_args
8+
from stagpy.config import Config
79

810

911
def test_no_args(capsys: CaptureFixture) -> None:
10-
stagpy.args.parse_args([])()
12+
conf = Config.default_()
13+
parse_args(conf, [])(conf)
1114
output = capsys.readouterr()
1215
expected = re.compile(
1316
r"StagPy is a tool to.*" r"Run `stagpy -h` for usage\n$", flags=re.DOTALL
@@ -16,8 +19,9 @@ def test_no_args(capsys: CaptureFixture) -> None:
1619

1720

1821
def test_help(capsys: CaptureFixture) -> None:
22+
conf = Config.default_()
1923
with pytest.raises(SystemExit):
20-
stagpy.args.parse_args(["-h"])
24+
parse_args(conf, ["-h"])
2125
output = capsys.readouterr()
2226
expected = re.compile(
2327
r"^usage:.*\nStagPy is a tool to.*\n"
@@ -29,8 +33,9 @@ def test_help(capsys: CaptureFixture) -> None:
2933

3034

3135
def test_invalid_argument(capsys: CaptureFixture) -> None:
36+
conf = Config.default_()
3237
with pytest.raises(SystemExit):
33-
stagpy.args.parse_args(["-dummyinvalidarg"])
38+
parse_args(conf, ["-dummyinvalidarg"])
3439
output = capsys.readouterr()
3540
expected = re.compile(
3641
r"^usage: .*error: unrecognized arguments:.*\n$", flags=re.DOTALL
@@ -39,48 +44,57 @@ def test_invalid_argument(capsys: CaptureFixture) -> None:
3944

4045

4146
def test_invalid_subcmd(capsys: CaptureFixture) -> None:
47+
conf = Config.default_()
4248
with pytest.raises(SystemExit):
43-
stagpy.args.parse_args(["dummyinvalidcmd"])
49+
parse_args(conf, ["dummyinvalidcmd"])
4450
output = capsys.readouterr()
4551
expected = re.compile(r"^usage: .*error:.*invalid choice:.*\n$", flags=re.DOTALL)
4652
assert expected.fullmatch(output.err)
4753

4854

4955
def test_field_subcmd() -> None:
50-
func = stagpy.args.parse_args(["field"])
56+
conf = Config.default_()
57+
func = parse_args(conf, ["field"])
5158
assert func is stagpy.field.cmd
5259

5360

5461
def test_rprof_subcmd() -> None:
55-
func = stagpy.args.parse_args(["rprof"])
62+
conf = Config.default_()
63+
func = parse_args(conf, ["rprof"])
5664
assert func is stagpy.rprof.cmd
5765

5866

5967
def test_time_cmd() -> None:
60-
func = stagpy.args.parse_args(["time"])
68+
conf = Config.default_()
69+
func = parse_args(conf, ["time"])
6170
assert func is stagpy.time_series.cmd
6271

6372

6473
def test_plates_subcmd() -> None:
65-
func = stagpy.args.parse_args(["plates"])
74+
conf = Config.default_()
75+
func = parse_args(conf, ["plates"])
6676
assert func is stagpy.plates.cmd
6777

6878

6979
def test_info_subcmd() -> None:
70-
func = stagpy.args.parse_args(["info"])
80+
conf = Config.default_()
81+
func = parse_args(conf, ["info"])
7182
assert func is stagpy.commands.info_cmd
7283

7384

7485
def test_var_subcmd() -> None:
75-
func = stagpy.args.parse_args(["var"])
86+
conf = Config.default_()
87+
func = parse_args(conf, ["var"])
7688
assert func is stagpy.commands.var_cmd
7789

7890

7991
def test_version_subcmd() -> None:
80-
func = stagpy.args.parse_args(["version"])
92+
conf = Config.default_()
93+
func = parse_args(conf, ["version"])
8194
assert func is stagpy.commands.version_cmd
8295

8396

8497
def test_config_subcmd() -> None:
85-
func = stagpy.args.parse_args(["config"])
98+
conf = Config.default_()
99+
func = parse_args(conf, ["config"])
86100
assert func is stagpy.commands.config_cmd

tests/test_commands.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,36 +3,37 @@
33

44
from pytest import CaptureFixture
55

6-
import stagpy.commands
6+
from stagpy import __version__, commands
7+
from stagpy.config import Config
78

89

910
def test_info_cmd(capsys: CaptureFixture, example_dir: Path) -> None:
10-
stagpy.conf.core.path = example_dir
11-
stagpy.commands.info_cmd()
11+
conf = Config.default_()
12+
conf.core.path = example_dir
13+
commands.info_cmd(conf)
1214
output = capsys.readouterr()
1315
expected = re.compile(
1416
r"^StagYY run in.*\n.* x .*\n\nStep.*, snapshot.*\n t.*\n\n$", flags=re.DOTALL
1517
)
1618
assert expected.fullmatch(output.out)
17-
del stagpy.conf.core.path
1819

1920

2021
def test_var_cmd(capsys: CaptureFixture) -> None:
21-
stagpy.commands.var_cmd()
22+
commands.var_cmd(Config.default_())
2223
output = capsys.readouterr()
2324
expected = re.compile(r"field:\n.*\nrprof:\n.*\ntime:\n.*\n.*$", flags=re.DOTALL)
2425
assert expected.fullmatch(output.out)
2526

2627

2728
def test_version_cmd(capsys: CaptureFixture) -> None:
28-
stagpy.commands.version_cmd()
29+
commands.version_cmd(Config.default_())
2930
output = capsys.readouterr()
30-
expected = "stagpy version: {}\n".format(stagpy.__version__)
31+
expected = "stagpy version: {}\n".format(__version__)
3132
assert output.out == expected
3233

3334

3435
def test_config_cmd(capsys: CaptureFixture) -> None:
35-
stagpy.commands.config_cmd()
36+
commands.config_cmd(Config.default_())
3637
output = capsys.readouterr()
3738
expected = "(c|f): available only as CLI argument/in the config file"
3839
assert output.out.startswith(expected)

0 commit comments

Comments
 (0)