Commit a8acf22b authored by Lorenz Steinert's avatar Lorenz Steinert

get_ldap working

parent 7cdc36b8
......@@ -6,20 +6,21 @@ Created on Fri Nov 24 15:35:04 2017
@author: lorico
"""
import ldap3 as ldap
import time
import os
import re
import os
import time
import ldap3 as ldap
try:
import pwd
import pwd
except ImportError:
import getpass
pwd = None
import getpass
pwd = None
class Protokollskript:
"""Class die die protokoll .tex datei erstellt"""
def current_user(self):
"""get the current user"""
if pwd:
return pwd.getpwuid(os.geteuid()).pw_name
else:
......@@ -47,80 +48,124 @@ class Protokollskript:
self.reg_A = re.compile('\"A')
self.reg_O = re.compile('\"O')
self.reg_U = re.compile('\"U')
self.reg_kov = re.compile('Kovac')
self.fsr = [re.sub(re.escape('\t'),' ', line.rstrip('\n')) for line in open('fsr','r').readlines()]
print(self.fsr)
self.reg_Kovac = re.compile('Kovac')
self.fsr = [re.sub(re.escape('\t'), ' ', line.rstrip('\n')) for line in open('fsr', 'r').readlines()]
self.fsr_extern = [re.sub(re.escape('\t'), ' ' , line.rstrip('\n')) for line in open('fsr_extern', 'r').readlines()]
def get_protokollant_ldap(self):
def get_protokollant_ldap(self,protokollant):
try:
conn = ldap.Connection(self.server)
conn.bind()
#l = ldap.open(server)
#l.simple_bind_s("","")
fil = "(uid="+self.userid+")"
if conn.search('ou=people,'+self.base,fil,attributes=['gecos']):
self.protokollant = " ".join(str(conn.entries[0]['gecos']).split(", ")[::-1])
if conn.search('ou=people,'+self.base, fil, attributes=['gecos']):
return (" ".join(str(conn.entries[0]['gecos']).split(", ")[::-1]))
else:
return protokollant
except ldap.core.exceptions.LDAPException as error_message:
print("Couldn't Connect to ldap. %s " % error_message)
return protokollant
def get_fsr_ldap(self):
def get_fsr_ldap(self,fsr):
try:
conn = ldap.Connection(self.server)
conn.bind()
fil = '(& (cn=fsr) (!cn=extern))'
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=['gecos']):
fsr_ += [self.reg_A.sub('Ä',self.reg_O.sub('Ö',self.reg_U.sub('Ü',self.reg_kov.sub(re.escape('Kova\v{c}'),self.reg_a.sub('ä',self.reg_o.sub('ö',self.reg_u.sub('ü',str(conn.entries[0]['gecos']))))))))]
fsr_ += [self.reg_Kovac.sub('Kova\v{c}', self.reg_A.sub('Ä', self.reg_O.sub('Ö', self.reg_U.sub('Ü', self.reg_a.sub('ä', self.reg_o.sub('ö', self.reg_u.sub('ü', str(conn.entries[0]['gecos']))))))))]
fsr_.sort()
fsr_ = [" ".join(i.split(', ')[::-1]) for i in fsr_]
self.fsr = fsr_
return fsr_
else:
return fsr
except ldap.core.exceptions.LDAPException as error_message:
print("Couldn't Connect to ldap. %s " % error_message)
return fsr
def get_sprecher(self):
def get_fsr_extern_ldap(self,fsr_extern):
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=['gecos']):
fsr_extern_ += [self.reg_Kovac.sub('Kova\v{c}', self.reg_A.sub('Ä', self.reg_O.sub('Ö', self.reg_U.sub('Ü', self.reg_a.sub('ä', self.reg_o.sub('ö', self.reg_u.sub('ü', str(conn.entries[0]['gecos']))))))))]
fsr_extern_.sort()
fsr_extern_ = [" ".join(i.split(', ')[::-1]) for i in fsr_extern_]
return fsr_extern_
else:
return fsr_extern
except ldap.core.exceptions.LDAPException as error_message:
print("Couldn't Connect to ldap. %s " % error_message)
return fsr_extern
def get_sprecher(self,sprecher):
stmp = input("Sprecher ["+self.sprecher+"]: ")
if stmp != "":
self.sprecher = stmp
return stmp
else:
return sprecher
def get_protokollant(self):
def get_protokollant(self,protokollant):
ptmp = input("Protokollant ["+self.protokollant+"]: ")
if ptmp != "":
self.protokollant = ptmp
return ptmp
else:
return protokollant
def get_date(self):
def get_date(self,date):
dtmp = input("Datum ["+self.date+"]: ")
if dtmp != "":
self.date = dtmp
return dtmp
else:
date
def get_editor(self):
def get_editor(self,editor):
etmp = input("Editor ["+self.editor+"]: ")
if etmp != "":
self.editor = etmp
return etmp
else:
return editor
def main(self):
self.get_protokollant_ldap()
self.protokollant = self.get_protokollant_ldap(self.protokollant)
print("Wenn der Standardwert (in []) stimmt einfach Enter!\n")
#get data form user
self.get_sprecher()
self.get_protokollant()
self.get_date()
self.get_editor()
self.get_fsr_ldap()
self.sprecher = self.get_sprecher(self.sprecher)
self.protokollant = self.get_protokollant(self.protokollant)
self.date = self.get_date(self.date)
self.editor = self.get_editor(self.editor)
self.fsr = self.get_fsr_ldap(self.fsr)
self.fsr_extern = self.get_fsr_extern_ldap(self.fsr_extern)
print(self.fsr)
print(self.fsr_extern)
outdir = "../"+str(self.year)+"/fsr_physik_sitzung_"+str(self.year)+"-"+str(self.mon)+"-"+str(self.day)+"/"
#os.mkdir(outdir,mode=777)
if not os.path.exists(outdir):
os.makedirs(outdir)
outfile = outdir+"/fsr_physik_protokoll_"+str(self.year)+"-"+str(self.mon)+"-"+str(self.day)+".tex"
#daten = open('fsr','r')
#daten_ext = open('fsr_extern','r')
#head = open('head.tex','r')
#foot = open('foot.tex','r')
with open('head.tex','r') as f:
f_head = f.read()
with open('foot.tex','r') as f:
f_foot = f.read()
print(f_head)
print(f_foot)
#print(daten,daten_ext,head,foot)
if __name__=="__main__":
......
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