connectors/__init__.py
changeset 2329 e5703dc8848e
parent 2312 84b3cc18893b
child 2338 2c3222433244
equal deleted inserted replaced
2328:7eb6cb70bf5b 2329:e5703dc8848e
    28 
    28 
    29 from __future__ import absolute_import
    29 from __future__ import absolute_import
    30 from os import listdir, path
    30 from os import listdir, path
    31 import util.paths as paths
    31 import util.paths as paths
    32 
    32 
    33 _base_path = paths.AbsDir(__file__)
    33 connectors_packages = ["PYRO","WAMP"]
       
    34 
    34 
    35 
    35 def _GetLocalConnectorClassFactory(name):
    36 def _GetLocalConnectorClassFactory(name):
    36     return lambda: getattr(__import__(name, globals(), locals()), name + "_connector_factory")
    37     return lambda: getattr(__import__(name, globals(), locals()), name + "_connector_factory")
    37 
    38 
    38 
    39 
    39 def _GetLocalConnectorClassDialog(name):
    40 connectors = {name: _GetLocalConnectorClassFactory(name)
    40     return lambda: getattr(__import__(name + '.dialog', globals(), locals(), fromlist=['dialog']), name + "_connector_dialog")
    41               for name in connectors_packages}
    41 
    42 
       
    43 _dialogs_imported = False
       
    44 per_URI_connectors = None
       
    45 schemes = None 
    42 
    46 
    43 def _GetLocalConnectorURITypes(name):
    47 # lazy import of connectors dialogs, only if used
    44     return lambda: getattr(__import__(name + '.dialog', globals(), locals(), fromlist=['dialog']), "URITypes", None)
    48 def _Import_Dialogs():
       
    49     global per_URI_connectors, schemes, _dialogs_imported
       
    50     if not _dialogs_imported: 
       
    51         _dialogs_imported = True
       
    52         per_URI_connectors = {}
       
    53         schemes = []
       
    54         for con_name in connectors_packages:
       
    55             module =  __import__(con_name + '_dialog', globals(), locals())
    45 
    56 
    46 
    57             for scheme in module.Schemes:
    47 connectors = {name:
    58                 per_URI_connectors[scheme] = getattr(module, con_name + '_dialog')
    48               _GetLocalConnectorClassFactory(name)
    59                 schemes += [scheme]
    49               for name in listdir(_base_path)
       
    50               if (path.isdir(path.join(_base_path, name)) and
       
    51                   not name.startswith("__"))}
       
    52 
       
    53 connectors_dialog = {name:
       
    54                      {"function": _GetLocalConnectorClassDialog(name), "URITypes": _GetLocalConnectorURITypes(name)}
       
    55                      for name in listdir(_base_path)
       
    56                      if (path.isdir(path.join(_base_path, name)) and
       
    57                          not name.startswith("__"))}
       
    58 
    60 
    59 
    61 
    60 def ConnectorFactory(uri, confnodesroot):
    62 def ConnectorFactory(uri, confnodesroot):
    61     """
    63     """
    62     Return a connector corresponding to the URI
    64     Return a connector corresponding to the URI
    81     # import module according to uri type
    83     # import module according to uri type
    82     connectorclass = connectors[servicetype]()
    84     connectorclass = connectors[servicetype]()
    83     return connectorclass(uri, confnodesroot)
    85     return connectorclass(uri, confnodesroot)
    84 
    86 
    85 
    87 
    86 def ConnectorDialog(conn_type, confnodesroot):
    88 def EditorClassFromScheme(scheme):
    87     if conn_type not in connectors_dialog:
    89     _Import_Dialogs()
    88         return None
    90     return per_URI_connectors.get(scheme, None) 
    89 
    91 
    90     connectorclass = connectors_dialog[conn_type]["function"]()
    92 def ConnectorSchemes():
    91     return connectorclass(confnodesroot)
    93     _Import_Dialogs()
    92 
    94     return schemes
    93 
       
    94 def GetConnectorFromURI(uri):
       
    95     typeOfConnector = None
       
    96     for conn_type in connectors_dialog:
       
    97         connectorTypes = connectors_dialog[conn_type]["URITypes"]()
       
    98         if connectorTypes and uri in connectorTypes:
       
    99             typeOfConnector = conn_type
       
   100             break
       
   101 
       
   102     return typeOfConnector