Commit 8b3c1bba authored by Lorenz Steinert's avatar Lorenz Steinert

removed bugs and refractored code a bit

parent 01b0e176
......@@ -45,7 +45,7 @@ class FsrLdap:
if testing:
conn = ldap.Connection(self.server, ldap.MOCK_SYNC)
else:
conn = ldap:Connection(self.server)
conn = ldap.Connection(self.server)
conn.bind()
fil = "(uid="+current_user()+")"
if conn.search('ou=people,' + self.base, fil,
......@@ -61,14 +61,14 @@ class FsrLdap:
print("Couldn't Connect to ldap.\nUsing fallback default.\n")
return get_protokollant(config)
def get_fsr_ldap(self, share_dir=None, gen_fallback=False):
def get_fsr_ldap(self, share_dir=None, gen_fallback=False, testing=False):
"""get fsr from LDAP"""
print("Fetching FSR from LDAP ...")
try:
if testing:
conn = ldap.Connection(self.server, ldap.MOCK_SYNC)
conn = ldap.Connection(self.server, client_strategy=ldap.MOCK_SYNC)
else:
conn = ldap:Connection(self.server)
conn = ldap.Connection(self.server)
conn.bind()
fil = '(cn=intern)'
fsr = []
......@@ -95,14 +95,14 @@ class FsrLdap:
if not gen_fallback:
return get_fallback_fsr(share_dir)
def get_fsr_extern_ldap(self, share_dir=None, gen_fallback=False):
def get_fsr_extern_ldap(self, share_dir=None, gen_fallback=False, testing=False):
"""get the EFSR from LDAP"""
print("Fetching EFSR from LDAP ...")
try:
if testing:
conn = ldap.Connection(self.server, ldap.MOCK_SYNC)
conn = ldap.Connection(self.server, client_strategy=ldap.MOCK_SYNC)
else:
conn = ldap:Connection(self.server)
conn = ldap.Connection(self.server)
conn.bind()
fil = '(cn=extern)'
fsr_extern = []
......@@ -133,33 +133,29 @@ class FsrLdap:
def get_fallback_fsr(share_dir):
"""get fsr form fallback file"""
if os.path.isfile(os.path.join(share_dir, 'fsr')):
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]))
else:
print(os.path.abspath(os.path.join(share_dir, 'fsr'))
+ ' not found. Using empty FSR.')
return []
with codecs.open(os.path.join(share_dir, 'fsr'), 'r', 'utf-8') as fobj:
fsr = sorted([re.sub(re.escape('\t'), ' ',
line.rstrip('\n'))
for line in fobj.readlines()],
key=lambda x: ''.join(x.split(' ')[::-1]))
return fsr
print(os.path.abspath(os.path.join(share_dir, 'fsr'))
+ ' not found. Using empty FSR.')
return []
def get_fallback_fsr_extern(share_dir):
"""get fsr_extern from fallback file"""
if os.path.isfile(os.path.join(share_dir, 'fsr_extern')):
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]))
else:
print(os.path.abspath(os.path.join(share_dir, 'fsr_extern'))
+ ' not found. Using empty FSR_extern.')
return []
with codecs.open(os.path.join(share_dir, 'fsr_extern'), 'r', 'utf-8') as fobj:
fsr_extern = sorted([re.sub(re.escape('\t'), ' ',
line.rstrip('\n'))
for line in fobj.readlines()],
key=lambda x: ''.join(x.split(' ')[::-1]))
return fsr_extern
print(os.path.abspath(os.path.join(share_dir, 'fsr_extern'))
+ ' not found. Using empty FSR_extern.')
return []
def get_sprecher(config=None):
......@@ -228,19 +224,17 @@ def get_config(rundir, config_path=None, default=False):
config = configparser.ConfigParser()
config.read(config_path)
return config
else:
print(config_path + ': Not a file')
sys.exit(2)
print(config_path + ': Not a file')
sys.exit(2)
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)
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()
......@@ -251,9 +245,8 @@ def get_config(rundir, config_path=None, default=False):
config = configparser.ConfigParser()
config.read(os.path.join(rundir, '../etc/proto.ini.default'))
return config
else:
print('No ini file found. Using hardcoded defaults.')
return None
print('No ini file found. Using hardcoded defaults.')
return None
def get_share(rundir, config=None, sharedir=None):
......@@ -261,21 +254,18 @@ def get_share(rundir, config=None, sharedir=None):
if sharedir:
if os.path.isdir(sharedir):
return sharedir
else:
print(sharedir + ': Not a Directory')
sys.exit(2)
print(sharedir + ': Not a Directory')
sys.exit(2)
elif config:
if os.path.isdir(config['PROTOKOLL']['share']):
return config['PROTOKOLL']['share']
else:
print(config['PROTOKOLL']['share'] + ': Not a Directory')
sys.exit(2)
print(config['PROTOKOLL']['share'] + ': Not a Directory')
sys.exit(2)
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')))
sys.exit(1)
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):
......@@ -283,47 +273,41 @@ def get_path(rundir, config=None, path=None):
if path:
if os.path.isdir(path):
return path
else:
print(path + ': Not a Directory')
sys.exit(2)
print(path + ': Not a Directory')
sys.exit(2)
elif config:
if os.path.isdir(config['PROTOKOLL']['path']):
return config['PROTOKOLL']['path']
else:
print(config['PROTOKOLL']['path'] + ': Not a Directory')
sys.exit(2)
print(config['PROTOKOLL']['path'] + ': Not a Directory')
sys.exit(2)
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, '../..')))
sys.exit(1)
print('No Protokoll Directory found at '
+ os.path.abspath(os.path.join(rundir, '../..')))
sys.exit(1)
def get_ldap_server(config=None, server=None):
"""get the ldap server for the fsr lookup"""
if server:
return server
elif config:
if config:
return config['SERVER']['NAME']
else:
print("No LDAP Server supplied. Triing to use \"rincwind.fs.physik.uni-kl.de\".")
return "rincewind.fs.physik.uni-kl.de"
print("No LDAP Server supplied. Triing to use \"rincwind.fs.physik.uni-kl.de\".")
return "rincewind.fs.physik.uni-kl.de"
def get_ldap_base(config=None, base=None):
"""get the search base for the ldap server"""
if base:
return base
elif config:
if config:
return config['SERVER']['BASE']
else:
print("No LDAP search base supplied. Triing to use \"dc=fs,dc=physik,dc=uni-kl,dc=de\".")
return "dc=fs,dc=physik,dc=uni-kl,dc=de"
print("No LDAP search base supplied. Triing to use \"dc=fs,dc=physik,dc=uni-kl,dc=de\".")
return "dc=fs,dc=physik,dc=uni-kl,dc=de"
def get_server_timeout(config=None, test=None):
"""get the timeout for the connection to the ldap server"""
if test:
return 1
elif config:
if config:
return config['SERVER'].getint('connect_timeout')
else:
return 10
return 10
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