-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added pdf renderer, while making the file writing class generic.
- Loading branch information
Showing
4 changed files
with
54 additions
and
6 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
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,31 @@ | ||
from abc import ABC, abstractmethod | ||
from datetime import date | ||
|
||
class ReportWriter(ABC): | ||
def __init__(self): | ||
super().__init__() | ||
|
||
@abstractmethod | ||
def write(self, buffer, base_name): | ||
pass | ||
|
||
|
||
class ReportWriterHtml(ReportWriter): | ||
|
||
def write(self, buffer, base_name): | ||
filename = f"output/{date.today().strftime('%y-%m-%d')}_{base_name}.html" | ||
with open(filename, 'w', encoding='utf-8') as file: | ||
file.write(buffer) | ||
|
||
|
||
class ReportWriterPdf(ReportWriter): | ||
|
||
def __init__(self): | ||
try: | ||
from weasyprint import HTML | ||
except ImportError: | ||
raise ImportError('Failed to import weasyprint') | ||
super.__init__() | ||
|
||
def write(self, buffer, base_name): | ||
HTML(string=buffer).write_pdf(f"output/{date.today().strftime('%y-%m-%d')}_{base_name}.pdf") |
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
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