Skip to content

Commit e7856b9

Browse files
authored
Merge pull request #88 from StagPython/rm-global-config
Remove global configuration folder
2 parents daa14d9 + 62dba12 commit e7856b9

File tree

13 files changed

+65
-126
lines changed

13 files changed

+65
-126
lines changed

docs/conf.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,6 @@
2020

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

23-
# do not try to create/read config files
24-
os.environ["STAGPY_ISOLATED"] = "True"
25-
2623
import stagpy
2724

2825
html_theme = "sphinx_rtd_theme"

docs/sources/apiref/config.rst

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,6 @@ config
44
.. automodule:: stagpy.config
55
:members:
66

7-
.. data:: HOME_DIR
8-
:annotation: = pathlib.Path.home()
9-
10-
Home directory.
11-
12-
.. data:: CONFIG_DIR
13-
:annotation: = HOME_DIR / '.config' / 'stagpy'
14-
15-
StagPy configuration directory.
16-
17-
.. data:: CONFIG_FILE
18-
:annotation: = CONFIG_DIR / 'config.toml'
19-
20-
Path of global configuration file.
21-
227
.. data:: CONFIG_LOCAL
238
:annotation: = pathlib.Path('.stagpy.toml')
249

docs/sources/apiref/parfile.rst

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,6 @@ parfile
44
.. automodule:: stagpy.parfile
55
:members:
66

7-
.. data:: PAR_DFLT_FILE
8-
:annotation: = config.CONFIG_DIR / 'par'
9-
10-
Path of default par file used to fill missing entries by :func:`readpar`.
11-
127
.. data:: PAR_DEFAULT
138

149
Defaut value of all the StagYY input parameters used to fill missing

docs/sources/cli.rst

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,5 @@ is displayed.
5353

5454
.. option:: --create
5555

56-
Create a new config file from scratch. This is useful if your configuration
57-
file is broken or you want to reset the modifications you made to your
58-
configuration.
59-
60-
.. option:: --update
61-
62-
Add missing entries to your config file (or create a new one if necessary).
63-
64-
.. option:: --edit
65-
66-
Open your config file in ``vim``.
56+
Create a new config file from scratch.
6757

docs/sources/install.rst

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,22 @@ for more information about installing Python packages.
3232
Some setup
3333
----------
3434

35-
Run the following once to create your config file (in ``~/.config/stagpy/``)::
35+
Run the following to create a local config file (``.stagpy.toml``)::
3636

3737
% stagpy config --create
3838

3939
You can enable command-line auto-completion if you use either bash or zsh.
4040

41-
Add this to your ``~/.bashrc`` file::
41+
For bash::
4242

43-
source ~/.config/stagpy/bash/stagpy.sh
43+
# adapt path as appropriate for your system
44+
% mkdir -p ~/.local/share/bash-completion/completions
45+
% cd !$
46+
% stagpy completions --bash
4447

45-
Or this to your ``~/.zshrc`` file::
48+
For zsh, with ``fpath+=~/.zfunc`` in your ``.zshrc``::
4649

47-
source ~/.config/stagpy/zsh/_stagpy.sh
50+
% cd ~/.zfunc
51+
% stagpy completions --zsh
4852

4953
Enjoy!

docs/sources/stagyydata.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ Parameters set in the ``par`` file are accessible through the
8181
a dictionary of dictionaries. For example, to access the Rayleigh number from
8282
the ``refstate`` section of the par file, one can use
8383
``sdat.par['refstate']['ra0']``. Parameters that are not set in the par file
84-
are given a default value according to the par file ``~/.config/stagpy/par``.
84+
are given a default value.
8585

8686
Radial profiles
8787
---------------

docs/sources/tuto.rst

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -112,13 +112,13 @@ use the :class:`~stagpy.stagyydata.StagyyData` class.
112112
Plotting style
113113
--------------
114114

115-
StagPy defines two custom plotting styles for matplotlib, ``stagpy-paper`` (the
116-
default) and ``stagpy-slides``. You can edit them to your convenience, they
117-
are in the ``~/.config/stagpy`` directory. You can specify which style to use
118-
with ``plot.mplstyle``, available in the command line interface with the
119-
``--mplstyle`` option. You can specify a space-separated list to combine
120-
several styles. For example, if you want a dark-background figure with a
121-
font size adapted for slides, you can use the following command::
122-
123-
% stagpy field --mplstyle='dark_background stagpy-slides'
115+
For convenience, StagPy defines two custom plotting styles for matplotlib,
116+
``stagpy-paper`` (the default when using the CLI) and ``stagpy-slides``. You
117+
can specify which style to use with ``plot.mplstyle``, available in the command
118+
line interface with the ``--mplstyle`` option. You can specify a
119+
comma-separated list to combine several styles. For example, if you want a
120+
dark-background figure with a font size adapted for slides, you can use the
121+
following command::
122+
123+
% stagpy field --mplstyle='dark_background,stagpy-slides'
124124

stagpy/__init__.py

Lines changed: 4 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@
44
documentation at
55
https://stagpy.readthedocs.io/en/stable/
66
7-
If the environment variable STAGPY_ISOLATED is set to a truthy value, StagPy
8-
does not attempt to read any configuration file (including mplstyle).
9-
107
When using the CLI interface, if the environment variable STAGPY_DEBUG is set
118
to a truthy value, warnings are issued normally and StagpyError are raised.
129
Otherwise, warnings are ignored and only a short form of encountered
@@ -18,17 +15,15 @@
1815

1916
from __future__ import annotations
2017

21-
import importlib.resources as imlr
2218
import os
23-
import shutil
2419
import signal
2520
import sys
2621
import typing
2722

28-
from . import _styles, config
23+
from . import config
2924

3025
if typing.TYPE_CHECKING:
31-
from typing import Any, Iterator, NoReturn
26+
from typing import Any, NoReturn
3227

3328

3429
def _env(var: str) -> bool:
@@ -38,7 +33,6 @@ def _env(var: str) -> bool:
3833

3934

4035
DEBUG = _env("STAGPY_DEBUG")
41-
ISOLATED = _env("STAGPY_ISOLATED")
4236

4337

4438
def sigint_handler(*_: Any) -> NoReturn:
@@ -51,28 +45,6 @@ def sigint_handler(*_: Any) -> NoReturn:
5145
sys.exit()
5246

5347

54-
def _iter_styles() -> Iterator[str]:
55-
for resource in imlr.contents(_styles):
56-
if resource.endswith(".mplstyle"):
57-
yield resource
58-
59-
60-
def _check_config() -> None:
61-
"""Create config files as necessary."""
62-
config.CONFIG_DIR.mkdir(parents=True, exist_ok=True)
63-
verfile = config.CONFIG_DIR / ".version"
64-
uptodate = verfile.is_file() and verfile.read_text() == __version__
65-
if not uptodate:
66-
verfile.write_text(__version__)
67-
if not (uptodate and config.CONFIG_FILE.is_file()):
68-
conf.to_file_(config.CONFIG_FILE)
69-
for stfile in _iter_styles():
70-
stfile_conf = config.CONFIG_DIR / stfile
71-
if not (uptodate and stfile_conf.is_file()):
72-
with imlr.path(_styles, stfile) as stfile_local:
73-
shutil.copy(str(stfile_local), str(stfile_conf))
74-
75-
7648
if DEBUG:
7749
print(
7850
"StagPy runs in DEBUG mode because the environment variable",
@@ -89,11 +61,8 @@ def _check_config() -> None:
8961
__version__ = "unknown"
9062

9163
conf = config.Config.default_()
92-
if not ISOLATED:
93-
_check_config()
94-
conf.update_from_file_(config.CONFIG_FILE)
95-
if config.CONFIG_LOCAL.is_file():
96-
conf.update_from_file_(config.CONFIG_LOCAL)
64+
if config.CONFIG_LOCAL.is_file():
65+
conf.update_from_file_(config.CONFIG_LOCAL)
9766

9867
if not DEBUG:
9968
signal.signal(signal.SIGINT, _PREV_INT)

stagpy/args.py

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,25 @@
55
import importlib.resources as imlr
66
import typing
77
from inspect import isfunction
8+
from pathlib import Path
89
from types import MappingProxyType
910

1011
import matplotlib.pyplot as plt
1112
import matplotlib.style as mpls
1213
from loam.cli import CLIManager, Subcmd
1314

15+
from . import __doc__ as doc_module
1416
from . import (
15-
ISOLATED,
1617
_styles,
1718
commands,
1819
conf,
19-
config,
2020
field,
2121
plates,
2222
refstate,
2323
rprof,
2424
time_series,
2525
)
26-
from . import __doc__ as doc_module
2726
from ._helpers import baredoc
28-
from .config import CONFIG_DIR
2927

3028
if typing.TYPE_CHECKING:
3129
from typing import Any, Callable, List, Optional
@@ -47,11 +45,6 @@ def _load_mplstyle() -> None:
4745
"""Try to load conf.plot.mplstyle matplotlib style."""
4846
for style in conf.plot.mplstyle:
4947
style_fname = style + ".mplstyle"
50-
if not ISOLATED:
51-
stfile = config.CONFIG_DIR / style_fname
52-
if stfile.is_file():
53-
mpls.use(str(stfile))
54-
continue
5548
# try packaged version
5649
if imlr.is_resource(_styles, style_fname):
5750
with imlr.path(_styles, style_fname) as stfile:
@@ -90,14 +83,24 @@ def parse_args(arglist: Optional[List[str]] = None) -> Callable[[], None]:
9083
Returns:
9184
the function implementing the sub command to be executed.
9285
"""
93-
climan = CLIManager(conf, **SUB_CMDS)
94-
95-
bash_script = CONFIG_DIR / "bash" / "stagpy.sh"
96-
bash_script.parent.mkdir(parents=True, exist_ok=True)
97-
climan.bash_complete(bash_script, "stagpy")
98-
zsh_script = CONFIG_DIR / "zsh" / "_stagpy.sh"
99-
zsh_script.parent.mkdir(parents=True, exist_ok=True)
100-
climan.zsh_complete(zsh_script, "stagpy", sourceable=True)
86+
87+
def compl_cmd() -> None:
88+
if conf.completions.zsh:
89+
filepath = Path("_stagpy.sh")
90+
print(f"writing zsh completion file {filepath}")
91+
climan.zsh_complete(filepath, "stagpy", sourceable=True)
92+
elif conf.completions.bash:
93+
filepath = Path("stagpy.sh")
94+
print(f"writing bash completion file {filepath}")
95+
climan.bash_complete(filepath, "stagpy")
96+
else:
97+
print("please choose a shell, `--help` for available options")
98+
99+
climan = CLIManager(
100+
conf,
101+
**SUB_CMDS,
102+
completions=Subcmd("generate completion scripts", func=compl_cmd),
103+
)
101104

102105
cmd_args = climan.parse_args(arglist)
103106
sub_cmd = cmd_args.loam_sub_name

stagpy/commands.py

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,11 @@
99
from shutil import get_terminal_size
1010
from textwrap import TextWrapper, indent
1111

12-
import loam.tools
1312
import pandas
1413

1514
from . import __version__, conf, phyvars, stagyydata
1615
from ._helpers import baredoc
17-
from .config import CONFIG_FILE
16+
from .config import CONFIG_LOCAL
1817

1918
if typing.TYPE_CHECKING:
2019
from typing import Callable, Iterable, Mapping, Optional, Sequence, Tuple, Union
@@ -200,11 +199,7 @@ def config_cmd() -> None:
200199
Other Parameters:
201200
conf.config
202201
"""
203-
if not (
204-
conf.common.config
205-
or conf.config.create
206-
or conf.config.update
207-
or conf.config.edit
208-
):
202+
if conf.config.create:
203+
conf.default_().to_file_(CONFIG_LOCAL)
204+
else:
209205
config_pp(sec.name for sec in fields(conf))
210-
loam.tools.config_cmd_handler(conf, conf.config, CONFIG_FILE)

0 commit comments

Comments
 (0)