-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathhelpers.py
More file actions
266 lines (232 loc) · 8.41 KB
/
helpers.py
File metadata and controls
266 lines (232 loc) · 8.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
# Copyright © 2025 CloudBlue. All rights reserved.
import functools
import json
import os
from click import ClickException
from interrogatio.core.dialog import dialogus
from openpyxl import Workbook
from connect.cli import get_version
from connect.cli.core.terminal import console
from connect.cli.plugins.project.renderer import BoilerplateRenderer
from connect.cli.plugins.project.report.validations import validate_reports_json, validators
from connect.cli.plugins.project.report.wizard import (
ADD_REPORT_QUESTIONS,
BOOTSTRAP_QUESTIONS,
BOOTSTRAP_SUMMARY,
REPORT_ADD_WIZARD_INTRO,
REPORT_BOOTSTRAP_WIZARD_INTRO,
REPORT_SUMMARY,
)
from connect.cli.plugins.project.utils import show_validation_result_table
def bootstrap_report_project(output_dir, overwrite):
console.secho('Bootstraping report project...\n', fg='blue')
answers = dialogus(
BOOTSTRAP_QUESTIONS,
'Reports project bootstrap',
intro=REPORT_BOOTSTRAP_WIZARD_INTRO,
summary=BOOTSTRAP_SUMMARY,
finish_text='Create',
previous_text='Back',
)
if not answers:
raise ClickException('Aborted by user input')
project_dir = os.path.join(output_dir, answers['project_slug'])
if not overwrite and os.path.exists(project_dir):
raise ClickException(f'The destination directory {project_dir} already exists.')
exclude = []
if answers['use_github_actions'] == 'n':
exclude.extend(['.github', '.github/**/*'])
answers['cli_version'] = get_version()
renderer = BoilerplateRenderer(
context=answers,
template_folder=os.path.join(os.path.dirname(__file__), 'templates', 'bootstrap'),
output_dir=project_dir,
overwrite=overwrite,
exclude=exclude,
post_render=functools.partial(
generate_empty_xlsx,
os.path.join(
answers['project_slug'],
answers['package_name'],
answers['initial_report_slug'],
),
),
)
renderer.render()
console.markdown(open(f'{project_dir}/HOWTO.md', 'r').read())
def add_report(project_dir, package_name):
project_path = os.path.join(project_dir, package_name)
if not os.path.exists(project_path):
raise ClickException(
f'The directory package called `{project_path}` does not exist,'
'\nPlease, create it or choose an existing one using `-n` option.',
)
# Required: check if the project descriptor is valid
validation_result = validate_reports_json(project_dir, None)
if validation_result.items:
errors = [error.message for error in validation_result.items]
raise ClickException(f'Invalid `reports.json`: {errors}')
console.secho(f'Adding new report to project {project_dir}...\n', fg='blue')
answers = dialogus(
ADD_REPORT_QUESTIONS,
'Reports add project',
intro=REPORT_ADD_WIZARD_INTRO,
summary=REPORT_SUMMARY,
finish_text='Create',
previous_text='Back',
)
if not answers:
raise ClickException('Aborted by user input')
report_path = os.path.join(project_path, answers['initial_report_slug'])
if os.path.exists(report_path):
raise ClickException(
f'\nThe report directory called `{report_path}` already exists, '
'\nplease, choose other report name or delete the existing one.',
)
template_folder = os.path.join(os.path.dirname(__file__), 'templates', 'add')
renderer = BoilerplateRenderer(
context=answers,
template_folder=template_folder,
output_dir=project_path,
post_render=functools.partial(
generate_empty_xlsx,
answers['initial_report_slug'],
),
)
renderer.render()
_add_report_to_descriptor(
project_dir=project_dir,
package_dir=package_name,
context=renderer.context,
)
console.secho(
f'\nReport {answers["initial_report_slug"]} has been successfully added to the project'
f' {project_dir}.',
fg='blue',
)
def _add_report_to_descriptor(project_dir, package_dir, context):
project_descriptor = os.path.join(project_dir, 'reports.json')
project_desc = json.load(open(project_descriptor, 'r'))
report = {
'name': context['initial_report_name'],
'readme_file': os.path.join(package_dir, context['initial_report_slug'], 'README.md'),
'entrypoint': f"{package_dir}.{context['initial_report_slug']}.entrypoint.generate",
'audience': [
'provider',
'vendor',
],
'report_spec': '2',
'parameters': [],
'renderers': [
{
'id': 'xlsx',
'type': 'xlsx',
'default': True if context['initial_report_renderer'] == 'xlsx' else False,
'description': 'Export data in Microsoft Excel 2020 format.',
'template': os.path.join(
package_dir,
context['initial_report_slug'],
'templates',
'xlsx',
'template.xlsx',
),
'args': {
'start_row': 2,
'start_col': 1,
},
},
{
'id': 'json',
'type': 'json',
'default': True if context['initial_report_renderer'] == 'json' else False,
'description': 'Export data as JSON',
},
{
'id': 'csv',
'type': 'csv',
'default': True if context['initial_report_renderer'] == 'csv' else False,
'description': 'Export data as CSV',
},
{
'id': 'xml',
'type': 'jinja2',
'default': True if context['initial_report_renderer'] == 'xml' else False,
'description': 'Export data as XML',
'template': os.path.join(
package_dir,
context['initial_report_slug'],
'templates',
'xml',
'template.xml.j2',
),
},
{
'id': 'pdf-portrait',
'type': 'pdf',
'default': True if context['initial_report_renderer'] == 'pdf' else False,
'description': 'Export data as PDF (portrait)',
'template': os.path.join(
package_dir,
context['initial_report_slug'],
'templates',
'pdf',
'template.html.j2',
),
'args': {
'css_file': os.path.join(
package_dir,
context['initial_report_slug'],
'templates',
'pdf',
'template.css',
),
},
},
],
}
project_desc['reports'].append(report)
json.dump(
project_desc,
open(f'{project_dir}/reports.json', 'w'),
indent=4,
)
def generate_empty_xlsx(dest_dir, base_dir, context):
destination = os.path.join(
base_dir,
dest_dir,
'templates',
'xlsx',
)
os.makedirs(destination, exist_ok=True)
wb = Workbook(
write_only=True,
)
wb.create_sheet('Data')
file_path = os.path.join(
destination,
'template.xlsx',
)
wb.save(
file_path,
)
console.print(f'File {file_path} generated [bold green]\u2713[/bold green]')
def validate_report_project(project_dir): # noqa: CCR001
console.secho(f'Validating project {project_dir}...\n', fg='blue')
context = {}
validation_items = []
for validator in validators:
result = validator(project_dir, context)
validation_items.extend(result.items)
if result.must_exit:
break
if result.context:
context.update(result.context)
if validation_items:
console.markdown('# Report validation results')
show_validation_result_table(validation_items)
console.secho(
f'Warning/errors have been found while validating the Report Project {project_dir}.',
fg='yellow',
)
else:
console.secho(f'Report Project {project_dir} has been successfully validated.', fg='green')