mirror of
https://github.com/tahoe-lafs/tahoe-lafs.git
synced 2025-01-03 03:36:44 +00:00
Merge pull request #1326 from tahoe-lafs/4056.provides
attrs "provides()"
This commit is contained in:
commit
d0e1d8e598
@ -31,9 +31,15 @@ from twisted.internet.defer import (
|
|||||||
from twisted.internet.task import (
|
from twisted.internet.task import (
|
||||||
deferLater,
|
deferLater,
|
||||||
)
|
)
|
||||||
from twisted.internet.protocol import ProcessProtocol # see ticket 4056
|
from twisted.internet.interfaces import (
|
||||||
|
IProcessTransport,
|
||||||
|
IProcessProtocol,
|
||||||
|
)
|
||||||
from twisted.internet.error import ProcessTerminated
|
from twisted.internet.error import ProcessTerminated
|
||||||
|
|
||||||
|
from allmydata.util.attrs_provides import (
|
||||||
|
provides,
|
||||||
|
)
|
||||||
from allmydata.node import read_config
|
from allmydata.node import read_config
|
||||||
from .util import (
|
from .util import (
|
||||||
_CollectOutputProtocol,
|
_CollectOutputProtocol,
|
||||||
@ -67,16 +73,11 @@ class FlogGatherer(object):
|
|||||||
"""
|
"""
|
||||||
Flog Gatherer process.
|
Flog Gatherer process.
|
||||||
"""
|
"""
|
||||||
|
process = attr.ib(
|
||||||
# it would be best to use attr.validators.provides() here with the
|
validator=provides(IProcessTransport)
|
||||||
# corresponding Twisted interface (IProcessTransport,
|
)
|
||||||
# IProcessProtocol) but that is deprecated; please replace with
|
|
||||||
# our own "provides" as part of
|
|
||||||
# https://tahoe-lafs.org/trac/tahoe-lafs/ticket/4056#ticket
|
|
||||||
# insisting on a subclass is narrower than necessary
|
|
||||||
process = attr.ib()
|
|
||||||
protocol = attr.ib(
|
protocol = attr.ib(
|
||||||
validator=attr.validators.instance_of(ProcessProtocol)
|
validator=provides(IProcessProtocol)
|
||||||
)
|
)
|
||||||
furl = attr.ib()
|
furl = attr.ib()
|
||||||
|
|
||||||
@ -156,7 +157,7 @@ class StorageServer(object):
|
|||||||
validator=attr.validators.instance_of(TahoeProcess)
|
validator=attr.validators.instance_of(TahoeProcess)
|
||||||
)
|
)
|
||||||
protocol = attr.ib(
|
protocol = attr.ib(
|
||||||
validator=attr.validators.instance_of(ProcessProtocol)
|
validator=provides(IProcessProtocol)
|
||||||
)
|
)
|
||||||
|
|
||||||
@inlineCallbacks
|
@inlineCallbacks
|
||||||
@ -208,7 +209,7 @@ class Client(object):
|
|||||||
validator=attr.validators.instance_of(TahoeProcess)
|
validator=attr.validators.instance_of(TahoeProcess)
|
||||||
)
|
)
|
||||||
protocol = attr.ib(
|
protocol = attr.ib(
|
||||||
validator=attr.validators.instance_of(ProcessProtocol)
|
validator=provides(IProcessProtocol)
|
||||||
)
|
)
|
||||||
request = attr.ib() # original request, for addfinalizer()
|
request = attr.ib() # original request, for addfinalizer()
|
||||||
|
|
||||||
@ -336,7 +337,7 @@ class Introducer(object):
|
|||||||
validator=attr.validators.instance_of(TahoeProcess)
|
validator=attr.validators.instance_of(TahoeProcess)
|
||||||
)
|
)
|
||||||
protocol = attr.ib(
|
protocol = attr.ib(
|
||||||
validator=attr.validators.instance_of(ProcessProtocol)
|
validator=provides(IProcessProtocol)
|
||||||
)
|
)
|
||||||
furl = attr.ib()
|
furl = attr.ib()
|
||||||
|
|
||||||
|
3
newsfragments/4056.bugfix
Normal file
3
newsfragments/4056.bugfix
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
Provide our own copy of attrs' "provides()" validator
|
||||||
|
|
||||||
|
This validator is deprecated and slated for removal; that project's suggestion is to copy the code to our project.
|
@ -88,6 +88,7 @@ from allmydata.util.rrefutil import add_version_to_remote_reference
|
|||||||
from allmydata.util.hashutil import permute_server_hash
|
from allmydata.util.hashutil import permute_server_hash
|
||||||
from allmydata.util.dictutil import BytesKeyDict, UnicodeKeyDict
|
from allmydata.util.dictutil import BytesKeyDict, UnicodeKeyDict
|
||||||
from allmydata.util.deferredutil import async_to_deferred, race
|
from allmydata.util.deferredutil import async_to_deferred, race
|
||||||
|
from allmydata.util.attrs_provides import provides
|
||||||
from allmydata.storage.http_client import (
|
from allmydata.storage.http_client import (
|
||||||
StorageClient, StorageClientImmutables, StorageClientGeneral,
|
StorageClient, StorageClientImmutables, StorageClientGeneral,
|
||||||
ClientException as HTTPClientException, StorageClientMutables,
|
ClientException as HTTPClientException, StorageClientMutables,
|
||||||
@ -659,7 +660,7 @@ class _FoolscapStorage(object):
|
|||||||
permutation_seed = attr.ib()
|
permutation_seed = attr.ib()
|
||||||
tubid = attr.ib()
|
tubid = attr.ib()
|
||||||
|
|
||||||
storage_server = attr.ib(validator=attr.validators.provides(IStorageServer))
|
storage_server = attr.ib(validator=provides(IStorageServer))
|
||||||
|
|
||||||
_furl = attr.ib()
|
_furl = attr.ib()
|
||||||
_short_description = attr.ib()
|
_short_description = attr.ib()
|
||||||
|
50
src/allmydata/util/attrs_provides.py
Normal file
50
src/allmydata/util/attrs_provides.py
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
"""
|
||||||
|
Utilities related to attrs
|
||||||
|
|
||||||
|
Handling for zope.interface is deprecated in attrs so we copy the
|
||||||
|
relevant support method here since we depend on zope.interface anyway
|
||||||
|
"""
|
||||||
|
|
||||||
|
from attr._make import attrs, attrib
|
||||||
|
|
||||||
|
|
||||||
|
@attrs(repr=False, slots=True, hash=True)
|
||||||
|
class _ProvidesValidator:
|
||||||
|
interface = attrib()
|
||||||
|
|
||||||
|
def __call__(self, inst, attr, value):
|
||||||
|
"""
|
||||||
|
We use a callable class to be able to change the ``__repr__``.
|
||||||
|
"""
|
||||||
|
if not self.interface.providedBy(value):
|
||||||
|
raise TypeError(
|
||||||
|
"'{name}' must provide {interface!r} which {value!r} "
|
||||||
|
"doesn't.".format(
|
||||||
|
name=attr.name, interface=self.interface, value=value
|
||||||
|
),
|
||||||
|
attr,
|
||||||
|
self.interface,
|
||||||
|
value,
|
||||||
|
)
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
return "<provides validator for interface {interface!r}>".format(
|
||||||
|
interface=self.interface
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def provides(interface):
|
||||||
|
"""
|
||||||
|
A validator that raises a `TypeError` if the initializer is called
|
||||||
|
with an object that does not provide the requested *interface* (checks are
|
||||||
|
performed using ``interface.providedBy(value)`` (see `zope.interface
|
||||||
|
<https://zopeinterface.readthedocs.io/en/latest/>`_).
|
||||||
|
|
||||||
|
:param interface: The interface to check for.
|
||||||
|
:type interface: ``zope.interface.Interface``
|
||||||
|
|
||||||
|
:raises TypeError: With a human readable error message, the attribute
|
||||||
|
(of type `attrs.Attribute`), the expected interface, and the
|
||||||
|
value it got.
|
||||||
|
"""
|
||||||
|
return _ProvidesValidator(interface)
|
Loading…
Reference in New Issue
Block a user