Skip to content

Commit

Permalink
Add support for current Python versions.
Browse files Browse the repository at this point in the history
* Lint the code
* isort imports
  • Loading branch information
Michael Howitz committed Jul 14, 2022
1 parent cfb1773 commit 957fecc
Show file tree
Hide file tree
Showing 9 changed files with 101 additions and 62 deletions.
8 changes: 5 additions & 3 deletions CHANGES.rst
Expand Up @@ -2,10 +2,12 @@
Changes
=========

4.2.1 (unreleased)
==================
4.3 (unreleased)
================

- Drop support for Python 3.4.

- Nothing changed yet.
- Add support for Python 3.7, 3.8, 3.9, 3.10.


4.2.0 (2017-10-01)
Expand Down
17 changes: 13 additions & 4 deletions setup.py
Expand Up @@ -14,16 +14,21 @@
"""Setup for zope.principalregistry package
"""
import os
from setuptools import setup, find_packages

from setuptools import find_packages
from setuptools import setup


def read(*rnames):
with open(os.path.join(os.path.dirname(__file__), *rnames)) as f:
return f.read()


def alltests():
import os
import sys
import unittest

# use the zope.testrunner machinery to find all the
# test suites we've put under ourselves
import zope.testrunner.find
Expand All @@ -35,13 +40,14 @@ def alltests():
suites = list(zope.testrunner.find.find_suites(options))
return unittest.TestSuite(suites)


TESTS_REQUIRE = [
'zope.testing',
'zope.testrunner',
]

setup(name='zope.principalregistry',
version='4.2.1.dev0',
version='4.3.dev0',
author='Zope Foundation and Contributors',
author_email='zope-dev@zope.org',
description='Global principal registry component for Zope3',
Expand All @@ -60,9 +66,12 @@ def alltests():
'Programming Language :: Python :: 2',
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.4',
'Programming Language :: Python :: 3.5',
'Programming Language :: Python :: 3.6',
'Programming Language :: Python :: 3.7',
'Programming Language :: Python :: 3.8',
'Programming Language :: Python :: 3.9',
'Programming Language :: Python :: 3.10',
'Programming Language :: Python :: Implementation :: CPython',
'Programming Language :: Python :: Implementation :: PyPy',
'Natural Language :: English',
Expand Down Expand Up @@ -94,4 +103,4 @@ def alltests():
test_suite='__main__.alltests',
include_package_data=True,
zip_safe=False,
)
)
2 changes: 1 addition & 1 deletion src/zope/__init__.py
@@ -1 +1 @@
__import__('pkg_resources').declare_namespace(__name__) # pragma: no cover
__import__('pkg_resources').declare_namespace(__name__) # pragma: no cover
34 changes: 16 additions & 18 deletions src/zope/principalregistry/metaconfigure.py
Expand Up @@ -13,10 +13,10 @@
##############################################################################
"""Directives for defining principals and groups
"""
from zope import component
from zope.component.zcml import utility
from zope.authentication import interfaces
from zope.component.zcml import utility

from zope import component
from zope.principalregistry import principalregistry


Expand All @@ -28,11 +28,10 @@ def _principal():
if group is not None:
_everybodyGroup(group.id)


def principal(_context, id, title, login,
password, description='', password_manager="Plain Text"):
"""
Implementation of :class:`zope.principalregistry.metadirectives.IDefinePrincipalDirective`.
"""
"""Implementation of :class:`zope.principalregistry.metadirectives.IDefinePrincipalDirective`.""" # noqa: E501 line too long
# Make sure password is encoded to bytes, which is required by the
# principal registry.
password = password.encode('utf-8')
Expand All @@ -51,10 +50,9 @@ def _unauthenticatedPrincipal():
if group is not None:
_everybodyGroup(group.id)


def unauthenticatedPrincipal(_context, id, title, description=''):
"""
Implementation of :class:`zope.principalregistry.metadirectives.IDefineUnauthenticatedPrincipalDirective`.
"""
"""Implementation of :class:`zope.principalregistry.metadirectives.IDefineUnauthenticatedPrincipalDirective`.""" # noqa: E501 line too long
principal = principalregistry.UnauthenticatedPrincipal(
id, title, description)
_context.action(
Expand All @@ -68,15 +66,15 @@ def unauthenticatedPrincipal(_context, id, title, description=''):
args=(),
)


def _unauthenticatedGroup(group):
p = principalregistry.principalRegistry.unauthenticatedPrincipal()
if p is not None:
p.groups.append(group)


def unauthenticatedGroup(_context, id, title, description=''):
"""
Implementation of :class:`zope.principalregistry.metadirectives.IDefineUnauthenticatedGroupDirective`.
"""
"""Implementation of :class:`zope.principalregistry.metadirectives.IDefineUnauthenticatedGroupDirective`.""" # noqa: E501 line too long
principal = principalregistry.UnauthenticatedGroup(
id, title, description)
utility(_context, interfaces.IUnauthenticatedGroup, principal)
Expand All @@ -91,17 +89,17 @@ def unauthenticatedGroup(_context, id, title, description=''):
args=(principal, ),
)


def _authenticatedGroup(group):
for p in principalregistry.principalRegistry.getPrincipals(''):
if not isinstance(p, principalregistry.Principal):
continue
if group not in p.groups:
p.groups.append(group)


def authenticatedGroup(_context, id, title, description=''):
"""
Implementation of :class:`zope.principalregistry.metadirectives.IDefineAuthenticatedGroupDirective`.
"""
"""Implementation of :class:`zope.principalregistry.metadirectives.IDefineAuthenticatedGroupDirective`.""" # noqa: E501 line too long
principal = principalregistry.AuthenticatedGroup(
id, title, description)
utility(_context, interfaces.IAuthenticatedGroup, principal)
Expand All @@ -116,6 +114,7 @@ def authenticatedGroup(_context, id, title, description=''):
args=(principal, ),
)


def _everybodyGroup(group):
for p in principalregistry.principalRegistry.getPrincipals(''):
if not isinstance(p, principalregistry.Principal):
Expand All @@ -126,18 +125,17 @@ def _everybodyGroup(group):
if p is not None:
p.groups.append(group)


def everybodyGroup(_context, id, title, description=''):
"""
Implementation of :class:`zope.principalregistry.metadirectives.IDefineEverybodyGroupDirective`.
"""
"""Implementation of :class:`zope.principalregistry.metadirectives.IDefineEverybodyGroupDirective`.""" # noqa: E501 line too long
principal = principalregistry.EverybodyGroup(
id, title, description)
utility(_context, interfaces.IEveryoneGroup, principal)
_context.action(
discriminator=None,
callable=_everybodyGroup,
args=(principal.id, ),
)
)
_context.action(
discriminator=None,
callable=principalregistry.principalRegistry.registerGroup,
Expand Down
12 changes: 10 additions & 2 deletions src/zope/principalregistry/metadirectives.py
Expand Up @@ -14,7 +14,9 @@
"""Schemas for directives that define principals and groups
"""
from zope.interface import Interface
from zope.schema import Id, TextLine
from zope.schema import Id
from zope.schema import TextLine


class TextId(Id):
"""
Expand All @@ -24,7 +26,8 @@ class TextId(Id):
the text type.
"""

_type = str if str is not bytes else unicode
_type = str if str is not bytes else unicode # PY2 # noqa: F821 undefined


class IBasePrincipalDirective(Interface):
"""Base interface for principal definition directives."""
Expand All @@ -44,6 +47,7 @@ class IBasePrincipalDirective(Interface):
description=u"Provides a description for the object.",
required=False)


class IDefinePrincipalDirective(IBasePrincipalDirective):
"""Define a new principal."""

Expand All @@ -64,14 +68,18 @@ class IDefinePrincipalDirective(IBasePrincipalDirective):
default=u"Plain Text"
)


class IDefineUnauthenticatedPrincipalDirective(IBasePrincipalDirective):
"""Define a new unauthenticated principal."""


class IDefineUnauthenticatedGroupDirective(IBasePrincipalDirective):
"""Define the unauthenticated group."""


class IDefineAuthenticatedGroupDirective(IBasePrincipalDirective):
"""Define the authenticated group."""


class IDefineEverybodyGroupDirective(IBasePrincipalDirective):
"""Define the everybody group."""
59 changes: 36 additions & 23 deletions src/zope/principalregistry/principalregistry.py
Expand Up @@ -13,26 +13,25 @@
##############################################################################
"""Global Authentication Utility or Principal Registry
"""
import zope.security.management
from zope.authentication.interfaces import IAuthenticatedGroup
from zope.authentication.interfaces import IAuthentication
from zope.authentication.interfaces import IEveryoneGroup
from zope.authentication.interfaces import ILoginPassword
from zope.authentication.interfaces import ILogout
from zope.authentication.interfaces import IUnauthenticatedGroup
from zope.authentication.interfaces import IUnauthenticatedPrincipal
from zope.authentication.interfaces import PrincipalLookupError
from zope.component import getUtility
from zope.interface import implementer
import zope.security.management
from zope.security.interfaces import IGroupAwarePrincipal
from zope.password.interfaces import IPasswordManager
from zope.security.interfaces import IGroupAwarePrincipal

from zope.authentication.interfaces import (
IAuthentication,
PrincipalLookupError,
ILoginPassword,
ILogout,
IUnauthenticatedPrincipal,
IUnauthenticatedGroup,
IAuthenticatedGroup,
IEveryoneGroup,
)

def _as_text(s):
return s.decode('utf-8') if isinstance(s, bytes) else s


class DuplicateLogin(Exception):
pass

Expand All @@ -44,7 +43,8 @@ class DuplicateId(Exception):
@implementer(IAuthentication, ILogout)
class PrincipalRegistry(object):
"""
An in-memory implementation of :class:`zope.authentication.interfaces.IAuthentication`
An in-memory implementation of
:class:`zope.authentication.interfaces.IAuthentication`
and :class:`zope.authentication.interfaces.ILogout`.
"""

Expand Down Expand Up @@ -120,8 +120,14 @@ def __init__(self):
self.__principalsById = {}
self.__principalsByLogin = {}

def definePrincipal(self, principal, title, description=u'',
login=u'', password=b'', passwordManagerName='Plain Text'):
def definePrincipal(
self,
principal,
title,
description=u'',
login=u'',
password=b'',
passwordManagerName='Plain Text'):
id = _as_text(principal)
title = _as_text(title)
description = _as_text(description)
Expand Down Expand Up @@ -154,6 +160,7 @@ def _clear(self):
self.__defaultid = None
self.__defaultObject = None


#: The global registry that the ZCML directives will
#: modify.
principalRegistry = PrincipalRegistry()
Expand All @@ -162,12 +169,13 @@ def _clear(self):
# simpler.
try:
from zope.testing.cleanup import addCleanUp
except ImportError: # pragma: no cover
except ImportError: # pragma: no cover
pass
else:
addCleanUp(principalRegistry._clear)
del addCleanUp


class PrincipalBase(object):

__name__ = __parent__ = None
Expand All @@ -178,15 +186,18 @@ def __init__(self, id, title, description):
self.description = _as_text(description)
self.groups = []


class Group(PrincipalBase):

def getLogin(self):
return u'' # to make registry search happy
return u'' # to make registry search happy


@implementer(IGroupAwarePrincipal)
class Principal(PrincipalBase):
"""
The default implementation of :class:`zope.security.interfaces.IGroupAwarePrincipal`
The default implementation of
:class:`zope.security.interfaces.IGroupAwarePrincipal`
that :class:`PrincipalRegistry` will create.
"""

Expand All @@ -210,12 +221,12 @@ def validate(self, pw):

@implementer(IUnauthenticatedPrincipal)
class UnauthenticatedPrincipal(PrincipalBase):
"""An implementation of :class:`zope.authentication.interfaces.IUnauthenticatedPrincipal`."""
"""An implementation of :class:`zope.authentication.interfaces.IUnauthenticatedPrincipal`.""" # noqa: E501 line too long


fallback_unauthenticated_principal = (
UnauthenticatedPrincipal(
__name__+'.fallback_unauthenticated_principal',
__name__ + '.fallback_unauthenticated_principal',
'Fallback unauthenticated principal',
'The default unauthenticated principal. Used as a fallback to '
'allow challenging for a user even if the IAuthentication returned '
Expand All @@ -224,12 +235,14 @@ class UnauthenticatedPrincipal(PrincipalBase):

@implementer(IUnauthenticatedGroup)
class UnauthenticatedGroup(Group):
"""An implementation of :class:`zope.authentication.interfaces.IUnauthenticatedGroup`."""
"""An implementation of :class:`zope.authentication.interfaces.IUnauthenticatedGroup`.""" # noqa: E501 line too long


@implementer(IAuthenticatedGroup)
class AuthenticatedGroup(Group):
"""An implementation of :class:`zope.authentication.interfaces.IAuthenticatedGroup`."""
"""An implementation of :class:`zope.authentication.interfaces.IAuthenticatedGroup`.""" # noqa: E501 line too long


@implementer(IEveryoneGroup)
class EverybodyGroup(Group):
"""An implementation of :class:`zope.authentication.interfaces.IEverybodyGroup`."""
"""An implementation of :class:`zope.authentication.interfaces.IEverybodyGroup`.""" # noqa: E501 line too long
2 changes: 1 addition & 1 deletion src/zope/principalregistry/tests/__init__.py
@@ -1 +1 @@
# i am a package
# i am a package

0 comments on commit 957fecc

Please sign in to comment.