connectors/__init__.py
changeset 3884 34da877021d5
parent 3847 832c257d5618
equal deleted inserted replaced
3883:a6e7dd8bac36 3884:34da877021d5
    29 import os
    29 import os
    30 import importlib
    30 import importlib
    31 from os import listdir, path
    31 from os import listdir, path
    32 from connectors.ConnectorBase import ConnectorBase
    32 from connectors.ConnectorBase import ConnectorBase
    33 
    33 
    34 connectors_packages = ["PYRO"]
    34 connectors_packages = ["ERPC", "WAMP"]
    35 
    35 
    36 
    36 
    37 def _GetLocalConnectorClassFactory(name):
    37 def _GetLocalConnectorClassFactory(name):
    38     return lambda: getattr(importlib.import_module(f"connectors.{name}"),
    38     return lambda: getattr(importlib.import_module(f"connectors.{name}"),
    39                            f"{name}_connector_factory")
    39                            f"{name}_connector_factory")
    69     Return a connector corresponding to the URI
    69     Return a connector corresponding to the URI
    70     or None if cannot connect to URI
    70     or None if cannot connect to URI
    71     """
    71     """
    72     _scheme = uri.split("://")[0].upper()
    72     _scheme = uri.split("://")[0].upper()
    73 
    73 
    74     # commented code to enable for MDNS:// support
       
    75     # _scheme, location = uri.split("://")
       
    76     # _scheme = _scheme.upper()
       
    77 
       
    78     if _scheme == "LOCAL":
    74     if _scheme == "LOCAL":
    79         # Local is special case
    75         # Local is special case
    80         # pyro connection to local runtime
    76         # ERPC connection to local runtime
    81         # started on demand, listening on random port
    77         # started on demand, listening on random port
    82         scheme = "PYRO"
    78         scheme = "ERPC"
    83         runtime_port = confnodesroot.StartLocalRuntime()
    79         runtime_port = confnodesroot.StartLocalRuntime()
    84         uri = f"PYRO://{LocalHost}:{runtime_port}"
    80         uri = f"ERPC://{LocalHost}:{runtime_port}"
    85 
       
    86     # commented code to enable for MDNS:// support
       
    87     # elif _scheme == "MDNS":
       
    88     #     try:
       
    89     #         from zeroconf import Zeroconf
       
    90     #         r = Zeroconf()
       
    91     #         i = r.get_service_info(zeroconf_service_type, location)
       
    92     #         if i is None:
       
    93     #             raise Exception("'%s' not found" % location)
       
    94     #         ip = str(socket.inet_ntoa(i.address))
       
    95     #         port = str(i.port)
       
    96     #         newlocation = ip + ':' + port
       
    97     #         confnodesroot.logger.write(_("'{a1}' is located at {a2}\n").format(a1=location, a2=newlocation))
       
    98     #         location = newlocation
       
    99     #         # not a bug, but a workaround against obvious downgrade attack
       
   100     #         scheme = "PYROS"
       
   101     #         r.close()
       
   102     #     except Exception:
       
   103     #         confnodesroot.logger.write_error(_("MDNS resolution failure for '%s'\n") % location)
       
   104     #         confnodesroot.logger.write_error(traceback.format_exc())
       
   105     #         return None
       
   106 
    81 
   107     elif _scheme in connectors:
    82     elif _scheme in connectors:
   108         scheme = _scheme
    83         scheme = _scheme
   109     elif _scheme[-1] == 'S' and _scheme[:-1] in connectors:
    84     elif _scheme[-1] == 'S' and _scheme[:-1] in connectors:
   110         scheme = _scheme[:-1]
    85         scheme = _scheme[:-1]
   111     else:
    86     else:
   112         return None
    87         return None
   113 
    88 
   114     # import module according to uri type and get connector specific baseclass
    89     return (connectors[scheme]
   115     # first call to import the module,
    90             ()  # triggers import
   116     # then call with parameters to create the class
    91             (uri, confnodesroot))  # creates object
   117     connector_specific_class = connectors[scheme]()(uri, confnodesroot)
       
   118 
       
   119     if connector_specific_class is None:
       
   120         return None
       
   121 
       
   122     # new class inheriting from generic and specific connector base classes
       
   123     return type(_scheme + "_connector",
       
   124                 (ConnectorBase, connector_specific_class), {})()
       
   125 
       
   126 
    92 
   127 def EditorClassFromScheme(scheme):
    93 def EditorClassFromScheme(scheme):
   128     _Import_Dialogs()
    94     _Import_Dialogs()
   129     return per_URI_connectors.get(scheme, None)
    95     return per_URI_connectors.get(scheme, None)
   130 
    96