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: Edouard@2329: connectors_packages = ["PYRO","WAMP"] Edouard@2329: andrej@1731: andrej@1731: def _GetLocalConnectorClassFactory(name): andrej@1731: return lambda: getattr(__import__(name, globals(), locals()), name + "_connector_factory") andrej@1731: andrej@2182: Edouard@2329: connectors = {name: _GetLocalConnectorClassFactory(name) Edouard@2329: for name in connectors_packages} denis@2007: Edouard@2329: _dialogs_imported = False Edouard@2329: per_URI_connectors = None Edouard@2329: schemes = None andrej@2182: Edouard@2329: # lazy import of connectors dialogs, only if used Edouard@2329: def _Import_Dialogs(): Edouard@2329: global per_URI_connectors, schemes, _dialogs_imported Edouard@2329: if not _dialogs_imported: Edouard@2329: _dialogs_imported = True Edouard@2329: per_URI_connectors = {} Edouard@2329: schemes = [] Edouard@2329: for con_name in connectors_packages: Edouard@2329: module = __import__(con_name + '_dialog', globals(), locals()) denis@2007: Edouard@2329: for scheme in module.Schemes: Edouard@2329: per_URI_connectors[scheme] = getattr(module, con_name + '_dialog') Edouard@2329: schemes += [scheme] 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: andrej@2182: Edouard@2329: def EditorClassFromScheme(scheme): Edouard@2329: _Import_Dialogs() Edouard@2329: return per_URI_connectors.get(scheme, None) denis@2007: Edouard@2329: def ConnectorSchemes(): Edouard@2329: _Import_Dialogs() Edouard@2329: return schemes