Commit 92a3d2a0 authored by Lorenz Steinert's avatar Lorenz Steinert
Browse files

changed to module esme

parent e22d9ffa
......@@ -8,312 +8,185 @@ Created on Fri Nov 24 15:35:04 2017
import re
import os
import sys
import shutil
import time
import argparse
import codecs
import configparser
import ldap3 as ldap
try:
import pwd
except ImportError:
import getpass
pwd = None
def current_user():
"""get the current user"""
if pwd:
return pwd.getpwuid(os.geteuid()).pw_name
return getpass.getuser()
class Protokollskript:
"""Class die die protokoll .tex datei erstellt"""
def __init__(self, opt):
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')
self.opt = parser.parse_args()
self.config = configparser.ConfigParser()
self.config.read('./proto.ini')
if self.opt.test:
timeout = 1
else:
timeout = self.config['SERVER'].getint('connect_timeout')
self.server = ldap.Server("rincewind.fs.physik.uni-kl.de", connect_timeout=timeout)
self.base = "dc=fs,dc=physik,dc=uni-kl,dc=de"
if self.opt.test:
self.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
self.date = (day+"."+mon+"."+year, year, mon, day)
self.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')}
self.fsr = ['', '']
self.fsr[0] = sorted([re.sub(re.escape('\t'), ' ',
line.rstrip('\n'))
for line in codecs.open('./fsr', 'r',
'utf-8').readlines()],
key=lambda x: ''.join(x.split(' ')[::-1]))
self.fsr[1] = sorted([re.sub(re.escape('\t'), ' ',
line.rstrip('\n'))
for line in codecs.open('./fsr_extern', 'r',
'utf-8').readlines()],
key=lambda x: ''.join(x.split(' ')[::-1]))
def get_protokollant_ldap(self, protokollant):
"""search the LDAP for the current user"""
print("Fetching Protokollant from LDAP ...")
try:
conn = ldap.Connection(self.server)
conn.bind()
fil = "(uid="+current_user()+")"
if conn.search('ou=people,'+self.base, fil, attributes=['displayName']):
return " ".join(str(conn.entries[0]['displayName']).split(", ")[::-1])
return protokollant
except ldap.core.exceptions.LDAPException as error_message:
if self.opt.debug:
print("Couldn't Connect to ldap.\nUsing fallback default.\n%s\n" % error_message)
else:
print("Couldn't Connect to ldap.\nUsing fallback default.\n")
return protokollant
def get_fsr_ldap(self, fsr):
"""get fsr from LDAP"""
print("Fetching FSR from LDAP ...")
try:
conn = ldap.Connection(self.server)
conn.bind()
fil = '(cn=intern)'
fsr_ = []
if conn.search('ou=group,'+self.base, fil,
attributes=['memberUid']):
for i in conn.entries[0]['memberUid']:
if conn.search('ou=people,' + self.base,
'(uid='+i+')', attributes=['displayName']):
fsr_ += [str(conn.entries[0]['displayName'])]
fsr_.sort()
fsr_ = [" ".join(i.split(', ')[::-1]) for i in fsr_]
return fsr_
return fsr
except ldap.core.exceptions.LDAPException as error_message:
if self.opt.debug:
print("Couldn't Connect to ldap.\nUsing fallback File.\n%s\n" % error_message)
else:
print("Couldn't Connect to ldap.\nUsing fallback File.\n")
return fsr
def get_fsr_extern_ldap(self, fsr_extern):
"""get the EFSR from LDAP"""
print("Fetching EFSR from LDAP ...")
try:
conn = ldap.Connection(self.server)
conn.bind()
fil = '(cn=extern)'
fsr_extern_ = []
if conn.search('ou=group,'+self.base, fil,
attributes=['memberUid']):
for i in conn.entries[0]['memberUid']:
if conn.search('ou=people,' + self.base,
'(uid='+i+')', attributes=['displayName']):
fsr_extern_ += [str(conn.entries[0]['displayName'])]
fsr_extern_.sort()
fsr_extern_ = [" ".join(i.split(', ')[::-1]) for i in fsr_extern_]
return fsr_extern_
return fsr_extern
except ldap.core.exceptions.LDAPException as error_message:
if self.opt.debug:
print("Couldn't Connect to ldap.\nUsing fallback File.\n%s\n" % error_message)
from src.esme import *
if __name__ == "__main__":
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')
OPT = PARSER.parse_args()
CONFIG = configparser.ConfigParser()
CONFIG.read('./proto.ini')
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'))
for line in codecs.open('./fsr', 'r',
'utf-8').readlines()],
key=lambda x: ''.join(x.split(' ')[::-1]))
fsr[1] = sorted([re.sub(re.escape('\t'), ' ',
line.rstrip('\n'))
for line in codecs.open('./fsr_extern', 'r',
'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
date = (day+"."+mon+"."+year, year, mon, day)
print("\n")
protokollant = ESME.get_protokollant_ldap(CONFIG['DEFAULT']['Protokollant'])
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
sprecher = get_sprecher(CONFIG['DEFAULT']['Sprecher'])
protokollant = get_protokollant(protokollant)
date = get_date(date)
# self.editor = self.get_editor(self.editor)
OUTDIR = "../" + date[1] + "/fsr_physik_sitzung_" + date[1] + "-" \
+ date[2] + "-" + date[3] + "/"
if not os.path.exists(OUTDIR):
os.makedirs(OUTDIR)
shutil.copyfile("./FS-Logo.pdf", OUTDIR + "FS-Logo.pdf")
OUTFILE = OUTDIR + "/fsr_physik_protokoll_" + date[1] + "-" \
+ date[2] + "-" + date[3] + ".tex"
with codecs.open('name.tex', 'r', 'utf-8') as fobj_name:
f_name = fobj_name.read()
with codecs.open('acroname.tex', 'r', 'utf-8') as fobj_acroname:
f_acroname = fobj_acroname.read()
with codecs.open('acro.tex', 'r', 'utf-8') as fobj_acro:
f_acro = fobj_acro.read()
with codecs.open('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)
if OPT.Vollversamlung:
with codecs.open('foot_VV.tex', 'r', 'utf-8') as fobj_foot:
f_foot = fobj_foot.read()
else:
with codecs.open('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)
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 += " "
else:
print("Couldn't Connect to ldap.\nUsing fallback File.\n")
return fsr_extern
@staticmethod
def get_sprecher(sprecher):
"""get sprecher from user input"""
stmp = input("Sprecher ["+sprecher+"]: ")
if stmp != "":
return stmp
return sprecher
@staticmethod
def get_protokollant(protokollant):
"""get protokollant from user input"""
ptmp = input("Protokollant ["+protokollant+"]: ")
if ptmp != "":
return ptmp
return protokollant
@staticmethod
def get_date(date):
"""get date from user input"""
dtmp = input("Datum ["+date[0]+"]: ")
if dtmp != "":
day, mon, year = dtmp.split('.')
if len(mon) < 2: mon = "0" + mon
if len(day) < 2: day = "0" + day
date = day + "." + mon + "." + year
return (date, year, mon, day)
return date
@staticmethod
def get_editor(editor):
"""get the editor to use from user input"""
etmp = input("Editor ["+editor+"]: ")
if etmp != "":
return etmp
return editor
def main(self):
"""main function of the class"""
num_cols = self.config['DEFAULT'].getint('num_cols')
num_cols_ext = self.config['DEFAULT'].getint('num_cols_ext')
print("\n")
protokollant = self.get_protokollant_ldap(self.config['DEFAULT']['Protokollant'])
self.fsr[0] = self.get_fsr_ldap(self.fsr[0])
self.fsr[1] = self.get_fsr_extern_ldap(self.fsr[1])
print("\n")
print("Wenn der Standardwert (in []) stimmt einfach Enter!\n")
# get data form user
sprecher = self.get_sprecher(self.config['DEFAULT']['Sprecher'])
protokollant = self.get_protokollant(protokollant)
self.date = self.get_date(self.date)
# self.editor = self.get_editor(self.editor)
outdir = "../" + self.date[1] + "/fsr_physik_sitzung_" + self.date[1] + "-" \
+ self.date[2] + "-" + self.date[3] + "/"
if not os.path.exists(outdir):
os.makedirs(outdir)
shutil.copyfile("./FS-Logo.pdf", outdir + "FS-Logo.pdf")
outfile = outdir + "/fsr_physik_protokoll_" + self.date[1] + "-" \
+ self.date[2] + "-" + self.date[3] + ".tex"
with codecs.open('name.tex', 'r', 'utf-8') as fobj_name:
f_name = fobj_name.read()
with codecs.open('acroname.tex', 'r', 'utf-8') as fobj_acroname:
f_acroname = fobj_acroname.read()
with codecs.open('acro.tex', 'r', 'utf-8') as fobj_acro:
f_acro = fobj_acro.read()
with codecs.open('head.tex', 'r', 'utf-8') as fobj_head:
f_head = fobj_head.read()
f_head = self.reg['FSRN'].sub(f_name, f_head)
f_head = self.reg['ACRON'].sub(f_acroname, f_head)
f_head = self.reg['ACRO'].sub(f_acro, f_head)
f_head = self.reg['SPR'].sub(sprecher, f_head)
f_head = self.reg['PRO'].sub(protokollant, f_head)
f_head = self.reg['DATE'].sub(self.date[0], f_head)
if self.opt.Vollversamlung:
with codecs.open('foot_VV.tex', 'r', 'utf-8') as fobj_foot:
f_foot = fobj_foot.read()
else:
with codecs.open('foot.tex', 'r', 'utf-8') as fobj_foot:
f_foot = fobj_foot.read()
f_foot = self.reg['SPR'].sub(sprecher, f_foot)
f_foot = self.reg['PRO'].sub(protokollant, f_foot)
f_foot = self.reg['DATE'].sub(self.date[0], f_foot)
msg = ""
msg += f_head
msg += "~\\newline\n"
if not self.opt.Vollversamlung:
tabdef = "||"
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(self.fsr[0], range(len(self.fsr[0]))):
if i[1] % num_cols == 0:
msg += " "
else:
msg += "&"
msg += " &" + i[0]
if i[1] % num_cols == num_cols - 1:
msg += "\\\\\\hline\n"
else:
msg += "\n"
if not len(self.fsr[0])%num_cols == 0:
for i in range(num_cols - len(self.fsr[0])%num_cols):
msg += "& &\n"
msg += "&"
msg += " &" + i[0]
if i[1] % NUM_COLS == NUM_COLS - 1:
msg += "\\\\\\hline\n"
else:
msg += "\n"
msg += r"\end{tabular}}\normalsize\vspace{1em}"
msg += "\n\\newline\n"
if not len(fsr[0]) % NUM_COLS == 0:
for i in range(NUM_COLS - len(fsr[0]) % NUM_COLS):
msg += "& &\n"
msg += "\\\\\\hline\n"
tabdef = "||"
for i in range(num_cols_ext):
tabdef += "c|l||"
msg += "Externe Referenten\\\\\n\\\\\n" \
+ r"\scriptsize{\begin{tabular}{" \
+ tabdef + "}\n\\hline\n"
msg += r"\end{tabular}}\normalsize\vspace{1em}"
msg += "\n\\newline\n"
for i in zip(self.fsr[1], range(len(self.fsr[1]))):
if i[1] % num_cols_ext == 0:
msg += " "
else:
msg += "&"
for i in range(NUM_COLS_EXT):
TABDEF += "c|l||"
msg += "Externe Referenten\\\\\n\\\\\n" \
+ r"\scriptsize{\begin{tabular}{" \
+ TABDEF + "}\n\\hline\n"
msg += " &" +i[0]
for i in zip(fsr[1], range(len(fsr[1]))):
if i[1] % NUM_COLS_EXT == 0:
msg += " "
else:
msg += "&"
if i[1] % num_cols_ext == num_cols_ext - 1:
msg += "\\\\\\hline\n"
else:
msg += "\n"
msg += " &" + i[0]
if not len(self.fsr[1])%num_cols_ext == 0:
for i in range(num_cols_ext - len(self.fsr[1])%num_cols_ext):
msg += "& &\n"
if i[1] % NUM_COLS_EXT == NUM_COLS_EXT - 1:
msg += "\\\\\\hline\n"
else:
msg += "\n"
msg += "\\end{tabular}}\\normalsize"
msg += f_foot
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"
with codecs.open(outfile, 'w', 'utf-8') as fobj_out:
fobj_out.write(msg)
msg += "\\end{tabular}}\\normalsize"
msg += f_foot
if __name__ == "__main__":
S = Protokollskript(sys.argv)
S.main()
with codecs.open(OUTFILE, 'w', 'utf-8') as fobj_out:
fobj_out.write(msg)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment