| 1 |
""" |
| 2 |
An effort to provide a backward-compatible Pyro4 API layer, |
| 3 |
to make porting existing code from Pyro4 to Pyro5 easier. |
| 4 |
|
| 5 |
This only works for code that imported Pyro4 symbols from the Pyro4 module |
| 6 |
directly, instead of from one of Pyro4's sub modules. So, for instance: |
| 7 |
from Pyro4 import Proxy instead of: from Pyro4.core import Proxy |
| 8 |
|
| 9 |
*some* submodules are more or less emulated such as Pyro4.errors, Pyro4.socketutil. |
| 10 |
|
| 11 |
So, you may first have to convert your old code to use the importing scheme to |
| 12 |
only import the Pyro4 module and not from its submodules, and then you should |
| 13 |
insert this at the top to enable the compatibility layer: |
| 14 |
from Pyro5.compatibility import Pyro4 |
| 15 |
|
| 16 |
|
| 17 |
Pyro - Python Remote Objects. Copyright by Irmen de Jong ([email protected]). |
| 18 |
""" |
| 19 |
|
| 20 |
import sys |
| 21 |
import ipaddress |
| 22 |
|
| 23 |
from .. import api |
| 24 |
from .. import errors |
| 25 |
from .. import serializers |
| 26 |
from .. import socketutil as socketutil_pyro5 |
| 27 |
from ..configure import Configuration |
| 28 |
|
| 29 |
|
| 30 |
__all__ = ["config", "URI", "Proxy", "Daemon", "callback", "batch", |
| 31 |
"asyncproxy", "oneway", "expose", "behavior", "current_context", |
| 32 |
"locateNS", "resolve", "Future", "errors"] |
| 33 |
|
| 34 |
|
| 35 |
# symbols that are no longer available in Pyro5 and that we don't emulate: |
| 36 |
|
| 37 |
def asyncproxy(*args, **kwargs): |
| 38 |
raise NotImplementedError("async proxy is no longer available in Pyro5") |
| 39 |
|
| 40 |
|
| 41 |
class Future(object): |
| 42 |
def __init__(self, *args): |
| 43 |
raise NotImplementedError("Pyro5 no longer provides its own Future class, " |
| 44 |
"you should use Python's concurrent.futures module instead for that") |
| 45 |
|
| 46 |
|
| 47 |
class NamespaceInterceptor: |
| 48 |
def __init__(self, namespace): |
| 49 |
self.namespace = namespace |
| 50 |
|
| 51 |
def __getattr__(self, item): |
| 52 |
raise NotImplementedError("The Pyro4 compatibility layer doesn't provide the Pyro4.{0} module, " |
| 53 |
"first make sure the code only uses symbols from the Pyro4 package directly" |
| 54 |
.format(self.namespace)) |
| 55 |
|
| 56 |
|
| 57 |
naming = NamespaceInterceptor("naming") |
| 58 |
core = NamespaceInterceptor("core") |
| 59 |
message = NamespaceInterceptor("message") |
| 60 |
|
| 61 |
|
| 62 |
# compatibility wrappers for the other symbols: |
| 63 |
|
| 64 |
__version__ = api.__version__ |
| 65 |
callback = api.callback |
| 66 |
oneway = api.oneway |
| 67 |
expose = api.expose |
| 68 |
behavior = api.behavior |
| 69 |
current_context = api.current_context |
| 70 |
|
| 71 |
|
| 72 |
class CompatConfiguration(Configuration): |
| 73 |
def asDict(self): |
| 74 |
return self.as_dict() |
| 75 |
|
| 76 |
|
| 77 |
config = CompatConfiguration() |
| 78 |
|
| 79 |
|
| 80 |
class URI(api.URI): |
| 81 |
pass |
| 82 |
|
| 83 |
|
| 84 |
class Proxy(api.Proxy): |
| 85 |
def _pyroAsync(self, asynchronous=True): |
| 86 |
raise NotImplementedError("async proxy is no longer available in Pyro5") |
| 87 |
|
| 88 |
@property |
| 89 |
def _pyroHmacKey(self): |
| 90 |
raise NotImplementedError("pyro5 doesn't have hmacs anymore") |
| 91 |
|
| 92 |
def __setattr__(self, name, value): |
| 93 |
if name == "_pyroHmacKey": |
| 94 |
raise NotImplementedError("pyro5 doesn't have hmacs anymore") |
| 95 |
return super().__setattr__(name, value) |
| 96 |
|
| 97 |
|
| 98 |
class Daemon(api.Daemon): |
| 99 |
pass |
| 100 |
|
| 101 |
|
| 102 |
def locateNS(host=None, port=None, broadcast=True, hmac_key=None): |
| 103 |
if hmac_key: |
| 104 |
raise NotImplementedError("hmac_key is no longer available in Pyro5, consider using 2-way SSL instead") |
| 105 |
return api.locate_ns(host, port, broadcast) |
| 106 |
|
| 107 |
|
| 108 |
def resolve(uri, hmac_key=None): |
| 109 |
if hmac_key: |
| 110 |
raise NotImplementedError("hmac_key is no longer available in Pyro5, consider using 2-way SSL instead") |
| 111 |
return api.resolve(uri) |
| 112 |
|
| 113 |
|
| 114 |
class BatchProxy(api.BatchProxy): |
| 115 |
def __call__(self, oneway=False, asynchronous=False): |
| 116 |
if asynchronous: |
| 117 |
raise NotImplementedError("async proxy is no longer available in Pyro5") |
| 118 |
return super().__call__(oneway) |
| 119 |
|
| 120 |
|
| 121 |
def batch(proxy): |
| 122 |
return BatchProxy(proxy) |
| 123 |
|
| 124 |
|
| 125 |
class UtilModule: |
| 126 |
@staticmethod |
| 127 |
def getPyroTraceback(ex_type=None, ex_value=None, ex_tb=None): |
| 128 |
return errors.get_pyro_traceback(ex_type, ex_value, ex_tb) |
| 129 |
|
| 130 |
@staticmethod |
| 131 |
def formatTraceback(ex_type=None, ex_value=None, ex_tb=None, detailed=False): |
| 132 |
return errors.format_traceback(ex_type, ex_value, ex_tb, detailed) |
| 133 |
|
| 134 |
SerializerBase = serializers.SerializerBase |
| 135 |
|
| 136 |
def excepthook(self, *args, **kwargs): |
| 137 |
return errors.excepthook(*args, **kwargs) |
| 138 |
|
| 139 |
|
| 140 |
util = UtilModule() |
| 141 |
|
| 142 |
|
| 143 |
class SocketUtilModule: |
| 144 |
@staticmethod |
| 145 |
def getIpVersion(hostnameOrAddress): |
| 146 |
return ipaddress.ip_address(hostnameOrAddress).version |
| 147 |
|
| 148 |
@staticmethod |
| 149 |
def getIpAddress(hostname, workaround127=False, ipVersion=None): |
| 150 |
return str(socketutil_pyro5.get_ip_address(hostname, workaround127, ipVersion)) |
| 151 |
|
| 152 |
@staticmethod |
| 153 |
def getInterfaceAddress(ip_address): |
| 154 |
return str(socketutil_pyro5.get_interface(ip_address).ip) |
| 155 |
|
| 156 |
@staticmethod |
| 157 |
def createSocket(bind=None, connect=None, reuseaddr=False, keepalive=True, |
| 158 |
timeout=-1, noinherit=False, |
| 159 |
ipv6=False, nodelay=True, sslContext=None): |
| 160 |
return socketutil_pyro5.create_socket(bind, connect, reuseaddr, keepalive, |
| 161 |
timeout, noinherit, ipv6, nodelay, sslContext) |
| 162 |
|
| 163 |
@staticmethod |
| 164 |
def createBroadcastSocket(bind=None, reuseaddr=False, timeout=-1, ipv6=False): |
| 165 |
return socketutil_pyro5.create_bc_socket(bind, reuseaddr, timeout, ipv6) |
| 166 |
|
| 167 |
@staticmethod |
| 168 |
def receiveData(sock, size): |
| 169 |
return socketutil_pyro5.receive_data(sock, size) |
| 170 |
|
| 171 |
@staticmethod |
| 172 |
def sendData(sock, data): |
| 173 |
return socketutil_pyro5.send_data(sock, data) |
| 174 |
|
| 175 |
|
| 176 |
socketutil = SocketUtilModule() |
| 177 |
|
| 178 |
|
| 179 |
class ConstantsModule: |
| 180 |
from .. import __version__ as VERSION |
| 181 |
from ..core import DAEMON_NAME, NAMESERVER_NAME |
| 182 |
from ..protocol import PROTOCOL_VERSION |
| 183 |
|
| 184 |
|
| 185 |
constants = ConstantsModule() |
| 186 |
|
| 187 |
|
| 188 |
# make sure that subsequent from Pyro4 import ... will work: |
| 189 |
sys.modules["Pyro4"] = sys.modules[__name__] |
| 190 |
sys.modules["Pyro4.errors"] = errors |
| 191 |
sys.modules["Pyro4.core"] = core |
| 192 |
sys.modules["Pyro4.naming"] = naming |
| 193 |
sys.modules["Pyro4.util"] = util |
| 194 |
sys.modules["Pyro4.socketutil"] = socketutil |
| 195 |
sys.modules["Pyro4.constants"] = constants |
| 196 |
sys.modules["Pyro4.message"] = message |