proto.py 5.63 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
import os
Lorenz Steinert's avatar
Lorenz Steinert committed
11
import shutil
Lorenz Steinert's avatar
Lorenz Steinert committed
12
import codecs
Lorenz Steinert's avatar
Lorenz Steinert committed
13
import argparse
Lorenz Steinert's avatar
Lorenz Steinert committed
14
from module.esme import fsr_ldap, get_date, get_sprecher, get_config, get_share, get_path
Lorenz Steinert's avatar
Lorenz Steinert committed
15

Lorenz Steinert's avatar
Lorenz Steinert committed
16

Lorenz Steinert's avatar
Lorenz Steinert committed
17 18 19 20
def build_parser():
    """definition for the cli-arguments of the skript"""
    parser = argparse.ArgumentParser(description="Create Protokoll")
    parser.add_argument('-d', '--debug', '-v', '--verbose',
Lorenz Steinert's avatar
Lorenz Steinert committed
21
                        action='store_true', help='enable debug mode')
Lorenz Steinert's avatar
Lorenz Steinert committed
22
    parser.add_argument('-VV', '--Vollversamlung',
Lorenz Steinert's avatar
Lorenz Steinert committed
23
                        action='store_true', help='gennerate VV Protokoll')
Lorenz Steinert's avatar
Lorenz Steinert committed
24
    parser.add_argument('-t', '--test',
Lorenz Steinert's avatar
Lorenz Steinert committed
25
                        action='store_true', help='enable test mode')
Lorenz Steinert's avatar
Lorenz Steinert committed
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
    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')

    return parser.parse_args()


def gen_head(share_dir, reg, sprecher, protokollant, date):
    """generate the head of the protokoll"""

    with codecs.open(os.path.join(share_dir, 'name.tex'),
                     'r', 'utf-8') as fobj_name:
        f_name = fobj_name.read()

    with codecs.open(os.path.join(share_dir, 'acroname.tex'),
                     'r', 'utf-8') as fobj_acroname:
        f_acroname = fobj_acroname.read()

    with codecs.open(os.path.join(share_dir, 'acro.tex'),
                     'r', 'utf-8') as fobj_acro:
        f_acro = fobj_acro.read()

    with codecs.open(os.path.join(share_dir, 'head.tex'),
                     'r', 'utf-8') as fobj_head:
        f_head = fobj_head.read()

    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)
    f_head = reg['DATE'].sub(date[0], f_head)

    return f_head


def gen_attendance_tab(fsr, num_cols=4, ext=False):
    """generate the atandance tables"""
    fsr_tab, tabdef = '', '||'
    for i in range(num_cols):
        tabdef += "c|l||"

    if ext:
        fsr_tab += "Externe Referenten\\\\\n\\\\\n" \
                   + r"\scriptsize{\begin{tabular}{" \
                   + tabdef + "}\n\\hline\n"
    else:
        fsr_tab += "FSR-Mitglieder\\\\\n\\\\\n\\scriptsize{\\begin{tabular}{" \
                   + tabdef + "}\n\\hline\n"

    for i in zip(fsr, range(len(fsr))):
        if i[1] % num_cols == 0:
            fsr_tab += " "
        else:
            fsr_tab += "&"

        fsr_tab += "  &" + i[0]

        if i[1] % num_cols == num_cols - 1:
            fsr_tab += "\\\\\\hline\n"
        else:
            fsr_tab += "\n"

    if not len(fsr) % num_cols == 0:
        for i in range(num_cols - len(fsr) % num_cols):
            fsr_tab += "&  &\n"
        fsr_tab += "\\\\\\hline\n"

    if ext:
        fsr_tab += "\\end{tabular}}\\normalsize"
    else:
        fsr_tab += r"\end{tabular}}\normalsize\vspace{1em}"
        fsr_tab += "\n\\newline\n"

    return fsr_tab


def gen_foot(share_dir, vollversamlung, reg, sprecher, protokollant, date):
    """generate the foot of the protokoll"""
    if vollversamlung:
        with codecs.open(os.path.join(share_dir, 'foot_VV.tex'),
                         'r', 'utf-8') as fobj_foot:
            f_foot = fobj_foot.read()
    else:
        with codecs.open(os.path.join(share_dir, 'foot.tex'),
                         'r', 'utf-8') as fobj_foot:
            f_foot = fobj_foot.read()

    f_foot = reg['SPR'].sub(sprecher, f_foot)
    f_foot = reg['PRO'].sub(protokollant, f_foot)
    f_foot = reg['DATE'].sub(date[0], f_foot)

    return f_foot
Lorenz Steinert's avatar
Lorenz Steinert committed
119

Lorenz Steinert's avatar
Lorenz Steinert committed
120

Lorenz Steinert's avatar
Lorenz Steinert committed
121 122 123 124 125
if __name__ == "__main__":

    RUNDIR = os.path.dirname(os.path.realpath(__file__))
    OPT = build_parser()
    CONFIG = get_config(RUNDIR, OPT.config)
Lorenz Steinert's avatar
Lorenz Steinert committed
126
    SHARE_DIR = get_share(RUNDIR, CONFIG, OPT.sharedir)
Lorenz Steinert's avatar
Lorenz Steinert committed
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150

    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 = "||"

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

    print("\n")
    print("Wenn der Standardwert (in []) stimmt einfach Enter!\n")

    # get data form user
Lorenz Steinert's avatar
Lorenz Steinert committed
151
    SPRECHER = get_sprecher(CONFIG['DEFAULT']['Sprecher'])
Lorenz Steinert's avatar
Lorenz Steinert committed
152 153 154 155 156
    PROTOKOLLANT = ESME.get_protokollant_ldap(CONFIG['DEFAULT']['Protokollant'])
    DATE = get_date(OPT.test)

    FSR = [ESME.get_fsr_ldap(SHARE_DIR),
           ESME.get_fsr_extern_ldap(SHARE_DIR)]
Lorenz Steinert's avatar
Lorenz Steinert committed
157

Lorenz Steinert's avatar
Lorenz Steinert committed
158
    OUTDIR = get_path(RUNDIR, CONFIG, OPT.path)
Lorenz Steinert's avatar
Lorenz Steinert committed
159 160 161
    if not os.path.exists(OUTDIR):
        os.makedirs(OUTDIR)

Lorenz Steinert's avatar
Lorenz Steinert committed
162
    OUTFILE = os.path.join(OUTDIR, "fsr_physik_protokoll_"
Lorenz Steinert's avatar
Lorenz Steinert committed
163
                           + '-'.join(DATE[1:]) + ".tex")
Lorenz Steinert's avatar
Lorenz Steinert committed
164

Lorenz Steinert's avatar
Lorenz Steinert committed
165 166
    shutil.copyfile(os.path.join(SHARE_DIR, "FS-Logo.pdf"),
                    os.path.join(OUTDIR, "FS-Logo.pdf"))
Lorenz Steinert's avatar
Lorenz Steinert committed
167

Lorenz Steinert's avatar
Lorenz Steinert committed
168
    msg = gen_head(SHARE_DIR, REG, SPRECHER, PROTOKOLLANT, DATE)
Lorenz Steinert's avatar
Lorenz Steinert committed
169 170 171 172

    msg += "~\\newline\n"

    if not OPT.Vollversamlung:
Lorenz Steinert's avatar
Lorenz Steinert committed
173 174 175 176 177
        msg += gen_attendance_tab(FSR[0], NUM_COLS)
        msg += gen_attendance_tab(FSR[1], NUM_COLS_EXT, ext=True)

    msg += gen_foot(SHARE_DIR, REG, OPT.Vollversamlung,
                    SPRECHER, PROTOKOLLANT, DATE)
Steinert's avatar
Steinert committed
178

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