|
32 | 32 | import logging
|
33 | 33 | from itertools import chain
|
34 | 34 | from pathlib import Path
|
35 |
| -from typing import Dict, Iterable, Tuple |
| 35 | +from typing import Iterable, Tuple |
36 | 36 |
|
37 | 37 | import click
|
38 |
| -from kedro.framework.cli.catalog import catalog as catalog_group |
39 |
| -from kedro.framework.cli.jupyter import jupyter as jupyter_group |
40 |
| -from kedro.framework.cli.pipeline import pipeline as pipeline_group |
41 |
| -from kedro.framework.cli.project import project_group |
42 |
| -from kedro.framework.cli.utils import KedroCliError, env_option, split_string |
| 38 | +from kedro.framework.cli.utils import ( |
| 39 | + KedroCliError, |
| 40 | + _config_file_callback, |
| 41 | + _reformat_load_versions, |
| 42 | + _split_params, |
| 43 | + env_option, |
| 44 | + split_string, |
| 45 | +) |
43 | 46 | from kedro.framework.session import KedroSession
|
44 | 47 | from kedro.utils import load_obj
|
45 | 48 |
|
46 | 49 | CONTEXT_SETTINGS = dict(help_option_names=["-h", "--help"])
|
47 | 50 |
|
48 |
| -# get our package onto the python path |
49 |
| -PROJ_PATH = Path(__file__).resolve().parent |
50 |
| - |
51 |
| -ENV_ARG_HELP = """Run the pipeline in a configured environment. If not specified, |
52 |
| -pipeline will run using environment `local`.""" |
53 | 51 | FROM_INPUTS_HELP = (
|
54 | 52 | """A list of dataset names which should be used as a starting point."""
|
55 | 53 | )
|
|
79 | 77 | so parameter values are allowed to contain colons, parameter keys are not."""
|
80 | 78 |
|
81 | 79 |
|
82 |
| -def _config_file_callback(ctx, param, value): # pylint: disable=unused-argument |
83 |
| - """Config file callback, that replaces command line options with config file |
84 |
| - values. If command line options are passed, they override config file values. |
85 |
| - """ |
86 |
| - # for performance reasons |
87 |
| - import anyconfig # pylint: disable=import-outside-toplevel |
88 |
| - |
89 |
| - ctx.default_map = ctx.default_map or {} |
90 |
| - section = ctx.info_name |
91 |
| - |
92 |
| - if value: |
93 |
| - config = anyconfig.load(value)[section] |
94 |
| - ctx.default_map.update(config) |
95 |
| - |
96 |
| - return value |
97 |
| - |
98 |
| - |
99 | 80 | def _get_values_as_tuple(values: Iterable[str]) -> Tuple[str, ...]:
|
100 | 81 | return tuple(chain.from_iterable(value.split(",") for value in values))
|
101 | 82 |
|
102 | 83 |
|
103 |
| -def _reformat_load_versions( # pylint: disable=unused-argument |
104 |
| - ctx, param, value |
105 |
| -) -> Dict[str, str]: |
106 |
| - """Reformat data structure from tuple to dictionary for `load-version`, e.g: |
107 |
| - ('dataset1:time1', 'dataset2:time2') -> {"dataset1": "time1", "dataset2": "time2"}. |
108 |
| - """ |
109 |
| - load_versions_dict = {} |
110 |
| - |
111 |
| - for load_version in value: |
112 |
| - load_version_list = load_version.split(":", 1) |
113 |
| - if len(load_version_list) != 2: |
114 |
| - raise KedroCliError( |
115 |
| - f"Expected the form of `load_version` to be " |
116 |
| - f"`dataset_name:YYYY-MM-DDThh.mm.ss.sssZ`," |
117 |
| - f"found {load_version} instead" |
118 |
| - ) |
119 |
| - load_versions_dict[load_version_list[0]] = load_version_list[1] |
120 |
| - |
121 |
| - return load_versions_dict |
122 |
| - |
123 |
| - |
124 |
| -def _split_params(ctx, param, value): |
125 |
| - if isinstance(value, dict): |
126 |
| - return value |
127 |
| - result = {} |
128 |
| - for item in split_string(ctx, param, value): |
129 |
| - item = item.split(":", 1) |
130 |
| - if len(item) != 2: |
131 |
| - ctx.fail( |
132 |
| - f"Invalid format of `{param.name}` option: " |
133 |
| - f"Item `{item[0]}` must contain " |
134 |
| - f"a key and a value separated by `:`." |
135 |
| - ) |
136 |
| - key = item[0].strip() |
137 |
| - if not key: |
138 |
| - ctx.fail( |
139 |
| - f"Invalid format of `{param.name}` option: Parameter key " |
140 |
| - f"cannot be an empty string." |
141 |
| - ) |
142 |
| - value = item[1].strip() |
143 |
| - result[key] = _try_convert_to_numeric(value) |
144 |
| - return result |
145 |
| - |
146 |
| - |
147 |
| -def _try_convert_to_numeric(value): |
148 |
| - try: |
149 |
| - value = float(value) |
150 |
| - except ValueError: |
151 |
| - return value |
152 |
| - return int(value) if value.is_integer() else value |
153 |
| - |
154 |
| - |
155 | 84 | @click.group(context_settings=CONTEXT_SETTINGS, name=__file__)
|
156 | 85 | def cli():
|
157 | 86 | """Command line tools for manipulating a Kedro project."""
|
@@ -239,11 +168,3 @@ def run(
|
239 | 168 | # Logging parameters for some e2e tests
|
240 | 169 | params_to_log = session.load_context().params
|
241 | 170 | logging.info("Parameters: %s", json.dumps(params_to_log, sort_keys=True))
|
242 |
| - |
243 |
| - |
244 |
| -cli.add_command(pipeline_group) |
245 |
| -cli.add_command(catalog_group) |
246 |
| -cli.add_command(jupyter_group) |
247 |
| - |
248 |
| -for command in project_group.commands.values(): |
249 |
| - cli.add_command(command) |
0 commit comments