Skip to content

Commit

Permalink
Merge pull request #659 from AnswerDotAI/config_parser
Browse files Browse the repository at this point in the history
Update Config to support ConfigParser keyword arguments
  • Loading branch information
jph00 authored Dec 26, 2024
2 parents 4f5a823 + f8ac714 commit e4335a1
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 6 deletions.
4 changes: 2 additions & 2 deletions fastcore/foundation.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,11 +260,11 @@ def read_config_file(file, **kwargs):
# %% ../nbs/02_foundation.ipynb
class Config:
"Reading and writing `ConfigParser` ini files"
def __init__(self, cfg_path, cfg_name, create=None, save=True, extra_files=None, types=None):
def __init__(self, cfg_path, cfg_name, create=None, save=True, extra_files=None, types=None, **cfg_kwargs):
self.types = types or {}
cfg_path = Path(cfg_path).expanduser().absolute()
self.config_path,self.config_file = cfg_path,cfg_path/cfg_name
self._cfg = ConfigParser()
self._cfg = ConfigParser(**cfg_kwargs)
self.d = self._cfg['DEFAULT']
found = [Path(o) for o in self._cfg.read(L(extra_files)+[self.config_file], encoding='utf8')]
if self.config_file not in found and create is not None:
Expand Down
56 changes: 52 additions & 4 deletions nbs/02_foundation.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -1227,7 +1227,7 @@
"\n",
"> L.groupby (key, val=<function noop>)\n",
"\n",
"*Same as `groupby`*"
"*Same as `fastcore.basics.groupby`*"
],
"text/plain": [
"---\n",
Expand All @@ -1238,7 +1238,7 @@
"\n",
"> L.groupby (key, val=<function noop>)\n",
"\n",
"*Same as `groupby`*"
"*Same as `fastcore.basics.groupby`*"
]
},
"execution_count": null,
Expand Down Expand Up @@ -2301,11 +2301,11 @@
"#|export\n",
"class Config:\n",
" \"Reading and writing `ConfigParser` ini files\"\n",
" def __init__(self, cfg_path, cfg_name, create=None, save=True, extra_files=None, types=None):\n",
" def __init__(self, cfg_path, cfg_name, create=None, save=True, extra_files=None, types=None, **cfg_kwargs):\n",
" self.types = types or {}\n",
" cfg_path = Path(cfg_path).expanduser().absolute()\n",
" self.config_path,self.config_file = cfg_path,cfg_path/cfg_name\n",
" self._cfg = ConfigParser()\n",
" self._cfg = ConfigParser(**cfg_kwargs)\n",
" self.d = self._cfg['DEFAULT']\n",
" found = [Path(o) for o in self._cfg.read(L(extra_files)+[self.config_file], encoding='utf8')]\n",
" if self.config_file not in found and create is not None:\n",
Expand Down Expand Up @@ -2421,6 +2421,54 @@
"assert not Path('../tmp.ini').exists()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You can also pass in `ConfigParser` `kwargs` to change the behavior of how your configuration file will be parsed. For example, by default, inline comments are not handled by `Config`. However, if you pass in the `inline_comment_prefixes` with whatever your comment symbol is, you'll overwrite this behavior. "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Create a complete example config file with comments\n",
"cfg_str = \"\"\"\\\n",
"[DEFAULT]\n",
"user = fastai # inline comment\n",
"\n",
"# Library configuration\n",
"lib_name = fastcore\n",
"\n",
"# Paths\n",
"some_path = test \n",
"\n",
"# Feature flags\n",
"some_bool = True\n",
"\n",
"# Numeric settings\n",
"some_num = # missing value\n",
"\"\"\"\n",
"\n",
"with open('../tmp.ini', 'w') as f:\n",
" f.write(cfg_str)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Now read it back to verify\n",
"try: cfg = Config('..', 'tmp.ini', inline_comment_prefixes=('#'))\n",
"finally: os.unlink('../tmp.ini')\n",
"test_eq(cfg.user,'fastai')\n",
"test_eq(cfg.some_num,'')"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand Down

0 comments on commit e4335a1

Please sign in to comment.