author | Andrey Skvortsov <andrej.skvortzov@gmail.com> |
Thu, 16 Aug 2018 13:42:18 +0300 | |
changeset 2289 | cf0934b42d66 |
parent 2010 | bb9c28bd204f |
child 2312 | 84b3cc18893b |
child 2416 | 1ca207782dde |
permissions | -rwxr-xr-x |
1731
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
diff
changeset
|
1 |
#!/usr/bin/env python |
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
diff
changeset
|
2 |
# -*- coding: utf-8 -*- |
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
diff
changeset
|
3 |
|
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
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:
1595
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:
1595
diff
changeset
|
6 |
# |
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
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:
1595
diff
changeset
|
8 |
# |
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
diff
changeset
|
9 |
# See COPYING file for copyrights details. |
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
diff
changeset
|
10 |
# |
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
diff
changeset
|
11 |
# 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:
1595
diff
changeset
|
12 |
# 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:
1595
diff
changeset
|
13 |
# 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:
1595
diff
changeset
|
14 |
# 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:
1595
diff
changeset
|
15 |
# |
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
diff
changeset
|
16 |
# 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:
1595
diff
changeset
|
17 |
# 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:
1595
diff
changeset
|
18 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
diff
changeset
|
19 |
# GNU General Public License for more details. |
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
diff
changeset
|
20 |
# |
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
diff
changeset
|
21 |
# 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:
1595
diff
changeset
|
22 |
# 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:
1595
diff
changeset
|
23 |
# 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:
1595
diff
changeset
|
24 |
|
1826
91796f408540
fix usage of python2-only print statement
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1783
diff
changeset
|
25 |
|
1881
091005ec69c4
fix pylint py3k conversion warning: "(no-absolute-import) import missing `from __future__ import absolute_import`"
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1847
diff
changeset
|
26 |
from __future__ import absolute_import |
1826
91796f408540
fix usage of python2-only print statement
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1783
diff
changeset
|
27 |
from __future__ import print_function |
1731
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
diff
changeset
|
28 |
import traceback |
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
diff
changeset
|
29 |
from time import sleep |
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
diff
changeset
|
30 |
import copy |
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
diff
changeset
|
31 |
import socket |
1783
3311eea28d56
clean-up: fix PEP8 E402 module level import not at top of file
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1750
diff
changeset
|
32 |
import os.path |
3311eea28d56
clean-up: fix PEP8 E402 module level import not at top of file
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1750
diff
changeset
|
33 |
|
1832
0f1081928d65
fix wrong-import-order. first standard modules are imported, then others
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1830
diff
changeset
|
34 |
import Pyro |
0f1081928d65
fix wrong-import-order. first standard modules are imported, then others
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1830
diff
changeset
|
35 |
import Pyro.core |
0f1081928d65
fix wrong-import-order. first standard modules are imported, then others
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1830
diff
changeset
|
36 |
import Pyro.util |
0f1081928d65
fix wrong-import-order. first standard modules are imported, then others
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1830
diff
changeset
|
37 |
from Pyro.errors import PyroError |
0f1081928d65
fix wrong-import-order. first standard modules are imported, then others
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1830
diff
changeset
|
38 |
|
0f1081928d65
fix wrong-import-order. first standard modules are imported, then others
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1830
diff
changeset
|
39 |
|
1731
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
diff
changeset
|
40 |
service_type = '_PYRO._tcp.local.' |
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
diff
changeset
|
41 |
# this module attribute contains a list of DNS-SD (Zeroconf) service types |
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
diff
changeset
|
42 |
# supported by this connector confnode. |
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
diff
changeset
|
43 |
# |
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
diff
changeset
|
44 |
# for connectors that do not support DNS-SD, this attribute can be omitted |
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
diff
changeset
|
45 |
# or set to an empty list. |
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
diff
changeset
|
46 |
|
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1731
diff
changeset
|
47 |
|
1731
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
diff
changeset
|
48 |
def PYRO_connector_factory(uri, confnodesroot): |
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
diff
changeset
|
49 |
""" |
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
diff
changeset
|
50 |
This returns the connector to Pyro style PLCobject |
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
diff
changeset
|
51 |
""" |
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
diff
changeset
|
52 |
confnodesroot.logger.write(_("PYRO connecting to URI : %s\n") % uri) |
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
diff
changeset
|
53 |
|
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
diff
changeset
|
54 |
servicetype, location = uri.split("://") |
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
diff
changeset
|
55 |
if servicetype == "PYROS": |
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
diff
changeset
|
56 |
schemename = "PYROLOCSSL" |
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
diff
changeset
|
57 |
# Protect against name->IP substitution in Pyro3 |
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
diff
changeset
|
58 |
Pyro.config.PYRO_DNS_URI = True |
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
diff
changeset
|
59 |
# Beware Pyro lib need str path, not unicode |
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
diff
changeset
|
60 |
# don't rely on PYRO_STORAGE ! see documentation |
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
diff
changeset
|
61 |
Pyro.config.PYROSSL_CERTDIR = os.path.abspath(str(confnodesroot.ProjectPath) + '/certs') |
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
diff
changeset
|
62 |
if not os.path.exists(Pyro.config.PYROSSL_CERTDIR): |
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
diff
changeset
|
63 |
confnodesroot.logger.write_error( |
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
diff
changeset
|
64 |
'Error : the directory %s is missing for SSL certificates (certs_dir).' |
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
diff
changeset
|
65 |
'Please fix it in your project.\n' % Pyro.config.PYROSSL_CERTDIR) |
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
diff
changeset
|
66 |
return None |
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
diff
changeset
|
67 |
else: |
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
diff
changeset
|
68 |
confnodesroot.logger.write(_("PYRO using certificates in '%s' \n") |
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
diff
changeset
|
69 |
% (Pyro.config.PYROSSL_CERTDIR)) |
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
diff
changeset
|
70 |
Pyro.config.PYROSSL_CERT = "client.crt" |
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
diff
changeset
|
71 |
Pyro.config.PYROSSL_KEY = "client.key" |
1750
acf02488f37f
clean-up: fix PEP8 E306 expected 1 blank line before a nested definition, found X
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1744
diff
changeset
|
72 |
|
1731
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
diff
changeset
|
73 |
# Ugly Monkey Patching |
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
diff
changeset
|
74 |
def _gettimeout(self): |
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
diff
changeset
|
75 |
return self.timeout |
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
diff
changeset
|
76 |
|
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
diff
changeset
|
77 |
def _settimeout(self, timeout): |
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
diff
changeset
|
78 |
self.timeout = timeout |
1919
ccea0fa6ea91
Another set of meaningless changes to satisfy PEP8 and PyLint.
Edouard Tisserant
parents:
1881
diff
changeset
|
79 |
from M2Crypto.SSL import Connection # pylint: disable=import-error |
1731
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
diff
changeset
|
80 |
Connection.timeout = None |
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
diff
changeset
|
81 |
Connection.gettimeout = _gettimeout |
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
diff
changeset
|
82 |
Connection.settimeout = _settimeout |
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
diff
changeset
|
83 |
# M2Crypto.SSL.Checker.WrongHost: Peer certificate commonName does not |
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
diff
changeset
|
84 |
# match host, expected 127.0.0.1, got server |
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
diff
changeset
|
85 |
Connection.clientPostConnectionCheck = None |
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
diff
changeset
|
86 |
else: |
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
diff
changeset
|
87 |
schemename = "PYROLOC" |
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
diff
changeset
|
88 |
if location.find(service_type) != -1: |
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
diff
changeset
|
89 |
try: |
1830
e598d1acf354
remove in-tree version of Zeroconf and use upstream from
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1826
diff
changeset
|
90 |
from zeroconf import Zeroconf |
1731
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
diff
changeset
|
91 |
r = Zeroconf() |
1830
e598d1acf354
remove in-tree version of Zeroconf and use upstream from
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1826
diff
changeset
|
92 |
i = r.get_service_info(service_type, location) |
1731
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
diff
changeset
|
93 |
if i is None: |
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
diff
changeset
|
94 |
raise Exception("'%s' not found" % location) |
1830
e598d1acf354
remove in-tree version of Zeroconf and use upstream from
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1826
diff
changeset
|
95 |
ip = str(socket.inet_ntoa(i.address)) |
e598d1acf354
remove in-tree version of Zeroconf and use upstream from
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1826
diff
changeset
|
96 |
port = str(i.port) |
1731
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
diff
changeset
|
97 |
newlocation = ip + ':' + port |
1744
69dfdb26f600
clean-up: fix PEP8 E251 unexpected spaces around keyword / parameter equals
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1736
diff
changeset
|
98 |
confnodesroot.logger.write(_("'{a1}' is located at {a2}\n").format(a1=location, a2=newlocation)) |
1731
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
diff
changeset
|
99 |
location = newlocation |
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
diff
changeset
|
100 |
r.close() |
1846
14b40afccd69
remove unused variables found by pylint
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1837
diff
changeset
|
101 |
except Exception: |
1731
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
diff
changeset
|
102 |
confnodesroot.logger.write_error(_("MDNS resolution failure for '%s'\n") % location) |
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
diff
changeset
|
103 |
confnodesroot.logger.write_error(traceback.format_exc()) |
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
diff
changeset
|
104 |
return None |
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
diff
changeset
|
105 |
|
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
diff
changeset
|
106 |
# Try to get the proxy object |
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
diff
changeset
|
107 |
try: |
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
diff
changeset
|
108 |
RemotePLCObjectProxy = Pyro.core.getAttrProxyForURI(schemename + "://" + location + "/PLCObject") |
1846
14b40afccd69
remove unused variables found by pylint
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1837
diff
changeset
|
109 |
except Exception: |
1731
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
diff
changeset
|
110 |
confnodesroot.logger.write_error(_("Connection to '%s' failed.\n") % location) |
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
diff
changeset
|
111 |
confnodesroot.logger.write_error(traceback.format_exc()) |
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
diff
changeset
|
112 |
return None |
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
diff
changeset
|
113 |
|
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
diff
changeset
|
114 |
def PyroCatcher(func, default=None): |
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
diff
changeset
|
115 |
""" |
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
diff
changeset
|
116 |
A function that catch a Pyro exceptions, write error to logger |
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
diff
changeset
|
117 |
and return default value when it happen |
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
diff
changeset
|
118 |
""" |
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
diff
changeset
|
119 |
def catcher_func(*args, **kwargs): |
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
diff
changeset
|
120 |
try: |
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
diff
changeset
|
121 |
return func(*args, **kwargs) |
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
diff
changeset
|
122 |
except Pyro.errors.ConnectionClosedError, e: |
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
diff
changeset
|
123 |
confnodesroot.logger.write_error(_("Connection lost!\n")) |
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
diff
changeset
|
124 |
confnodesroot._SetConnector(None) |
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
diff
changeset
|
125 |
except Pyro.errors.ProtocolError, e: |
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
diff
changeset
|
126 |
confnodesroot.logger.write_error(_("Pyro exception: %s\n") % e) |
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
diff
changeset
|
127 |
except Exception, e: |
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
diff
changeset
|
128 |
# confnodesroot.logger.write_error(traceback.format_exc()) |
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
diff
changeset
|
129 |
errmess = ''.join(Pyro.util.getPyroTraceback(e)) |
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
diff
changeset
|
130 |
confnodesroot.logger.write_error(errmess + "\n") |
1826
91796f408540
fix usage of python2-only print statement
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1783
diff
changeset
|
131 |
print(errmess) |
1731
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
diff
changeset
|
132 |
confnodesroot._SetConnector(None) |
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
diff
changeset
|
133 |
return default |
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
diff
changeset
|
134 |
return catcher_func |
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
diff
changeset
|
135 |
|
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
diff
changeset
|
136 |
# Check connection is effective. |
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
diff
changeset
|
137 |
# lambda is for getattr of GetPLCstatus to happen inside catcher |
1833
2269739dd098
fix unnecessary lambda and enable corresponding pylint check
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1832
diff
changeset
|
138 |
if PyroCatcher(RemotePLCObjectProxy.GetPLCstatus)() is None: |
1731
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
diff
changeset
|
139 |
confnodesroot.logger.write_error(_("Cannot get PLC status - connection failed.\n")) |
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
diff
changeset
|
140 |
return None |
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
diff
changeset
|
141 |
|
1995
691d119ba20f
Removed DebugThread. Take advantage of the fact that buffering is done in runtime. No need to poll for traces more than ten per second, then use simple wxTimer for that. Also removed Debug Lock since now everything happens in wx' mainloop.
Edouard Tisserant
parents:
1982
diff
changeset
|
142 |
_special_return_funcs = { |
691d119ba20f
Removed DebugThread. Take advantage of the fact that buffering is done in runtime. No need to poll for traces more than ten per second, then use simple wxTimer for that. Also removed Debug Lock since now everything happens in wx' mainloop.
Edouard Tisserant
parents:
1982
diff
changeset
|
143 |
"StartPLC": False, |
691d119ba20f
Removed DebugThread. Take advantage of the fact that buffering is done in runtime. No need to poll for traces more than ten per second, then use simple wxTimer for that. Also removed Debug Lock since now everything happens in wx' mainloop.
Edouard Tisserant
parents:
1982
diff
changeset
|
144 |
"GetTraceVariables": ("Broken", None), |
691d119ba20f
Removed DebugThread. Take advantage of the fact that buffering is done in runtime. No need to poll for traces more than ten per second, then use simple wxTimer for that. Also removed Debug Lock since now everything happens in wx' mainloop.
Edouard Tisserant
parents:
1982
diff
changeset
|
145 |
"GetPLCstatus": ("Broken", None), |
691d119ba20f
Removed DebugThread. Take advantage of the fact that buffering is done in runtime. No need to poll for traces more than ten per second, then use simple wxTimer for that. Also removed Debug Lock since now everything happens in wx' mainloop.
Edouard Tisserant
parents:
1982
diff
changeset
|
146 |
"RemoteExec": (-1, "RemoteExec script failed!") |
691d119ba20f
Removed DebugThread. Take advantage of the fact that buffering is done in runtime. No need to poll for traces more than ten per second, then use simple wxTimer for that. Also removed Debug Lock since now everything happens in wx' mainloop.
Edouard Tisserant
parents:
1982
diff
changeset
|
147 |
} |
1997 | 148 |
|
1731
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
diff
changeset
|
149 |
class PyroProxyProxy(object): |
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
diff
changeset
|
150 |
""" |
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
diff
changeset
|
151 |
A proxy proxy class to handle Beremiz Pyro interface specific behavior. |
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
diff
changeset
|
152 |
And to put Pyro exception catcher in between caller and Pyro proxy |
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
diff
changeset
|
153 |
""" |
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
diff
changeset
|
154 |
def __getattr__(self, attrName): |
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
diff
changeset
|
155 |
member = self.__dict__.get(attrName, None) |
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
diff
changeset
|
156 |
if member is None: |
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
diff
changeset
|
157 |
def my_local_func(*args, **kwargs): |
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
diff
changeset
|
158 |
return RemotePLCObjectProxy.__getattr__(attrName)(*args, **kwargs) |
1995
691d119ba20f
Removed DebugThread. Take advantage of the fact that buffering is done in runtime. No need to poll for traces more than ten per second, then use simple wxTimer for that. Also removed Debug Lock since now everything happens in wx' mainloop.
Edouard Tisserant
parents:
1982
diff
changeset
|
159 |
member = PyroCatcher(my_local_func, _special_return_funcs.get(attrName, None)) |
1731
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
diff
changeset
|
160 |
self.__dict__[attrName] = member |
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
diff
changeset
|
161 |
return member |
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
diff
changeset
|
162 |
|
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
diff
changeset
|
163 |
return PyroProxyProxy() |