connectors/PYRO/PSK_Adapter.py
changeset 2550 f2af2a655868
parent 2544 640d639d9bd8
child 2588 25f19b76c7b6
--- a/connectors/PYRO/PSK_Adapter.py	Fri Mar 22 11:10:37 2019 +0100
+++ b/connectors/PYRO/PSK_Adapter.py	Fri Mar 22 13:26:31 2019 +0100
@@ -1,10 +1,40 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+# This file is part of Beremiz, a Integrated Development Environment for
+# programming IEC 61131-3 automates supporting plcopen standard and CanFestival.
+#
+# Copyright (C) 2019: Edouard TISSERANT
+#
+# See COPYING file for copyrights details.
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+
+
+"""
+The TLS-PSK adapter that handles SSL connections instead of regular sockets,
+but using Pre Shared Keys instead of Certificates
+"""
+
 from __future__ import absolute_import
 from __future__ import print_function
 
 import socket
 import re
+import ssl
 import sslpsk
-import ssl
 import Pyro
 from Pyro.core import PyroURI
 from Pyro.protocol import _connect_socket, TCPConnection, PYROAdapter
@@ -12,13 +42,13 @@
 from Pyro.util import Log
 
 
-# The TLS-PSK adapter that handles SSL connections instead of regular sockets,
-# but using Pre Shared Keys instead of Certificates
-#
 class PYROPSKAdapter(PYROAdapter):
-    # This is essentialy the same as in Pyro/protocol.py
-    # only raw_sock wrapping into sock through sslpsk.wrap_socket was added
-    # Pyro unfortunately doesn't allow cleaner customization
+    """
+    This is essentialy the same as in Pyro/protocol.py
+    only raw_sock wrapping into sock through sslpsk.wrap_socket was added
+    Pyro unfortunately doesn't allow cleaner customization
+    """
+
     def bindToURI(self, URI):
         with self.lock:   # only 1 thread at a time can bind the URI
             try:
@@ -37,7 +67,7 @@
                 # receive the authentication challenge string, and use that to build the actual identification string.
                 try:
                     authChallenge = self.recvAuthChallenge(conn)
-                except ProtocolError, x:
+                except ProtocolError as x:
                     # check if we were denied
                     if hasattr(x, "partialMsg") and x.partialMsg[:len(self.denyMSG)] == self.denyMSG:
                         raise ConnectionDeniedError(Pyro.constants.deniedReasons[int(x.partialMsg[-1])])
@@ -70,9 +100,6 @@
     return _getProtocolAdapter(protocol)
 
 
-Pyro.protocol.getProtocolAdapter = getProtocolAdapter
-
-
 _processStringURI = Pyro.core.processStringURI
 
 
@@ -91,4 +118,13 @@
     return _processStringURI(URI)
 
 
-Pyro.core.processStringURI = processStringURI
+def setupPSKAdapter():
+    """
+    Add PyroAdapter to the list of available in
+    Pyro adapters and handle new supported protocols
+
+    This function should be called after
+    reimport of Pyro module to enable PYROS:// again.
+    """
+    Pyro.protocol.getProtocolAdapter = getProtocolAdapter
+    Pyro.core.processStringURI = processStringURI