Skip to content

Commit 544e80d

Browse files
committed
fixes #762
1 parent cfee390 commit 544e80d

4 files changed

Lines changed: 92 additions & 120 deletions

File tree

fastcore/_modidx.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,8 @@
431431
'fastcore.foundation.curryable': ('foundation.html#curryable', 'fastcore/foundation.py'),
432432
'fastcore.foundation.cycle': ('foundation.html#cycle', 'fastcore/foundation.py'),
433433
'fastcore.foundation.docs': ('foundation.html#docs', 'fastcore/foundation.py'),
434+
'fastcore.foundation.find_file_parents': ( 'foundation.html#find_file_parents',
435+
'fastcore/foundation.py'),
434436
'fastcore.foundation.flatmap': ('foundation.html#flatmap', 'fastcore/foundation.py'),
435437
'fastcore.foundation.is_bool': ('foundation.html#is_bool', 'fastcore/foundation.py'),
436438
'fastcore.foundation.is_indexer': ('foundation.html#is_indexer', 'fastcore/foundation.py'),

fastcore/foundation.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
# %% auto #0
66
__all__ = ['working_directory', 'add_docs', 'docs', 'coll_repr', 'is_bool', 'mask2idxs', 'cycle', 'zip_cycle', 'is_indexer',
77
'product', 'flatmap', 'CollBase', 'L', 'curryable', 'splitter', 'linesplitter', 'save_config_file',
8-
'read_config_file', 'Config']
8+
'read_config_file', 'find_file_parents', 'Config']
99

1010
# %% ../nbs/02_foundation.ipynb #0f974791
1111
from .imports import *
@@ -624,6 +624,12 @@ def read_config_file(file, **kwargs):
624624
config.read(file, encoding='utf8')
625625
return config['DEFAULT']
626626

627+
# %% ../nbs/02_foundation.ipynb #95834369
628+
def find_file_parents(fname, frompath=None):
629+
"Search `cfg_path` and its parents to find `cfg_name`"
630+
p = Path(frompath or Path.cwd()).expanduser().absolute()
631+
return first(o for o in [p, *p.parents] if (o/fname).exists())
632+
627633
# %% ../nbs/02_foundation.ipynb #e06640e8
628634
class Config:
629635
"Reading and writing `ConfigParser` ini files"
@@ -663,6 +669,5 @@ def path(self,k,default=None):
663669
@classmethod
664670
def find(cls, cfg_name, cfg_path=None, **kwargs):
665671
"Search `cfg_path` and its parents to find `cfg_name`"
666-
p = Path(cfg_path or Path.cwd()).expanduser().absolute()
667-
return first(cls(o, cfg_name, **kwargs)
668-
for o in [p, *p.parents] if (o/cfg_name).exists())
672+
p = find_file_parents(cfg_name, cfg_path)
673+
return cls(p, cfg_name, **kwargs)

nbs/02_foundation.ipynb

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4064,6 +4064,44 @@
40644064
"dict(res)"
40654065
]
40664066
},
4067+
{
4068+
"cell_type": "code",
4069+
"execution_count": null,
4070+
"id": "95834369",
4071+
"metadata": {},
4072+
"outputs": [],
4073+
"source": [
4074+
"#| export\n",
4075+
"def find_file_parents(fname, frompath=None):\n",
4076+
" \"Search `cfg_path` and its parents to find `cfg_name`\"\n",
4077+
" p = Path(frompath or Path.cwd()).expanduser().absolute()\n",
4078+
" return first(o for o in [p, *p.parents] if (o/fname).exists())"
4079+
]
4080+
},
4081+
{
4082+
"cell_type": "code",
4083+
"execution_count": null,
4084+
"id": "52f6aed5",
4085+
"metadata": {},
4086+
"outputs": [
4087+
{
4088+
"data": {
4089+
"text/plain": [
4090+
"Path('/Users/jhoward/aai-ws/fastcore')"
4091+
]
4092+
},
4093+
"execution_count": null,
4094+
"metadata": {},
4095+
"output_type": "execute_result"
4096+
}
4097+
],
4098+
"source": [
4099+
"save_config_file('../tmp.ini', _d)\n",
4100+
"try: found = find_file_parents('tmp.ini')\n",
4101+
"finally: os.unlink('../tmp.ini')\n",
4102+
"found"
4103+
]
4104+
},
40674105
{
40684106
"cell_type": "code",
40694107
"execution_count": null,
@@ -4110,9 +4148,8 @@
41104148
" @classmethod\n",
41114149
" def find(cls, cfg_name, cfg_path=None, **kwargs):\n",
41124150
" \"Search `cfg_path` and its parents to find `cfg_name`\"\n",
4113-
" p = Path(cfg_path or Path.cwd()).expanduser().absolute()\n",
4114-
" return first(cls(o, cfg_name, **kwargs)\n",
4115-
" for o in [p, *p.parents] if (o/cfg_name).exists())"
4151+
" p = find_file_parents(cfg_name, cfg_path)\n",
4152+
" return cls(p, cfg_name, **kwargs)"
41164153
]
41174154
},
41184155
{
@@ -4262,7 +4299,7 @@
42624299
"text/markdown": [
42634300
"---\n",
42644301
"\n",
4265-
"[source](https://github.com/AnswerDotAI/fastcore/blob/main/fastcore/foundation.py#L650){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
4302+
"[source](https://github.com/AnswerDotAI/fastcore/blob/main/fastcore/foundation.py#L656){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
42664303
"\n",
42674304
"### Config.get\n",
42684305
"\n",
@@ -4371,7 +4408,7 @@
43714408
"text/markdown": [
43724409
"---\n",
43734410
"\n",
4374-
"[source](https://github.com/AnswerDotAI/fastcore/blob/main/fastcore/foundation.py#L664){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
4411+
"[source](https://github.com/AnswerDotAI/fastcore/blob/main/fastcore/foundation.py#L670){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
43754412
"\n",
43764413
"### Config.find\n",
43774414
"\n",
@@ -4436,10 +4473,7 @@
44364473
]
44374474
}
44384475
],
4439-
"metadata": {
4440-
"solveit_dialog_mode": "learning",
4441-
"solveit_ver": 2
4442-
},
4476+
"metadata": {},
44434477
"nbformat": 4,
44444478
"nbformat_minor": 5
44454479
}

0 commit comments

Comments
 (0)