author | Edouard Tisserant <edouard.tisserant@gmail.com> |
Sat, 09 Dec 2023 01:01:48 +0100 | |
changeset 3882 | c7ec55cbd35a |
parent 3881 | 0b3ac94f494c |
permissions | -rw-r--r-- |
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 |
|
1731
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
diff
changeset
|
26 |
from time import sleep |
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
diff
changeset
|
27 |
import copy |
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
diff
changeset
|
28 |
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
|
29 |
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
|
30 |
|
3776 | 31 |
import Pyro5 |
32 |
import Pyro5.client |
|
33 |
import Pyro5.errors |
|
1832
0f1081928d65
fix wrong-import-order. first standard modules are imported, then others
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1830
diff
changeset
|
34 |
|
3776 | 35 |
# TODO: PSK |
36 |
||
3750
f62625418bff
automated conversion using 2to3-3.9 tool
GP Orcullo <kinsamanka@gmail.com>
parents:
2622
diff
changeset
|
37 |
import importlib |
1832
0f1081928d65
fix wrong-import-order. first standard modules are imported, then others
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1830
diff
changeset
|
38 |
|
2543
2befed4d6ca8
Fix Pyro work with SSL wrapper (sslpsk)
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
2538
diff
changeset
|
39 |
|
3808
3e219f00151a
Use msgpack and remove serpent's bytes workaround
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3805
diff
changeset
|
40 |
Pyro5.config.SERIALIZER = "msgpack" |
1731
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
diff
changeset
|
41 |
|
1736
7e61baa047f0
clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1731
diff
changeset
|
42 |
|
1731
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
diff
changeset
|
43 |
def PYRO_connector_factory(uri, confnodesroot): |
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
diff
changeset
|
44 |
""" |
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
diff
changeset
|
45 |
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
|
46 |
""" |
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
diff
changeset
|
47 |
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
|
48 |
|
2338 | 49 |
scheme, location = uri.split("://") |
3776 | 50 |
|
51 |
# TODO: use ssl |
|
52 |
||
53 |
schemename = "PYRO" |
|
2312 | 54 |
|
1731
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
diff
changeset
|
55 |
# Try to get the proxy object |
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
diff
changeset
|
56 |
try: |
3776 | 57 |
RemotePLCObjectProxy = Pyro5.client.Proxy(f"{schemename}:PLCObject@{location}") |
2536
2747d6e72eb8
Fix invalid python3 syntax
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
2493
diff
changeset
|
58 |
except Exception as e: |
2492
7dd551ac2fa0
check_sources.sh makes me become even less productive
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
2478
diff
changeset
|
59 |
confnodesroot.logger.write_error( |
7dd551ac2fa0
check_sources.sh makes me become even less productive
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
2478
diff
changeset
|
60 |
_("Connection to {loc} failed with exception {ex}\n").format( |
2538 | 61 |
loc=location, ex=str(e))) |
1731
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
diff
changeset
|
62 |
return None |
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
diff
changeset
|
63 |
|
3776 | 64 |
RemotePLCObjectProxy._pyroTimeout = 60 |
2468
046303391dea
PYRO(S): Added timeout on client side to allow unfreezing IDE after some time when communication with target dropped
Edouard Tisserant
parents:
2463
diff
changeset
|
65 |
|
3861
7e17f7e02a2b
Runtime: add GetVersions() call to PLCObject, use it in web settings and expose it in WAMP and Pyro.
Edouard Tisserant
parents:
2622
diff
changeset
|
66 |
class MissingCallException(Exception): |
7e17f7e02a2b
Runtime: add GetVersions() call to PLCObject, use it in web settings and expose it in WAMP and Pyro.
Edouard Tisserant
parents:
2622
diff
changeset
|
67 |
pass |
7e17f7e02a2b
Runtime: add GetVersions() call to PLCObject, use it in web settings and expose it in WAMP and Pyro.
Edouard Tisserant
parents:
2622
diff
changeset
|
68 |
|
1731
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
diff
changeset
|
69 |
def PyroCatcher(func, default=None): |
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
diff
changeset
|
70 |
""" |
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
diff
changeset
|
71 |
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
|
72 |
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
|
73 |
""" |
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
diff
changeset
|
74 |
def catcher_func(*args, **kwargs): |
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
diff
changeset
|
75 |
try: |
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
diff
changeset
|
76 |
return func(*args, **kwargs) |
3776 | 77 |
except Pyro5.errors.ConnectionClosedError as e: |
2622
e10a1095f577
Fixed repetitive freezing of IDE when connection drops.
Edouard Tisserant
parents:
2588
diff
changeset
|
78 |
confnodesroot._SetConnector(None) |
1731
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
diff
changeset
|
79 |
confnodesroot.logger.write_error(_("Connection lost!\n")) |
3776 | 80 |
except Pyro5.errors.ProtocolError as e: |
1731
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
diff
changeset
|
81 |
confnodesroot.logger.write_error(_("Pyro exception: %s\n") % e) |
3861
7e17f7e02a2b
Runtime: add GetVersions() call to PLCObject, use it in web settings and expose it in WAMP and Pyro.
Edouard Tisserant
parents:
2622
diff
changeset
|
82 |
except MissingCallException as e: |
7e17f7e02a2b
Runtime: add GetVersions() call to PLCObject, use it in web settings and expose it in WAMP and Pyro.
Edouard Tisserant
parents:
2622
diff
changeset
|
83 |
confnodesroot.logger.write_warning(_("Remote call not supported: %s\n") % e.message) |
2418
5587c490a070
Use python 3 compatible exception syntax everywhere
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
2416
diff
changeset
|
84 |
except Exception as e: |
3805
5a66d4be2e49
Pyro5: fix runtime typo + IDE exception handling
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3776
diff
changeset
|
85 |
errmess = ''.join(Pyro5.errors.get_pyro_traceback()) |
1731
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
diff
changeset
|
86 |
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
|
87 |
print(errmess) |
1731
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
diff
changeset
|
88 |
confnodesroot._SetConnector(None) |
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
diff
changeset
|
89 |
return default |
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
diff
changeset
|
90 |
return catcher_func |
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
diff
changeset
|
91 |
|
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
diff
changeset
|
92 |
# Check connection is effective. |
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
diff
changeset
|
93 |
# lambda is for getattr of GetPLCstatus to happen inside catcher |
2324
1cf3768ebf85
Automatically get PSK and ID when connecting to PYRO[S], so that future connection through PYROS can use that same key. Also fixed case to UPPER for *PSK.
Edouard Tisserant
parents:
2314
diff
changeset
|
94 |
IDPSK = PyroCatcher(RemotePLCObjectProxy.GetPLCID)() |
1cf3768ebf85
Automatically get PSK and ID when connecting to PYRO[S], so that future connection through PYROS can use that same key. Also fixed case to UPPER for *PSK.
Edouard Tisserant
parents:
2314
diff
changeset
|
95 |
if IDPSK is None: |
2430
65ff9a309ff3
Fixed regression in connecting to LOCAL:// targets
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
2429
diff
changeset
|
96 |
confnodesroot.logger.write_warning(_("PLC did not provide identity and security infomation.\n")) |
65ff9a309ff3
Fixed regression in connecting to LOCAL:// targets
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
2429
diff
changeset
|
97 |
else: |
2492
7dd551ac2fa0
check_sources.sh makes me become even less productive
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
2478
diff
changeset
|
98 |
ID, secret = IDPSK |
2430
65ff9a309ff3
Fixed regression in connecting to LOCAL:// targets
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
2429
diff
changeset
|
99 |
PSK.UpdateID(confnodesroot.ProjectPath, ID, secret, uri) |
1731
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
diff
changeset
|
100 |
|
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
diff
changeset
|
101 |
class PyroProxyProxy(object): |
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
diff
changeset
|
102 |
""" |
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
diff
changeset
|
103 |
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
|
104 |
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
|
105 |
""" |
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
diff
changeset
|
106 |
def __getattr__(self, attrName): |
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
diff
changeset
|
107 |
member = self.__dict__.get(attrName, None) |
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
diff
changeset
|
108 |
if member is None: |
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
diff
changeset
|
109 |
def my_local_func(*args, **kwargs): |
3861
7e17f7e02a2b
Runtime: add GetVersions() call to PLCObject, use it in web settings and expose it in WAMP and Pyro.
Edouard Tisserant
parents:
2622
diff
changeset
|
110 |
call = RemotePLCObjectProxy.__getattr__(attrName) |
7e17f7e02a2b
Runtime: add GetVersions() call to PLCObject, use it in web settings and expose it in WAMP and Pyro.
Edouard Tisserant
parents:
2622
diff
changeset
|
111 |
if call is None: |
7e17f7e02a2b
Runtime: add GetVersions() call to PLCObject, use it in web settings and expose it in WAMP and Pyro.
Edouard Tisserant
parents:
2622
diff
changeset
|
112 |
raise MissingCallException(attrName) |
7e17f7e02a2b
Runtime: add GetVersions() call to PLCObject, use it in web settings and expose it in WAMP and Pyro.
Edouard Tisserant
parents:
2622
diff
changeset
|
113 |
else: |
7e17f7e02a2b
Runtime: add GetVersions() call to PLCObject, use it in web settings and expose it in WAMP and Pyro.
Edouard Tisserant
parents:
2622
diff
changeset
|
114 |
return call(*args, **kwargs) |
7e17f7e02a2b
Runtime: add GetVersions() call to PLCObject, use it in web settings and expose it in WAMP and Pyro.
Edouard Tisserant
parents:
2622
diff
changeset
|
115 |
member = PyroCatcher(my_local_func, self.PLCObjDefaults.get(attrName, None)) |
1731
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
diff
changeset
|
116 |
self.__dict__[attrName] = member |
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
diff
changeset
|
117 |
return member |
6ebd9c40b2be
convert some files from CRLF to LF
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
1595
diff
changeset
|
118 |
|
2463
8742337a9fe3
Chunk based transfer for PLC binary and extra files, and some collateral code refactoring.
Edouard Tisserant
parents:
2430
diff
changeset
|
119 |
return PyroProxyProxy |