pterClasses.py 3.95 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import os
import smtplib
from dataclasses import dataclass
from email import encoders
from email.mime.base import MIMEBase
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from enum import Enum
from pathlib import Path
from typing import List


@dataclass
class Gremium:
    name: str
    fullName: str
    template: str

    def fullTemplate(self):
        from linkToPDF2 import opt
        return str(Path(os.path.join(opt.TEMPLATE_FOLDER, self.template)).absolute())


GREMIUM_FR = Gremium("finf", "Fachrat Informatik", "pandoc.finf.tex")
GREMIUM_FSR = Gremium("fsr", "Fachschaftsrat Elektrotechnik und Informatik", "pandoc.fsr.tex")


@dataclass
class MailParticipant:
    mail: str
    alias: str = None

    def __post_init__(self):
        self.alias = self.mail

    def __str__(self):
        return f"{self.alias} <{self.mail}>"

    @staticmethod
    def FR_INFO():
        return MailParticipant("fr-info@finf.uni-hannover.de", "FR-Info")

    @staticmethod
    def FR():
        return MailParticipant("fr@finf.uni-hannover.de", "Fachrat Informatik")


def sendFRMail(text: str, subject: str, sender: MailParticipant, receivers: List[MailParticipant],
               attachment: "BasePad" = None, *_, ldapPassword=None):
    if ldapPassword is None:
        raise ValueError("Require ldapPassword")
    mail = MIMEMultipart()
    mail['Content-transfer-encoding'] = '8bit'
    mail['From'] = sender.alias
    mail['Subject'] = subject
    receiveralias = ", ".join([r.alias for r in receivers])
    mail['To'] = receiveralias

    msg = MIMEText(text, "plain", "utf-8")
    mail.attach(msg)
    if attachment is not None:
        payload = MIMEBase('application', 'octet-stream')
        with open(str(attachment.compiledPath), "rb") as d:
            payload.set_payload(d.read())
        encoders.encode_base64(payload)
        payload.add_header('Content-Disposition',
                           f"attachment; filename=\"{attachment.displayName}.pdf\"")
        mail.attach(payload)

    try:
        server = smtplib.SMTP("mail.finf.uni-hannover.de", 587)
        server.starttls()
        server.login(sender.mail, ldapPassword)
        server.sendmail(
            sender.mail, [r.mail for r in receivers], mail.as_string())
        server.quit()
        print(f"Email \"{subject}\" an {receivers} erfolgreich versendet")
    except smtplib.SMTPException:
        print(
            f"Fehler beim Senden der Email \"{subject}\" von {sender} an {receivers}")
        raise


class GitHelper:
    def __init__(self, repository: str, repository_path: str):
        self.repository = repository
        self.repository_path = repository_path

    def config(self, email, name):
        cmd = f'git -C {self.repository_path} config user.name {name}'
        os.system(cmd)
        cmd = f'git -C {self.repository_path} config user.email {email}'
        os.system(cmd)

    def clone(self):
        cmd = f'git clone {self.repository} {self.repository_path}'
        os.system(cmd)

    def forcePull(self):
        cmd = f'git -C {self.repository_path} fetch origin master'
        os.system(cmd)
        cmd = f'git -C {self.repository_path} reset --hard origin/master'
        os.system(cmd)

    def update(self):
        if os.path.exists(os.path.join(self.repository_path, "/.git")):
            self.forcePull()
        else:
            self.clone()

    def add(self, file: str):
        cmd = f'git -C {self.repository_path} add {file}'
        os.system(cmd)

    def commit(self, msg: str):
        cmd = f'git -C {self.repository_path} commit -m \"{msg}\"'
        os.system(cmd)

    def push(self):
        cmd = f'git -C {self.repository_path} push origin master'
        os.system(cmd)


class SemesterPart(Enum):
    Summer = "Sommersemester"
    Winter = "Wintersemester"

    def short(self):
        if self == SemesterPart.Summer:
            return "SS"
        elif self == SemesterPart.Winter:
            return "WS"
        raise ValueError("Unknown Semesterpart")