From 74dc6c58941b61f838ee4e9cfcbc25df3c7b86c7 Mon Sep 17 00:00:00 2001 From: kakwa Date: Wed, 6 Feb 2019 22:32:40 +0100 Subject: [PATCH] various changes to support python3 * changes in urllib imports since quote_plus in urllib with python 2 and in urllib.parse in python 3 * changes in imports for Sets since set is a native type in python 3 and doesn't requires an import * fix in __import__, '-1' level for module path discovery is not supported anymore, switching to 0 (absolute import only). --- ldapcherry/__init__.py | 39 +++++++++++++++++------------- ldapcherry/attributes.py | 6 +++-- ldapcherry/backend/backendDemo.py | 24 ++++++++++++------- ldapcherry/backend/backendLdap.py | 11 +++++---- ldapcherry/roles.py | 40 ++++++++++++++++--------------- 5 files changed, 70 insertions(+), 50 deletions(-) diff --git a/ldapcherry/__init__.py b/ldapcherry/__init__.py index a7d5c18..33b2063 100644 --- a/ldapcherry/__init__.py +++ b/ldapcherry/__init__.py @@ -15,10 +15,9 @@ import logging import logging.handlers from operator import itemgetter from socket import error as socket_error -import urllib import cgi -from exceptions import * +from ldapcherry.exceptions import * from ldapcherry.lclogging import * from ldapcherry.roles import Roles from ldapcherry.attributes import Attributes @@ -31,7 +30,13 @@ from cherrypy.lib.httputil import parse_query_string from mako.template import Template from mako import lookup from mako import exceptions -from sets import Set + + +if sys.version < '3': + from sets import Set as set + from urllib import quote_plus +else: + from urllib.parse import quote_plus SESSION_KEY = '_cp_username' @@ -68,8 +73,8 @@ class LdapCherry(object): data[d] = self._escape_list(data[d]) elif isinstance(data[d], dict): data[d] = self._escape_dict(data[d]) - elif isinstance(data[d], Set): - data[d] = Set(self._escape_list(data[d])) + elif isinstance(data[d], set): + data[d] = set(self._escape_list(data[d])) else: data[d] = cgi.escape(data[d], True) return data @@ -178,7 +183,7 @@ class LdapCherry(object): except Exception as e: raise MissingParameter('backends', backend + '.module') try: - bc = __import__(module, globals(), locals(), ['Backend'], -1) + bc = __import__(module, globals(), locals(), ['Backend'], 0) except Exception as e: self._handle_exception(e) raise BackendModuleLoadingFail(module) @@ -219,7 +224,7 @@ class LdapCherry(object): 'ldapcherry.ppolicy' ) try: - pp = __import__(module, globals(), locals(), ['PPolicy'], -1) + pp = __import__(module, globals(), locals(), ['PPolicy'], 0) except: raise BackendModuleLoadingFail(module) if 'ppolicy' in config: @@ -590,7 +595,7 @@ class LdapCherry(object): else: qs = '?' + cherrypy.request.query_string # Escaped version of the requested URL - quoted_requrl = urllib.quote_plus(cherrypy.url() + qs) + quoted_requrl = quote_plus(cherrypy.url() + qs) if not username: # return to login page (with quoted url in query string) if redir_login: @@ -695,7 +700,7 @@ class LdapCherry(object): roles.append(r) groups = self.roles.get_groups(roles) for b in groups: - self.backends[b].add_to_groups(username, Set(groups[b])) + self.backends[b].add_to_groups(username, set(groups[b])) cherrypy.log.error( msg="user '" + username + "' made member of " + @@ -823,10 +828,10 @@ class LdapCherry(object): if b not in g: g[b] = [] tmp = \ - Set(groups_add[b]) - \ - Set(groups_keep[b]) - \ - Set(groups_current[b]) - \ - Set(lonely_groups[b]) + set(groups_add[b]) - \ + set(groups_keep[b]) - \ + set(groups_current[b]) - \ + set(lonely_groups[b]) cherrypy.log.error( msg="user '" + username + "' added to groups: " + str(list(tmp)) + " in backend '" + b + "'", @@ -840,11 +845,11 @@ class LdapCherry(object): g[b] = [] tmp = \ ( - (Set(groups_rm[b]) | Set(groups_remove[b])) - - (Set(groups_keep[b]) | Set(groups_add[b])) + (set(groups_rm[b]) | set(groups_remove[b])) - + (set(groups_keep[b]) | set(groups_add[b])) ) & \ ( - Set(groups_current[b]) | Set(lonely_groups[b]) + set(groups_current[b]) | set(lonely_groups[b]) ) cherrypy.log.error( msg="user '" + username + "' removed from groups: " + @@ -933,7 +938,7 @@ class LdapCherry(object): if url is None: qs = '' else: - qs = '?url=' + urllib.quote_plus(url) + qs = '?url=' + quote_plus(url) raise cherrypy.HTTPRedirect("/signin" + qs) @cherrypy.expose diff --git a/ldapcherry/attributes.py b/ldapcherry/attributes.py index 01fa396..75c863e 100644 --- a/ldapcherry/attributes.py +++ b/ldapcherry/attributes.py @@ -12,9 +12,11 @@ import re from ldapcherry.pyyamlwrapper import loadNoDump from ldapcherry.pyyamlwrapper import DumplicatedKey from ldapcherry.exceptions import * -from sets import Set import yaml +if sys.version < '3': + from sets import Set as set + # List of available types for form types = ['string', 'textfield', 'email', 'int', 'stringlist', 'fix', 'password'] @@ -24,7 +26,7 @@ class Attributes: def __init__(self, attributes_file): self.attributes_file = attributes_file - self.backends = Set([]) + self.backends = set([]) self.self_attributes = {} self.backend_attributes = {} self.displayed_attributes = {} diff --git a/ldapcherry/backend/backendDemo.py b/ldapcherry/backend/backendDemo.py index db3559b..0377bad 100644 --- a/ldapcherry/backend/backendDemo.py +++ b/ldapcherry/backend/backendDemo.py @@ -7,7 +7,11 @@ # This is a demo backend -from sets import Set + +import sys +if sys.version < '3': + from sets import Set as set + import ldapcherry.backend from ldapcherry.exceptions import UserDoesntExist, \ GroupDoesntExist, MissingParameter, \ @@ -37,12 +41,12 @@ class Backend(ldapcherry.backend.Backend): self.backend_name = name admin_user = self.get_param('admin.user', 'admin') admin_password = self.get_param('admin.password', 'admin') - admin_groups = Set(re.split('\W+', self.get_param('admin.groups'))) + admin_groups = set(self._basic_splitter(self.get_param('admin.groups'))) basic_user = self.get_param('basic.user', 'user') basic_password = self.get_param('basic.password', 'user') - basic_groups = Set(re.split('\W+', self.get_param('basic.groups'))) + basic_groups = set(self._basic_splitter(self.get_param('basic.groups'))) pwd_attr = self.get_param('pwd_attr') - self.search_attrs = Set( + self.search_attrs = set( re.split('\W+', self.get_param('search_attributes')), ) self.pwd_attr = pwd_attr @@ -60,6 +64,10 @@ class Backend(ldapcherry.backend.Backend): 'groups': basic_groups, } + @staticmethod + def _basic_splitter(in_str): + return [re.sub(r'(?