proto.py 8.32 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
11
import sys
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
import argparse
15
from module.esme import FsrLdap, get_date, get_sprecher, get_config, get_share, get_path
Lorenz Steinert's avatar
Lorenz Steinert committed
16

Lorenz Steinert's avatar
Lorenz Steinert committed
17

Lorenz Steinert's avatar
Lorenz Steinert committed
18 19 20 21
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
22
                        action='store_true', help='enable debug mode')
Lorenz Steinert's avatar
Lorenz Steinert committed
23
    parser.add_argument('-VV', '--Vollversamlung',
Lorenz Steinert's avatar
Lorenz Steinert committed
24
                        action='store_true', help='gennerate VV Protokoll')
Lorenz Steinert's avatar
Lorenz Steinert committed
25
    parser.add_argument('-t', '--test',
Lorenz Steinert's avatar
Lorenz Steinert committed
26
                        action='store_true', help='enable test mode')
Lorenz Steinert's avatar
Lorenz Steinert committed
27 28 29 30 31 32 33 34 35
    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"""
Lorenz Steinert's avatar
Lorenz Steinert committed
36
    #look for the name.tex and if not found ignore it
37 38 39 40 41
    if os.path.isfile(os.path.join(share_dir, 'name.tex')):
        with codecs.open(os.path.join(share_dir, 'name.tex'),
                         'r', 'utf-8') as fobj_name:
            f_name = fobj_name.read()
    else:
42 43
        print(os.path.abspath(os.path.join(share_dir, 'name.tex'))
              + " not found. Ignoring")
44 45
        f_name = ""

Lorenz Steinert's avatar
Lorenz Steinert committed
46 47
    #look for the acroname.tex and the acro.tex if one is not found
    #ignore both
48
    if (os.path.isfile(os.path.join(share_dir, 'acroname.tex'))
49 50
            and os.path.isfile(os.path.join(share_dir, 'acro.tex'))):

51 52 53 54 55 56 57 58
        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()
    else:
59
        print(os.path.abspath(os.path.join(share_dir, 'acroname.tex'))
60 61
              + " or "
              + os.path.abspath(os.path.join(share_dir, 'acro.tex'))
62
              + " not found. Ignoring")
63 64 65
        f_acroname = ""
        f_acro = ""

Lorenz Steinert's avatar
Lorenz Steinert committed
66
    #look for teh head.tex and abort if it is not found 
67 68 69 70 71 72 73 74
    if os.path.isfile(os.path.join(share_dir, 'head.tex')):
        with codecs.open(os.path.join(share_dir, 'head.tex'),
                         'r', 'utf-8') as fobj_head:
            f_head = fobj_head.read()
    else:
        print(os.path.abspath(os.path.join(share_dir, 'head.tex'))
              + " not found. Aborting!")
        sys.exit(1)
Lorenz Steinert's avatar
Lorenz Steinert committed
75

Lorenz Steinert's avatar
Lorenz Steinert committed
76
    #Replace the paceholder strings with the files
Lorenz Steinert's avatar
Lorenz Steinert committed
77 78 79 80 81 82 83 84 85
    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

Lorenz Steinert's avatar
Lorenz Steinert committed
86 87 88 89 90 91 92 93 94
def resort_fsr(fsr, num_cols=5):
    fsr += [""] * (num_cols - len(fsr)%num_cols)
    depth = len(fsr)//num_cols

    tmp = [[] for i in range(depth)]
    for index, elem in zip(range(len(fsr)), fsr):
        tmp[index%depth] += [elem]

    return [j for i in tmp for j in i]
Lorenz Steinert's avatar
Lorenz Steinert committed
95

Lorenz Steinert's avatar
Lorenz Steinert committed
96
def gen_attendance_tab(fsr, num_cols=5, ext=False):
Lorenz Steinert's avatar
Lorenz Steinert committed
97
    """generate the atandance tables"""
Lorenz Steinert's avatar
Lorenz Steinert committed
98 99 100

    fsr = resort_fsr(fsr, num_cols)

Lorenz Steinert's avatar
Lorenz Steinert committed
101
    fsr_tab, tabdef = '', '||'
Lorenz Steinert's avatar
Lorenz Steinert committed
102 103

    tabdef = '||' + 'c|l||'*num_cols
Lorenz Steinert's avatar
Lorenz Steinert committed
104 105 106 107 108 109 110 111 112

    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"

Lorenz Steinert's avatar
Lorenz Steinert committed
113 114
    for elem, index in zip(fsr, range(len(fsr))):
        if index % num_cols == 0:
Lorenz Steinert's avatar
Lorenz Steinert committed
115 116 117 118
            fsr_tab += " "
        else:
            fsr_tab += "&"

Lorenz Steinert's avatar
Lorenz Steinert committed
119
        fsr_tab += "  &" + elem
Lorenz Steinert's avatar
Lorenz Steinert committed
120

Lorenz Steinert's avatar
Lorenz Steinert committed
121
        if index % num_cols == num_cols - 1:
Lorenz Steinert's avatar
Lorenz Steinert committed
122 123 124 125
            fsr_tab += "\\\\\\hline\n"
        else:
            fsr_tab += "\n"

Lorenz Steinert's avatar
Lorenz Steinert committed
126
    #add empty cells to padd the last row
Lorenz Steinert's avatar
Lorenz Steinert committed
127 128 129 130
#    if not len(fsr) % num_cols == 0:
#        for i in range(num_cols - len(fsr) % num_cols):
#            fsr_tab += "&  &\n"
#        fsr_tab += "\\\\\\hline\n"
Lorenz Steinert's avatar
Lorenz Steinert committed
131 132 133 134 135 136 137 138 139 140 141 142

    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"""
Lorenz Steinert's avatar
Lorenz Steinert committed
143 144
    #look for the foot*.tex skripts
    # if not found abort
Lorenz Steinert's avatar
Lorenz Steinert committed
145
    if vollversamlung:
146 147 148 149 150 151 152 153
        if os.path.isfile(os.path.join(share_dir, 'foot_VV.tex')):
            with codecs.open(os.path.join(share_dir, 'foot_VV.tex'),
                             'r', 'utf-8') as fobj_foot:
                f_foot = fobj_foot.read()
        else:
            print(os.path.abspath(os.path.join(share_dir, 'foot_VV.tex'))
                  + " not found. Aborting")
            sys.exit(1)
Lorenz Steinert's avatar
Lorenz Steinert committed
154
    else:
155 156 157 158 159 160 161 162
        if os.path.isfile(os.path.join(share_dir, 'foot.tex')):
            with codecs.open(os.path.join(share_dir, 'foot.tex'),
                             'r', 'utf-8') as fobj_foot:
                f_foot = fobj_foot.read()
        else:
            print(os.path.abspath(os.path.join(share_dir, 'foot.tex'))
                  + " not found. Aborting")
            sys.exit(1)
Lorenz Steinert's avatar
Lorenz Steinert committed
163

Lorenz Steinert's avatar
Lorenz Steinert committed
164
    #replace the placeholder strings in f_foot
Lorenz Steinert's avatar
Lorenz Steinert committed
165 166 167 168 169
    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
170

Lorenz Steinert's avatar
Lorenz Steinert committed
171

Lorenz Steinert's avatar
Lorenz Steinert committed
172 173 174 175 176
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
177
    SHARE_DIR = get_share(RUNDIR, CONFIG, OPT.sharedir)
Lorenz Steinert's avatar
Lorenz Steinert committed
178 179 180 181 182 183 184 185

    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')}

186 187 188 189 190 191
    if CONFIG:
        NUM_COLS = CONFIG['DEFAULT'].getint('NUM_COLS')
        NUM_COLS_EXT = CONFIG['DEFAULT'].getint('NUM_COLS_EXT')
    else:
        NUM_COLS = 5
        NUM_COLS_EXT = 5
Lorenz Steinert's avatar
Lorenz Steinert committed
192

Lorenz Steinert's avatar
Lorenz Steinert committed
193
    #Set the timeout to 1 if we just want to test stuff
Lorenz Steinert's avatar
Lorenz Steinert committed
194
    if OPT.test:
195 196
        ESME = FsrLdap(OPT.debug,
                       timeout=1)
Lorenz Steinert's avatar
Lorenz Steinert committed
197
    else:
198 199
        ESME = FsrLdap(OPT.debug,
                       timeout=CONFIG['SERVER'].getint('connect_timeout'))
Lorenz Steinert's avatar
Lorenz Steinert committed
200 201 202 203

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

204 205 206 207 208 209
    if CONFIG:
        SPRECHER = get_sprecher(CONFIG['DEFAULT']['Sprecher'])
        PROTOKOLLANT = ESME.get_protokollant_ldap(CONFIG['DEFAULT']['Protokollant'])
    else:
        SPRECHER = get_sprecher('')
        PROTOKOLLANT = ESME.get_protokollant_ldap('')
Lorenz Steinert's avatar
Lorenz Steinert committed
210

Lorenz Steinert's avatar
Lorenz Steinert committed
211 212
    DATE = get_date(OPT.test)

Lorenz Steinert's avatar
Lorenz Steinert committed
213 214
    #Fetch the FSR from the Server
    #and if not able to connect fall back to the textfiles
Lorenz Steinert's avatar
Lorenz Steinert committed
215 216
    FSR = [ESME.get_fsr_ldap(SHARE_DIR),
           ESME.get_fsr_extern_ldap(SHARE_DIR)]
Lorenz Steinert's avatar
Lorenz Steinert committed
217

Lorenz Steinert's avatar
Lorenz Steinert committed
218 219
    #Generate the Path for the Protokoll in depandance of OPT.VV
    #and make the directory
220 221 222 223 224 225
    if OPT.Vollversamlung:
        OUTDIR = os.path.join(get_path(RUNDIR, CONFIG, OPT.path), DATE[1],
                              "fsr_physik_vollversamlung_" + '-'.join(DATE[1:]))
    else:
        OUTDIR = os.path.join(get_path(RUNDIR, CONFIG, OPT.path), DATE[1],
                              "fsr_physik_sitzung_" + '-'.join(DATE[1:]))
Lorenz Steinert's avatar
Lorenz Steinert committed
226 227 228
    if not os.path.exists(OUTDIR):
        os.makedirs(OUTDIR)

Lorenz Steinert's avatar
Lorenz Steinert committed
229
    #Generate the path of the .tex file
Lorenz Steinert's avatar
Lorenz Steinert committed
230
    OUTFILE = os.path.join(OUTDIR, "fsr_physik_protokoll_"
Lorenz Steinert's avatar
Lorenz Steinert committed
231
                           + '-'.join(DATE[1:]) + ".tex")
Lorenz Steinert's avatar
Lorenz Steinert committed
232

Lorenz Steinert's avatar
Lorenz Steinert committed
233 234
    shutil.copyfile(os.path.join(SHARE_DIR, "FS-Logo.pdf"),
                    os.path.join(OUTDIR, "FS-Logo.pdf"))
Lorenz Steinert's avatar
Lorenz Steinert committed
235

Lorenz Steinert's avatar
Lorenz Steinert committed
236
    msg = gen_head(SHARE_DIR, REG, SPRECHER, PROTOKOLLANT, DATE)
Lorenz Steinert's avatar
Lorenz Steinert committed
237 238 239 240

    msg += "~\\newline\n"

    if not OPT.Vollversamlung:
Lorenz Steinert's avatar
Lorenz Steinert committed
241 242 243
        msg += gen_attendance_tab(FSR[0], NUM_COLS)
        msg += gen_attendance_tab(FSR[1], NUM_COLS_EXT, ext=True)

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

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