Commit a8937bf2 authored by Lorenz Steinert's avatar Lorenz Steinert

added better argumentparsing

parent fa9d3fa6
......@@ -11,6 +11,7 @@ import os
import sys
import shutil
import time
import argparse
import codecs
import configparser
import ldap3 as ldap
......@@ -33,20 +34,36 @@ 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()
print(self.opt)
self.config = configparser.ConfigParser()
self.config.read('./proto.ini')
self.server = ldap.Server("rincewind.fs.physik.uni-kl.de", connect_timeout=self.config['SERVER'].getint('connect_timeout'))
self.base = "dc=fs,dc=physik,dc=uni-kl,dc=de"
if self.opt.test:
timeout = 1
else:
timeout = self.config['SERVER'].getint('connect_timeout')
year = str(time.localtime()[0])
mon = str(time.localtime()[1])
day = str(time.localtime()[2])
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 len(mon) < 2: mon = "0"+mon
if len(day) < 2: day = "0"+day
self.date = (day+"."+mon+"."+year, year, mon, day)
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'),
......@@ -57,31 +74,17 @@ class Protokollskript:
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]))
self.opt = {'-d': False, '-VV': False, '-h': False}
for i in opt[1:]:
if i in self.opt.keys():
self.opt[i] = True
else:
print(i + ' ist keine Option für das Protokollskript. \
\nVerfügbare Optionen sind: \n' +
str(list(self.opt.keys())))
raise SystemExit(0)
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"""
......@@ -94,11 +97,10 @@ class Protokollskript:
return " ".join(str(conn.entries[0]['displayName']).split(", ")[::-1])
return protokollant
except ldap.core.exceptions.LDAPException as error_message:
if self.opt['-d']:
print("Couldn't Connect to ldap.\n \
Using fallback default.\n %s " % 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.")
print("Couldn't Connect to ldap.\nUsing fallback default.\n")
return protokollant
def get_fsr_ldap(self, fsr):
......@@ -120,10 +122,10 @@ class Protokollskript:
return fsr_
return fsr
except ldap.core.exceptions.LDAPException as error_message:
if self.opt['-d']:
print("Couldn't Connect to ldap.\nUsing fallback File.\n %s " % 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.")
print("Couldn't Connect to ldap.\nUsing fallback File.\n")
return fsr
def get_fsr_extern_ldap(self, fsr_extern):
......@@ -145,10 +147,10 @@ class Protokollskript:
return fsr_extern_
return fsr_extern
except ldap.core.exceptions.LDAPException as error_message:
if self.opt['-d']:
print("Couldn't Connect to ldap.\nUsing fallback File.\n %s " % 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.")
print("Couldn't Connect to ldap.\nUsing fallback File.\n")
return fsr_extern
@staticmethod
......@@ -187,20 +189,9 @@ class Protokollskript:
return etmp
return editor
@staticmethod
def help():
print('')
print('proto.py : Erstellt ein Sitzungs-Protokoll')
print('proto.py -h : Gibt diese Hilfe aus.')
print('proto.py -VV: erstellt ein VV-Protokoll.')
raise SystemExit(0)
def main(self):
"""main function of the class"""
if self.opt['-h']: self.help()
num_cols = self.config['DEFAULT'].getint('num_cols')
num_cols_ext = self.config['DEFAULT'].getint('num_cols_ext')
......@@ -246,7 +237,7 @@ class Protokollskript:
f_head = self.reg['PRO'].sub(protokollant, f_head)
f_head = self.reg['DATE'].sub(self.date[0], f_head)
if self.opt['-VV']:
if self.opt.Vollversamlung:
with codecs.open('foot_VV.tex', 'r', 'utf-8') as fobj_foot:
f_foot = fobj_foot.read()
else:
......@@ -263,7 +254,7 @@ class Protokollskript:
msg += "~\\newline\n"
if not self.opt['-VV']:
if not self.opt.Vollversamlung:
tabdef = "||"
for i in range(num_cols):
tabdef += "c|l||"
......
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