Merge pull request #527 from meejah/492.attrs-deprecated

incorporate deprecated attrs code
This commit is contained in:
meejah
2024-05-29 16:56:40 -06:00
committed by GitHub
19 changed files with 140 additions and 22 deletions

View File

@@ -3,6 +3,7 @@ User-visible changes in "magic-wormhole":
## Upcoming Release
* add release-notes here when making PRs
* Incorporate attrs' zope-interface support (#492)
## Release 0.14.0 (28-Mar-2024)

View File

@@ -1,7 +1,7 @@
from __future__ import absolute_import, print_function, unicode_literals
from attr import attrib, attrs
from attr.validators import provides
from wormhole.util import provides
from automat import MethodicalMachine
from zope.interface import implementer

View File

@@ -1,7 +1,7 @@
import re
from attr import attrib, attrs
from attr.validators import instance_of, optional, provides
from attr.validators import instance_of, optional
from automat import MethodicalMachine
from twisted.python import log
from zope.interface import implementer
@@ -23,7 +23,7 @@ from ._terminator import Terminator
from ._wordlist import PGPWordList
from .errors import (LonelyError, OnlyOneCodeError, ServerError, WelcomeError,
WrongPasswordError, _UnknownPhaseError)
from .util import bytes_to_dict
from .util import bytes_to_dict, provides
@attrs

View File

@@ -1,11 +1,11 @@
from __future__ import print_function, absolute_import, unicode_literals
from zope.interface import implementer
from attr import attrs, attrib
from attr.validators import provides
from automat import MethodicalMachine
from . import _interfaces
from ._nameplate import validate_nameplate
from .errors import KeyFormatError
from .util import provides
def validate_code(code):

View File

@@ -1,6 +1,6 @@
from collections import namedtuple
from attr import attrs, attrib
from attr.validators import instance_of, provides
from attr.validators import instance_of
from automat import MethodicalMachine
from zope.interface import Interface, implementer
from twisted.python import log
@@ -8,6 +8,7 @@ from twisted.internet.protocol import Protocol
from twisted.internet.interfaces import ITransport
from .._interfaces import IDilationConnector
from ..observer import OneShotObserver
from ..util import provides
from .encode import to_be4, from_be4
from .roles import LEADER, FOLLOWER
from ._noise import NoiseInvalidMessage, NoiseHandshakeError, NOISE_MAX_PAYLOAD, NOISE_MAX_CIPHERTEXT

View File

@@ -2,7 +2,7 @@ from __future__ import print_function, unicode_literals
from collections import defaultdict
from binascii import hexlify
from attr import attrs, attrib
from attr.validators import instance_of, provides, optional
from attr.validators import instance_of, optional
from automat import MethodicalMachine
from zope.interface import implementer
from twisted.internet.task import deferLater
@@ -16,7 +16,7 @@ from .. import ipaddrs # TODO: move into _dilation/
from .._interfaces import IDilationConnector, IDilationManager
from ..timing import DebugTiming
from ..observer import EmptyableSet
from ..util import HKDF, to_unicode
from ..util import HKDF, to_unicode, provides
from .connection import DilatedConnectionProtocol, KCM
from .roles import LEADER

View File

@@ -1,9 +1,9 @@
from __future__ import print_function, unicode_literals
from attr import attrs, attrib
from attr.validators import provides
from zope.interface import implementer
from twisted.python import log
from .._interfaces import IDilationManager, IInbound, ISubChannel
from ..util import provides
from .subchannel import (SubChannel, _SubchannelAddress)

View File

@@ -2,7 +2,7 @@ import os
from collections import deque
from collections.abc import Sequence
from attr import attrs, attrib
from attr.validators import provides, instance_of, optional
from attr.validators import instance_of, optional
from automat import MethodicalMachine
from zope.interface import implementer
from twisted.internet.defer import Deferred
@@ -10,7 +10,7 @@ from twisted.internet.interfaces import (IStreamClientEndpoint,
IStreamServerEndpoint)
from twisted.python import log, failure
from .._interfaces import IDilator, IDilationManager, ISend, ITerminator
from ..util import dict_to_bytes, bytes_to_dict, bytes_to_hexstr
from ..util import dict_to_bytes, bytes_to_dict, bytes_to_hexstr, provides
from ..observer import OneShotObserver
from .._key import derive_key
from .subchannel import (SubChannel, _SubchannelAddress, _WormholeAddress,

View File

@@ -1,12 +1,12 @@
from __future__ import print_function, unicode_literals
from collections import deque
from attr import attrs, attrib
from attr.validators import provides
from zope.interface import implementer
from twisted.internet.interfaces import IPushProducer, IPullProducer
from twisted.python import log
from twisted.python.reflect import safe_str
from .._interfaces import IDilationManager, IOutbound
from ..util import provides
from .connection import KCM, Ping, Pong, Ack

View File

@@ -1,6 +1,6 @@
from collections import deque
from attr import attrs, attrib
from attr.validators import instance_of, provides
from attr.validators import instance_of
from zope.interface import implementer
from twisted.internet.defer import inlineCallbacks, returnValue
from twisted.internet.interfaces import (ITransport, IProducer, IConsumer,
@@ -12,6 +12,7 @@ from twisted.internet.error import ConnectionDone
from automat import MethodicalMachine
from .._interfaces import ISubChannel, IDilationManager
from ..observer import OneShotObserver
from ..util import provides
# each subchannel frame (the data passed into transport.write(data)) gets a
# 9-byte header prefix (type, subchannel id, and sequence number), then gets

View File

@@ -6,14 +6,13 @@ from __future__ import absolute_import, print_function, unicode_literals
import threading
from attr import attrib, attrs
from attr.validators import provides
from automat import MethodicalMachine
from twisted.internet import defer
from zope.interface import implementer
from . import _interfaces, errors
from ._nameplate import validate_nameplate
from .util import provides
def first(outputs):
return list(outputs)[0]

View File

@@ -1,7 +1,7 @@
from hashlib import sha256
from attr import attrib, attrs
from attr.validators import instance_of, provides
from attr.validators import instance_of
from automat import MethodicalMachine
from nacl import utils
from nacl.exceptions import CryptoError
@@ -11,7 +11,7 @@ from zope.interface import implementer
from . import _interfaces
from .util import (bytes_to_dict, bytes_to_hexstr, dict_to_bytes,
hexstr_to_bytes, to_bytes, HKDF)
hexstr_to_bytes, to_bytes, HKDF, provides)
CryptoError
__all__ = ["derive_key", "derive_phase_key", "CryptoError", "Key"]

View File

@@ -1,11 +1,11 @@
from __future__ import absolute_import, print_function, unicode_literals
from attr import attrib, attrs
from attr.validators import provides
from automat import MethodicalMachine
from zope.interface import implementer
from . import _interfaces
from .util import provides
@attrs

View File

@@ -1,11 +1,12 @@
from __future__ import absolute_import, print_function, unicode_literals
from attr import attrib, attrs
from attr.validators import instance_of, provides
from attr.validators import instance_of
from automat import MethodicalMachine
from zope.interface import implementer
from . import _interfaces
from .util import provides
@attrs

View File

@@ -1,12 +1,13 @@
from __future__ import absolute_import, print_function, unicode_literals
from attr import attrib, attrs
from attr.validators import instance_of, provides
from attr.validators import instance_of
from automat import MethodicalMachine
from zope.interface import implementer
from . import _interfaces
from ._key import CryptoError, decrypt_data, derive_key, derive_phase_key
from .util import provides
@attrs

View File

@@ -1,7 +1,7 @@
import os
from urllib.parse import urlparse
from attr import attrs, attrib
from attr.validators import provides, instance_of, optional
from attr.validators import instance_of, optional
from zope.interface import implementer
from twisted.python import log
from twisted.internet import defer, endpoints, task
@@ -9,7 +9,7 @@ from twisted.application import internet
from autobahn.twisted import websocket
from . import _interfaces, errors
from .util import (bytes_to_hexstr, hexstr_to_bytes, bytes_to_dict,
dict_to_bytes)
dict_to_bytes, provides)
class WSClient(websocket.WebSocketClientProtocol):

View File

@@ -1,12 +1,13 @@
from __future__ import absolute_import, print_function, unicode_literals
from attr import attrib, attrs
from attr.validators import instance_of, provides
from attr.validators import instance_of
from automat import MethodicalMachine
from zope.interface import implementer
from . import _interfaces
from ._key import derive_phase_key, encrypt_data
from .util import provides
@attrs

View File

@@ -0,0 +1,73 @@
import zope.interface
from twisted.trial import unittest
from attr import Attribute
from attr._make import NOTHING
from wormhole.util import provides
class IFoo(zope.interface.Interface):
"""
An interface.
"""
def f():
"""
A function called f.
"""
def simple_attr(name):
return Attribute(
name=name,
default=NOTHING,
validator=None,
repr=True,
cmp=None,
eq=True,
hash=None,
init=True,
converter=None,
kw_only=False,
inherited=False,
)
class TestProvides(unittest.TestCase):
"""
Tests for `provides`.
"""
def test_success(self):
"""
Nothing happens if value provides requested interface.
"""
@zope.interface.implementer(IFoo)
class C(object):
def f(self):
pass
v = provides(IFoo)
v(None, simple_attr("x"), C())
def test_fail(self):
"""
Raises `TypeError` if interfaces isn't provided by value.
"""
value = object()
a = simple_attr("x")
v = provides(IFoo)
with self.assertRaises(TypeError):
v(None, a, value)
def test_repr(self):
"""
Returned validator has a useful `__repr__`.
"""
v = provides(IFoo)
assert (
"<provides validator for interface {interface!r}>".format(
interface=IFoo
)
) == repr(v)

View File

@@ -5,6 +5,7 @@ import unicodedata
from binascii import hexlify, unhexlify
from cryptography.hazmat.primitives.kdf import hkdf
from cryptography.hazmat.primitives import hashes
from attr import attrs, attrib
def HKDF(skm, outlen, salt=None, CTXinfo=b""):
@@ -77,3 +78,42 @@ def estimate_free_space(target):
return s.f_frsize * s.f_bfree
except AttributeError:
return None
@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):
msg = "'{name}' must provide {interface!r} which {value!r} doesn't.".format(
name=attr.name, interface=self.interface, value=value
)
raise TypeError(
msg,
attr,
self.interface,
value,
)
def __repr__(self):
return f"<provides validator for interface {self.interface!r}>"
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)``.
: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)