| OLD | NEW |
| (Empty) |
| 1 # Copyright (c) 2001-2004 Twisted Matrix Laboratories. | |
| 2 # See LICENSE for details. | |
| 3 | |
| 4 | |
| 5 """Address objects for network connections.""" | |
| 6 | |
| 7 import warnings, os | |
| 8 from zope.interface import implements | |
| 9 from twisted.internet.interfaces import IAddress | |
| 10 | |
| 11 | |
| 12 class IPv4Address(object): | |
| 13 """ | |
| 14 Object representing an IPv4 socket endpoint. | |
| 15 | |
| 16 @ivar type: A string describing the type of transport, either 'TCP' or 'UDP'
. | |
| 17 @ivar host: A string containing the dotted-quad IP address. | |
| 18 @ivar port: An integer representing the port number. | |
| 19 """ | |
| 20 | |
| 21 # _bwHack is given to old users who think we are a tuple. They expected | |
| 22 # addr[0] to define the socket type rather than the address family, so | |
| 23 # the value comes from a different namespace than the new .type value: | |
| 24 | |
| 25 # type = map[_bwHack] | |
| 26 # map = { 'SSL': 'TCP', 'INET': 'TCP', 'INET_UDP': 'UDP' } | |
| 27 | |
| 28 implements(IAddress) | |
| 29 | |
| 30 def __init__(self, type, host, port, _bwHack = None): | |
| 31 assert type in ('TCP', 'UDP') | |
| 32 self.type = type | |
| 33 self.host = host | |
| 34 self.port = port | |
| 35 self._bwHack = _bwHack | |
| 36 | |
| 37 def __getitem__(self, index): | |
| 38 warnings.warn("IPv4Address.__getitem__ is deprecated. Use attributes in
stead.", | |
| 39 category=DeprecationWarning, stacklevel=2) | |
| 40 return (self._bwHack or self.type, self.host, self.port).__getitem__(ind
ex) | |
| 41 | |
| 42 def __getslice__(self, start, stop): | |
| 43 warnings.warn("IPv4Address.__getitem__ is deprecated. Use attributes in
stead.", | |
| 44 category=DeprecationWarning, stacklevel=2) | |
| 45 return (self._bwHack or self.type, self.host, self.port)[start:stop] | |
| 46 | |
| 47 def __eq__(self, other): | |
| 48 if isinstance(other, tuple): | |
| 49 return tuple(self) == other | |
| 50 elif isinstance(other, IPv4Address): | |
| 51 a = (self.type, self.host, self.port) | |
| 52 b = (other.type, other.host, other.port) | |
| 53 return a == b | |
| 54 return False | |
| 55 | |
| 56 def __str__(self): | |
| 57 return 'IPv4Address(%s, %r, %d)' % (self.type, self.host, self.port) | |
| 58 | |
| 59 | |
| 60 class UNIXAddress(object): | |
| 61 """ | |
| 62 Object representing a UNIX socket endpoint. | |
| 63 | |
| 64 @ivar name: The filename associated with this socket. | |
| 65 @type name: C{str} | |
| 66 """ | |
| 67 | |
| 68 implements(IAddress) | |
| 69 | |
| 70 def __init__(self, name, _bwHack='UNIX'): | |
| 71 self.name = name | |
| 72 self._bwHack = _bwHack | |
| 73 | |
| 74 def __getitem__(self, index): | |
| 75 warnings.warn("UNIXAddress.__getitem__ is deprecated. Use attributes in
stead.", | |
| 76 category=DeprecationWarning, stacklevel=2) | |
| 77 return (self._bwHack, self.name).__getitem__(index) | |
| 78 | |
| 79 def __getslice__(self, start, stop): | |
| 80 warnings.warn("UNIXAddress.__getitem__ is deprecated. Use attributes in
stead.", | |
| 81 category=DeprecationWarning, stacklevel=2) | |
| 82 return (self._bwHack, self.name)[start:stop] | |
| 83 | |
| 84 def __eq__(self, other): | |
| 85 if isinstance(other, tuple): | |
| 86 return tuple(self) == other | |
| 87 elif isinstance(other, UNIXAddress): | |
| 88 try: | |
| 89 return os.path.samefile(self.name, other.name) | |
| 90 except OSError: | |
| 91 pass | |
| 92 return False | |
| 93 | |
| 94 def __str__(self): | |
| 95 return 'UNIXSocket(%r)' % (self.name,) | |
| 96 | |
| 97 | |
| 98 # These are for buildFactory backwards compatability due to | |
| 99 # stupidity-induced inconsistency. | |
| 100 | |
| 101 class _ServerFactoryIPv4Address(IPv4Address): | |
| 102 """Backwards compatability hack. Just like IPv4Address in practice.""" | |
| 103 | |
| 104 def __eq__(self, other): | |
| 105 if isinstance(other, tuple): | |
| 106 warnings.warn("IPv4Address.__getitem__ is deprecated. Use attribute
s instead.", | |
| 107 category=DeprecationWarning, stacklevel=2) | |
| 108 return (self.host, self.port) == other | |
| 109 elif isinstance(other, IPv4Address): | |
| 110 a = (self.type, self.host, self.port) | |
| 111 b = (other.type, other.host, other.port) | |
| 112 return a == b | |
| 113 return False | |
| OLD | NEW |