88
99import click
1010import pytask
11+ from pytask .shared import get_first_not_none_value
1112from pytask .shared import to_list
1213
13-
1414IGNORED_FILES_AND_FOLDERS = [
1515 "*/.git/*" ,
1616 "*/__pycache__/*" ,
2323def pytask_configure (pm , config_from_cli ):
2424 config = {"pm" : pm , "terminal_width" : _get_terminal_width ()}
2525
26- paths = _get_first_not_none_value (config_from_cli , key = "paths" , callback = to_list )
26+ paths = get_first_not_none_value (config_from_cli , key = "paths" , callback = to_list )
2727 paths = [Path (p ).resolve () for path in paths for p in glob .glob (path .as_posix ())]
2828 config ["paths" ] = paths if paths else [Path .cwd ().resolve ()]
2929
@@ -44,7 +44,7 @@ def pytask_configure(pm, config_from_cli):
4444@pytask .hookimpl
4545def pytask_parse_config (config , config_from_cli , config_from_file ):
4646 config ["ignore" ] = (
47- _get_first_not_none_value (
47+ get_first_not_none_value (
4848 config_from_cli ,
4949 config_from_file ,
5050 key = "ignore" ,
@@ -54,35 +54,13 @@ def pytask_parse_config(config, config_from_cli, config_from_file):
5454 + IGNORED_FILES_AND_FOLDERS
5555 )
5656
57- config ["debug_pytask" ] = _get_first_not_none_value (
57+ config ["debug_pytask" ] = get_first_not_none_value (
5858 config_from_cli , config_from_file , key = "debug_pytask" , default = False
5959 )
6060 if config ["debug_pytask" ]:
6161 config ["pm" ].trace .root .setwriter (click .echo )
6262 config ["pm" ].enable_tracing ()
6363
64- provider = _get_first_not_none_value (
65- config_from_cli , config_from_file , key = "database_provider" , default = "sqlite"
66- )
67- filename = _get_first_not_none_value (
68- config_from_cli ,
69- config_from_file ,
70- key = "database_filename" ,
71- default = ".pytask.sqlite3" ,
72- )
73- create_db = _get_first_not_none_value (
74- config_from_cli , config_from_file , key = "database_create_db" , default = True
75- )
76- create_tables = _get_first_not_none_value (
77- config_from_cli , config_from_file , key = "database_create_tables" , default = True
78- )
79- config ["database" ] = {
80- "provider" : provider ,
81- "filename" : Path (config ["root" ], filename ).resolve ().as_posix (),
82- "create_db" : create_db ,
83- "create_tables" : create_tables ,
84- }
85-
8664
8765def _find_project_root_and_ini (paths ):
8866 try :
@@ -126,42 +104,11 @@ def _read_config(path):
126104 return dict (config ["pytask" ])
127105
128106
129- def _get_first_not_none_value (* configs , key , default = None , callback = None ):
130- """Get the first non-None value for a key from a list of dictionaries.
131-
132- This function allows to prioritize information from many configurations by changing
133- the order of the inputs while also providing a default.
134-
135- Examples
136- --------
137- >>> _get_first_not_none_value({"a": None}, {"a": 1}, key="a")
138- 1
139-
140- >>> _get_first_not_none_value({"a": None}, {"a": None}, key="a", default="default")
141- 'default'
142-
143- >>> _get_first_not_none_value({}, {}, key="a", default="default")
144- 'default'
145-
146- >>> _get_first_not_none_value({"a": None}, {"a": "b"}, key="a", callback=to_list)
147- ['b']
148-
149- """
150- return next (
151- (
152- config [key ] if callback is None else callback (config [key ])
153- for config in configs
154- if config .get (key , None ) is not None
155- ),
156- default ,
157- )
158-
159-
160107def _get_terminal_width () -> int :
161108 """Get the window width of the terminal."""
162109 width , _ = shutil .get_terminal_size (fallback = (80 , 24 ))
163110
164- # The Windows get_terminal_size may be bogus, let's sanify a bit.
111+ # The Windows get_terminal_size may be bogus, let's sanitize a bit.
165112 if width < 40 :
166113 width = 80
167114
0 commit comments