Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

py39 and pylint cleanup #50

Merged
merged 1 commit into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions lib/vsc/ldap/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
#
# Copyright 2009-2023 Ghent University
#
Expand Down Expand Up @@ -54,45 +53,45 @@ class NoSuchUserError(Exception):
"""If a user cannot be found in the LDAP."""
def __init__(self, name):
"""Initialisation."""
super(NoSuchUserError, self).__init__()
super().__init__()
self.name = name


class UserAlreadyExistsError(Exception):
"""If a user already is present in the LDAP, i.e., the dn already exists."""
def __init__(self, name):
"""Initialisation."""
super(UserAlreadyExistsError, self).__init__()
super().__init__()
self.name = name


class NoSuchVoError(Exception):
"""If a VO cannot be found in the LDAP."""
def __init__(self, name):
"""Initialisation."""
super(NoSuchVoError, self).__init__()
super().__init__()
self.name = name


class NoSuchGroupError(Exception):
"""If a group cannot be found in the LDAP."""
def __init__(self, name):
"""Initialisation."""
super(NoSuchGroupError, self).__init__()
super().__init__()
self.name = name


class NoSuchProjectError(Exception):
"""If a project cannot be found in the LDAP."""
def __init__(self, name):
"""Initialisation."""
super(NoSuchProjectError, self).__init__()
super().__init__()
self.name = name


class GroupAlreadyExistsError(Exception):
"""If a group is already present, i.e., the dn already exists."""
def __init__(self, name):
"""Initialisation."""
super(GroupAlreadyExistsError, self).__init__()
super().__init__()
self.name = name
27 changes: 12 additions & 15 deletions lib/vsc/ldap/filters.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
#
# Copyright 2009-2023 Ghent University
#
Expand Down Expand Up @@ -83,7 +82,7 @@ class LdapFilterError(Exception):
pass


class LdapFilter(object):
class LdapFilter:
"""Representing an LDAP filter with operators between the filter values.

This is implemented as a tree, where the nodes are the operations, e.g.,
Expand Down Expand Up @@ -172,7 +171,7 @@ def _to_string(self, previous_operator=None):

if self.left is None:
# single value, self.root should be a string not representing an operator
return "(%s)" % (self.root)
return f"({self.root})"

left_string = self.left._to_string(self.root)
if not self.right is None:
Expand All @@ -181,9 +180,9 @@ def _to_string(self, previous_operator=None):
right_string = ""

if self.root == previous_operator:
return "%s%s" % (left_string, right_string)
return f"{left_string}{right_string}"
else:
return "(%s%s%s)" % (self.root, left_string, right_string)
return f"({self.root}{left_string}{right_string})"

def _combine(self, operator, value=None):
"""Updates the tree with a new root, i.e., the given operator and
Expand Down Expand Up @@ -215,17 +214,15 @@ def __init__(self, value, timestamp, comparator):
will be converted to a format LDAP groks.
@type comparator: string representing a comparison operation, e.g., <=, >=
"""
super(TimestampFilter, self).__init__(value)
super().__init__(value)
self.timestamp = convert_timestamp(timestamp)[1]
if comparator != '>=' and comparator != '<=':
raise LdapFilterError()
self.comparator = comparator

def __str__(self):
"""Converts the filter to an LDAP understood string."""
return "(& (modifyTimestamp%s%s) %s)" % (self.comparator,
self.timestamp,
super(TimestampFilter, self).__str__())
return f"(& (modifyTimestamp{self.comparator}{self.timestamp}) {super().__str__()})"


class NewerThanFilter(TimestampFilter):
Expand All @@ -237,7 +234,7 @@ def __init__(self, value, timestamp):
@type timestamp: string or datetime instance representing a timestamp. This value
will be converted to a format LDAP groks.
"""
super(NewerThanFilter, self).__init__(value, timestamp, '>=')
super().__init__(value, timestamp, '>=')


class OlderThanFilter(TimestampFilter):
Expand All @@ -249,32 +246,32 @@ def __init__(self, value, timestamp):
@type timestamp: string or datetime instance representing a timestamp. This value
will be converted to a format LDAP groks.
"""
super(OlderThanFilter, self).__init__(value, timestamp, '<=')
super().__init__(value, timestamp, '<=')


class CnFilter(LdapFilter):
"""Representa a filter that matches a given common name."""

def __init__(self, cn):
super(CnFilter, self).__init__("cn=%s" % (cn))
super().__init__(f"cn={cn}")


class MemberFilter(LdapFilter):
"""Represents a filter that looks if a member is listed in the memberUid."""

def __init__(self, user_id):
super(MemberFilter, self).__init__("memberUid=%s" % (user_id))
super().__init__(f"memberUid={user_id}")


class LoginFilter(LdapFilter):
"""Represents a filter that looks up a user based on his institute login name."""

def __init__(self, login):
super(LoginFilter, self).__init__("login=%s" % (login))
super().__init__(f"login={login}")


class InstituteFilter(LdapFilter):
"""Represents a filter that looks up a user based on his institute login name."""

def __init__(self, institute):
super(InstituteFilter, self).__init__("institute=%s" % (institute))
super().__init__(f"institute={institute}")
5 changes: 2 additions & 3 deletions lib/vsc/ldap/group.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
#
# Copyright 2009-2023 Ghent University
#
Expand Down Expand Up @@ -69,12 +68,12 @@ def __init__(self, group_id):

@raise NoSuchGroupError if the group cannot be found.
"""
super(LdapGroup, self).__init__(self.LDAP_OBJECT_CLASS_ATTRIBUTES)
super().__init__(self.LDAP_OBJECT_CLASS_ATTRIBUTES)
self.group_id = group_id

def get_ldap_info(self):
"""Retrieve the data from the LDAP to initially fill up the ldap_info field."""
group_ldap_info = self.ldap_query.group_filter_search("cn=%s" % (self.group_id))
group_ldap_info = self.ldap_query.group_filter_search(f"cn={self.group_id}")
if len(group_ldap_info) == 0:
logging.error("Could not find a group in the LDAP with the ID %s, raising NoSuchGroupError",
self.group_id)
Expand Down
7 changes: 3 additions & 4 deletions lib/vsc/ldap/project.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
#
# Copyright 2009-2023 Ghent University
#
Expand Down Expand Up @@ -74,12 +73,12 @@ def __init__(self, project_id):

@raise NoSuchProjectError if the project cannot be found.
"""
super(LdapProject, self).__init__(self.LDAP_OBJECT_CLASS_ATTRIBUTES)
super().__init__(self.LDAP_OBJECT_CLASS_ATTRIBUTES)
self.project_id = project_id

def get_ldap_info(self):
"""Retrieve the data from the LDAP to initially fill up the ldap_info field."""
project_ldap_info = self.ldap_query.project_filter_search("cn=%s" % (self.project_id))
project_ldap_info = self.ldap_query.project_filter_search(f"cn={self.project_id}")
if len(project_ldap_info) == 0:
logging.error("Could not find a project in the LDAP with the ID %s, raising NoSuchGroupError",
self.project_id)
Expand Down Expand Up @@ -113,7 +112,7 @@ def get_for_member(user):
"""

# get all the projects in the LDAP
projects_info = user.ldap_query.projects_filter_search("memberUid=%s" % user.user_id)
projects_info = user.ldap_query.projects_filter_search(f"memberUid={user.user_id}")

projects = []
for p_info in projects_info:
Expand Down
3 changes: 1 addition & 2 deletions lib/vsc/ldap/user.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
#
# Copyright 2009-2023 Ghent University
#
Expand Down Expand Up @@ -78,7 +77,7 @@ def __init__(self, user_id):

@type user_id: string representing the ID of the user, i.e., his cn in LDAP.
"""
super(LdapUser, self).__init__(self.LDAP_OBJECT_CLASS_ATTRIBUTES)
super().__init__(self.LDAP_OBJECT_CLASS_ATTRIBUTES)

self.user_id = user_id
self.vo = None
Expand Down
44 changes: 21 additions & 23 deletions lib/vsc/ldap/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
#
# Copyright 2009-2023 Ghent University
#
Expand Down Expand Up @@ -45,7 +44,7 @@
EMPTY_GECOS_DURING_MODIFY = "EMPTYGECOSDURINGMODIFY"


class LdapConfiguration(object):
class LdapConfiguration:
"""Represents some LDAP configuration.

@param url: url to ldap server (default None)
Expand Down Expand Up @@ -78,7 +77,7 @@ class SchemaConfiguration(LdapConfiguration):
"""Represents an LDAP configuration with some extra schema-related information."""

def __init__(self):
super(SchemaConfiguration, self).__init__()
super().__init__()

self.user_dn_base = None
self.group_dn_base = None
Expand All @@ -91,7 +90,7 @@ def __init__(self):
self.vo_multi_value_attributes = None


class LdapConnection(object):
class LdapConnection:
"""Represents a connection to an LDAP server.

- Offers a set of convenience functions for querying and updating the server.
Expand All @@ -111,6 +110,7 @@ def __init__(self, configuration):
"""
self.configuration = configuration
self.ldap_connection = None
self.ldap_url = None

@TryOrFail(3, (ldap.LDAPError,), 10)
def connect(self):
Expand Down Expand Up @@ -193,7 +193,7 @@ def search_sync(self, ldap_filter, base, attributes=None):
self.bind()

# ldap_filter can also be an LdapFilter instance
ldap_filter = "%s" % ldap_filter
ldap_filter = f"{ldap_filter}"

try:
res = self.ldap_connection.search_s(base, ldap.SCOPE_SUBTREE, ldap_filter, attributes)
Expand All @@ -218,7 +218,7 @@ def search_async_timeout(self, ldap_filter, base, attributes=None, timeout=10):
attrs_only = False

# filter can also be LdapFilter instance
ldap_filter = "%s" % ldap_filter
ldap_filter = f"{ldap_filter}"

try:
res = self.ldap_connection.search_st(base, ldap.SCOPE_SUBTREE, ldap_filter, attributes, attrs_only, timeout)
Expand Down Expand Up @@ -274,8 +274,8 @@ def add(self, dn, attributes):
if self.ldap_connection is None:
self.bind()

changes = [(k, [v]) for (k, v) in attributes if not type(v) == list]
changes.extend([(k, v) for (k, v) in attributes if type(v) == list])
changes = [(k, [v]) for (k, v) in attributes if not isinstance(v, list)]
changes.extend([(k, v) for (k, v) in attributes if isinstance(v, list)])
logging.info("Adding for dn=%s with changes = %s", dn, changes)

try:
Expand Down Expand Up @@ -462,8 +462,8 @@ def user_search(self, user_id, institute, attributes=None):

@returns: a dictionary, with the values for the requested attributes for the given user
"""
login_filter = LdapFilter("instituteLogin=%s" % (user_id))
institute_filter = LdapFilter("institute=%s" % (institute))
login_filter = LdapFilter(f"instituteLogin={user_id}")
institute_filter = LdapFilter(f"institute={institute}")

result = self.user_filter_search(login_filter & institute_filter, attributes)
logging.debug("user_search for %s, %s yields %s", user_id, institute, result)
Expand Down Expand Up @@ -504,7 +504,7 @@ def __modify(self, current, dn, attributes):
current_ = {}
for key in attributes.keys():
current_[key] = current.get(key, [])
if current_[key] is '':
if current_[key] == '':
logging.warning("Replacing empty string for key %s with %s before making modlist for dn %s",
key, EMPTY_GECOS_DURING_MODIFY, dn)
current_[key] = EMPTY_GECOS_DURING_MODIFY # hack to allow replacing empty strings
Expand All @@ -525,7 +525,7 @@ def group_modify(self, cn, attributes):

@raise: NoSuchGroupError
"""
dn = "cn=%s,%s" % (cn, self.configuration.group_dn_base)
dn = f"cn={cn},{self.configuration.group_dn_base}"
current = self.group_filter_search(CnFilter(cn))
if current is None:
logging.error("group_modify did not find group with cn = %s (dn = %s)", cn, dn)
Expand All @@ -542,7 +542,7 @@ def user_modify(self, cn, attributes):

@raise: NoSuchUserError
"""
dn = "cn=%s,%s" % (cn, self.configuration.user_dn_base)
dn = f"cn={cn},{self.configuration.user_dn_base}"
current = self.user_filter_search(CnFilter(cn))
if current is None:
logging.error("user_modify did not find user with cn = %s (dn = %s)", cn, dn)
Expand All @@ -559,7 +559,7 @@ def project_modify(self, cn, attributes):

@raise: NoSuchProjectError
"""
dn = "cn=%s,%s" % (cn, self.configuration.project_dn_base)
dn = f"cn={cn},{self.configuration.project_dn_base}"
current = self.project_filter_search(CnFilter(cn))
if current is None:
logging.error("project_modify did not find project with cn = %s (dn = %s)", cn, dn)
Expand All @@ -574,8 +574,8 @@ def user_add(self, cn, attributes):
@type cn: string representing the common name for the user. Together with the subtree, this forms the dn.
@type attributes: dictionary with attributes for which a value should be added
"""
dn = "cn=%s,%s" % (cn, self.configuration.user_dn_base)
attributes = {key:[v.encode("utf-8") if type(v) == str else v for v in values]
dn = f"cn={cn},{self.configuration.user_dn_base}"
attributes = {key:[v.encode("utf-8") if isinstance(v, str) else v for v in values]
for key, values in attributes.items()}
self.ldap.add(dn, attributes.items())

Expand All @@ -585,8 +585,8 @@ def group_add(self, cn, attributes):
@type cn: string representing the common name for the group. Together with the subtree, this forms the dn.
@type attributes: dictionary with attributes for which a value should be added
"""
dn = "cn=%s,%s" % (cn, self.configuration.group_dn_base)
attributes = {key:[v.encode("utf-8") if type(v) == str else v for v in values]
dn = f"cn={cn},{self.configuration.group_dn_base}"
attributes = {key:[v.encode("utf-8") if isinstance(v, str) else v for v in values]
for key, values in attributes.items()}
self.ldap.add(dn, attributes.items())

Expand All @@ -596,8 +596,8 @@ def project_add(self, cn, attributes):
@type cn: string representing the common name for the project. Together with the subtree, this forms the dn.
@type attributes: dictionary with attributes for which a value should be added
"""
dn = "cn=%s,%s" % (cn, self.configuration.project_dn_base)
attributes = {key:[v.encode("utf-8") if type(v) == str else v for v in values]
dn = f"cn={cn},{self.configuration.project_dn_base}"
attributes = {key:[v.encode("utf-8") if isinstance(v, str) else v for v in values]
for key, values in attributes.items()}
self.ldap.add(dn, attributes.items())

Expand Down Expand Up @@ -678,7 +678,7 @@ def get_schema(self, ldap_obj_class_name_or_oid, reload=False, do_reload=False):
return self.schema[ldap_obj_class_name_or_oid]


class LdapEntity(object):
class LdapEntity:
"""Base class for all things LDAP that work on a higher level."""

def __init__(self, object_classes=None):
Expand All @@ -703,7 +703,6 @@ def modify_ldap(self, attributes):

Should be iplemented by deriving classes.
"""
pass

def __getattr__(self, name):
"""Getter for the LdapUser fields. Only accessed for fields that are in
Expand Down Expand Up @@ -758,7 +757,6 @@ def __setattr__(self, name, value):
except ldap.LDAPError:
logging.error("Could not save the new value %s for %s with cn=%s to the LDAP",
value, name, self.vsc_user_id)
pass
else:
object.__setattr__(self, name, value)
except AttributeError:
Expand Down
Loading