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: edouard@3721: import os kinsamanka@3757: import importlib andrej@1731: from os import listdir, path Edouard@2463: from connectors.ConnectorBase import ConnectorBase andrej@1731: edouard@3884: connectors_packages = ["ERPC", "WAMP"] Edouard@2329: andrej@1731: andrej@1731: def _GetLocalConnectorClassFactory(name): kinsamanka@3757: return lambda: getattr(importlib.import_module(f"connectors.{name}"), kinsamanka@3757: f"{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@2492: schemes = None edouard@2492: 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@2492: 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: kinsamanka@3757: module = importlib.import_module(f"connectors.{con_name}_dialog") 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: edouard@3847: LocalHost = os.environ.get("BEREMIZ_LOCAL_HOST", "127.0.0.1") edouard@3721: 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: """ Edouard@2463: _scheme = uri.split("://")[0].upper() Edouard@2478: Edouard@2463: if _scheme == "LOCAL": andrej@1731: # Local is special case edouard@3884: # ERPC connection to local runtime andrej@1731: # started on demand, listening on random port edouard@3884: scheme = "ERPC" edouard@3501: runtime_port = confnodesroot.StartLocalRuntime() edouard@3884: uri = f"ERPC://{LocalHost}:{runtime_port}" Edouard@2478: Edouard@2463: elif _scheme in connectors: Edouard@2463: scheme = _scheme Edouard@2463: elif _scheme[-1] == 'S' and _scheme[:-1] in connectors: Edouard@2463: scheme = _scheme[:-1] andrej@1731: else: andrej@1731: return None andrej@1731: edouard@3884: return (connectors[scheme] edouard@3884: () # triggers import edouard@3884: (uri, confnodesroot)) # creates object edouard@2492: Edouard@2329: def EditorClassFromScheme(scheme): Edouard@2329: _Import_Dialogs() edouard@2492: return per_URI_connectors.get(scheme, None) edouard@2492: denis@2007: Edouard@2329: def ConnectorSchemes(): Edouard@2329: _Import_Dialogs() Edouard@2329: return schemes