Commit 9735feac authored by Lorenz Steinert's avatar Lorenz Steinert

added logging suport

parent 3310433d
......@@ -11,6 +11,7 @@ import re
import sys
import time
import codecs
import logging
import configparser
from enum import Flag, auto
import ldap3 as ldap
......@@ -66,7 +67,7 @@ class FsrLdap:
def get_protokollant_ldap(self, config, testing=False):
"""search the LDAP for the current user"""
print("Fetching Protokollant from LDAP ...")
logging.info("Fetching Protokollant from LDAP ...")
try:
if testing:
conn = ldap.Connection(self.server, ldap.MOCK_SYNC)
......@@ -76,20 +77,20 @@ class FsrLdap:
fil = "(uid="+current_user()+")"
if conn.search('ou=people,' + self.base, fil,
attributes=['displayName']):
print("Done")
logging.info("Done")
return " ".join(str(conn.entries[0]['displayName']).split(", ")[::-1])
return get_protokollant(config)
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)
logging.warning("Couldn't Connect to ldap. \
Using fallback default.%s" % error_message)
else:
print("Couldn't Connect to ldap.\nUsing fallback default.\n")
logging.warning("Couldn't Connect to ldap. Using fallback default.")
return get_protokollant(config)
def _get_fsr_ldap(self):
"""get fsr from LDAP"""
print("Fetching FSR from LDAP ...")
logging.info("Fetching FSR from LDAP ...")
try:
fil = '(cn=intern)'
fsr = []
......@@ -101,15 +102,15 @@ class FsrLdap:
fsr += [str(self.conn.entries[0]['displayName'])]
fsr.sort()
fsr = [" ".join(i.split(', ')[::-1]) for i in fsr]
print("Done")
logging.info("Done")
return fsr
return Error.UNSEC_SEARCH
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)
logging.warning("Couldn't Connect to ldap. \
Using fallback File. %s" % error_message)
else:
print("Couldn't Connect to ldap.\nUsing fallback File.\n")
logging.warning("Couldn't Connect to ldap. Using fallback File. ")
return (Error.LDAP_FAIL, error_message)
@staticmethod
......@@ -141,7 +142,7 @@ class FsrLdap:
def _get_fsr_extern_ldap(self):
"""get the EFSR from LDAP"""
print("Fetching EFSR from LDAP ...")
logging.info("Fetching EFSR from LDAP ...")
try:
fil = '(cn=extern)'
fsr_extern = []
......@@ -153,15 +154,15 @@ class FsrLdap:
fsr_extern += [str(self.conn.entries[0]['displayName'])]
fsr_extern.sort()
fsr_extern = [" ".join(i.split(', ')[::-1]) for i in fsr_extern]
print("Done")
logging.info("Done")
return fsr_extern
return Error.UNSEC_SEARCH
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)
logging.warning("Couldn't Connect to ldap. \
Using fallback File. %s" % error_message)
else:
print("Couldn't Connect to ldap.\nUsing fallback File.\n")
logging.warning("Couldn't Connect to ldap. Using fallback File.")
return (Error.LDAP_FAIL, error_message)
@staticmethod
......@@ -256,14 +257,14 @@ def get_config(rundir, config_path=None, default=False):
config = configparser.ConfigParser()
config.read(config_path)
return config
print(config_path + ': Not a file')
logging.warning(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
print('No default File found at '
logging.warning('No default File found at '
+ os.path.abspath(os.path.join(rundir,
'../etc/proto.ini.default')))
sys.exit(1)
......@@ -273,11 +274,11 @@ def get_config(rundir, config_path=None, default=False):
config.read(os.path.join(rundir, '../etc/proto.ini'))
return config
elif os.path.isfile(os.path.join(rundir, '../etc/proto.ini.default')):
print('No non default ini file found using default.')
logging.warning('No non default ini file found using default file.')
config = configparser.ConfigParser()
config.read(os.path.join(rundir, '../etc/proto.ini.default'))
return config
print('No ini file found. Using hardcoded defaults.')
logging.warning('No ini file found. Using hardcoded defaults.')
return None
......@@ -286,16 +287,16 @@ def get_share(rundir, config=None, sharedir=None):
if sharedir:
if os.path.isdir(sharedir):
return sharedir
print(sharedir + ': Not a Directory')
logging.warning(sharedir + ': Not a Directory')
sys.exit(2)
elif config:
if os.path.isdir(config['PROTOKOLL']['share']):
return config['PROTOKOLL']['share']
print(config['PROTOKOLL']['share'] + ': Not a Directory')
logging.warning(config['PROTOKOLL']['share'] + ': Not a Directory')
sys.exit(2)
elif os.path.isdir(os.path.join(rundir, '../share')):
return os.path.join(rundir, '../share')
print('No share Directory found at '
logging.warning('No share Directory found at '
+ os.path.abspath(os.path.join(rundir, '../share')))
sys.exit(1)
......@@ -305,17 +306,17 @@ def get_path(rundir, config=None, path=None):
if path:
if os.path.isdir(path):
return path
print(path + ': Not a Directory')
logging.warning(path + ': Not a Directory')
sys.exit(2)
elif config:
if os.path.isdir(config['PROTOKOLL']['path']):
return config['PROTOKOLL']['path']
print(config['PROTOKOLL']['path'] + ': Not a Directory')
logging.warning(config['PROTOKOLL']['path'] + ': Not a Directory')
sys.exit(2)
elif os.path.isdir(os.path.join(rundir, '../..')):
return os.path.join(rundir, '../..')
print('No Protokoll Directory found at '
+ os.path.abspath(os.path.join(rundir, '../..')))
logging.warning('No Protokoll Directory found at '
+ os.path.abspath(os.path.join(rundir, '../..')))
sys.exit(1)
......@@ -325,7 +326,7 @@ def get_ldap_server(config=None, server=None):
return server
if config:
return config['SERVER']['NAME']
print("No LDAP Server supplied. Triing to use \"rincwind.fs.physik.uni-kl.de\".")
logging.info("No LDAP Server supplied. Triing to use \"rincwind.fs.physik.uni-kl.de\".")
return "rincewind.fs.physik.uni-kl.de"
......@@ -335,7 +336,7 @@ def get_ldap_base(config=None, base=None):
return base
if config:
return config['SERVER']['BASE']
print("No LDAP search base supplied. Triing to use \"dc=fs,dc=physik,dc=uni-kl,dc=de\".")
logging.info("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"
......
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