Commit 03ee269e authored by Lorenz Steinert's avatar Lorenz Steinert

rewrite of skript

Rewrote the Skript.
Das Skript benutzt nun mehr funktionen und ist größtenteils style guide
conform.
parent 8f7d0ed3
......@@ -7,7 +7,10 @@ Created on Fri Nov 24 15:35:04 2017
"""
import os
import re
import sys
import time
import codecs
import configparser
import ldap3 as ldap
......@@ -45,66 +48,88 @@ class fsr_ldap:
if conn.search('ou=people,'+self.base, fil,
attributes=['displayName']):
return " ".join(str(conn.entries[0]['displayName']).split(", ")[::-1])
return protokollant
return get_protokollant(protokollant)
except ldap.core.exceptions.LDAPException as error_message:
if self.debug:
print("Couldn't Connect to ldap.\n \
Using fallback default.\n%s\n" % error_message)
else:
print("Couldn't Connect to ldap.\nUsing fallback default.\n")
return protokollant
return get_protokollant(protokollant)
def get_fsr_ldap(self, fsr):
def get_fsr_ldap(self, share_dir):
"""get fsr from LDAP"""
print("Fetching FSR from LDAP ...")
try:
conn = ldap.Connection(self.server)
conn.bind()
fil = '(cn=intern)'
fsr_ = []
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
fsr += [str(conn.entries[0]['displayName'])]
fsr.sort()
fsr = [" ".join(i.split(', ')[::-1]) for i in fsr]
return fsr
return get_fallback_fsr(share_dir)
except ldap.core.exceptions.LDAPException as error_message:
if self.debug:
print("Couldn't Connect to ldap.\n \
Using fallback File.\n%s\n" % error_message)
else:
print("Couldn't Connect to ldap.\nUsing fallback File.\n")
return fsr
return get_fallback_fsr(share_dir)
def get_fsr_extern_ldap(self, fsr_extern):
def get_fsr_extern_ldap(self, share_dir):
"""get the EFSR from LDAP"""
print("Fetching EFSR from LDAP ...")
try:
conn = ldap.Connection(self.server)
conn.bind()
fil = '(cn=extern)'
fsr_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
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 get_fallback_fsr_extern(share_dir)
except ldap.core.exceptions.LDAPException as error_message:
if self.debug:
print("Couldn't Connect to ldap.\n \
Using fallback File.\n%s\n" % error_message)
else:
print("Couldn't Connect to ldap.\nUsing fallback File.\n")
return fsr_extern
return get_fallback_fsr_extern(share_dir)
def get_fallback_fsr(share_dir):
"""get fsr form fallback file"""
return sorted([re.sub(re.escape('\t'), ' ',
line.rstrip('\n'))
for line in codecs.open(os.path.join(share_dir,
'fsr'),
'r',
'utf-8').readlines()],
key=lambda x: ''.join(x.split(' ')[::-1]))
def get_fallback_fsr_extern(share_dir):
"""get fsr_extern from fallback file"""
return sorted([re.sub(re.escape('\t'), ' ',
line.rstrip('\n'))
for line in codecs.open(os.path.join(share_dir,
'fsr_extern'),
'r',
'utf-8').readlines()],
key=lambda x: ''.join(x.split(' ')[::-1]))
def get_sprecher(sprecher):
......@@ -123,8 +148,20 @@ def get_protokollant(protokollant):
return protokollant
def get_date(date):
"""get date from user input"""
def get_date(test=False):
"""get current date then ask user"""
if 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)
dtmp = input("Datum ["+date[0]+"]: ")
if dtmp != "":
day, mon, year = dtmp.split('.')
......@@ -134,6 +171,7 @@ def get_date(date):
day = "0" + day
date = day + "." + mon + "." + year
return (date, year, mon, day)
return date
......@@ -145,7 +183,8 @@ def get_editor(editor):
return editor
def get_config(rundir, config_path=None):
def get_config(rundir, config_path=None, default=False):
"""get the location of the config file and load configparser"""
if config_path:
if os.path.isfile(config_path):
config = configparser.ConfigParser()
......@@ -154,7 +193,18 @@ def get_config(rundir, config_path=None):
else:
print(config_path + ': Not a file')
sys.exit(2)
elif os.path.isfile(os.path.join(rundir, '../etc/proto.ini')):
elif default:
if os.path.isfile(os.path.join(rundir, '../etc/proto.ini.default')):
config = configparser.ConfigParser()
config.read(os.path.join(rundir, '../etc/proto.ini.default'))
return config
else:
print('No default File found at '
+ os.path.abspath(os.path.join(rundir,
'../etc/proto.ini.default')))
sys.exit(1)
elif os.path.isfile(os.path.join(rundir,
'../etc/proto.ini')) and not default:
config = configparser.ConfigParser()
config.read(os.path.join(rundir, '../etc/proto.ini'))
return config
......@@ -164,6 +214,7 @@ def get_config(rundir, config_path=None):
def get_share(rundir, config=None, sharedir=None):
"""get the location of the share directory"""
if sharedir:
if os.path.isdir(sharedir):
return sharedir
......@@ -179,11 +230,13 @@ def get_share(rundir, config=None, sharedir=None):
elif os.path.isdir(os.path.join(rundir, '../share')):
return os.path.join(rundir, '../share')
else:
print('No share Directory found at ' + os.path.abspath(os.path.join(rundir, '../share')))
print('No share Directory found at '
+ os.path.abspath(os.path.join(rundir, '../share')))
sys.exit(1)
def get_path(rundir, config=None, path=None):
"""get the location of the protokoll directory"""
if path:
if os.path.isdir(path):
return path
......@@ -199,5 +252,6 @@ def get_path(rundir, config=None, path=None):
elif os.path.isdir(os.path.join(rundir, '../..')):
return os.path.join(rundir, '../..')
else:
print('No Protokoll Directory found at ' + os.path.abspath(os.path.join(rundir, '../..')))
print('No Protokoll Directory found at '
+ os.path.abspath(os.path.join(rundir, '../..')))
sys.exit(1)
This diff is collapsed.
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