7
7
__all__ = ['nbdev_create_config' , 'get_config' , 'config_key' , 'create_output' , 'show_src' , 'update_version' , 'add_init' ,
8
8
'write_cells' ]
9
9
10
- # %% ../nbs/api/01_config.ipynb 2
10
+ # %% ../nbs/api/01_config.ipynb
11
11
_doc_ = """Read and write nbdev's `settings.ini` file.
12
12
`get_config` is the main function for reading settings."""
13
13
14
- # %% ../nbs/api/01_config.ipynb 3
14
+ # %% ../nbs/api/01_config.ipynb
15
15
from datetime import datetime
16
16
from fastcore .docments import *
17
17
from fastcore .utils import *
25
25
from execnb .nbio import read_nb ,NbCell
26
26
from urllib .error import HTTPError
27
27
28
- # %% ../nbs/api/01_config.ipynb 8
28
+ # %% ../nbs/api/01_config.ipynb
29
29
_nbdev_home_dir = 'nbdev' # sub-directory of xdg base dir
30
30
_nbdev_cfg_name = 'settings.ini'
31
31
32
- # %% ../nbs/api/01_config.ipynb 9
32
+ # %% ../nbs/api/01_config.ipynb
33
33
def _git_repo ():
34
34
try : return repo_details (run ('git config --get remote.origin.url' ))[1 ]
35
35
except OSError : return
36
36
37
- # %% ../nbs/api/01_config.ipynb 11
37
+ # %% ../nbs/api/01_config.ipynb
38
38
# When adding a named default to the list below, be sure that that name
39
39
# is also added to one of the sections in `_nbdev_cfg_sections` as well,
40
40
# or it won't get written by `nbdev_create_config`:
@@ -82,7 +82,7 @@ def _apply_defaults(
82
82
cfg [k ] = v
83
83
return cfg
84
84
85
- # %% ../nbs/api/01_config.ipynb 12
85
+ # %% ../nbs/api/01_config.ipynb
86
86
def _get_info (owner , repo , default_branch = 'main' , default_kw = 'nbdev' ):
87
87
from ghapi .all import GhApi
88
88
api = GhApi (owner = owner , repo = repo , token = os .getenv ('GITHUB_TOKEN' ))
@@ -98,7 +98,7 @@ def _get_info(owner, repo, default_branch='main', default_kw='nbdev'):
98
98
99
99
return r .default_branch , default_kw if not getattr (r , 'topics' , []) else ' ' .join (r .topics ), r .description
100
100
101
- # %% ../nbs/api/01_config.ipynb 14
101
+ # %% ../nbs/api/01_config.ipynb
102
102
def _fetch_from_git (raise_err = False ):
103
103
"Get information for settings.ini from the user."
104
104
res = {}
@@ -114,7 +114,7 @@ def _fetch_from_git(raise_err=False):
114
114
else : res ['lib_name' ] = res ['repo' ].replace ('-' ,'_' )
115
115
return res
116
116
117
- # %% ../nbs/api/01_config.ipynb 16
117
+ # %% ../nbs/api/01_config.ipynb
118
118
def _prompt_user (cfg , inferred ):
119
119
"Let user input values not in `cfg` or `inferred`."
120
120
res = cfg .copy ()
@@ -128,7 +128,7 @@ def _prompt_user(cfg, inferred):
128
128
print (msg + res [k ]+ ' # Automatically inferred from git' )
129
129
return res
130
130
131
- # %% ../nbs/api/01_config.ipynb 18
131
+ # %% ../nbs/api/01_config.ipynb
132
132
def _cfg2txt (cfg , head , sections , tail = '' ):
133
133
"Render `cfg` with commented sections."
134
134
nm = cfg .d .name
@@ -140,7 +140,7 @@ def _cfg2txt(cfg, head, sections, tail=''):
140
140
res += tail
141
141
return res .strip ()
142
142
143
- # %% ../nbs/api/01_config.ipynb 20
143
+ # %% ../nbs/api/01_config.ipynb
144
144
_nbdev_cfg_head = '''# All sections below are required unless otherwise specified.
145
145
# See https://github.com/fastai/nbdev/blob/master/settings.ini for examples.
146
146
@@ -157,7 +157,7 @@ def _cfg2txt(cfg, head, sections, tail=''):
157
157
# package_data =
158
158
'''
159
159
160
- # %% ../nbs/api/01_config.ipynb 21
160
+ # %% ../nbs/api/01_config.ipynb
161
161
@call_parse
162
162
@delegates (_apply_defaults , but = 'cfg' )
163
163
def nbdev_create_config (
@@ -183,19 +183,19 @@ def nbdev_create_config(
183
183
cfg_fn = Path (path )/ cfg_name
184
184
print (f'{ cfg_fn } created.' )
185
185
186
- # %% ../nbs/api/01_config.ipynb 24
186
+ # %% ../nbs/api/01_config.ipynb
187
187
def _nbdev_config_file (cfg_name = _nbdev_cfg_name , path = None ):
188
188
cfg_path = path = Path .cwd () if path is None else Path (path )
189
189
while cfg_path != cfg_path .parent and not (cfg_path / cfg_name ).exists (): cfg_path = cfg_path .parent
190
190
if not (cfg_path / cfg_name ).exists (): cfg_path = path
191
191
return cfg_path / cfg_name
192
192
193
- # %% ../nbs/api/01_config.ipynb 26
193
+ # %% ../nbs/api/01_config.ipynb
194
194
def _xdg_config_paths (cfg_name = _nbdev_cfg_name ):
195
195
xdg_config_paths = reversed ([xdg_config_home ()]+ xdg_config_dirs ())
196
196
return [o / _nbdev_home_dir / cfg_name for o in xdg_config_paths ]
197
197
198
- # %% ../nbs/api/01_config.ipynb 27
198
+ # %% ../nbs/api/01_config.ipynb
199
199
def _type (t ): return bool if t == bool_arg else t
200
200
_types = {k :_type (v ['anno' ]) for k ,v in docments (_apply_defaults ,full = True ,returns = False ).items () if k != 'cfg' }
201
201
@@ -207,22 +207,22 @@ def get_config(cfg_name=_nbdev_cfg_name, path=None):
207
207
cfg = Config (cfg_file .parent , cfg_file .name , extra_files = extra_files , types = _types )
208
208
return _apply_defaults (cfg )
209
209
210
- # %% ../nbs/api/01_config.ipynb 42
210
+ # %% ../nbs/api/01_config.ipynb
211
211
def config_key (c , default = None , path = True , missing_ok = None ):
212
212
"Deprecated: use `get_config().get` or `get_config().path` instead."
213
213
warn ("`config_key` is deprecated. Use `get_config().get` or `get_config().path` instead." , DeprecationWarning )
214
214
return get_config ().path (c , default ) if path else get_config ().get (c , default )
215
215
216
- # %% ../nbs/api/01_config.ipynb 44
216
+ # %% ../nbs/api/01_config.ipynb
217
217
def create_output (txt , mime ):
218
218
"Add a cell output containing `txt` of the `mime` text MIME sub-type"
219
219
return [{"data" : { f"text/{ mime } " : str (txt ).splitlines (True ) },
220
220
"execution_count" : 1 , "metadata" : {}, "output_type" : "execute_result" }]
221
221
222
- # %% ../nbs/api/01_config.ipynb 45
222
+ # %% ../nbs/api/01_config.ipynb
223
223
def show_src (src , lang = 'python' ): return Markdown (f'```{ lang } \n { src } \n ```' )
224
224
225
- # %% ../nbs/api/01_config.ipynb 48
225
+ # %% ../nbs/api/01_config.ipynb
226
226
_re_version = re .compile (r'^__version__\s*=.*$' , re .MULTILINE )
227
227
_init = '__init__.py'
228
228
@@ -251,15 +251,15 @@ def add_init(path=None):
251
251
if _has_py (fs ) or any (filter (_has_py , subds )) and not (r / _init ).exists (): (r / _init ).touch ()
252
252
if get_config ().get ('put_version_in_init' , True ): update_version (path )
253
253
254
- # %% ../nbs/api/01_config.ipynb 51
254
+ # %% ../nbs/api/01_config.ipynb
255
255
def write_cells (cells , hdr , file , offset = 0 , cell_number = True ):
256
256
"Write `cells` to `file` along with header `hdr` starting at index `offset` (mainly for nbdev internal use)."
257
257
for cell in cells :
258
258
if cell .source .strip ():
259
259
idx = f" { cell .idx_ + offset } " if cell_number else ""
260
260
file .write (f'\n \n { hdr } { idx } \n { cell .source } ' )
261
261
262
- # %% ../nbs/api/01_config.ipynb 52
262
+ # %% ../nbs/api/01_config.ipynb
263
263
def _basic_export_nb (fname , name , dest = None ):
264
264
"Basic exporter to bootstrap nbdev."
265
265
if dest is None : dest = get_config ().lib_path
0 commit comments