proto.py 8.99 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
Lorenz Steinert's avatar
Lorenz Steinert committed
15
from module.esme import FsrLdap, get_date, get_sprecher, get_config, get_share, get_path, get_ldap_server, get_ldap_base, get_server_timeout, Error  # noqa: E501
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
    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')
30 31
    parser.add_argument('-S', '--Server', help='set the ldap Server')
    parser.add_argument('-b', '--base', help='set the ldap Server search base')
Lorenz Steinert's avatar
Lorenz Steinert committed
32 33 34 35

    return parser.parse_args()


36
def gen_header(share_dir, reg, sprecher, protokollant, date):
Lorenz Steinert's avatar
Lorenz Steinert committed
37
    """generate the head of the protokoll"""
Lorenz Steinert's avatar
Lorenz Steinert committed
38
    # look for the name.tex and if not found ignore it
39 40 41 42 43
    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:
44 45
        print(os.path.abspath(os.path.join(share_dir, 'name.tex'))
              + " not found. Ignoring")
46 47
        f_name = ""

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

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

Lorenz Steinert's avatar
Lorenz Steinert committed
68
    # look for teh head.tex and abort if it is not found
69 70 71 72 73 74 75 76
    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
77

Lorenz Steinert's avatar
Lorenz Steinert committed
78
    # Replace the paceholder strings with the files
Lorenz Steinert's avatar
Lorenz Steinert committed
79 80 81 82 83 84 85 86 87 88
    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
89
def gen_attendance_tab(fsr, num_cols=5, ext=False):
Lorenz Steinert's avatar
Lorenz Steinert committed
90
    """generate the atandance tables"""
Lorenz Steinert's avatar
Lorenz Steinert committed
91

Lorenz Steinert's avatar
Lorenz Steinert committed
92
    tabdef = '||' + 'c|l||' * num_cols
Lorenz Steinert's avatar
Lorenz Steinert committed
93

Lorenz Steinert's avatar
Lorenz Steinert committed
94 95
    fsr_tab = ''

Lorenz Steinert's avatar
Lorenz Steinert committed
96 97 98 99 100 101 102 103
    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
104 105
    for elem, index in zip(fsr, range(len(fsr))):
        if index % num_cols == 0:
Lorenz Steinert's avatar
Lorenz Steinert committed
106 107 108 109
            fsr_tab += " "
        else:
            fsr_tab += "&"

Lorenz Steinert's avatar
Lorenz Steinert committed
110
        fsr_tab += "  &" + elem
Lorenz Steinert's avatar
Lorenz Steinert committed
111

Lorenz Steinert's avatar
Lorenz Steinert committed
112
        if index % num_cols == num_cols - 1:
Lorenz Steinert's avatar
Lorenz Steinert committed
113 114 115 116 117 118 119 120 121 122 123 124 125
            fsr_tab += "\\\\\\hline\n"
        else:
            fsr_tab += "\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


126
def gen_footer(share_dir, vollversamlung, reg, sprecher, protokollant, date):
Lorenz Steinert's avatar
Lorenz Steinert committed
127
    """generate the foot of the protokoll"""
Lorenz Steinert's avatar
Lorenz Steinert committed
128
    # look for the foot*.tex skripts
Lorenz Steinert's avatar
Lorenz Steinert committed
129
    # if not found abort
Lorenz Steinert's avatar
Lorenz Steinert committed
130
    if vollversamlung:
131 132 133 134 135 136 137 138
        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
139
    else:
140 141 142 143 144 145 146 147
        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
148

Lorenz Steinert's avatar
Lorenz Steinert committed
149
    # replace the placeholder strings in f_foot
Lorenz Steinert's avatar
Lorenz Steinert committed
150 151 152 153 154
    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
155

Lorenz Steinert's avatar
Lorenz Steinert committed
156

Lorenz Steinert's avatar
Lorenz Steinert committed
157 158 159 160 161
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
162
    if CONFIG is Error.NO_FILE:
Lorenz Steinert's avatar
Lorenz Steinert committed
163 164
        print("No config-file found. Stoping.")
        sys.exit(CONFIG)
Lorenz Steinert's avatar
Lorenz Steinert committed
165
    elif CONFIG is Error.NO_DEFAULT_FILE:
Lorenz Steinert's avatar
Lorenz Steinert committed
166 167
        print("No default config-file found. Stopping.")
        sys.exit(CONFIG)
168

Lorenz Steinert's avatar
Lorenz Steinert committed
169
    SHARE_DIR = get_share(RUNDIR, CONFIG, OPT.sharedir)
Lorenz Steinert's avatar
Lorenz Steinert committed
170
    if SHARE_DIR is Error.NO_DIR:
Lorenz Steinert's avatar
Lorenz Steinert committed
171 172
        print("No sharedir found. Stopping.")
        sys.exit(SHARE_DIR)
Lorenz Steinert's avatar
Lorenz Steinert committed
173
    elif SHARE_DIR is Error.NO_DEFAULT_DIR:
Lorenz Steinert's avatar
Lorenz Steinert committed
174 175
        print("No default sharedir found. Stopping.")
        sys.exit(SHARE_DIR)
Lorenz Steinert's avatar
Lorenz Steinert committed
176
    LDAP_SERVER = get_ldap_server(CONFIG, OPT.Server)
177
    LDAP_SERVER_BASE = get_ldap_base(CONFIG, OPT.base)
178
    SERVER_TIMEOUT = get_server_timeout(CONFIG, OPT.test)
Lorenz Steinert's avatar
Lorenz Steinert committed
179 180 181 182 183 184 185 186

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

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

Lorenz Steinert's avatar
Lorenz Steinert committed
194
    ESME = FsrLdap(LDAP_SERVER, LDAP_SERVER_BASE, SHARE_DIR, CONFIG, OPT.debug,
195
                   SERVER_TIMEOUT)
Lorenz Steinert's avatar
Lorenz Steinert committed
196 197 198 199

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

200
    SPRECHER = get_sprecher(CONFIG)
Lorenz Steinert's avatar
Lorenz Steinert committed
201
    PROTOKOLLANT = ESME.get_protokollant(CONFIG)
Lorenz Steinert's avatar
Lorenz Steinert committed
202

Lorenz Steinert's avatar
Lorenz Steinert committed
203 204
    DATE = get_date(OPT.test)

Lorenz Steinert's avatar
Lorenz Steinert committed
205 206
    # Fetch the FSR from the Server
    # and if not able to connect fall back to the textfiles
207
    FSR = [ESME.get_fsr(), ESME.get_fsr_extern()]
Lorenz Steinert's avatar
Lorenz Steinert committed
208

Lorenz Steinert's avatar
Lorenz Steinert committed
209 210
    # Generate the Path for the Protokoll in depandance of OPT.VV
    # and make the directory
211 212
    if OPT.Vollversamlung:
        OUTDIR = os.path.join(get_path(RUNDIR, CONFIG, OPT.path), DATE[1],
Lorenz Steinert's avatar
Lorenz Steinert committed
213 214
                              "fsr_physik_vollversamlung_"
                              + '-'.join(DATE[1:]))
215 216 217 218 219
        # Generate the path of the .tex files
        OUTFILE_HEADER = os.path.join(OUTDIR, "fsr_physik_protokoll_vv_"
                                      + '-'.join(DATE[1:]) + "_header.tex")
        OUTFILE_BODY = os.path.join(OUTDIR, "fsr_physik_protokoll_vv_"
                                    + '-'.join(DATE[1:]) + ".tex")
220 221 222
    else:
        OUTDIR = os.path.join(get_path(RUNDIR, CONFIG, OPT.path), DATE[1],
                              "fsr_physik_sitzung_" + '-'.join(DATE[1:]))
223
        # Generate the path of the .tex file
224 225 226 227
        OUTFILE_HEADER = os.path.join(OUTDIR, "fsr_physik_protokoll_"
                                      + '-'.join(DATE[1:]) + "_header.tex")
        OUTFILE_BODY = os.path.join(OUTDIR, "fsr_physik_protokoll_"
                                    + '-'.join(DATE[1:]) + ".tex")
Lorenz Steinert's avatar
Lorenz Steinert committed
228 229 230
    if not os.path.exists(OUTDIR):
        os.makedirs(OUTDIR)

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

234 235 236
    header = gen_header(SHARE_DIR, REG, SPRECHER, PROTOKOLLANT, DATE)

    body = "\\input{" + OUTFILE_HEADER + "}\n"
Lorenz Steinert's avatar
Lorenz Steinert committed
237

238
    body += "~\\newline\n"
Lorenz Steinert's avatar
Lorenz Steinert committed
239 240

    if not OPT.Vollversamlung:
241 242 243 244 245
        body += gen_attendance_tab(FSR[0], NUM_COLS)
        body += gen_attendance_tab(FSR[1], NUM_COLS_EXT, ext=True)

    footer = gen_footer(SHARE_DIR, OPT.Vollversamlung, REG,
                        SPRECHER, PROTOKOLLANT, DATE)
Lorenz Steinert's avatar
Lorenz Steinert committed
246

247
    body += footer
Steinert's avatar
Steinert committed
248

249 250 251 252
    with codecs.open(OUTFILE_HEADER, 'w', 'utf-8') as fobj_out:
        fobj_out.write(header)
    with codecs.open(OUTFILE_BODY, 'w', 'utf-8') as fobj_out:
        fobj_out.write(body)