add image, rework SemesterDocument
This commit is contained in:
BIN
docs/images/statistics.png
Normal file
BIN
docs/images/statistics.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 30 KiB |
@@ -6,22 +6,57 @@ from docx.oxml import OxmlElement
|
|||||||
from docx.oxml.ns import qn
|
from docx.oxml.ns import qn
|
||||||
import os
|
import os
|
||||||
from os.path import basename
|
from os.path import basename
|
||||||
|
from loguru import logger as log
|
||||||
|
import sys
|
||||||
|
|
||||||
|
|
||||||
|
logger = log
|
||||||
|
logger.remove()
|
||||||
|
logger.add("logs/application.log", rotation="1 week", enqueue=True)
|
||||||
|
log.add(
|
||||||
|
f"logs/{datetime.now().strftime('%Y-%m-%d')}.log",
|
||||||
|
rotation="1 day",
|
||||||
|
compression="zip",
|
||||||
|
)
|
||||||
|
|
||||||
|
# logger.add(sys.stderr, format="{time} {level} {message}", level="INFO")
|
||||||
|
logger.add(sys.stdout)
|
||||||
|
|
||||||
|
|
||||||
|
class SemesterError(Exception):
|
||||||
|
"""Custom exception for semester-related errors."""
|
||||||
|
|
||||||
|
def __init__(self, message):
|
||||||
|
super().__init__(message)
|
||||||
|
logger.error(message)
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return f"SemesterError: {self.args[0]}"
|
||||||
|
|
||||||
|
|
||||||
class SemesterDocument:
|
class SemesterDocument:
|
||||||
def __init__(self, apparats: list[tuple[int, str]], semester: str, filename):
|
def __init__(
|
||||||
assert isinstance(apparats, list), "Apparats must be a list of tuples"
|
self,
|
||||||
assert all(isinstance(apparat, tuple) for apparat in apparats), (
|
apparats: list[tuple[int, str]],
|
||||||
|
semester: str,
|
||||||
|
filename,
|
||||||
|
config,
|
||||||
|
full: bool = False,
|
||||||
|
):
|
||||||
|
assert isinstance(apparats, list), SemesterError(
|
||||||
"Apparats must be a list of tuples"
|
"Apparats must be a list of tuples"
|
||||||
)
|
)
|
||||||
assert all(isinstance(apparat[0], int) for apparat in apparats), (
|
assert all(isinstance(apparat, tuple) for apparat in apparats), SemesterError(
|
||||||
|
"Apparats must be a list of tuples"
|
||||||
|
)
|
||||||
|
assert all(isinstance(apparat[0], int) for apparat in apparats), SemesterError(
|
||||||
"Apparat numbers must be integers"
|
"Apparat numbers must be integers"
|
||||||
)
|
)
|
||||||
assert all(isinstance(apparat[1], str) for apparat in apparats), (
|
assert all(isinstance(apparat[1], str) for apparat in apparats), SemesterError(
|
||||||
"Apparat names must be strings"
|
"Apparat names must be strings"
|
||||||
)
|
)
|
||||||
assert isinstance(semester, str), "Semester must be a string"
|
assert isinstance(semester, str), SemesterError("Semester must be a string")
|
||||||
assert "." not in filename and isinstance(filename, str), (
|
assert "." not in filename and isinstance(filename, str), SemesterError(
|
||||||
"Filename must be a string and not contain an extension"
|
"Filename must be a string and not contain an extension"
|
||||||
)
|
)
|
||||||
self.doc = Document()
|
self.doc = Document()
|
||||||
@@ -35,7 +70,17 @@ class SemesterDocument:
|
|||||||
self.color_red = RGBColor(255, 0, 0)
|
self.color_red = RGBColor(255, 0, 0)
|
||||||
self.color_blue = RGBColor(0, 0, 255)
|
self.color_blue = RGBColor(0, 0, 255)
|
||||||
self.filename = filename
|
self.filename = filename
|
||||||
|
self.settings = config
|
||||||
|
if full:
|
||||||
|
logger.info("Full document generation")
|
||||||
|
self.make_document()
|
||||||
|
logger.info("Document created")
|
||||||
|
self.create_pdf()
|
||||||
|
logger.info("PDF created")
|
||||||
|
self.print_document()
|
||||||
|
logger.info("Document printed")
|
||||||
|
self.cleanup()
|
||||||
|
logger.info("Cleanup done")
|
||||||
def set_table_border(self, table):
|
def set_table_border(self, table):
|
||||||
"""
|
"""
|
||||||
Adds a full border to the table.
|
Adds a full border to the table.
|
||||||
@@ -161,8 +206,8 @@ class SemesterDocument:
|
|||||||
from email.mime.multipart import MIMEMultipart
|
from email.mime.multipart import MIMEMultipart
|
||||||
from email.mime.application import MIMEApplication
|
from email.mime.application import MIMEApplication
|
||||||
from email.mime.text import MIMEText
|
from email.mime.text import MIMEText
|
||||||
from src import settings as config
|
|
||||||
|
|
||||||
|
config = self.settings
|
||||||
smtp = config.mail.smtp_server
|
smtp = config.mail.smtp_server
|
||||||
port = config.mail.port
|
port = config.mail.port
|
||||||
sender_email = config.mail.sender
|
sender_email = config.mail.sender
|
||||||
@@ -202,7 +247,7 @@ class SemesterDocument:
|
|||||||
doc.SaveAs(f"{curdir}/{self.filename}.pdf", FileFormat=17)
|
doc.SaveAs(f"{curdir}/{self.filename}.pdf", FileFormat=17)
|
||||||
doc.Close()
|
doc.Close()
|
||||||
word.Quit()
|
word.Quit()
|
||||||
print("PDF saved")
|
logger.debug("PDF saved")
|
||||||
|
|
||||||
def cleanup(self):
|
def cleanup(self):
|
||||||
os.remove(f"{self.filename}.docx")
|
os.remove(f"{self.filename}.docx")
|
||||||
@@ -210,14 +255,15 @@ class SemesterDocument:
|
|||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
apparat = [(i, f"Item {i}") for i in range(405, 438)]
|
pass
|
||||||
doc = SemesterDocument(
|
# apparat = [(i, f"Item {i}") for i in range(405, 438)]
|
||||||
apparat,
|
# doc = SemesterDocument(
|
||||||
"WiSe 24/25",
|
# apparat,
|
||||||
"semap",
|
# "WiSe 24/25",
|
||||||
)
|
# "semap",
|
||||||
doc.make_document()
|
# )
|
||||||
doc.create_pdf()
|
# doc.make_document()
|
||||||
|
# doc.create_pdf()
|
||||||
# doc.print_document()
|
# doc.print_document()
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user