andrej@1731: #!/usr/bin/env python andrej@1731: # -*- coding: utf-8 -*- andrej@1731: andrej@1731: # This file is part of Beremiz, a Integrated Development Environment for andrej@1731: # programming IEC 61131-3 automates supporting plcopen standard and CanFestival. andrej@1731: # andrej@1731: # Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD andrej@1731: # Copyright (C) 2017: Andrey Skvortsov andrej@1731: # andrej@1731: # See COPYING file for copyrights details. andrej@1731: # andrej@1731: # This program is free software; you can redistribute it and/or andrej@1731: # modify it under the terms of the GNU General Public License andrej@1731: # as published by the Free Software Foundation; either version 2 andrej@1731: # of the License, or (at your option) any later version. andrej@1731: # andrej@1731: # This program is distributed in the hope that it will be useful, andrej@1731: # but WITHOUT ANY WARRANTY; without even the implied warranty of andrej@1731: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the andrej@1731: # GNU General Public License for more details. andrej@1731: # andrej@1731: # You should have received a copy of the GNU General Public License andrej@1731: # along with this program; if not, write to the Free Software andrej@1731: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. andrej@1731: andrej@1731: # Package initialisation andrej@1731: andrej@1881: andrej@1881: from __future__ import absolute_import andrej@1731: from os import listdir, path andrej@1731: import util.paths as paths andrej@1731: andrej@1731: _base_path = paths.AbsDir(__file__) andrej@1731: andrej@1731: andrej@1731: def _GetLocalConnectorClassFactory(name): andrej@1731: return lambda: getattr(__import__(name, globals(), locals()), name + "_connector_factory") andrej@1731: denis@2007: def _GetLocalConnectorClassDialog(name): denis@2007: return lambda: getattr(__import__(name + '.dialog', globals(), locals(), fromlist=['dialog']), name + "_connector_dialog") denis@2007: denis@2007: def _GetLocalConnectorURITypes(name): denis@2007: return lambda: getattr(__import__(name + '.dialog', globals(), locals(), fromlist=['dialog']), "URITypes", None) denis@2007: andrej@1749: andrej@1767: connectors = {name: andrej@1767: _GetLocalConnectorClassFactory(name) andrej@1767: for name in listdir(_base_path) andrej@1785: if (path.isdir(path.join(_base_path, name)) and andrej@1785: not name.startswith("__"))} andrej@1731: denis@2007: connectors_dialog = {name: denis@2007: {"function":_GetLocalConnectorClassDialog(name), "URITypes": _GetLocalConnectorURITypes(name)} denis@2007: for name in listdir(_base_path) denis@2007: if (path.isdir(path.join(_base_path, name)) and denis@2007: not name.startswith("__"))} denis@2007: andrej@1731: andrej@1731: def ConnectorFactory(uri, confnodesroot): andrej@1731: """ andrej@1731: Return a connector corresponding to the URI andrej@1731: or None if cannot connect to URI andrej@1731: """ andrej@1731: servicetype = uri.split("://")[0].upper() andrej@1731: if servicetype == "LOCAL": andrej@1731: # Local is special case andrej@1731: # pyro connection to local runtime andrej@1731: # started on demand, listening on random port andrej@1731: servicetype = "PYRO" andrej@1731: runtime_port = confnodesroot.AppFrame.StartLocalRuntime( andrej@1731: taskbaricon=True) andrej@1731: uri = "PYROLOC://127.0.0.1:" + str(runtime_port) andrej@1731: elif servicetype in connectors: andrej@1731: pass andrej@1731: elif servicetype[-1] == 'S' and servicetype[:-1] in connectors: andrej@1731: servicetype = servicetype[:-1] andrej@1731: else: andrej@1731: return None andrej@1731: andrej@1731: # import module according to uri type andrej@1731: connectorclass = connectors[servicetype]() andrej@1731: return connectorclass(uri, confnodesroot) denis@2007: denis@2007: def ConnectorDialog(conn_type, confnodesroot): denis@2007: if conn_type not in connectors_dialog: denis@2007: return None denis@2007: denis@2007: connectorclass = connectors_dialog[conn_type]["function"]() denis@2007: return connectorclass(confnodesroot) denis@2007: denis@2007: def GetConnectorFromURI(uri): denis@2007: typeOfConnector = None denis@2007: for conn_type in connectors_dialog: denis@2007: connectorTypes = connectors_dialog[conn_type]["URITypes"]() denis@2007: if connectorTypes and uri in connectorTypes: denis@2007: typeOfConnector = conn_type denis@2007: break denis@2007: denis@2007: return typeOfConnector