|
| 1 | +import json |
1 | 2 | import logging |
2 | 3 | import typing as t |
3 | 4 |
|
4 | | -from cookiecutter_python.handle.interpreters_support import handle as get_interpreters |
5 | | - |
6 | | -from .load_config import get_interpreters_from_yaml |
7 | | - |
8 | 5 | GivenInterpreters = t.Mapping[str, t.Sequence[str]] |
9 | 6 |
|
10 | 7 | logger = logging.getLogger(__name__) |
11 | 8 |
|
12 | 9 |
|
13 | | -def supported_interpreters(config_file: str, no_input: bool) -> t.Optional[GivenInterpreters]: |
14 | | - # Interactive Mode: ask for user input with Interactive (console) Dialog |
15 | | - if not no_input: # render checkbox console ui, and return user selection |
16 | | - # TODO: verify that the below works! |
17 | | - return check_box_dialog(config_file=config_file) |
18 | | - # NON Interactive Mode: try to automatically read interpreters from config |
19 | | - if config_file: |
20 | | - # read 'default_context.interpreters' from User Config yaml file |
21 | | - return get_interpreters_from_yaml(config_file) |
22 | | - ## No User Config yaml file, was supplied through CLI parameters, at runtime |
23 | | - return None |
| 10 | +def parse_context(config_file: str): |
| 11 | + # initialize data |
| 12 | + user_context = {} |
| 13 | + from pathlib import Path |
| 14 | + |
| 15 | + my_dir = Path(__file__).parent.absolute() |
| 16 | + |
| 17 | + # use whatever way cookiecutter uses to render the cookiecutter.json |
| 18 | + # cookie uses Jinja2 |
| 19 | + |
| 20 | + from jinja2 import Environment, FileSystemLoader |
| 21 | + |
| 22 | + # render file |
| 23 | + env = Environment( |
| 24 | + loader=FileSystemLoader(str(my_dir / '..')), |
| 25 | + extensions=['jinja2_time.TimeExtension'], # shipped with cookiecutter 1.7 |
| 26 | + ) |
| 27 | + template = env.get_template('cookiecutter.json') |
| 28 | + rendered = template.render({'cookiecutter': {}}) |
| 29 | + |
| 30 | + assert isinstance(rendered, str) |
24 | 31 |
|
| 32 | + cook_json: t.Mapping[str, t.Any] = json.loads(rendered) |
| 33 | + |
| 34 | + # in cookiecutter 1.7, a 'choice' variable is a json array |
| 35 | + choices = {k: v for k, v in cook_json.items() if isinstance(v, list)} |
| 36 | + assert 'rtd_python_version' in choices, f"KEYS: {choices.keys()}" |
| 37 | + choice_defaults = {k: v[0] for k, v in choices.items()} |
| 38 | + |
| 39 | + # start |
| 40 | + c = cook_json['interpreters'] |
| 41 | + |
| 42 | + cookie_defaults = dict(cook_json, **choice_defaults) |
25 | 43 |
|
26 | | -def check_box_dialog(config_file: t.Optional[str] = None) -> GivenInterpreters: |
27 | | - defaults: t.Optional[t.Sequence[str]] = None |
28 | 44 | if config_file: |
29 | | - interpreters_data: t.Optional[GivenInterpreters] = get_interpreters_from_yaml( |
30 | | - config_file |
31 | | - ) |
32 | | - if interpreters_data: |
33 | | - defaults = interpreters_data.get('supported-interpreters', None) |
34 | | - return get_interpreters(choices=defaults) |
| 45 | + from .user_config_proxy import get_user_config |
| 46 | + |
| 47 | + data = get_user_config(config_file, default_config=False) |
| 48 | + # data = load_yaml(config_file) |
| 49 | + user_context = data['default_context'] |
| 50 | + c = json.loads(user_context.get('interpreters', '{}')) |
| 51 | + |
| 52 | + context_defaults = dict(cookie_defaults, **user_context) |
| 53 | + |
| 54 | + from cookiecutter_python.handle.interactive_cli_pipeline import ( |
| 55 | + InteractiveDialogsPipeline, |
| 56 | + ) |
| 57 | + |
| 58 | + pipe = InteractiveDialogsPipeline() |
| 59 | + |
| 60 | + # c = json.loads(context_defaults.get('interpreters', '{}')) |
| 61 | + cc = c.get('supported-interpreters', ["3.6", "3.7", "3.8", "3.9", "3.10", "3.11"]) |
| 62 | + # assert choices['rtd_python_version'] == [], f"DEBUG: {choices['rtd_python_version']}" |
| 63 | + res = pipe.process( |
| 64 | + [ |
| 65 | + { |
| 66 | + "project_name": context_defaults['project_name'], |
| 67 | + "project_type": { |
| 68 | + 'default': context_defaults['project_type'], |
| 69 | + 'choices': choices['project_type'], |
| 70 | + }, |
| 71 | + "full_name": context_defaults['full_name'], |
| 72 | + "author_email": context_defaults['author_email'], |
| 73 | + "github_username": context_defaults['github_username'], |
| 74 | + "project_short_description": context_defaults['project_short_description'], |
| 75 | + # "release_date": context_defaults['release_date'], |
| 76 | + # "year": context_defaults['year'], |
| 77 | + "version": context_defaults['version'], |
| 78 | + "initialize_git_repo": { |
| 79 | + 'default': context_defaults['initialize_git_repo'], |
| 80 | + 'choices': choices['initialize_git_repo'], |
| 81 | + }, |
| 82 | + "supported-interpreters": { |
| 83 | + # 'default': context_defaults['initialize_git_repo'], |
| 84 | + 'choices': [(choice, True) for choice in cc], |
| 85 | + }, |
| 86 | + "docs_builder": { |
| 87 | + 'default': context_defaults['docs_builder'], |
| 88 | + 'choices': choices['docs_builder'], |
| 89 | + }, |
| 90 | + "rtd_python_version": { |
| 91 | + 'default': context_defaults['rtd_python_version'], |
| 92 | + 'choices': choices['rtd_python_version'], |
| 93 | + }, |
| 94 | + } |
| 95 | + ] |
| 96 | + ) |
| 97 | + return res |
0 commit comments