-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Finalised unit tests for HTML report and create TemplateHTML class
- Loading branch information
1 parent
1df791f
commit fba1a60
Showing
6 changed files
with
409 additions
and
202 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
"""Utils to assist in the creation of a HTML report for GTFS.""" | ||
from typing import Union | ||
import pathlib | ||
import shutil | ||
import os | ||
|
||
from transport_performance.utils.defence import ( | ||
_bool_defence, | ||
_string_defence, | ||
_is_path_like, | ||
) | ||
|
||
|
||
class TemplateHTML: | ||
"""A class for inserting HTML string into a docstring.""" | ||
|
||
def __init__(self, path: Union[str, pathlib.Path]) -> None: | ||
"""Initialise the TemplateHTML object. | ||
Parameters | ||
---------- | ||
path : Union[str, pathlib.Path] | ||
The file path of the html template | ||
Returns | ||
------- | ||
None | ||
""" | ||
_is_path_like(path, "path") | ||
with open(path, "r", encoding="utf8") as f: | ||
self.template = f.read() | ||
return None | ||
|
||
def insert( | ||
self, placeholder: str, value: str, replace_multiple: bool = False | ||
) -> None: | ||
"""Insert values into the html template. | ||
Parameters | ||
---------- | ||
placeholder : str | ||
The placeholder name in the template. | ||
This is a string. In the template it | ||
should be surrounded by sqsuare brackets. | ||
value : str | ||
The value to place in the placeholder | ||
location. | ||
replace_multiple : bool, optional | ||
Whether or not to replace multiple | ||
placeholders that share the same | ||
placeholder value, | ||
by default False | ||
Returns | ||
------- | ||
None | ||
""" | ||
_string_defence(placeholder, "placeholder") | ||
_string_defence(value, "value") | ||
_bool_defence(replace_multiple, "replace_multiple") | ||
occurences = len(self.template.split(f"[{placeholder}]")) - 1 | ||
if occurences > 1 and not replace_multiple: | ||
raise ValueError( | ||
"You have selected not to replace multiple" | ||
"placeholders of the same value, however" | ||
"placeholders occur more than once. \n" | ||
"If you would like to allow this, set the" | ||
"replace_multiple param to True" | ||
) | ||
|
||
self.template = self.template.replace(f"[{placeholder}]", value) | ||
|
||
def get_template(self) -> str: | ||
"""Get the template attribute of the TemplateHTML object. | ||
Returns | ||
------- | ||
str | ||
The template attribute | ||
""" | ||
return self.template | ||
|
||
|
||
def set_up_report_dir( | ||
path: Union[str, pathlib.Path] = "outputs", overwrite: bool = False | ||
) -> None: | ||
"""Set up the directory that will hold the report. | ||
Parameters | ||
---------- | ||
path : Union[str, pathlib.Path], optional | ||
The path to the directory, | ||
by default "outputs" | ||
overwrite : bool, optional | ||
Whether or not to overwrite any current reports, | ||
by default False | ||
Returns | ||
------- | ||
None | ||
""" | ||
# defences | ||
if not os.path.exists(path): | ||
raise FileNotFoundError( | ||
"The specified path does not exist. " f"Path passed: {str(path)}" | ||
) | ||
|
||
if os.path.exists(f"{path}/gtfs_report") and not overwrite: | ||
raise FileExistsError( | ||
"Report already exists at path: " | ||
f"[{path}]." | ||
"Consider setting overwrite=True" | ||
"if you'd like to overwrite this." | ||
) | ||
|
||
try: | ||
os.mkdir(f"{path}/gtfs_report") | ||
except FileExistsError: | ||
pass | ||
shutil.copy( | ||
src="src/transport_performance/gtfs/report/css_styles/styles.css", | ||
dst=f"{path}/gtfs_report", | ||
) | ||
return None |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.