connectors/__init__.py
changeset 733 915be999f3f0
parent 717 1c23952dbde1
child 763 c1104099c151
equal deleted inserted replaced
732:c4b0f117e106 733:915be999f3f0
    21 
    21 
    22 # Package initialisation
    22 # Package initialisation
    23 
    23 
    24 from os import listdir, path
    24 from os import listdir, path
    25 
    25 
    26 import PYRO
       
    27 
    26 
    28 _base_path = path.split(__file__)[0]
    27 _base_path = path.split(__file__)[0]
    29 
    28 
    30 connector_types = [name for name in listdir(_base_path)
       
    31                         if path.isdir(path.join(_base_path, name))
       
    32                             and name.lower() != ".hg"
       
    33                             and not name.startswith("__")]
       
    34 
       
    35 # a dict from a URI scheme (connector name) to connector module
       
    36 connector_modules = {}
       
    37 
    29 
    38 # a dict from a DNS-SD service type to a connector module that support it
    30 # a dict from a DNS-SD service type to a connector module that support it
    39 dnssd_connectors = {}
    31 dnssd_connectors = {"_PYRO._tcp.local.":"PYRO"}
    40 
    32 
    41 for t in connector_types:
    33 def _GetLocalConnectorClassFactory(name):
    42     new_module = getattr(__import__("connectors." + t), t)
    34     return lambda:getattr(__import__(name,globals(),locals()), name + "_connector_factory")
    43     connector_modules[t] = new_module
    35 
    44     
    36 connectors = {name:_GetLocalConnectorClassFactory(name) 
    45     if hasattr(new_module, "supported_dnssd_services"):
    37                   for name in listdir(_base_path) 
    46         for st in new_module.supported_dnssd_services:
    38                       if path.isdir(path.join(_base_path, name)) 
    47             dnssd_connectors[st] = new_module
    39                           and not name.startswith("__")}
    48 
    40 
    49 def ConnectorFactory(uri, confnodesroot):
    41 def ConnectorFactory(uri, confnodesroot):
    50     """
    42     """
    51     Return a connector corresponding to the URI
    43     Return a connector corresponding to the URI
    52     or None if cannot connect to URI
    44     or None if cannot connect to URI
    53     """
    45     """
    54     servicetype = uri.split("://")[0]
    46     servicetype = uri.split("://")[0]
    55     if servicetype in connector_types:
    47     if servicetype in connectors:
    56         # import module according to uri type
    48         # import module according to uri type
    57         connectormodule = connector_modules[servicetype]
    49         connectorclass = connectors[servicetype]()
    58         factoryname = servicetype + "_connector_factory"
       
    59         return getattr(connectormodule, factoryname)(uri, confnodesroot)
       
    60     elif servicetype == "LOCAL":
    50     elif servicetype == "LOCAL":
       
    51         from PYRO import PYRO_connector_factory as connectorclass
    61         runtime_port = confnodesroot.AppFrame.StartLocalRuntime(taskbaricon=True)
    52         runtime_port = confnodesroot.AppFrame.StartLocalRuntime(taskbaricon=True)
    62         return PYRO.PYRO_connector_factory(
    53         uri="PYRO://127.0.0.1:"+str(runtime_port)
    63                        "PYRO://127.0.0.1:"+str(runtime_port), 
       
    64                        confnodesroot)
       
    65     else :
    54     else :
    66         return None    
    55         return None    
       
    56     return connectorclass(uri, confnodesroot)
    67 
    57