|
1 | | -import io |
2 | | -import json |
3 | 1 | import logging |
4 | 2 | import os |
5 | 3 | import sys |
6 | 4 | import typing as t |
7 | 5 |
|
8 | | -import poyo |
9 | | -from cookiecutter.exceptions import InvalidConfiguration |
10 | | -from requests.exceptions import ConnectionError, JSONDecodeError |
| 6 | +from requests.exceptions import ConnectionError |
11 | 7 |
|
12 | 8 | from cookiecutter_python.backend.check_pypi import check_pypi |
13 | 9 | from cookiecutter_python.backend.check_pypi_handler import handler |
| 10 | +from cookiecutter_python.backend.load_config import get_interpreters_from_yaml |
| 11 | +from cookiecutter_python.handle.interpreters_support import handle as get_interpreters |
14 | 12 |
|
15 | 13 | from .cookiecutter_proxy import cookiecutter |
16 | 14 |
|
|
19 | 17 | my_dir = os.path.dirname(os.path.realpath(__file__)) |
20 | 18 |
|
21 | 19 |
|
22 | | -def load_yaml(config_file) -> t.Mapping: |
23 | | - # TODO use a proxy to load yaml |
24 | | - with io.open(config_file, encoding='utf-8') as file_handle: |
25 | | - try: |
26 | | - yaml_dict = poyo.parse_string(file_handle.read()) |
27 | | - except poyo.exceptions.PoyoException as error: |
28 | | - raise InvalidConfiguration( |
29 | | - 'Unable to parse YAML file {}. Error: {}' ''.format(config_file, error) |
30 | | - ) from error |
31 | | - return yaml_dict |
32 | | - |
33 | | - |
34 | 20 | GivenInterpreters = t.Mapping[str, t.Sequence[str]] |
35 | 21 |
|
36 | 22 |
|
37 | 23 | def supported_interpreters(config_file, no_input) -> t.Optional[GivenInterpreters]: |
38 | 24 | if not no_input: # interactive |
39 | 25 | if sys.version_info < (3, 10): |
40 | | - check_box_dialog(config_file=config_file) |
41 | | - # else return None: let generator backend (ie cookiecutter) handle |
42 | | - # receiving the 'supported-interpreters' information from user input |
43 | | - # non-interactive |
| 26 | + return check_box_dialog(config_file=config_file) |
| 27 | + return None |
44 | 28 | if config_file: |
45 | | - try: |
46 | | - return get_interpreters_from_yaml(config_file) |
47 | | - except ( |
48 | | - InvalidConfiguration, |
49 | | - UserConfigFormatError, |
50 | | - NoInterpretersInUserConfigException, |
51 | | - JSONDecodeError, |
52 | | - ): |
53 | | - pass |
| 29 | + return get_interpreters_from_yaml(config_file) |
54 | 30 | return None |
55 | 31 |
|
56 | 32 |
|
57 | 33 | def check_box_dialog(config_file=None) -> GivenInterpreters: |
58 | | - from cookiecutter_python.handle.interpreters_support import ( |
59 | | - handle as get_interpreters, |
60 | | - ) |
61 | | - |
62 | | - defaults = None |
| 34 | + defaults: t.Optional[t.Sequence[str]] = None |
63 | 35 | if config_file: |
64 | | - try: |
65 | | - defaults = get_interpreters_from_yaml(config_file)['supported-interpreters'] |
66 | | - except ( |
67 | | - InvalidConfiguration, |
68 | | - UserConfigFormatError, |
69 | | - NoInterpretersInUserConfigException, |
70 | | - JSONDecodeError, |
71 | | - ): |
72 | | - pass |
73 | | - return get_interpreters(choices=defaults) |
74 | | - |
75 | | - |
76 | | -def get_interpreters_from_yaml(config_file: str) -> GivenInterpreters: |
77 | | - """Parse the 'interpreters' variable out of the user's config yaml file. |
78 | | -
|
79 | | - Args: |
80 | | - config_file (str): path to the user's config yaml file |
81 | | -
|
82 | | - Raises: |
83 | | - InvalidConfiguration: if yaml parser fails to load the user's config |
84 | | - UserConfigFormatError: if yaml doesn't contain the 'default_context' key |
85 | | - NoInterpretersInUserConfigException: if yaml doesn't contain the |
86 | | - 'interpreters' key, under the 'default_context' key |
87 | | - JSONDecodeError: if json parser fails to load the 'interpreters' value |
88 | | -
|
89 | | - Returns: |
90 | | - GivenInterpreters: dictionary with intepreters as a sequence of strings, |
91 | | - mapped to the 'supported-interpreters' key |
92 | | - """ |
93 | | - data = load_yaml(config_file) |
94 | | - if 'default_context' not in data: |
95 | | - raise UserConfigFormatError( |
96 | | - "User config (is valid yaml but) does not contain a 'default_context' outer key!" |
97 | | - ) |
98 | | - context = data['default_context'] |
99 | | - if 'interpreters' not in context: |
100 | | - raise NoInterpretersInUserConfigException( |
101 | | - "No 'interpreters' key found in user's config (under the 'default_context' key)." |
| 36 | + interpreters_data: t.Optional[GivenInterpreters] = get_interpreters_from_yaml( |
| 37 | + config_file |
102 | 38 | ) |
103 | | - interpreters_data = json.loads(context['interpreters']) |
104 | | - if 'supported-interpreters' not in interpreters_data: |
105 | | - raise UserConfigFormatError( |
106 | | - "User config (is valid yaml but) does not contain a " |
107 | | - "'supported-interpreters' key in the 'interpreters' key" |
108 | | - ) |
109 | | - return {'supported-interpreters': interpreters_data['supported-interpreters']} |
| 39 | + if interpreters_data: |
| 40 | + defaults = interpreters_data.get('supported-interpreters', None) |
| 41 | + return get_interpreters(choices=defaults) |
110 | 42 |
|
111 | 43 |
|
112 | 44 | def generate( |
@@ -174,11 +106,3 @@ def generate( |
174 | 106 |
|
175 | 107 | class CheckPypiError(Exception): |
176 | 108 | pass |
177 | | - |
178 | | - |
179 | | -class UserConfigFormatError(Exception): |
180 | | - pass |
181 | | - |
182 | | - |
183 | | -class NoInterpretersInUserConfigException(Exception): |
184 | | - pass |
0 commit comments