|
| 1 | +"""Interactive Dialog wizards to gather User Input for Context variables.""" |
| 2 | + |
1 | 3 | import json |
2 | 4 | import logging |
3 | 5 | import typing as t |
@@ -69,51 +71,73 @@ def parse_context(config_file: str): |
69 | 71 | **{k: v[0] for k, v in choices.items() if k not in user_default_context}, |
70 | 72 | ) |
71 | 73 |
|
| 74 | + # Render cookiecutter.json again with context to resolve derived fields |
| 75 | + # This ensures derived fields like pkg_name get computed properly |
| 76 | + template = env.get_template('cookiecutter.json') |
| 77 | + rendered_with_context = template.render({'cookiecutter': context_defaults}) |
| 78 | + resolved_cook_json: t.Mapping[str, t.Any] = json.loads(rendered_with_context) |
| 79 | + |
| 80 | + # Update context_defaults with resolved derived fields, but preserve user config values |
| 81 | + for key, value in resolved_cook_json.items(): |
| 82 | + # Only update if the key wasn't explicitly provided by user |
| 83 | + if key not in user_default_context: |
| 84 | + context_defaults[key] = value |
| 85 | + |
72 | 86 | pipe = InteractiveDialogsPipeline() |
73 | 87 |
|
74 | | - # assert choices['rtd_python_version'] == [], f"DEBUG: {choices['rtd_python_version']}" |
75 | | - res = pipe.process( |
76 | | - [ |
77 | | - { |
78 | | - "project_name": context_defaults['project_name'], |
79 | | - "project_type": { |
80 | | - 'default': context_defaults['project_type'], |
81 | | - 'choices': choices['project_type'], |
82 | | - }, |
83 | | - "full_name": context_defaults['full_name'], |
84 | | - "author_email": context_defaults['author_email'], |
85 | | - "github_username": context_defaults['github_username'], |
86 | | - "project_short_description": context_defaults['project_short_description'], |
87 | | - # "release_date": context_defaults['release_date'], |
88 | | - # "year": context_defaults['year'], |
89 | | - "version": context_defaults['version'], |
90 | | - "initialize_git_repo": { |
91 | | - 'default': context_defaults['initialize_git_repo'], |
92 | | - 'choices': choices['initialize_git_repo'], |
93 | | - }, |
94 | | - "supported-interpreters": { |
95 | | - # 'default': context_defaults['initialize_git_repo'], |
96 | | - 'choices': [ |
97 | | - (choice, True) |
98 | | - for choice in user_interpreters.get( |
99 | | - 'supported-interpreters', |
100 | | - ["3.6", "3.7", "3.8", "3.9", "3.10", "3.11"], |
101 | | - ) |
102 | | - ], |
103 | | - }, |
104 | | - "docs_builder": { |
105 | | - 'default': context_defaults['docs_builder'], |
106 | | - 'choices': choices['docs_builder'], |
107 | | - }, |
108 | | - "rtd_python_version": { |
109 | | - 'default': context_defaults['rtd_python_version'], |
110 | | - 'choices': choices['rtd_python_version'], |
111 | | - }, |
112 | | - "cicd": { |
113 | | - 'default': context_defaults['cicd'], |
114 | | - 'choices': choices['cicd'], |
115 | | - }, |
| 88 | + # Build the context dynamically to include all fields |
| 89 | + interactive_context = {} |
| 90 | + |
| 91 | + # Simple fields (no choices) |
| 92 | + simple_fields = [ |
| 93 | + 'project_name', |
| 94 | + 'project_slug', |
| 95 | + 'pkg_name', |
| 96 | + 'repo_name', |
| 97 | + 'readthedocs_project_slug', |
| 98 | + 'docker_image', |
| 99 | + 'full_name', |
| 100 | + 'author', |
| 101 | + 'author_email', |
| 102 | + 'github_username', |
| 103 | + 'project_short_description', |
| 104 | + 'pypi_subtitle', |
| 105 | + 'version', |
| 106 | + ] |
| 107 | + |
| 108 | + for field in simple_fields: |
| 109 | + if field in context_defaults: |
| 110 | + interactive_context[field] = context_defaults[field] |
| 111 | + |
| 112 | + # Choice fields (with options) |
| 113 | + choice_fields = [ |
| 114 | + 'project_type', |
| 115 | + 'initialize_git_repo', |
| 116 | + 'docs_builder', |
| 117 | + 'rtd_python_version', |
| 118 | + 'cicd', |
| 119 | + # Include Grafana, Loki stack no/yes |
| 120 | + 'include_observability', # ["no", "yes"] |
| 121 | + ] |
| 122 | + |
| 123 | + for field in choice_fields: |
| 124 | + if field in choices: |
| 125 | + interactive_context[field] = { |
| 126 | + 'default': context_defaults[field], |
| 127 | + 'choices': choices[field], |
116 | 128 | } |
117 | | - ] |
118 | | - ) |
| 129 | + |
| 130 | + # Special handling for interpreters |
| 131 | + interactive_context["supported-interpreters"] = { |
| 132 | + 'choices': [ |
| 133 | + (choice, True) |
| 134 | + for choice in user_interpreters.get( |
| 135 | + 'supported-interpreters', |
| 136 | + ["3.6", "3.7", "3.8", "3.9", "3.10", "3.11"], |
| 137 | + ) |
| 138 | + ], |
| 139 | + } |
| 140 | + |
| 141 | + # assert choices['rtd_python_version'] == [], f"DEBUG: {choices['rtd_python_version']}" |
| 142 | + res = pipe.process([interactive_context]) |
119 | 143 | return res |
0 commit comments