Commit b7678739 authored by Dominik Woiwode's avatar Dominik Woiwode
Browse files

Merge branch 'pathlib' into 'pter2.0'

make use of pathlib

See merge request !3
parents 32d77f58 61601b90
......@@ -20,8 +20,7 @@ class Semester:
def __post_init__(self):
self.displayName = f"{self.part.value} {self.year}"
self.gravPath = os.path.join(opt.GIT_REPO_PATH, opt.GRAV_PROTOCOL_PATH,
self._gravName(), "protocol_semester.de.md")
self.gravPath = Path(opt.GIT_REPO_PATH) / opt.GRAV_PROTOCOL_PATH / self._gravName() / "protocol_semester.de.md"
def _gravName(self):
return f"{self.year - 2000}{self.part.short()}"
......@@ -39,12 +38,12 @@ class Semester:
return Semester(date.year - 1, SemesterPart.Winter)
def uploadToGrav(self):
if os.path.exists(self.gravPath):
if self.gravPath.exists():
return
git = opt.git
os.makedirs(str(Path(self.gravPath).parent), exist_ok=True)
with open(self.gravPath, "w") as d:
self.gravPath.parent.mkdir(parents=True, exist_ok=True)
with self.gravPath.open("w") as d:
d.write(f"---\ntitle: {self.displayName}\n---")
git.add(opt.GRAV_PROTOCOL_PATH)
......@@ -146,24 +145,23 @@ class BasePad:
@property
def rawPath(self) -> Path:
return Path(os.path.join(opt.DATA_FOLDER, "raw", f"{self.displayName}.md")).absolute()
return Path(opt.DATA_FOLDER).absolute() / "raw" / f"{self.displayName}.md"
@property
def compiledPath(self) -> Path:
return Path(os.path.join(opt.DATA_FOLDER, "result", f"{self.displayName}.pdf")).absolute()
return Path(opt.DATA_FOLDER).absolute() / "result" / f"{self.displayName}.pdf"
def download(self, override=True):
path = self.rawPath
content = self.source.retrieveContent()
if not override and os.path.exists(str(path)):
raise FileExistsError(f"{path} already exists!")
os.makedirs(str(path.parent), exist_ok=True)
with open(str(path), "w", encoding="utf-8") as d:
if not override and self.rawPath.exists():
raise FileExistsError(f"{self.rawPath} already exists!")
self.rawPath.parent.mkdir(parents=True, exist_ok=True)
with self.rawPath.open("w", encoding="utf-8") as d:
d.write(content)
self.source = LocalCache(self.source.url, str(path))
self.source = LocalCache(self.source.url, str(self.rawPath))
def compile(self, template: str = None) -> int:
os.makedirs(str(self.compiledPath.parent), exist_ok=True)
self.compiledPath.parent.mkdir(parents=True, exist_ok=True)
metadataStr = (" " +
" ".join([f'-M {key}="{val}"' for key, val in self.metadata.items()]))
templateStr = ""
......@@ -195,10 +193,9 @@ class ProtocolPad(BasePad):
@property
def gravPath(self) -> Path:
return Path(os.path.join(opt.GIT_REPO_PATH, opt.GRAV_PROTOCOL_PATH,
self.semester._gravName(),
self.date.strftime("%Y-%m-%d") + "-fgvv" * self.isFachgruppenvollversammlung,
"default.de.md")).absolute()
return Path(
opt.GIT_REPO_PATH).absolute() / opt.GRAV_PROTOCOL_PATH / self.semester._gravName() / self.date.strftime(
"%Y-%m-%d") / str("-fgvv" * self.isFachgruppenvollversammlung) / "default.de.md"
def parseTops(self):
# TODO: better top filter?
......@@ -277,7 +274,7 @@ class ProtocolPad(BasePad):
# Init semester
# TODO: better file management
self.semester.uploadToGrav()
if not os.path.exists(str(self.rawPath)):
if not self.rawPath.exists():
raise FileNotFoundError(f"{self.rawPath} doesn't exist")
# TODO: move conversion to another method?
......@@ -297,8 +294,8 @@ class ProtocolPad(BasePad):
"---\n\n"
"[MINITOC]\n\n")
# Upload
os.makedirs(str(self.gravPath.parent), exist_ok=True)
with open(str(self.gravPath), "w", encoding="utf-8") as f:
self.gravPath.parent.mkdir(parents=True, exist_ok=True)
with self.gravPath.open("w", encoding="utf-8") as f:
f.write(header + self.cleanedRaw())
git = opt.git
......
......@@ -19,7 +19,7 @@ class Gremium:
def fullTemplate(self):
from linkToPDF2 import opt
return str(Path(os.path.join(opt.TEMPLATE_FOLDER, self.template)).absolute())
return Path(opt.TEMPLATE_FOLDER).absolute() / self.template
GREMIUM_FR = Gremium("finf", "Fachrat Informatik", "pandoc.finf.tex")
......@@ -62,7 +62,7 @@ def sendFRMail(text: str, subject: str, sender: MailParticipant, receivers: List
mail.attach(msg)
if attachment is not None:
payload = MIMEBase('application', 'octet-stream')
with open(str(attachment.compiledPath), "rb") as d:
with attachment.compiledPath.open("rb") as d:
payload.set_payload(d.read())
encoders.encode_base64(payload)
payload.add_header('Content-Disposition',
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment