Repository: cthoyt/pystow

Issue:
Get configuration from file corresponding to leading part
If I want to use `zenodo:sandbox` as a key, it should figure to look in `zenodo.ini` and other `zenodo.*` files if they exist

Discussion:

Patch:
diff --git a/src/pystow/config_api.py b/src/pystow/config_api.py
index 270e4f5..dca56d4 100644
--- a/src/pystow/config_api.py
+++ b/src/pystow/config_api.py
@@ -77,6 +77,12 @@ def get_home(ensure_exists: bool = True) -> Path:
 def _get_cfp(module: str) -> ConfigParser:
     cfp = ConfigParser()
     directory = get_home()
+
+    # If a multi-part module was given like "zenodo:sandbox",
+    # then only look for the first part "zenodo" as the file name
+    if ":" in module:
+        module = module.split(":", 1)[0]
+
     filenames = [
         os.path.join(directory, "config.cfg"),
         os.path.join(directory, "config.ini"),
@@ -157,7 +163,12 @@ def write_config(module: str, key: str, value: str) -> None:
     """
     _get_cfp.cache_clear()
     cfp = ConfigParser()
-    path = get_home() / f"{module}.ini"
+
+    # If there's a multi-part module such as "zenodo:sandbox",
+    # then write to zenodo.ini with section [zenodo:sandbox]
+    fname = module.split(":", 1)[0] if ":" in module else module
+
+    path = get_home().joinpath(fname).with_suffix(".ini")
     cfp.read(path)
 
     # If the file did not exist, then this section will be empty


Test patch:
diff --git a/tests/test_config.py b/tests/test_config.py
index a641ad4..e16da5d 100644
--- a/tests/test_config.py
+++ b/tests/test_config.py
@@ -2,10 +2,12 @@
 
 """Test configuration loading."""
 
+import tempfile
 import unittest
+from pathlib import Path
 
 import pystow
-from pystow.config_api import _get_cfp
+from pystow.config_api import CONFIG_HOME_ENVVAR, _get_cfp
 from pystow.utils import mock_envvar
 
 
@@ -91,3 +93,18 @@ class TestConfig(unittest.TestCase):
         self.assertEqual(
             True, pystow.get_config(self.test_section, self.test_option, passthrough=1, dtype=bool)
         )
+
+    def test_subsection(self):
+        """Test subsections."""
+        with tempfile.TemporaryDirectory() as directory, mock_envvar(CONFIG_HOME_ENVVAR, directory):
+            directory = Path(directory)
+            path = directory.joinpath("test.ini")
+            self.assertFalse(path.is_file(), msg="file should not already exist")
+
+            self.assertIsNone(pystow.get_config("test:subtest", "key"))
+            self.assertFalse(path.is_file(), msg="getting config should not create a file")
+
+            pystow.write_config("test:subtest", "key", "value")
+            self.assertTrue(path.is_file(), msg=f"{list(directory.iterdir())}")
+
+            self.assertEqual("value", pystow.get_config("test:subtest", "key"))