connectors/__init__.py
author Andrey Skvortsov <andrej.skvortzov@gmail.com>
Fri, 12 Oct 2018 10:42:47 +0300
changeset 2452 2f2b2152580f
parent 2182 eeca1aff0691
child 2312 84b3cc18893b
permissions -rw-r--r--
python3 support: pylint, W1624 # (indexing-exception) Indexing exceptions will not work on Python 3
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
6ebd9c40b2be convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1680
diff changeset
    33
_base_path = paths.AbsDir(__file__)
6ebd9c40b2be convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1680
diff changeset
    34
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
2007
ef2d479f564f #2476 Connectors dialog module added.
dporopat <denis.poropat@smarteh.si>
parents: 2006
diff changeset
    40
def _GetLocalConnectorClassDialog(name):
ef2d479f564f #2476 Connectors dialog module added.
dporopat <denis.poropat@smarteh.si>
parents: 2006
diff changeset
    41
    return lambda: getattr(__import__(name + '.dialog', globals(), locals(), fromlist=['dialog']), name + "_connector_dialog")
ef2d479f564f #2476 Connectors dialog module added.
dporopat <denis.poropat@smarteh.si>
parents: 2006
diff changeset
    42
2182
eeca1aff0691 Fix linter errors
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2007
diff changeset
    43
2007
ef2d479f564f #2476 Connectors dialog module added.
dporopat <denis.poropat@smarteh.si>
parents: 2006
diff changeset
    44
def _GetLocalConnectorURITypes(name):
ef2d479f564f #2476 Connectors dialog module added.
dporopat <denis.poropat@smarteh.si>
parents: 2006
diff changeset
    45
    return lambda: getattr(__import__(name + '.dialog', globals(), locals(), fromlist=['dialog']), "URITypes", None)
ef2d479f564f #2476 Connectors dialog module added.
dporopat <denis.poropat@smarteh.si>
parents: 2006
diff changeset
    46
1749
d73b64672238 clean-up: fix PEP8 E305 expected 2 blank lines after class or function definition
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1740
diff changeset
    47
1767
c74815729afd clean-up: fix PEP8 E127 continuation line over-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1749
diff changeset
    48
connectors = {name:
c74815729afd clean-up: fix PEP8 E127 continuation line over-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1749
diff changeset
    49
              _GetLocalConnectorClassFactory(name)
c74815729afd clean-up: fix PEP8 E127 continuation line over-indented for visual indent
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1749
diff changeset
    50
              for name in listdir(_base_path)
1785
0ff2a45dcefa clean-up: fix PEP8 W503 line break before binary operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1773
diff changeset
    51
              if (path.isdir(path.join(_base_path, name)) and
0ff2a45dcefa clean-up: fix PEP8 W503 line break before binary operator
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1773
diff changeset
    52
                  not name.startswith("__"))}
1731
6ebd9c40b2be convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1680
diff changeset
    53
2007
ef2d479f564f #2476 Connectors dialog module added.
dporopat <denis.poropat@smarteh.si>
parents: 2006
diff changeset
    54
connectors_dialog = {name:
2182
eeca1aff0691 Fix linter errors
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2007
diff changeset
    55
                     {"function": _GetLocalConnectorClassDialog(name), "URITypes": _GetLocalConnectorURITypes(name)}
2007
ef2d479f564f #2476 Connectors dialog module added.
dporopat <denis.poropat@smarteh.si>
parents: 2006
diff changeset
    56
                     for name in listdir(_base_path)
ef2d479f564f #2476 Connectors dialog module added.
dporopat <denis.poropat@smarteh.si>
parents: 2006
diff changeset
    57
                     if (path.isdir(path.join(_base_path, name)) and
ef2d479f564f #2476 Connectors dialog module added.
dporopat <denis.poropat@smarteh.si>
parents: 2006
diff changeset
    58
                         not name.startswith("__"))}
ef2d479f564f #2476 Connectors dialog module added.
dporopat <denis.poropat@smarteh.si>
parents: 2006
diff changeset
    59
1731
6ebd9c40b2be convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1680
diff changeset
    60
6ebd9c40b2be convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1680
diff changeset
    61
def ConnectorFactory(uri, confnodesroot):
6ebd9c40b2be convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1680
diff changeset
    62
    """
6ebd9c40b2be convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1680
diff changeset
    63
    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
    64
    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
    65
    """
6ebd9c40b2be convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1680
diff changeset
    66
    servicetype = uri.split("://")[0].upper()
6ebd9c40b2be convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1680
diff changeset
    67
    if servicetype == "LOCAL":
6ebd9c40b2be convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1680
diff changeset
    68
        # Local is special case
6ebd9c40b2be convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1680
diff changeset
    69
        # pyro connection to local runtime
6ebd9c40b2be convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1680
diff changeset
    70
        # started on demand, listening on random port
6ebd9c40b2be convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1680
diff changeset
    71
        servicetype = "PYRO"
6ebd9c40b2be convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1680
diff changeset
    72
        runtime_port = confnodesroot.AppFrame.StartLocalRuntime(
6ebd9c40b2be convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1680
diff changeset
    73
            taskbaricon=True)
6ebd9c40b2be convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1680
diff changeset
    74
        uri = "PYROLOC://127.0.0.1:" + str(runtime_port)
6ebd9c40b2be convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1680
diff changeset
    75
    elif servicetype in connectors:
6ebd9c40b2be convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1680
diff changeset
    76
        pass
6ebd9c40b2be convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1680
diff changeset
    77
    elif servicetype[-1] == 'S' and servicetype[:-1] in connectors:
6ebd9c40b2be convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1680
diff changeset
    78
        servicetype = servicetype[:-1]
6ebd9c40b2be convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1680
diff changeset
    79
    else:
6ebd9c40b2be convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1680
diff changeset
    80
        return None
6ebd9c40b2be convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1680
diff changeset
    81
6ebd9c40b2be convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1680
diff changeset
    82
    # import module according to uri type
6ebd9c40b2be convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1680
diff changeset
    83
    connectorclass = connectors[servicetype]()
6ebd9c40b2be convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1680
diff changeset
    84
    return connectorclass(uri, confnodesroot)
2007
ef2d479f564f #2476 Connectors dialog module added.
dporopat <denis.poropat@smarteh.si>
parents: 2006
diff changeset
    85
2182
eeca1aff0691 Fix linter errors
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2007
diff changeset
    86
2007
ef2d479f564f #2476 Connectors dialog module added.
dporopat <denis.poropat@smarteh.si>
parents: 2006
diff changeset
    87
def ConnectorDialog(conn_type, confnodesroot):
ef2d479f564f #2476 Connectors dialog module added.
dporopat <denis.poropat@smarteh.si>
parents: 2006
diff changeset
    88
    if conn_type not in connectors_dialog:
ef2d479f564f #2476 Connectors dialog module added.
dporopat <denis.poropat@smarteh.si>
parents: 2006
diff changeset
    89
        return None
ef2d479f564f #2476 Connectors dialog module added.
dporopat <denis.poropat@smarteh.si>
parents: 2006
diff changeset
    90
ef2d479f564f #2476 Connectors dialog module added.
dporopat <denis.poropat@smarteh.si>
parents: 2006
diff changeset
    91
    connectorclass = connectors_dialog[conn_type]["function"]()
ef2d479f564f #2476 Connectors dialog module added.
dporopat <denis.poropat@smarteh.si>
parents: 2006
diff changeset
    92
    return connectorclass(confnodesroot)
ef2d479f564f #2476 Connectors dialog module added.
dporopat <denis.poropat@smarteh.si>
parents: 2006
diff changeset
    93
2182
eeca1aff0691 Fix linter errors
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2007
diff changeset
    94
2007
ef2d479f564f #2476 Connectors dialog module added.
dporopat <denis.poropat@smarteh.si>
parents: 2006
diff changeset
    95
def GetConnectorFromURI(uri):
ef2d479f564f #2476 Connectors dialog module added.
dporopat <denis.poropat@smarteh.si>
parents: 2006
diff changeset
    96
    typeOfConnector = None
ef2d479f564f #2476 Connectors dialog module added.
dporopat <denis.poropat@smarteh.si>
parents: 2006
diff changeset
    97
    for conn_type in connectors_dialog:
ef2d479f564f #2476 Connectors dialog module added.
dporopat <denis.poropat@smarteh.si>
parents: 2006
diff changeset
    98
        connectorTypes = connectors_dialog[conn_type]["URITypes"]()
ef2d479f564f #2476 Connectors dialog module added.
dporopat <denis.poropat@smarteh.si>
parents: 2006
diff changeset
    99
        if connectorTypes and uri in connectorTypes:
ef2d479f564f #2476 Connectors dialog module added.
dporopat <denis.poropat@smarteh.si>
parents: 2006
diff changeset
   100
            typeOfConnector = conn_type
ef2d479f564f #2476 Connectors dialog module added.
dporopat <denis.poropat@smarteh.si>
parents: 2006
diff changeset
   101
            break
ef2d479f564f #2476 Connectors dialog module added.
dporopat <denis.poropat@smarteh.si>
parents: 2006
diff changeset
   102
ef2d479f564f #2476 Connectors dialog module added.
dporopat <denis.poropat@smarteh.si>
parents: 2006
diff changeset
   103
    return typeOfConnector