Skip to content

Commit

Permalink
deactivate constant backing up of config file
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelaye committed Apr 30, 2024
1 parent 90c6c70 commit 4060e2f
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 50 deletions.
65 changes: 34 additions & 31 deletions notebooks/api/00_config.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"metadata": {},
"outputs": [],
"source": [
"#| default_exp config"
"# | default_exp config"
]
},
{
Expand All @@ -24,7 +24,7 @@
"metadata": {},
"outputs": [],
"source": [
"#| hide\n",
"# | hide\n",
"from nbdev.showdoc import show_doc"
]
},
Expand All @@ -34,8 +34,7 @@
"metadata": {},
"outputs": [],
"source": [
"#| export\n",
"import copy\n",
"# | export\n",
"import os\n",
"import shutil\n",
"from collections.abc import Mapping\n",
Expand All @@ -45,7 +44,6 @@
"from typing import Union\n",
"\n",
"import tomlkit as toml\n",
"\n",
"from fastcore.utils import AttrDict, Path"
]
},
Expand All @@ -55,22 +53,22 @@
"metadata": {},
"outputs": [],
"source": [
"#| export\n",
"# | export\n",
"\n",
"\n",
"def reset_non_urls(\n",
" source: dict, # source dictionary \n",
" reset: str = '', # value to reset non URLs to\n",
" source: dict, # source dictionary\n",
" reset: str = \"\", # value to reset non URLs to\n",
") -> dict:\n",
" \"\"\"Reset all non-URL values in the config file.\n",
" \n",
" This is useful for copying the private config file with new data items back into the \n",
"\n",
" This is useful for copying the private config file with new data items back into the\n",
" source tree for a clean commit.\n",
" \"\"\"\n",
" for key, value in source.items():\n",
" if isinstance(value, Mapping) and value:\n",
" reset_non_urls(value, reset)\n",
" elif not 'url' in key:\n",
" elif \"url\" not in key:\n",
" source[key] = reset\n",
" return source"
]
Expand All @@ -88,7 +86,7 @@
"metadata": {},
"outputs": [],
"source": [
"#| export\n",
"# | export\n",
"class Config:\n",
" \"\"\"Manage config stuff.\n",
"\n",
Expand All @@ -97,6 +95,7 @@
" At minimum, there should be the `storage_root` attribute for storing data\n",
" for this package.\n",
" \"\"\"\n",
"\n",
" # This part enables a config path location override using env PLANETARYPY_CONFIG\n",
" fname = \"planetarypy_config.toml\"\n",
" # separating fname from fpath so that resource_path below is correct.\n",
Expand All @@ -110,7 +109,7 @@
" p = files(\"planetarypy.data\").joinpath(self.fname)\n",
" shutil.copy(p, self.path)\n",
" self._read_config()\n",
" self._update_configfile()\n",
" # self._update_configfile() # this will be done differently very soon, now obsolete.\n",
"\n",
" def _read_config(self):\n",
" \"\"\"Read the configfile and store config dict.\n",
Expand All @@ -133,22 +132,24 @@
" return self.tomldoc\n",
"\n",
" def get_value(\n",
" self,\n",
" key: str # A nested key in dotted format, e.g. cassini.uvis.indexes\n",
" ) -> str: # Returning empty string if not existing, because Path('') is False which is handy (e.g. in ctx mod.)\n",
" self,\n",
" key: str, # A nested key in dotted format, e.g. cassini.uvis.indexes\n",
" ) -> (\n",
" str\n",
" ): # Returning empty string if not existing, because Path('') is False which is handy (e.g. in ctx mod.)\n",
" \"\"\"Get sub-dictionary by nested key.\"\"\"\n",
" if not key.startswith(\"missions\"):\n",
" key = \"missions.\" + key\n",
" try:\n",
" return reduce(lambda c, k: c[k], key.split(\".\"), self.d)\n",
" except toml.exceptions.NonExistentKey:\n",
" return ''\n",
" return \"\"\n",
"\n",
" def set_value(\n",
" self,\n",
" nested_key: str, # A nested key in dotted format, e.g. cassini.uvis.ring_summary\n",
" value: Union[float, str], # Value for the given key to be stored\n",
" save: bool = True, # Switch to control writing out to disk\n",
" self,\n",
" nested_key: str, # A nested key in dotted format, e.g. cassini.uvis.ring_summary\n",
" value: Union[float, str], # Value for the given key to be stored\n",
" save: bool = True, # Switch to control writing out to disk\n",
" ):\n",
" \"\"\"Set value in sub-dic using dotted key.\"\"\"\n",
" dic = self.tomldoc\n",
Expand Down Expand Up @@ -181,7 +182,9 @@
" at `Class.path`, either default or as given during init.\n",
" `storage_root` attribute is set as well.\n",
" \"\"\"\n",
" path = input(\"Provide the root storage path where all downloaded and produced data will be stored:\")\n",
" path = input(\n",
" \"Provide the root storage path where all downloaded and produced data will be stored:\"\n",
" )\n",
" self.tomldoc[\"storage_root\"] = path\n",
" self.storage_root = Path(path)\n",
" self.save()\n",
Expand All @@ -197,8 +200,8 @@
" return list(instruments.keys())\n",
"\n",
" def get_datalevels(\n",
" self,\n",
" mission_instrument, # mission.instrument code, e.g. mro.hirise\n",
" self,\n",
" mission_instrument, # mission.instrument code, e.g. mro.hirise\n",
" ):\n",
" \"\"\"Return configured data levels available for an instrument.\n",
"\n",
Expand Down Expand Up @@ -287,7 +290,7 @@
"metadata": {},
"outputs": [],
"source": [
"#| export\n",
"# | export\n",
"config = Config()"
]
},
Expand Down Expand Up @@ -386,7 +389,7 @@
}
],
"source": [
"config.get_value(index)['url']"
"config.get_value(index)[\"url\"]"
]
},
{
Expand Down Expand Up @@ -496,7 +499,7 @@
}
],
"source": [
"config.get_value(index)['url']"
"config.get_value(index)[\"url\"]"
]
},
{
Expand All @@ -516,7 +519,7 @@
}
],
"source": [
"config.get_value(\"mro.ctx.datalevels\")['edr']['url']"
"config.get_value(\"mro.ctx.datalevels\")[\"edr\"][\"url\"]"
]
},
{
Expand Down Expand Up @@ -592,7 +595,7 @@
"metadata": {},
"outputs": [],
"source": [
"assert config.path.name == '.planetarypy_config.toml'"
"assert config.path.name == \".planetarypy_config.toml\""
]
},
{
Expand Down Expand Up @@ -729,9 +732,9 @@
],
"metadata": {
"kernelspec": {
"display_name": "conda-env-py39-py",
"display_name": "py311",
"language": "python",
"name": "conda-env-py39-py"
"name": "python3"
}
},
"nbformat": 4,
Expand Down
41 changes: 22 additions & 19 deletions planetarypy/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
__all__ = ['config', 'reset_non_urls', 'Config']

# %% ../notebooks/api/00_config.ipynb 3
import copy
import os
import shutil
from collections.abc import Mapping
Expand All @@ -14,23 +13,22 @@
from typing import Union

import tomlkit as toml

from fastcore.utils import AttrDict, Path

# %% ../notebooks/api/00_config.ipynb 4
def reset_non_urls(
source: dict, # source dictionary
reset: str = '', # value to reset non URLs to
source: dict, # source dictionary
reset: str = "", # value to reset non URLs to
) -> dict:
"""Reset all non-URL values in the config file.
This is useful for copying the private config file with new data items back into the
This is useful for copying the private config file with new data items back into the
source tree for a clean commit.
"""
for key, value in source.items():
if isinstance(value, Mapping) and value:
reset_non_urls(value, reset)
elif not 'url' in key:
elif "url" not in key:
source[key] = reset
return source

Expand All @@ -43,6 +41,7 @@ class instance after initialization.
At minimum, there should be the `storage_root` attribute for storing data
for this package.
"""

# This part enables a config path location override using env PLANETARYPY_CONFIG
fname = "planetarypy_config.toml"
# separating fname from fpath so that resource_path below is correct.
Expand All @@ -56,7 +55,7 @@ def __init__(self, config_path: str = None): # str or pathlib.Path
p = files("planetarypy.data").joinpath(self.fname)
shutil.copy(p, self.path)
self._read_config()
self._update_configfile()
# self._update_configfile() # this will be done differently very soon, now obsolete.

def _read_config(self):
"""Read the configfile and store config dict.
Expand All @@ -79,22 +78,24 @@ def d(self):
return self.tomldoc

def get_value(
self,
key: str # A nested key in dotted format, e.g. cassini.uvis.indexes
) -> str: # Returning empty string if not existing, because Path('') is False which is handy (e.g. in ctx mod.)
self,
key: str, # A nested key in dotted format, e.g. cassini.uvis.indexes
) -> (
str
): # Returning empty string if not existing, because Path('') is False which is handy (e.g. in ctx mod.)
"""Get sub-dictionary by nested key."""
if not key.startswith("missions"):
key = "missions." + key
try:
return reduce(lambda c, k: c[k], key.split("."), self.d)
except toml.exceptions.NonExistentKey:
return ''
return ""

def set_value(
self,
nested_key: str, # A nested key in dotted format, e.g. cassini.uvis.ring_summary
value: Union[float, str], # Value for the given key to be stored
save: bool = True, # Switch to control writing out to disk
self,
nested_key: str, # A nested key in dotted format, e.g. cassini.uvis.ring_summary
value: Union[float, str], # Value for the given key to be stored
save: bool = True, # Switch to control writing out to disk
):
"""Set value in sub-dic using dotted key."""
dic = self.tomldoc
Expand Down Expand Up @@ -127,7 +128,9 @@ def ask_storage_root(self):
at `Class.path`, either default or as given during init.
`storage_root` attribute is set as well.
"""
path = input("Provide the root storage path where all downloaded and produced data will be stored:")
path = input(
"Provide the root storage path where all downloaded and produced data will be stored:"
)
self.tomldoc["storage_root"] = path
self.storage_root = Path(path)
self.save()
Expand All @@ -143,8 +146,8 @@ def list_instruments(self, mission):
return list(instruments.keys())

def get_datalevels(
self,
mission_instrument, # mission.instrument code, e.g. mro.hirise
self,
mission_instrument, # mission.instrument code, e.g. mro.hirise
):
"""Return configured data levels available for an instrument.
Expand Down

0 comments on commit 4060e2f

Please sign in to comment.