LdapServer: fix python3 compatibility

This commit is contained in:
Benjamin Renard 2021-03-24 12:33:45 +01:00
parent 3901c1bd49
commit bb5ef528b0

View file

@ -1,4 +1,4 @@
#!/usr/bin/python #!/usr/bin/env python3
import copy import copy
import datetime import datetime
@ -51,7 +51,7 @@ class LdapServer(object):
self.con = con self.con = con
return True return True
except ldap.LDAPError, e: except ldap.LDAPError as e:
self._error('LdapServer - Error connecting and binding to LDAP server : %s' % e,logging.CRITICAL) self._error('LdapServer - Error connecting and binding to LDAP server : %s' % e,logging.CRITICAL)
return False return False
return True return True
@ -121,12 +121,12 @@ class LdapServer(object):
attrs, attrs,
serverctrls=[page_control] serverctrls=[page_control]
) )
except ldap.LDAPError, e: except ldap.LDAPError as e:
self._error('LdapServer - Error running paged search on LDAP server: %s' % e, logging.CRITICAL) self._error('LdapServer - Error running paged search on LDAP server: %s' % e, logging.CRITICAL)
return False return False
try: try:
rtype, rdata, rmsgid, rctrls = self.con.result3(res_id) rtype, rdata, rmsgid, rctrls = self.con.result3(res_id)
except ldap.LDAPError, e: except ldap.LDAPError as e:
self._error('LdapServer - Error pulling paged search result from LDAP server: %s' % e, logging.CRITICAL) self._error('LdapServer - Error pulling paged search result from LDAP server: %s' % e, logging.CRITICAL)
return False return False
@ -163,7 +163,7 @@ class LdapServer(object):
self.logger.debug("LdapServer - Add %s" % dn) self.logger.debug("LdapServer - Add %s" % dn)
self.con.add_s(dn,ldif) self.con.add_s(dn,ldif)
return True return True
except ldap.LDAPError, e: except ldap.LDAPError as e:
self._error("LdapServer - Error adding %s : %s" % (dn,e), logging.ERROR) self._error("LdapServer - Error adding %s : %s" % (dn,e), logging.ERROR)
return False return False
@ -175,7 +175,7 @@ class LdapServer(object):
try: try:
self.con.modify_s(dn,ldif) self.con.modify_s(dn,ldif)
return True return True
except ldap.LDAPError, e: except ldap.LDAPError as e:
self._error("LdapServer - Error updating %s : %s\nOld : %s\nNew : %s" % (dn, e, old, new), logging.ERROR) self._error("LdapServer - Error updating %s : %s\nOld : %s\nNew : %s" % (dn, e, old, new), logging.ERROR)
return False return False
@ -226,7 +226,7 @@ class LdapServer(object):
) )
self.con.rename_s(dn, new_rdn, newsuperior=new_sup, delold=delete_old) self.con.rename_s(dn, new_rdn, newsuperior=new_sup, delold=delete_old)
return True return True
except ldap.LDAPError, e: except ldap.LDAPError as e:
self._error( self._error(
"LdapServer - Error renaming %s in %s (new superior: %s, delete old: %s): %s" % ( "LdapServer - Error renaming %s in %s (new superior: %s, delete old: %s): %s" % (
dn, dn,
@ -245,7 +245,7 @@ class LdapServer(object):
self.logger.debug("LdapServer - Delete %s" % dn) self.logger.debug("LdapServer - Delete %s" % dn)
self.con.delete_s(dn) self.con.delete_s(dn)
return True return True
except ldap.LDAPError, e: except ldap.LDAPError as e:
self._error("LdapServer - Error deleting %s : %s" % (dn,e), logging.ERROR) self._error("LdapServer - Error deleting %s : %s" % (dn,e), logging.ERROR)
return False return False
@ -388,50 +388,50 @@ def format_date(value, from_timezone=None, to_timezone=None, naive=None):
# #
if __name__ == '__main__': if __name__ == '__main__':
now = datetime.datetime.now().replace(tzinfo=dateutil.tz.tzlocal()) now = datetime.datetime.now().replace(tzinfo=dateutil.tz.tzlocal())
print "Now = %s" % now print("Now = %s" % now)
datestring_now = format_datetime(now) datestring_now = format_datetime(now)
print "format_datetime : %s" % datestring_now print("format_datetime : %s" % datestring_now)
print "format_datetime (from_timezone=utc) : %s" % format_datetime(now.replace(tzinfo=None), from_timezone=pytz.utc) print("format_datetime (from_timezone=utc) : %s" % format_datetime(now.replace(tzinfo=None), from_timezone=pytz.utc))
print "format_datetime (from_timezone=local) : %s" % format_datetime(now.replace(tzinfo=None), from_timezone=dateutil.tz.tzlocal()) print("format_datetime (from_timezone=local) : %s" % format_datetime(now.replace(tzinfo=None), from_timezone=dateutil.tz.tzlocal()))
print "format_datetime (from_timezone='local') : %s" % format_datetime(now.replace(tzinfo=None), from_timezone='local') print("format_datetime (from_timezone='local') : %s" % format_datetime(now.replace(tzinfo=None), from_timezone='local'))
print "format_datetime (from_timezone=Paris) : %s" % format_datetime(now.replace(tzinfo=None), from_timezone='Europe/Paris') print("format_datetime (from_timezone=Paris) : %s" % format_datetime(now.replace(tzinfo=None), from_timezone='Europe/Paris'))
print "format_datetime (to_timezone=utc) : %s" % format_datetime(now, to_timezone=pytz.utc) print("format_datetime (to_timezone=utc) : %s" % format_datetime(now, to_timezone=pytz.utc))
print "format_datetime (to_timezone=local) : %s" % format_datetime(now, to_timezone=dateutil.tz.tzlocal()) print("format_datetime (to_timezone=local) : %s" % format_datetime(now, to_timezone=dateutil.tz.tzlocal()))
print "format_datetime (to_timezone='local') : %s" % format_datetime(now, to_timezone='local') print("format_datetime (to_timezone='local') : %s" % format_datetime(now, to_timezone='local'))
print "format_datetime (to_timezone=Tokyo) : %s" % format_datetime(now, to_timezone='Asia/Tokyo') print("format_datetime (to_timezone=Tokyo) : %s" % format_datetime(now, to_timezone='Asia/Tokyo'))
print "format_datetime (naive=True) : %s" % format_datetime(now, naive=True) print("format_datetime (naive=True) : %s" % format_datetime(now, naive=True))
print "format_date : %s" % format_date(now) print("format_date : %s" % format_date(now))
print "format_date (from_timezone=utc) : %s" % format_date(now.replace(tzinfo=None), from_timezone=pytz.utc) print("format_date (from_timezone=utc) : %s" % format_date(now.replace(tzinfo=None), from_timezone=pytz.utc))
print "format_date (from_timezone=local) : %s" % format_date(now.replace(tzinfo=None), from_timezone=dateutil.tz.tzlocal()) print("format_date (from_timezone=local) : %s" % format_date(now.replace(tzinfo=None), from_timezone=dateutil.tz.tzlocal()))
print "format_date (from_timezone='local') : %s" % format_date(now.replace(tzinfo=None), from_timezone='local') print("format_date (from_timezone='local') : %s" % format_date(now.replace(tzinfo=None), from_timezone='local'))
print "format_date (from_timezone=Paris) : %s" % format_date(now.replace(tzinfo=None), from_timezone='Europe/Paris') print("format_date (from_timezone=Paris) : %s" % format_date(now.replace(tzinfo=None), from_timezone='Europe/Paris'))
print "format_date (to_timezone=utc) : %s" % format_date(now, to_timezone=pytz.utc) print("format_date (to_timezone=utc) : %s" % format_date(now, to_timezone=pytz.utc))
print "format_date (to_timezone=local) : %s" % format_date(now, to_timezone=dateutil.tz.tzlocal()) print("format_date (to_timezone=local) : %s" % format_date(now, to_timezone=dateutil.tz.tzlocal()))
print "format_date (to_timezone='local') : %s" % format_date(now, to_timezone='local') print("format_date (to_timezone='local') : %s" % format_date(now, to_timezone='local'))
print "format_date (to_timezone=Tokyo) : %s" % format_date(now, to_timezone='Asia/Tokyo') print("format_date (to_timezone=Tokyo) : %s" % format_date(now, to_timezone='Asia/Tokyo'))
print "format_date (naive=True) : %s" % format_date(now, naive=True) print("format_date (naive=True) : %s" % format_date(now, naive=True))
print "parse_datetime : %s" % parse_datetime(datestring_now) print("parse_datetime : %s" % parse_datetime(datestring_now))
print "parse_datetime (default_timezone=utc) : %s" % parse_datetime(datestring_now[0:-1], default_timezone=pytz.utc) print("parse_datetime (default_timezone=utc) : %s" % parse_datetime(datestring_now[0:-1], default_timezone=pytz.utc))
print "parse_datetime (default_timezone=local) : %s" % parse_datetime(datestring_now[0:-1], default_timezone=dateutil.tz.tzlocal()) print("parse_datetime (default_timezone=local) : %s" % parse_datetime(datestring_now[0:-1], default_timezone=dateutil.tz.tzlocal()))
print "parse_datetime (default_timezone='local') : %s" % parse_datetime(datestring_now[0:-1], default_timezone='local') print("parse_datetime (default_timezone='local') : %s" % parse_datetime(datestring_now[0:-1], default_timezone='local'))
print "parse_datetime (default_timezone=Paris) : %s" % parse_datetime(datestring_now[0:-1], default_timezone='Europe/Paris') print("parse_datetime (default_timezone=Paris) : %s" % parse_datetime(datestring_now[0:-1], default_timezone='Europe/Paris'))
print "parse_datetime (to_timezone=utc) : %s" % parse_datetime(datestring_now, to_timezone=pytz.utc) print("parse_datetime (to_timezone=utc) : %s" % parse_datetime(datestring_now, to_timezone=pytz.utc))
print "parse_datetime (to_timezone=local) : %s" % parse_datetime(datestring_now, to_timezone=dateutil.tz.tzlocal()) print("parse_datetime (to_timezone=local) : %s" % parse_datetime(datestring_now, to_timezone=dateutil.tz.tzlocal()))
print "parse_datetime (to_timezone='local') : %s" % parse_datetime(datestring_now, to_timezone='local') print("parse_datetime (to_timezone='local') : %s" % parse_datetime(datestring_now, to_timezone='local'))
print "parse_datetime (to_timezone=Tokyo) : %s" % parse_datetime(datestring_now, to_timezone='Asia/Tokyo') print("parse_datetime (to_timezone=Tokyo) : %s" % parse_datetime(datestring_now, to_timezone='Asia/Tokyo'))
print "parse_datetime (naive=True) : %s" % parse_datetime(datestring_now, naive=True) print("parse_datetime (naive=True) : %s" % parse_datetime(datestring_now, naive=True))
print "parse_date : %s" % parse_date(datestring_now) print("parse_date : %s" % parse_date(datestring_now))
print "parse_date (default_timezone=utc) : %s" % parse_date(datestring_now[0:-1], default_timezone=pytz.utc) print("parse_date (default_timezone=utc) : %s" % parse_date(datestring_now[0:-1], default_timezone=pytz.utc))
print "parse_date (default_timezone=local) : %s" % parse_date(datestring_now[0:-1], default_timezone=dateutil.tz.tzlocal()) print("parse_date (default_timezone=local) : %s" % parse_date(datestring_now[0:-1], default_timezone=dateutil.tz.tzlocal()))
print "parse_date (default_timezone='local') : %s" % parse_date(datestring_now[0:-1], default_timezone='local') print("parse_date (default_timezone='local') : %s" % parse_date(datestring_now[0:-1], default_timezone='local'))
print "parse_date (default_timezone=Paris) : %s" % parse_date(datestring_now[0:-1], default_timezone='Europe/Paris') print("parse_date (default_timezone=Paris) : %s" % parse_date(datestring_now[0:-1], default_timezone='Europe/Paris'))
print "parse_date (to_timezone=utc) : %s" % parse_date(datestring_now, to_timezone=pytz.utc) print("parse_date (to_timezone=utc) : %s" % parse_date(datestring_now, to_timezone=pytz.utc))
print "parse_date (to_timezone=local) : %s" % parse_date(datestring_now, to_timezone=dateutil.tz.tzlocal()) print("parse_date (to_timezone=local) : %s" % parse_date(datestring_now, to_timezone=dateutil.tz.tzlocal()))
print "parse_date (to_timezone='local') : %s" % parse_date(datestring_now, to_timezone='local') print("parse_date (to_timezone='local') : %s" % parse_date(datestring_now, to_timezone='local'))
print "parse_date (to_timezone=Tokyo) : %s" % parse_date(datestring_now, to_timezone='Asia/Tokyo') print("parse_date (to_timezone=Tokyo) : %s" % parse_date(datestring_now, to_timezone='Asia/Tokyo'))
print "parse_date (naive=True) : %s" % parse_date(datestring_now, naive=True) print("parse_date (naive=True) : %s" % parse_date(datestring_now, naive=True))