connectors/__init__.py
author Edouard Tisserant
Fri, 23 Nov 2018 12:13:24 +0100
changeset 2459 21164625b393
parent 2339 48b4eba13064
child 2463 8742337a9fe3
permissions -rw-r--r--
Merged. Some changes that should already have been incuded during previous merge (mostly about PlcStatus) have been included this time.
1731
6ebd9c40b2be convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1680
diff changeset
     1
#!/usr/bin/env python
6ebd9c40b2be convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1680
diff changeset
     2
# -*- coding: utf-8 -*-
6ebd9c40b2be convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1680
diff changeset
     3
6ebd9c40b2be convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1680
diff changeset
     4
# This file is part of Beremiz, a Integrated Development Environment for
6ebd9c40b2be convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1680
diff changeset
     5
# programming IEC 61131-3 automates supporting plcopen standard and CanFestival.
6ebd9c40b2be convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1680
diff changeset
     6
#
6ebd9c40b2be convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1680
diff changeset
     7
# Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD
6ebd9c40b2be convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1680
diff changeset
     8
# Copyright (C) 2017: Andrey Skvortsov
6ebd9c40b2be convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1680
diff changeset
     9
#
6ebd9c40b2be convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1680
diff changeset
    10
# See COPYING file for copyrights details.
6ebd9c40b2be convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1680
diff changeset
    11
#
6ebd9c40b2be convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1680
diff changeset
    12
# This program is free software; you can redistribute it and/or
6ebd9c40b2be convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1680
diff changeset
    13
# modify it under the terms of the GNU General Public License
6ebd9c40b2be convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1680
diff changeset
    14
# as published by the Free Software Foundation; either version 2
6ebd9c40b2be convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1680
diff changeset
    15
# of the License, or (at your option) any later version.
6ebd9c40b2be convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1680
diff changeset
    16
#
6ebd9c40b2be convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1680
diff changeset
    17
# This program is distributed in the hope that it will be useful,
6ebd9c40b2be convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1680
diff changeset
    18
# but WITHOUT ANY WARRANTY; without even the implied warranty of
6ebd9c40b2be convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1680
diff changeset
    19
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
6ebd9c40b2be convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1680
diff changeset
    20
# GNU General Public License for more details.
6ebd9c40b2be convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1680
diff changeset
    21
#
6ebd9c40b2be convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1680
diff changeset
    22
# You should have received a copy of the GNU General Public License
6ebd9c40b2be convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1680
diff changeset
    23
# along with this program; if not, write to the Free Software
6ebd9c40b2be convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1680
diff changeset
    24
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
6ebd9c40b2be convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1680
diff changeset
    25
6ebd9c40b2be convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1680
diff changeset
    26
# Package initialisation
6ebd9c40b2be convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1680
diff changeset
    27
1881
091005ec69c4 fix pylint py3k conversion warning: "(no-absolute-import) import missing `from __future__ import absolute_import`"
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1785
diff changeset
    28
091005ec69c4 fix pylint py3k conversion warning: "(no-absolute-import) import missing `from __future__ import absolute_import`"
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1785
diff changeset
    29
from __future__ import absolute_import
1731
6ebd9c40b2be convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1680
diff changeset
    30
from os import listdir, path
6ebd9c40b2be convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1680
diff changeset
    31
import util.paths as paths
6ebd9c40b2be convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1680
diff changeset
    32
2329
e5703dc8848e URI editor : Rewrote most of it, cleaner.
Edouard Tisserant
parents: 2312
diff changeset
    33
connectors_packages = ["PYRO","WAMP"]
e5703dc8848e URI editor : Rewrote most of it, cleaner.
Edouard Tisserant
parents: 2312
diff changeset
    34
1731
6ebd9c40b2be convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1680
diff changeset
    35
6ebd9c40b2be convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1680
diff changeset
    36
def _GetLocalConnectorClassFactory(name):
6ebd9c40b2be convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1680
diff changeset
    37
    return lambda: getattr(__import__(name, globals(), locals()), name + "_connector_factory")
6ebd9c40b2be convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1680
diff changeset
    38
2182
eeca1aff0691 Fix linter errors
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2007
diff changeset
    39
2329
e5703dc8848e URI editor : Rewrote most of it, cleaner.
Edouard Tisserant
parents: 2312
diff changeset
    40
connectors = {name: _GetLocalConnectorClassFactory(name)
e5703dc8848e URI editor : Rewrote most of it, cleaner.
Edouard Tisserant
parents: 2312
diff changeset
    41
              for name in connectors_packages}
2007
ef2d479f564f #2476 Connectors dialog module added.
dporopat <denis.poropat@smarteh.si>
parents: 2006
diff changeset
    42
2329
e5703dc8848e URI editor : Rewrote most of it, cleaner.
Edouard Tisserant
parents: 2312
diff changeset
    43
_dialogs_imported = False
e5703dc8848e URI editor : Rewrote most of it, cleaner.
Edouard Tisserant
parents: 2312
diff changeset
    44
per_URI_connectors = None
e5703dc8848e URI editor : Rewrote most of it, cleaner.
Edouard Tisserant
parents: 2312
diff changeset
    45
schemes = None 
2182
eeca1aff0691 Fix linter errors
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2007
diff changeset
    46
2329
e5703dc8848e URI editor : Rewrote most of it, cleaner.
Edouard Tisserant
parents: 2312
diff changeset
    47
# lazy import of connectors dialogs, only if used
e5703dc8848e URI editor : Rewrote most of it, cleaner.
Edouard Tisserant
parents: 2312
diff changeset
    48
def _Import_Dialogs():
e5703dc8848e URI editor : Rewrote most of it, cleaner.
Edouard Tisserant
parents: 2312
diff changeset
    49
    global per_URI_connectors, schemes, _dialogs_imported
e5703dc8848e URI editor : Rewrote most of it, cleaner.
Edouard Tisserant
parents: 2312
diff changeset
    50
    if not _dialogs_imported: 
e5703dc8848e URI editor : Rewrote most of it, cleaner.
Edouard Tisserant
parents: 2312
diff changeset
    51
        _dialogs_imported = True
e5703dc8848e URI editor : Rewrote most of it, cleaner.
Edouard Tisserant
parents: 2312
diff changeset
    52
        per_URI_connectors = {}
e5703dc8848e URI editor : Rewrote most of it, cleaner.
Edouard Tisserant
parents: 2312
diff changeset
    53
        schemes = []
e5703dc8848e URI editor : Rewrote most of it, cleaner.
Edouard Tisserant
parents: 2312
diff changeset
    54
        for con_name in connectors_packages:
e5703dc8848e URI editor : Rewrote most of it, cleaner.
Edouard Tisserant
parents: 2312
diff changeset
    55
            module =  __import__(con_name + '_dialog', globals(), locals())
2007
ef2d479f564f #2476 Connectors dialog module added.
dporopat <denis.poropat@smarteh.si>
parents: 2006
diff changeset
    56
2329
e5703dc8848e URI editor : Rewrote most of it, cleaner.
Edouard Tisserant
parents: 2312
diff changeset
    57
            for scheme in module.Schemes:
e5703dc8848e URI editor : Rewrote most of it, cleaner.
Edouard Tisserant
parents: 2312
diff changeset
    58
                per_URI_connectors[scheme] = getattr(module, con_name + '_dialog')
e5703dc8848e URI editor : Rewrote most of it, cleaner.
Edouard Tisserant
parents: 2312
diff changeset
    59
                schemes += [scheme]
2007
ef2d479f564f #2476 Connectors dialog module added.
dporopat <denis.poropat@smarteh.si>
parents: 2006
diff changeset
    60
1731
6ebd9c40b2be convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1680
diff changeset
    61
6ebd9c40b2be convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1680
diff changeset
    62
def ConnectorFactory(uri, confnodesroot):
6ebd9c40b2be convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1680
diff changeset
    63
    """
6ebd9c40b2be convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1680
diff changeset
    64
    Return a connector corresponding to the URI
6ebd9c40b2be convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1680
diff changeset
    65
    or None if cannot connect to URI
6ebd9c40b2be convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1680
diff changeset
    66
    """
2338
2c3222433244 Renaming servicetype into scheme.
Edouard Tisserant
parents: 2329
diff changeset
    67
    scheme = uri.split("://")[0].upper()
2c3222433244 Renaming servicetype into scheme.
Edouard Tisserant
parents: 2329
diff changeset
    68
    if scheme == "LOCAL":
1731
6ebd9c40b2be convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1680
diff changeset
    69
        # Local is special case
6ebd9c40b2be convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1680
diff changeset
    70
        # pyro connection to local runtime
6ebd9c40b2be convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1680
diff changeset
    71
        # started on demand, listening on random port
2338
2c3222433244 Renaming servicetype into scheme.
Edouard Tisserant
parents: 2329
diff changeset
    72
        scheme = "PYRO"
1731
6ebd9c40b2be convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1680
diff changeset
    73
        runtime_port = confnodesroot.AppFrame.StartLocalRuntime(
6ebd9c40b2be convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1680
diff changeset
    74
            taskbaricon=True)
6ebd9c40b2be convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1680
diff changeset
    75
        uri = "PYROLOC://127.0.0.1:" + str(runtime_port)
2338
2c3222433244 Renaming servicetype into scheme.
Edouard Tisserant
parents: 2329
diff changeset
    76
    elif scheme in connectors:
1731
6ebd9c40b2be convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1680
diff changeset
    77
        pass
2338
2c3222433244 Renaming servicetype into scheme.
Edouard Tisserant
parents: 2329
diff changeset
    78
    elif scheme[-1] == 'S' and scheme[:-1] in connectors:
2c3222433244 Renaming servicetype into scheme.
Edouard Tisserant
parents: 2329
diff changeset
    79
        scheme = scheme[:-1]
1731
6ebd9c40b2be convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1680
diff changeset
    80
    else:
6ebd9c40b2be convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1680
diff changeset
    81
        return None
6ebd9c40b2be convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1680
diff changeset
    82
6ebd9c40b2be convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1680
diff changeset
    83
    # import module according to uri type
2339
48b4eba13064 IDManager : refactored a bit, moved some code into PSKManagement.py. Now captures URI and PSK on new PYRO(S) and propose them when editing URI. Import/export still to be implemented.
Edouard Tisserant
parents: 2338
diff changeset
    84
    connectorclass = connectors[scheme]()
1731
6ebd9c40b2be convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1680
diff changeset
    85
    return connectorclass(uri, confnodesroot)
2007
ef2d479f564f #2476 Connectors dialog module added.
dporopat <denis.poropat@smarteh.si>
parents: 2006
diff changeset
    86
2182
eeca1aff0691 Fix linter errors
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2007
diff changeset
    87
2329
e5703dc8848e URI editor : Rewrote most of it, cleaner.
Edouard Tisserant
parents: 2312
diff changeset
    88
def EditorClassFromScheme(scheme):
e5703dc8848e URI editor : Rewrote most of it, cleaner.
Edouard Tisserant
parents: 2312
diff changeset
    89
    _Import_Dialogs()
e5703dc8848e URI editor : Rewrote most of it, cleaner.
Edouard Tisserant
parents: 2312
diff changeset
    90
    return per_URI_connectors.get(scheme, None) 
2007
ef2d479f564f #2476 Connectors dialog module added.
dporopat <denis.poropat@smarteh.si>
parents: 2006
diff changeset
    91
2329
e5703dc8848e URI editor : Rewrote most of it, cleaner.
Edouard Tisserant
parents: 2312
diff changeset
    92
def ConnectorSchemes():
e5703dc8848e URI editor : Rewrote most of it, cleaner.
Edouard Tisserant
parents: 2312
diff changeset
    93
    _Import_Dialogs()
e5703dc8848e URI editor : Rewrote most of it, cleaner.
Edouard Tisserant
parents: 2312
diff changeset
    94
    return schemes