connectors/PYRO/__init__.py
author Edouard Tisserant
Mon, 04 Apr 2011 11:07:53 +0200
changeset 595 6348c0110e0f
parent 493 015a803301b9
child 699 6ff64cadb1ff
permissions -rwxr-xr-x
Added support for handling ANY_DATE types in debugger type translation
203
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
     1
#!/usr/bin/env python
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
     2
# -*- coding: utf-8 -*-
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
     3
#
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
     4
#Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
     5
#
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
     6
#See COPYING file for copyrights details.
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
     7
#
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
     8
#This library is free software; you can redistribute it and/or
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
     9
#modify it under the terms of the GNU General Public
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    10
#License as published by the Free Software Foundation; either
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    11
#version 2.1 of the License, or (at your option) any later version.
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    12
#
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    13
#This library is distributed in the hope that it will be useful,
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    14
#but WITHOUT ANY WARRANTY; without even the implied warranty of
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    15
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    16
#General Public License for more details.
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    17
#
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    18
#You should have received a copy of the GNU General Public
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    19
#License along with this library; if not, write to the Free Software
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    20
#Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    21
import Pyro.core as pyro
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    22
from Pyro.errors import PyroError
477
f66a092b6e74 Arbitrary variable forcing
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 465
diff changeset
    23
import Pyro.util
203
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    24
import traceback
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    25
from time import sleep
231
f1db3ce8f40a Re-organized pyro connector proxy members mascarading
etisserant
parents: 218
diff changeset
    26
import copy
203
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    27
399
77e23bf04c33 Merging some improvements from BCT
laurent
parents: 361
diff changeset
    28
# this module attribute contains a list of DNS-SD (Zeroconf) service types
77e23bf04c33 Merging some improvements from BCT
laurent
parents: 361
diff changeset
    29
# supported by this connector plugin.
77e23bf04c33 Merging some improvements from BCT
laurent
parents: 361
diff changeset
    30
#
77e23bf04c33 Merging some improvements from BCT
laurent
parents: 361
diff changeset
    31
# for connectors that do not support DNS-SD, this attribute can be omitted
77e23bf04c33 Merging some improvements from BCT
laurent
parents: 361
diff changeset
    32
# or set to an empty list.
77e23bf04c33 Merging some improvements from BCT
laurent
parents: 361
diff changeset
    33
supported_dnssd_services = ["_PYRO._tcp.local."]
77e23bf04c33 Merging some improvements from BCT
laurent
parents: 361
diff changeset
    34
203
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    35
def PYRO_connector_factory(uri, pluginsroot):
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    36
    """
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    37
    This returns the connector to Pyro style PLCobject
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    38
    """
361
331d698e1118 Adding support for internationalization
laurent
parents: 354
diff changeset
    39
    pluginsroot.logger.write(_("Connecting to URI : %s\n")%uri)
203
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    40
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    41
    servicetype, location = uri.split("://")
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    42
    
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    43
    # Try to get the proxy object
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    44
    try :
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    45
        RemotePLCObjectProxy = pyro.getAttrProxyForURI("PYROLOC://"+location+"/PLCObject")
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    46
    except Exception, msg:
361
331d698e1118 Adding support for internationalization
laurent
parents: 354
diff changeset
    47
        pluginsroot.logger.write_error(_("Wrong URI, please check it !\n"))
203
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    48
        pluginsroot.logger.write_error(traceback.format_exc())
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    49
        return None
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    50
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    51
    def PyroCatcher(func, default=None):
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    52
        """
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    53
        A function that catch a pyro exceptions, write error to logger
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    54
        and return defaul value when it happen
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    55
        """
218
71fddab24be9 remove old code and fix typo
greg
parents: 203
diff changeset
    56
        def catcher_func(*args,**kwargs):
203
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    57
            try:
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    58
                return func(*args,**kwargs)
493
015a803301b9 Catch ProtocolError exception when connection failed
laurent
parents: 486
diff changeset
    59
            except Pyro.errors.ProtocolError, e:
015a803301b9 Catch ProtocolError exception when connection failed
laurent
parents: 486
diff changeset
    60
                pass
486
2e0fe44044b3 Catch Pyro exception when connection closed and print message
laurent
parents: 477
diff changeset
    61
            except Pyro.errors.ConnectionClosedError, e:
2e0fe44044b3 Catch Pyro exception when connection closed and print message
laurent
parents: 477
diff changeset
    62
                pluginsroot.logger.write_error("Connection lost!\n")
2e0fe44044b3 Catch Pyro exception when connection closed and print message
laurent
parents: 477
diff changeset
    63
                pluginsroot._connector = None
477
f66a092b6e74 Arbitrary variable forcing
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 465
diff changeset
    64
            except Exception,e:
203
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    65
                #pluginsroot.logger.write_error(traceback.format_exc())
477
f66a092b6e74 Arbitrary variable forcing
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 465
diff changeset
    66
                errmess = ''.join(Pyro.util.getPyroTraceback(e))
f66a092b6e74 Arbitrary variable forcing
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 465
diff changeset
    67
                pluginsroot.logger.write_error(errmess+"\n")
f66a092b6e74 Arbitrary variable forcing
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 465
diff changeset
    68
                print errmess
354
ff721067a75c Fix refresh of Beremiz button when connection lost
greg
parents: 349
diff changeset
    69
                pluginsroot._connector = None
203
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    70
                return default
218
71fddab24be9 remove old code and fix typo
greg
parents: 203
diff changeset
    71
        return catcher_func
203
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    72
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    73
    # Check connection is effective. 
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    74
    # lambda is for getattr of GetPLCstatus to happen inside catcher
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    75
    if PyroCatcher(lambda:RemotePLCObjectProxy.GetPLCstatus())() == None:
361
331d698e1118 Adding support for internationalization
laurent
parents: 354
diff changeset
    76
        pluginsroot.logger.write_error(_("Cannot get PLC status - connection failed.\n"))
203
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    77
        return None
231
f1db3ce8f40a Re-organized pyro connector proxy members mascarading
etisserant
parents: 218
diff changeset
    78
284
3fecc96090c8 Fixed problem with re-use of Pyro connector proxy copy across debug sessions
etisserant
parents: 235
diff changeset
    79
203
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    80
    class PyroProxyProxy:
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    81
        """
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    82
        A proxy proxy class to handle Beremiz Pyro interface specific behavior.
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    83
        And to put pyro exception catcher in between caller and pyro proxy
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    84
        """
284
3fecc96090c8 Fixed problem with re-use of Pyro connector proxy copy across debug sessions
etisserant
parents: 235
diff changeset
    85
        def __init__(self):
3fecc96090c8 Fixed problem with re-use of Pyro connector proxy copy across debug sessions
etisserant
parents: 235
diff changeset
    86
            # for safe use in from debug thread, must create a copy
3fecc96090c8 Fixed problem with re-use of Pyro connector proxy copy across debug sessions
etisserant
parents: 235
diff changeset
    87
            self.RemotePLCObjectProxyCopy = None
3fecc96090c8 Fixed problem with re-use of Pyro connector proxy copy across debug sessions
etisserant
parents: 235
diff changeset
    88
203
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    89
        def GetPyroProxy(self):
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    90
            """
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    91
            This func returns the real Pyro Proxy.
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    92
            Use this if you musn't keep reference to it.
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    93
            """
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
    94
            return RemotePLCObjectProxy
231
f1db3ce8f40a Re-organized pyro connector proxy members mascarading
etisserant
parents: 218
diff changeset
    95
235
a66e150f2888 Improved debug data feedback.
etisserant
parents: 231
diff changeset
    96
        def _PyroStartPLC(self, *args, **kwargs):
231
f1db3ce8f40a Re-organized pyro connector proxy members mascarading
etisserant
parents: 218
diff changeset
    97
            """
f1db3ce8f40a Re-organized pyro connector proxy members mascarading
etisserant
parents: 218
diff changeset
    98
            pluginsroot._connector.GetPyroProxy() is used 
f1db3ce8f40a Re-organized pyro connector proxy members mascarading
etisserant
parents: 218
diff changeset
    99
            rather than RemotePLCObjectProxy because
f1db3ce8f40a Re-organized pyro connector proxy members mascarading
etisserant
parents: 218
diff changeset
   100
            object is recreated meanwhile, 
f1db3ce8f40a Re-organized pyro connector proxy members mascarading
etisserant
parents: 218
diff changeset
   101
            so we must not keep ref to it here
f1db3ce8f40a Re-organized pyro connector proxy members mascarading
etisserant
parents: 218
diff changeset
   102
            """
465
67d32a91d70b Fixes in debug + reconnect to running PLC
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 446
diff changeset
   103
            current_status = pluginsroot._connector.GetPyroProxy().GetPLCstatus()
67d32a91d70b Fixes in debug + reconnect to running PLC
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 446
diff changeset
   104
            if current_status == "Dirty":
231
f1db3ce8f40a Re-organized pyro connector proxy members mascarading
etisserant
parents: 218
diff changeset
   105
                """
f1db3ce8f40a Re-organized pyro connector proxy members mascarading
etisserant
parents: 218
diff changeset
   106
                Some bad libs with static symbols may polute PLC
f1db3ce8f40a Re-organized pyro connector proxy members mascarading
etisserant
parents: 218
diff changeset
   107
                ask runtime to suicide and come back again
f1db3ce8f40a Re-organized pyro connector proxy members mascarading
etisserant
parents: 218
diff changeset
   108
                """
361
331d698e1118 Adding support for internationalization
laurent
parents: 354
diff changeset
   109
                pluginsroot.logger.write(_("Force runtime reload\n"))
231
f1db3ce8f40a Re-organized pyro connector proxy members mascarading
etisserant
parents: 218
diff changeset
   110
                pluginsroot._connector.GetPyroProxy().ForceReload()
f1db3ce8f40a Re-organized pyro connector proxy members mascarading
etisserant
parents: 218
diff changeset
   111
                pluginsroot._Disconnect()
f1db3ce8f40a Re-organized pyro connector proxy members mascarading
etisserant
parents: 218
diff changeset
   112
                # let remote PLC time to resurect.(freeze app)
f1db3ce8f40a Re-organized pyro connector proxy members mascarading
etisserant
parents: 218
diff changeset
   113
                sleep(0.5)
f1db3ce8f40a Re-organized pyro connector proxy members mascarading
etisserant
parents: 218
diff changeset
   114
                pluginsroot._Connect()
284
3fecc96090c8 Fixed problem with re-use of Pyro connector proxy copy across debug sessions
etisserant
parents: 235
diff changeset
   115
            self.RemotePLCObjectProxyCopy = copy.copy(pluginsroot._connector.GetPyroProxy())
235
a66e150f2888 Improved debug data feedback.
etisserant
parents: 231
diff changeset
   116
            return pluginsroot._connector.GetPyroProxy().StartPLC(*args, **kwargs)
231
f1db3ce8f40a Re-organized pyro connector proxy members mascarading
etisserant
parents: 218
diff changeset
   117
        StartPLC = PyroCatcher(_PyroStartPLC, False)
f1db3ce8f40a Re-organized pyro connector proxy members mascarading
etisserant
parents: 218
diff changeset
   118
f1db3ce8f40a Re-organized pyro connector proxy members mascarading
etisserant
parents: 218
diff changeset
   119
f1db3ce8f40a Re-organized pyro connector proxy members mascarading
etisserant
parents: 218
diff changeset
   120
        def _PyroGetTraceVariables(self):
f1db3ce8f40a Re-organized pyro connector proxy members mascarading
etisserant
parents: 218
diff changeset
   121
            """
f1db3ce8f40a Re-organized pyro connector proxy members mascarading
etisserant
parents: 218
diff changeset
   122
            for safe use in from debug thread, must use the copy
f1db3ce8f40a Re-organized pyro connector proxy members mascarading
etisserant
parents: 218
diff changeset
   123
            """
465
67d32a91d70b Fixes in debug + reconnect to running PLC
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 446
diff changeset
   124
            if self.RemotePLCObjectProxyCopy is None:
67d32a91d70b Fixes in debug + reconnect to running PLC
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 446
diff changeset
   125
                self.RemotePLCObjectProxyCopy = copy.copy(pluginsroot._connector.GetPyroProxy())
446
1edde533db19 Some cleanup in PLC status - removed that \"Starting\" state ...
ed
parents: 411
diff changeset
   126
            return self.RemotePLCObjectProxyCopy.GetTraceVariables()
1edde533db19 Some cleanup in PLC status - removed that \"Starting\" state ...
ed
parents: 411
diff changeset
   127
        GetTraceVariables = PyroCatcher(_PyroGetTraceVariables,("Broken",None,None))
231
f1db3ce8f40a Re-organized pyro connector proxy members mascarading
etisserant
parents: 218
diff changeset
   128
446
1edde533db19 Some cleanup in PLC status - removed that \"Starting\" state ...
ed
parents: 411
diff changeset
   129
        def _PyroGetPLCstatus(self):
1edde533db19 Some cleanup in PLC status - removed that \"Starting\" state ...
ed
parents: 411
diff changeset
   130
            return RemotePLCObjectProxy.GetPLCstatus()
1edde533db19 Some cleanup in PLC status - removed that \"Starting\" state ...
ed
parents: 411
diff changeset
   131
        GetPLCstatus = PyroCatcher(_PyroGetPLCstatus, "Broken")
1edde533db19 Some cleanup in PLC status - removed that \"Starting\" state ...
ed
parents: 411
diff changeset
   132
203
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   133
        def __getattr__(self, attrName):
231
f1db3ce8f40a Re-organized pyro connector proxy members mascarading
etisserant
parents: 218
diff changeset
   134
            member = self.__dict__.get(attrName, None)
f1db3ce8f40a Re-organized pyro connector proxy members mascarading
etisserant
parents: 218
diff changeset
   135
            if member is None:
f1db3ce8f40a Re-organized pyro connector proxy members mascarading
etisserant
parents: 218
diff changeset
   136
                def my_local_func(*args,**kwargs):
f1db3ce8f40a Re-organized pyro connector proxy members mascarading
etisserant
parents: 218
diff changeset
   137
                    return RemotePLCObjectProxy.__getattr__(attrName)(*args,**kwargs)
f1db3ce8f40a Re-organized pyro connector proxy members mascarading
etisserant
parents: 218
diff changeset
   138
                member = PyroCatcher(my_local_func, None)
203
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   139
                self.__dict__[attrName] = member
231
f1db3ce8f40a Re-organized pyro connector proxy members mascarading
etisserant
parents: 218
diff changeset
   140
            return member
f1db3ce8f40a Re-organized pyro connector proxy members mascarading
etisserant
parents: 218
diff changeset
   141
203
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   142
    return PyroProxyProxy()
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   143
    
cb9901076a21 Added concepts :
etisserant
parents:
diff changeset
   144