|
16 | 16 | from os import makedirs |
17 | 17 | from pathlib import Path |
18 | 18 |
|
19 | | -import pkg_resources |
| 19 | +import importlib.resources as resources |
20 | 20 | from lxml import etree |
21 | 21 |
|
22 | 22 | import mpas_analysis.version |
@@ -66,13 +66,13 @@ def generate_html(config, analyses, controlConfig, customConfigFiles): |
66 | 66 | try: |
67 | 67 | ComponentPage.add_image(fileName, config, components, |
68 | 68 | controlConfig) |
69 | | - except IOError: |
70 | | - print(' missing file {}'.format(fileName)) |
| 69 | + except IOError as e: |
| 70 | + print(f'Error reading {fileName}: {e}') |
71 | 71 | missingCount += 1 |
72 | 72 |
|
73 | 73 | if missingCount > 0: |
74 | | - print('Warning: {} XML files were missing and the analysis website' |
75 | | - ' will be incomplete.'.format(missingCount)) |
| 74 | + print(f'Warning: {missingCount} XML files could not be read and the ' |
| 75 | + f'analysis website will be incomplete.') |
76 | 76 | # generate the page for each component and add the component to the main |
77 | 77 | # page |
78 | 78 | for componentName, component in components.items(): |
@@ -155,24 +155,15 @@ def __init__(self, config, controlConfig, customConfigFiles): |
155 | 155 | self.customConfigFiles = customConfigFiles |
156 | 156 |
|
157 | 157 | # get template text |
158 | | - fileName = \ |
159 | | - pkg_resources.resource_filename(__name__, |
160 | | - "templates/main_page.html") |
161 | | - |
162 | | - with open(fileName, 'r') as templateFile: |
163 | | - self.pageTemplate = templateFile.read() |
164 | | - |
165 | | - fileName = \ |
166 | | - pkg_resources.resource_filename(__name__, |
167 | | - "templates/main_component.html") |
168 | | - with open(fileName, 'r') as templateFile: |
169 | | - self.componentTemplate = templateFile.read() |
170 | | - |
171 | | - fileName = \ |
172 | | - pkg_resources.resource_filename(__name__, |
173 | | - "templates/config.html") |
174 | | - with open(fileName, 'r') as templateFile: |
175 | | - self.configTemplate = templateFile.read() |
| 158 | + package = 'mpas_analysis.shared.html.templates' |
| 159 | + templates = {} |
| 160 | + for name in ['main_page', 'main_component', 'config']: |
| 161 | + resource = resources.files(package).joinpath(f'{name}.html') |
| 162 | + with resource.open('r') as templateFile: |
| 163 | + templates[name] = templateFile.read() |
| 164 | + self.pageTemplate = templates['main_page'] |
| 165 | + self.componentTemplate = templates['main_component'] |
| 166 | + self.configTemplate = templates['config'] |
176 | 167 |
|
177 | 168 | # start with no components |
178 | 169 | self.components = OrderedDict() |
@@ -273,45 +264,34 @@ def generate(self): |
273 | 264 |
|
274 | 265 | pageText = _replace_tempate_text(self.pageTemplate, replacements) |
275 | 266 |
|
276 | | - htmlBaseDirectory = build_config_full_path(self.config, 'output', |
277 | | - 'htmlSubdirectory') |
| 267 | + htmlBaseDirectory = build_config_full_path( |
| 268 | + self.config, 'output', 'htmlSubdirectory' |
| 269 | + ) |
278 | 270 |
|
279 | 271 | for subdir in ['css', 'js']: |
280 | | - try: |
281 | | - makedirs('{}/{}'.format(htmlBaseDirectory, subdir)) |
282 | | - except OSError: |
283 | | - pass |
| 272 | + makedirs(f'{htmlBaseDirectory}/{subdir}', exist_ok=True) |
284 | 273 |
|
285 | | - outFileName = '{}/index.html'.format(htmlBaseDirectory) |
| 274 | + outFileName = f'{htmlBaseDirectory}/index.html' |
286 | 275 |
|
287 | 276 | with open(outFileName, mode='w') as mainFile: |
288 | 277 | mainFile.write( |
289 | | - pageText.encode('ascii', |
290 | | - 'xmlcharrefreplace').decode('ascii')) |
| 278 | + pageText.encode('ascii', 'xmlcharrefreplace').decode('ascii') |
| 279 | + ) |
291 | 280 |
|
292 | 281 | # copy the css and js files as well as general images |
293 | | - fileName = \ |
294 | | - pkg_resources.resource_filename(__name__, |
295 | | - "templates/style.css") |
296 | | - copyfile(fileName, '{}/css/style.css'.format(htmlBaseDirectory)) |
297 | | - |
298 | | - fileName = \ |
299 | | - pkg_resources.resource_filename(__name__, |
300 | | - "templates/index.js") |
301 | | - copyfile(fileName, '{}/js/index.js'.format(htmlBaseDirectory)) |
302 | | - |
303 | | - fileName = \ |
304 | | - pkg_resources.resource_filename(__name__, |
305 | | - "templates/mpas_logo.png") |
306 | | - copyfile(fileName, '{}/mpas_logo.png'.format(htmlBaseDirectory)) |
307 | | - |
308 | | - fileName = \ |
309 | | - pkg_resources.resource_filename(__name__, |
310 | | - "templates/config.png") |
311 | | - copyfile(fileName, '{}/config.png'.format(htmlBaseDirectory)) |
312 | | - |
313 | | - with open('{}/complete.{}.cfg'.format(htmlBaseDirectory, |
314 | | - runName), 'w') as configFile: |
| 282 | + resource_targets = [ |
| 283 | + ("style.css", "css/style.css"), |
| 284 | + ("index.js", "js/index.js"), |
| 285 | + ("mpas_logo.png", "mpas_logo.png"), |
| 286 | + ("config.png", "config.png"), |
| 287 | + ] |
| 288 | + for resource_name, target_path in resource_targets: |
| 289 | + package = 'mpas_analysis.shared.html.templates' |
| 290 | + fileName = resources.files(package).joinpath(resource_name) |
| 291 | + copyfile(str(fileName), f'{htmlBaseDirectory}/{target_path}') |
| 292 | + |
| 293 | + outFileName = f'{htmlBaseDirectory}/complete.{runName}.cfg' |
| 294 | + with open(outFileName, 'w') as configFile: |
315 | 295 | self.config.write(configFile) |
316 | 296 |
|
317 | 297 |
|
@@ -386,11 +366,10 @@ def __init__(self, config, name, subdirectory, controlConfig=None): |
386 | 366 | for templateName in ['page', 'quicklink', 'group', 'gallery', 'image', |
387 | 367 | 'subtitle']: |
388 | 368 | # get template text |
389 | | - fileName = pkg_resources.resource_filename( |
390 | | - __name__, |
391 | | - "templates/component_{}.html".format(templateName)) |
392 | | - |
393 | | - with open(fileName, 'r') as templateFile: |
| 369 | + package = 'mpas_analysis.shared.html.templates' |
| 370 | + resource = resources.files(package).joinpath( |
| 371 | + f'component_{templateName}.html') |
| 372 | + with resource.open('r') as templateFile: |
394 | 373 | self.templates[templateName] = templateFile.read() |
395 | 374 |
|
396 | 375 | # start with no groups |
|
0 commit comments