Skip to content

Commit

Permalink
Format code using ruff
Browse files Browse the repository at this point in the history
  • Loading branch information
rnixx committed Feb 3, 2024
1 parent 7341bc2 commit 8d53d42
Show file tree
Hide file tree
Showing 8 changed files with 122 additions and 136 deletions.
6 changes: 4 additions & 2 deletions src/plumber/behavior.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
from plumber.compat import add_metaclass
from plumber.instructions import Instruction
from plumber.instructions import plumb

try:
from plumber.instructions import _implements

ZOPE_INTERFACE_AVAILABLE = True
except ImportError: # pragma: no cover
ZOPE_INTERFACE_AVAILABLE = False
Expand All @@ -16,6 +18,7 @@ class _Behavior(object):

class Instructions(object):
"""Adapter to set instructions on a behavior."""

attrname = '__plumbing_instructions__'

def __init__(self, behavior):
Expand Down Expand Up @@ -102,8 +105,7 @@ def __init__(cls, name, bases, dct):
continue
# stage1 instructions with the same name are ignored
if instr.__name__ in [
x.__name__ for x in instructions
if x.__stage__ == 'stage1'
x.__name__ for x in instructions if x.__stage__ == 'stage1'
]:
continue
instructions.append(instr)
Expand Down
4 changes: 3 additions & 1 deletion src/plumber/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
IS_PY2 = sys.version_info[0] < 3
IS_PYPY = '__pypy__' in sys.builtin_module_names
ITER_FUNC = 'iteritems' if IS_PY2 else 'items'
STR_TYPE = basestring if IS_PY2 else str
STR_TYPE = basestring if IS_PY2 else str # noqa


def add_metaclass(metaclass):
"""Class decorator for creating a class with a metaclass."""

# This is taken from six
def wrapper(cls):
orig_vars = cls.__dict__.copy()
Expand All @@ -25,4 +26,5 @@ def wrapper(cls):
for slots_var in slots: # pragma: no cover
orig_vars.pop(slots_var)
return metaclass(cls.__name__, cls.__bases__, orig_vars)

return wrapper
15 changes: 8 additions & 7 deletions src/plumber/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
class PlumbingCollision(RuntimeError):

def __init__(self, left, right):
self.left = left
self.right = right
msg = '\n'.join([
'',
' %s',
' with:',
' %s',
]) % (left, right)
msg = '\n'.join(
[
'',
' %s',
' with:',
' %s',
]
) % (left, right)
super(PlumbingCollision, self).__init__(msg)
21 changes: 18 additions & 3 deletions src/plumber/instructions.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
from __future__ import absolute_import
from plumber.compat import STR_TYPE
from plumber.exceptions import PlumbingCollision

try:
from zope.interface import classImplements
from zope.interface import implementedBy

ZOPE_INTERFACE_AVAILABLE = True
except ImportError: # pragma: no cover
ZOPE_INTERFACE_AVAILABLE = False
Expand All @@ -14,6 +16,7 @@
# Instruction base class and helper function
###############################################################################


def payload(item):
"""Get to the payload through a chain of instructions.
Expand Down Expand Up @@ -114,6 +117,7 @@ class Instruction(object):
An instruction works on the attribute sharing its name, parent is the part
declaring it. An instruction declares the stage to be applied in.
"""

__name__ = None
__parent__ = None
__stage__ = None
Expand Down Expand Up @@ -198,7 +202,7 @@ def __repr__(self):
cls=self.__class__.__name__,
name=self.name or 'None',
parent=self.__parent__ or 'None',
payload=repr(self.payload)
payload=repr(self.payload),
)

__str__ = __repr__
Expand All @@ -208,13 +212,15 @@ def __repr__(self):
# Stage 1 instructions
###############################################################################


class Stage1Instruction(Instruction):
"""Instructions installed in stage1.
- default
- override
- finalize
"""

__stage__ = 'stage1'


Expand Down Expand Up @@ -434,8 +440,10 @@ def __call__(self, dct, derived_members):
# Stage2 instructions
###############################################################################


class Stage2Instruction(Instruction):
"""Instructions installed in stage2: so far only plumb."""

__stage__ = 'stage2'

def __call__(self, cls):
Expand All @@ -449,17 +457,21 @@ def entrancefor(plumbing_method, next_):
The entrance returned is a closure with signature: (self, *args, **kw), it
wraps a call of plumbing_method curried with next_.
"""

def entrance(self, *args, **kw):
return plumbing_method(next_, self, *args, **kw)

entrance.__doc__ = plumb_str(plumbing_method.__doc__, next_.__doc__)
entrance.__name__ = plumbing_method.__name__
return entrance


def plumbingfor(plumbing_method, next_):
"""A plumbing method combining two plumbing methods."""

def plumbing(next__, self, *args, **kw):
return plumbing_method(entrancefor(next_, next__), self, *args, **kw)

plumbing.__doc__ = plumb_str(plumbing_method.__doc__, next_.__doc__)
plumbing.__name__ = plumbing_method.__name__
return plumbing
Expand Down Expand Up @@ -512,8 +524,9 @@ def __add__(self, right):
raise PlumbingCollision(self, right)
if not self.ok(self.payload, right.payload):
raise PlumbingCollision(self, right)
return plumb(self.plumb(plumbingfor, self.payload, right.payload),
name=self.name)
return plumb(
self.plumb(plumbingfor, self.payload, right.payload), name=self.name
)

def ok(self, p1, p2):
"""Check whether we can merge two payloads.
Expand Down Expand Up @@ -580,6 +593,7 @@ def __call__(self, cls):


if ZOPE_INTERFACE_AVAILABLE:

class _implements(Stage2Instruction):
"""classImplements interfaces.
Expand Down Expand Up @@ -622,6 +636,7 @@ class _implements(Stage2Instruction):
<Instruction 'None' of None payload='bar'>
"""

__name__ = '__interfaces__'

def __add__(self, right):
Expand Down
1 change: 1 addition & 0 deletions src/plumber/plumber.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class plumber(type):
Create and call a real plumber, for classes declaring a ``__plumbing__``
attribute (inheritance is not enough):
"""

__metaclass_hooks__ = list()

@classmethod
Expand Down
26 changes: 10 additions & 16 deletions src/plumber/tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,12 @@


optionflags = (
doctest.NORMALIZE_WHITESPACE
| doctest.ELLIPSIS
| doctest.REPORT_ONLY_FIRST_FAILURE
doctest.NORMALIZE_WHITESPACE | doctest.ELLIPSIS | doctest.REPORT_ONLY_FIRST_FAILURE
)


if not compat.IS_PY2 and not compat.IS_PYPY: # pragma: no cover
TESTFILES = [
'../../../README.rst',
'../behavior.py',
'../instructions.py'
]
TESTFILES = ['../../../README.rst', '../behavior.py', '../instructions.py']
else: # pragma: no cover
TESTFILES = []

Expand All @@ -27,14 +21,14 @@ def test_suite():

suite = unittest.TestSuite()
suite.addTest(unittest.findTestCases(test_plumber))
suite.addTests([
doctest.DocFileSuite(
testfile,
globs=dict(pprint=pprint),
optionflags=optionflags
)
for testfile in TESTFILES
])
suite.addTests(
[
doctest.DocFileSuite(
testfile, globs=dict(pprint=pprint), optionflags=optionflags
)
for testfile in TESTFILES
]
)
return suite


Expand Down
1 change: 1 addition & 0 deletions src/plumber/tests/globalmetaclass.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,5 @@ class BCClassReallyUsingAPlumbing:
True
"""

__plumbing__ = Behavior1
Loading

0 comments on commit 8d53d42

Please sign in to comment.