proto.py 6.77 KB
Newer Older
Steinert's avatar
Steinert committed
1 2 3 4 5
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Nov 24 15:35:04 2017

6
@author: Lorenz Steinert
Steinert's avatar
Steinert committed
7 8 9
"""

import re
Lorenz Steinert's avatar
Lorenz Steinert committed
10 11
import os
import time
Lorenz Steinert's avatar
Lorenz Steinert committed
12
import shutil
Lorenz Steinert's avatar
Lorenz Steinert committed
13
import codecs
Lorenz Steinert's avatar
Lorenz Steinert committed
14 15
import argparse
from module.esme import fsr_ldap, get_date, get_sprecher, get_protokollant, get_config, get_share, get_path
Lorenz Steinert's avatar
Lorenz Steinert committed
16 17 18

if __name__ == "__main__":

Lorenz Steinert's avatar
Lorenz Steinert committed
19 20
    RUNDIR = os.path.dirname(os.path.realpath(__file__))

Lorenz Steinert's avatar
Lorenz Steinert committed
21 22 23 24 25 26 27
    PARSER = argparse.ArgumentParser(description="Create Protokoll")
    PARSER.add_argument('-d', '--debug', '-v', '--verbose',
                        action='store_true', help='enable debug mode')
    PARSER.add_argument('-VV', '--Vollversamlung',
                        action='store_true', help='gennerate VV Protokoll')
    PARSER.add_argument('-t', '--test',
                        action='store_true', help='enable test mode')
Lorenz Steinert's avatar
Lorenz Steinert committed
28 29 30
    PARSER.add_argument('-p', '--path', help='set the protokoll directory')
    PARSER.add_argument('-c', '--config', help='set the config file')
    PARSER.add_argument('-s', '--sharedir', help='set the share directory')
Lorenz Steinert's avatar
Lorenz Steinert committed
31 32
    OPT = PARSER.parse_args()

Lorenz Steinert's avatar
Lorenz Steinert committed
33 34 35
    CONFIG = get_config(RUNDIR, OPT.config)

    SHARE_DIR = get_share(RUNDIR, CONFIG, OPT.sharedir)
Lorenz Steinert's avatar
Lorenz Steinert committed
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52

    REG = {'SPR': re.compile('%%SPRECHER'),
           'PRO': re.compile('%%PROTOKOLLANT'),
           'DATE': re.compile('%%DATE'),
           'ACRO': re.compile('%%ACRO'),
           'ACRON': re.compile('%%ACRONAME'),
           'FSRN': re.compile('%%FSRNAME')}

    NUM_COLS = CONFIG['DEFAULT'].getint('NUM_COLS')
    NUM_COLS_EXT = CONFIG['DEFAULT'].getint('NUM_COLS_EXT')

    TABDEF = "||"

    fsr = ['', '']

    fsr[0] = sorted([re.sub(re.escape('\t'), ' ',
                            line.rstrip('\n'))
Lorenz Steinert's avatar
Lorenz Steinert committed
53 54 55
                     for line in codecs.open(os.path.join(SHARE_DIR,
                                                          'fsr'),
                                             'r',
Lorenz Steinert's avatar
Lorenz Steinert committed
56 57 58 59 60
                                             'utf-8').readlines()],
                    key=lambda x: ''.join(x.split(' ')[::-1]))

    fsr[1] = sorted([re.sub(re.escape('\t'), ' ',
                            line.rstrip('\n'))
Lorenz Steinert's avatar
Lorenz Steinert committed
61 62 63
                     for line in codecs.open(os.path.join(SHARE_DIR,
                                                          'fsr_extern'),
                                             'r',
Lorenz Steinert's avatar
Lorenz Steinert committed
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
                                             'utf-8').readlines()],
                    key=lambda x: ''.join(x.split(' ')[::-1]))

    if OPT.test:
        ESME = fsr_ldap(OPT.debug,
                        timeout=1)
    else:
        ESME = fsr_ldap(OPT.debug,
                        timeout=CONFIG['SERVER'].getint('connect_timeout'))

    if OPT.test:
        date = ('01.01.2000', '2000', '01', '01')
    else:
        year = str(time.localtime()[0])
        mon = str(time.localtime()[1])
        if len(mon) < 2:
            mon = "0"+mon
        day = str(time.localtime()[2])
        if len(day) < 2:
            day = "0"+day
Lorenz Steinert's avatar
Lorenz Steinert committed
84
        date = (day + '.' + mon + '.' + year, year, mon, day)
Lorenz Steinert's avatar
Lorenz Steinert committed
85 86

    print("\n")
Lorenz Steinert's avatar
Lorenz Steinert committed
87
    PROTOKOLLANT = ESME.get_protokollant_ldap(CONFIG['DEFAULT']['Protokollant'])
Lorenz Steinert's avatar
Lorenz Steinert committed
88 89 90 91 92 93
    fsr[0] = ESME.get_fsr_ldap(fsr[0])
    fsr[1] = ESME.get_fsr_extern_ldap(fsr[1])
    print("\n")
    print("Wenn der Standardwert (in []) stimmt einfach Enter!\n")

    # get data form user
Lorenz Steinert's avatar
Lorenz Steinert committed
94 95
    SPRECHER = get_sprecher(CONFIG['DEFAULT']['Sprecher'])
    PROTOKOLLANT = get_protokollant(PROTOKOLLANT)
Lorenz Steinert's avatar
Lorenz Steinert committed
96 97 98
    date = get_date(date)
    # self.editor = self.get_editor(self.editor)

Lorenz Steinert's avatar
Lorenz Steinert committed
99
    OUTDIR = get_path(RUNDIR, CONFIG, OPT.path)
Lorenz Steinert's avatar
Lorenz Steinert committed
100 101 102
    if not os.path.exists(OUTDIR):
        os.makedirs(OUTDIR)

Lorenz Steinert's avatar
Lorenz Steinert committed
103 104
    shutil.copyfile(os.path.join(SHARE_DIR, "FS-Logo.pdf"),
                    os.path.join(OUTDIR, "FS-Logo.pdf"))
Lorenz Steinert's avatar
Lorenz Steinert committed
105

Lorenz Steinert's avatar
Lorenz Steinert committed
106 107
    OUTFILE = os.path.join(OUTDIR, "fsr_physik_protokoll_"
                           + '-'.join(date[1:]) + ".tex")
Lorenz Steinert's avatar
Lorenz Steinert committed
108

Lorenz Steinert's avatar
Lorenz Steinert committed
109 110 111
    with codecs.open(os.path.join(SHARE_DIR, 'name.tex'),
                     'r', 'utf-8') as fobj_name:
        F_NAME = fobj_name.read()
Lorenz Steinert's avatar
Lorenz Steinert committed
112

Lorenz Steinert's avatar
Lorenz Steinert committed
113 114 115
    with codecs.open(os.path.join(SHARE_DIR, 'acroname.tex'),
                     'r', 'utf-8') as fobj_acroname:
        F_ACRONAME = fobj_acroname.read()
Lorenz Steinert's avatar
Lorenz Steinert committed
116

Lorenz Steinert's avatar
Lorenz Steinert committed
117 118 119
    with codecs.open(os.path.join(SHARE_DIR, 'acro.tex'),
                     'r', 'utf-8') as fobj_acro:
        F_ACRO = fobj_acro.read()
Lorenz Steinert's avatar
Lorenz Steinert committed
120

Lorenz Steinert's avatar
Lorenz Steinert committed
121 122
    with codecs.open(os.path.join(SHARE_DIR, 'head.tex'),
                     'r', 'utf-8') as fobj_head:
Lorenz Steinert's avatar
Lorenz Steinert committed
123 124
        f_head = fobj_head.read()

Lorenz Steinert's avatar
Lorenz Steinert committed
125 126 127 128 129
    f_head = REG['FSRN'].sub(F_NAME, f_head)
    f_head = REG['ACRON'].sub(F_ACRONAME, f_head)
    f_head = REG['ACRO'].sub(F_ACRO, f_head)
    f_head = REG['SPR'].sub(SPRECHER, f_head)
    f_head = REG['PRO'].sub(PROTOKOLLANT, f_head)
Lorenz Steinert's avatar
Lorenz Steinert committed
130 131 132
    f_head = REG['DATE'].sub(date[0], f_head)

    if OPT.Vollversamlung:
Lorenz Steinert's avatar
Lorenz Steinert committed
133 134
        with codecs.open(os.path.join(SHARE_DIR, 'foot_VV.tex'),
                         'r', 'utf-8') as fobj_foot:
Lorenz Steinert's avatar
Lorenz Steinert committed
135 136
            f_foot = fobj_foot.read()
    else:
Lorenz Steinert's avatar
Lorenz Steinert committed
137 138
        with codecs.open(os.path.join(SHARE_DIR, 'foot.tex'),
                         'r', 'utf-8') as fobj_foot:
Lorenz Steinert's avatar
Lorenz Steinert committed
139 140
            f_foot = fobj_foot.read()

Lorenz Steinert's avatar
Lorenz Steinert committed
141 142
    f_foot = REG['SPR'].sub(SPRECHER, f_foot)
    f_foot = REG['PRO'].sub(PROTOKOLLANT, f_foot)
Lorenz Steinert's avatar
Lorenz Steinert committed
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
    f_foot = REG['DATE'].sub(date[0], f_foot)

    msg = ""

    msg += f_head

    msg += "~\\newline\n"

    if not OPT.Vollversamlung:
        for i in range(NUM_COLS):
            TABDEF += "c|l||"
        msg += "FSR-Mitglieder\\\\\n\\\\\n\\scriptsize{\\begin{tabular}{" \
               + TABDEF + "}\n\\hline\n"

        for i in zip(fsr[0], range(len(fsr[0]))):
            if i[1] % NUM_COLS == 0:
                msg += " "
160
            else:
Lorenz Steinert's avatar
Lorenz Steinert committed
161 162 163 164 165
                msg += "&"

            msg += "  &" + i[0]

            if i[1] % NUM_COLS == NUM_COLS - 1:
Lorenz Steinert's avatar
Lorenz Steinert committed
166
                msg += "\\\\\\hline\n"
Lorenz Steinert's avatar
Lorenz Steinert committed
167 168
            else:
                msg += "\n"
Lorenz Steinert's avatar
Lorenz Steinert committed
169

Lorenz Steinert's avatar
Lorenz Steinert committed
170 171 172 173
        if not len(fsr[0]) % NUM_COLS == 0:
            for i in range(NUM_COLS - len(fsr[0]) % NUM_COLS):
                msg += "&  &\n"
            msg += "\\\\\\hline\n"
174

Lorenz Steinert's avatar
Lorenz Steinert committed
175 176
        msg += r"\end{tabular}}\normalsize\vspace{1em}"
        msg += "\n\\newline\n"
177

Lorenz Steinert's avatar
Lorenz Steinert committed
178 179 180 181 182
        for i in range(NUM_COLS_EXT):
            TABDEF += "c|l||"
        msg += "Externe Referenten\\\\\n\\\\\n" \
               + r"\scriptsize{\begin{tabular}{" \
               + TABDEF + "}\n\\hline\n"
183

Lorenz Steinert's avatar
Lorenz Steinert committed
184 185 186 187 188
        for i in zip(fsr[1], range(len(fsr[1]))):
            if i[1] % NUM_COLS_EXT == 0:
                msg += " "
            else:
                msg += "&"
Lorenz Steinert's avatar
bla  
Lorenz Steinert committed
189

Lorenz Steinert's avatar
Lorenz Steinert committed
190
            msg += "  &" + i[0]
191

Lorenz Steinert's avatar
Lorenz Steinert committed
192
            if i[1] % NUM_COLS_EXT == NUM_COLS_EXT - 1:
193
                msg += "\\\\\\hline\n"
Lorenz Steinert's avatar
Lorenz Steinert committed
194 195
            else:
                msg += "\n"
196

Lorenz Steinert's avatar
Lorenz Steinert committed
197 198 199 200
        if not len(fsr[1]) % NUM_COLS_EXT == 0:
            for i in range(NUM_COLS_EXT - len(fsr[1]) % NUM_COLS_EXT):
                msg += "& &\n"
            msg += "\\\\\\hline\n"
Lorenz Steinert's avatar
bla  
Lorenz Steinert committed
201

Lorenz Steinert's avatar
Lorenz Steinert committed
202
        msg += "\\end{tabular}}\\normalsize"
203

Lorenz Steinert's avatar
Lorenz Steinert committed
204
    msg += f_foot
Steinert's avatar
Steinert committed
205

Lorenz Steinert's avatar
Lorenz Steinert committed
206 207
    with codecs.open(OUTFILE, 'w', 'utf-8') as fobj_out:
        fobj_out.write(msg)