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