Commit 287530ea authored by Lorenz Steinert's avatar Lorenz Steinert

added skript to generate fallback files

parent 63408309
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Nov 24 15:35:04 2017
@author: lorico
"""
import re
import os
import sys
import shutil
import time
import ldap3 as ldap
import codecs
class fetch():
def __init__(self):
self.server = ldap.Server("rincewind.fs.physik.uni-kl.de")
self.reg_a = re.compile('\"a')
self.reg_o = re.compile('\"o')
self.reg_u = re.compile('\"u')
self.reg_A = re.compile('\"A')
self.reg_O = re.compile('\"O')
self.reg_U = re.compile('\"U')
self.reg_Kovac = re.compile('Kovac')
self.base = "dc=fs,dc=physik,dc=uni-kl,dc=de"
def clean_string(self, data):
"""remove "A, "O, "U, "a, "o, "u from input"""
data = self.reg_Kovac.sub("Kova"+r"\v"+"{c}", data)
data = self.reg_A.sub('Ä', data)
data = self.reg_O.sub('Ö', data)
data = self.reg_U.sub('Ü', data)
data = self.reg_a.sub('ä', data)
data = self.reg_o.sub('ö', data)
data = self.reg_u.sub('ü', data)
return data
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=['gecos']):
fsr_ += [self.clean_string(str(conn.entries[0]['gecos']))]
fsr_.sort()
fsr_ = [" ".join(i.split(', ')[::-1]) for i in fsr_]
return fsr_
return fsr
except ldap.core.exceptions.LDAPException as error_message:
print("Couldn't Connect to ldap.\nUsing fallback File.\n %s " % error_message)
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=['gecos']):
fsr_extern_ += [self.clean_string(str(conn.entries[0]['gecos']))]
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:
print("Couldn't Connect to ldap.\nUsing fallback File.\n %s " % error_message)
return fsr_extern
def main(self):
fsr = self.get_fsr_ldap([])
fsr_extern = self.get_fsr_extern_ldap([])
fsr_out = ""
fsr_extern_out = ""
for i in fsr:
if "Sarah" in i:
fsr_out += r"Sarah Anna Kova\\\\v{c}"+"\n"
else:
fsr_out += i + "\n"
for i in fsr_extern:
if "Sarah" in i:
fsr_extern_out += r"Sarah Anna Kova\\\\v{c}"+"\n"
else:
fsr_extern_out += i + "\n"
with codecs.open('fsr', 'w', 'utf-8') as fobj_fsr_out:
fobj_fsr_out.write(fsr_out)
with codecs.open('fsr_extern', 'w', 'utf-8') as fobj_fsr_extern_out:
fobj_fsr_extern_out.write(fsr_extern_out)
if __name__=="__main__":
fetch().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