diff --git a/LdapServer.py b/LdapServer.py index 7c9c652..0732aec 100644 --- a/LdapServer.py +++ b/LdapServer.py @@ -1,4 +1,4 @@ -#!/usr/bin/python +#!/usr/bin/env python3 import copy import datetime @@ -51,7 +51,7 @@ class LdapServer(object): self.con = con 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) return False return True @@ -121,12 +121,12 @@ class LdapServer(object): attrs, 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) return False try: 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) return False @@ -163,7 +163,7 @@ class LdapServer(object): self.logger.debug("LdapServer - Add %s" % dn) self.con.add_s(dn,ldif) return True - except ldap.LDAPError, e: + except ldap.LDAPError as e: self._error("LdapServer - Error adding %s : %s" % (dn,e), logging.ERROR) return False @@ -175,7 +175,7 @@ class LdapServer(object): try: self.con.modify_s(dn,ldif) 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) return False @@ -206,7 +206,7 @@ class LdapServer(object): return '\n'.join(msg) def rename_object(self, dn, new_rdn, new_sup=None, delete_old=True): - # If new_rdn is a complete DN, split new RDN and new superior DN + # If new_rdn is a complete DN, split new RDN and new superior DN if len(new_rdn.split(',')) > 1: self.logger.debug( "LdapServer - Rename with a full new DN detected (%s): split new RDN and new superior DN", @@ -226,7 +226,7 @@ class LdapServer(object): ) self.con.rename_s(dn, new_rdn, newsuperior=new_sup, delold=delete_old) return True - except ldap.LDAPError, e: + except ldap.LDAPError as e: self._error( "LdapServer - Error renaming %s in %s (new superior: %s, delete old: %s): %s" % ( dn, @@ -245,7 +245,7 @@ class LdapServer(object): self.logger.debug("LdapServer - Delete %s" % dn) self.con.delete_s(dn) return True - except ldap.LDAPError, e: + except ldap.LDAPError as e: self._error("LdapServer - Error deleting %s : %s" % (dn,e), logging.ERROR) return False @@ -388,50 +388,50 @@ def format_date(value, from_timezone=None, to_timezone=None, naive=None): # if __name__ == '__main__': now = datetime.datetime.now().replace(tzinfo=dateutil.tz.tzlocal()) - print "Now = %s" % now + print("Now = %s" % now) datestring_now = format_datetime(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=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=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=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=Tokyo) : %s" % format_datetime(now, to_timezone='Asia/Tokyo') - print "format_datetime (naive=True) : %s" % format_datetime(now, naive=True) + 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=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=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=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=Tokyo) : %s" % format_datetime(now, to_timezone='Asia/Tokyo')) + print("format_datetime (naive=True) : %s" % format_datetime(now, naive=True)) - 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=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=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=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=Tokyo) : %s" % format_date(now, to_timezone='Asia/Tokyo') - print "format_date (naive=True) : %s" % format_date(now, naive=True) + 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=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=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=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=Tokyo) : %s" % format_date(now, to_timezone='Asia/Tokyo')) + print("format_date (naive=True) : %s" % format_date(now, naive=True)) - 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=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=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=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=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 : %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=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=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=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=Tokyo) : %s" % parse_datetime(datestring_now, to_timezone='Asia/Tokyo')) + print("parse_datetime (naive=True) : %s" % parse_datetime(datestring_now, naive=True)) - 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=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=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=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=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 : %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=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=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=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=Tokyo) : %s" % parse_date(datestring_now, to_timezone='Asia/Tokyo')) + print("parse_date (naive=True) : %s" % parse_date(datestring_now, naive=True))