Skip to content

Commit

Permalink
ENH: do not automatically create a yt configuration file
Browse files Browse the repository at this point in the history
  • Loading branch information
neutrinoceros committed Dec 6, 2021
1 parent d55e43a commit 64675fe
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 27 deletions.
12 changes: 0 additions & 12 deletions yt/config.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import os
import warnings

import toml

from yt.utilities.configure import YTConfig, config_dir, configuration_callbacks

Expand Down Expand Up @@ -68,15 +65,6 @@
_global_config_file = YTConfig.get_global_config_file()
_local_config_file = YTConfig.get_local_config_file()

if not os.path.exists(_global_config_file):
cfg = {"yt": {}} # type: ignore
try:
with open(_global_config_file, mode="w") as fd:
toml.dump(cfg, fd)
except OSError:
warnings.warn("unable to write new config file")


# Load the config
ytcfg = YTConfig()
ytcfg.update(ytcfg_defaults, metadata={"source": "defaults"})
Expand Down
4 changes: 3 additions & 1 deletion yt/fields/tests/test_fields_plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
import unittest

import yt
from yt.config import YTConfig, config_dir, ytcfg
from yt.config import ytcfg
from yt.testing import assert_raises, fake_random_ds
from yt.utilities.configure import YTConfig, config_dir

_TEST_PLUGIN = "_test_plugin.py"

Expand Down Expand Up @@ -35,6 +36,7 @@ class TestPluginFile(unittest.TestCase):
def setUpClass(cls):
cls.xdg_config_home = os.environ.get("XDG_CONFIG_HOME")
cls.tmpdir = tempfile.mkdtemp()
os.mkdir(os.path.join(cls.tmpdir, "yt"))
os.environ["XDG_CONFIG_HOME"] = cls.tmpdir
with open(YTConfig.get_global_config_file(), mode="w") as fh:
fh.write(_DUMMY_CFG_TOML)
Expand Down
21 changes: 14 additions & 7 deletions yt/utilities/command_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -1330,6 +1330,7 @@ def load_config(self, args):
elif getattr(args, "global", False):
config_file = global_config_file
else:
config_file: Optional[str] = None
if local_exists and global_exists:
s = (
"Yt detected a local and a global configuration file, refusing "
Expand All @@ -1347,14 +1348,15 @@ def load_config(self, args):
sys.exit(s)
elif local_exists:
config_file = local_config_file
else:
elif global_exists:
config_file = global_config_file
sys.stderr.write(f"INFO: using configuration file: {config_file}.\n")

if not os.path.exists(config_file):
with open(config_file, "w") as f:
f.write("[yt]\n")

if config_file is None:
print("WARNING: no configuration file installed.", file=sys.stderr)
else:
print(
f"INFO: reading configuration file: {config_file}", file=sys.stderr
)
CONFIG.read(config_file)

self.config_file = config_file
Expand Down Expand Up @@ -1407,7 +1409,12 @@ def __call__(self, args):
from yt.utilities.configure import set_config

self.load_config(args)

if self.config_file is None:
self.config_file = os.path.join(os.getcwd(), "yt.toml")
print(
f"INFO: configuration will be written to {self.config_file}",
file=sys.stderr,
)
set_config(args.section, args.option, args.value, self.config_file)


Expand Down
7 changes: 0 additions & 7 deletions yt/utilities/configure.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import os
import warnings
from types import FunctionType
from typing import List

Expand All @@ -16,12 +15,6 @@ def config_dir():
"XDG_CONFIG_HOME", os.path.join(os.path.expanduser("~"), ".config")
)
conf_dir = os.path.join(config_root, "yt")

if not os.path.exists(conf_dir):
try:
os.makedirs(conf_dir)
except OSError:
warnings.warn("unable to create yt config directory")
return conf_dir


Expand Down
8 changes: 8 additions & 0 deletions yt/utilities/tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,20 @@ def setUp(self):
self.xdg_config_home = os.environ.get("XDG_CONFIG_HOME")
self.tmpdir = tempfile.mkdtemp()
os.environ["XDG_CONFIG_HOME"] = self.tmpdir
os.mkdir(os.path.join(self.tmpdir, "yt"))

# run inside another temporary directory to avoid poluting the
# local space when we dump configuration to a local yt.toml file
self.origin = os.getcwd()
os.chdir(tempfile.mkdtemp())

def tearDown(self):
shutil.rmtree(self.tmpdir)
if self.xdg_config_home:
os.environ["XDG_CONFIG_HOME"] = self.xdg_config_home
else:
os.environ.pop("XDG_CONFIG_HOME")
os.chdir(self.origin)

def _runYTConfig(self, args):
args = ["yt", "config"] + args
Expand Down Expand Up @@ -138,6 +145,7 @@ def remove_spaces_and_breaks(s):
def tearDown(self):
if os.path.exists(YTConfig.get_global_config_file()):
os.remove(YTConfig.get_global_config_file())
super().tearDown()


class TestYTConfigGlobalLocal(TestYTConfig):
Expand Down

0 comments on commit 64675fe

Please sign in to comment.