runtime/PLCObject.py
changeset 332 4f0e1d66bba5
parent 331 191a568a2a17
child 336 ae3488c79283
equal deleted inserted replaced
331:191a568a2a17 332:4f0e1d66bba5
    23 #Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    23 #Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    24 
    24 
    25 import Pyro.core as pyro
    25 import Pyro.core as pyro
    26 from threading import Timer, Thread
    26 from threading import Timer, Thread
    27 import ctypes, os, commands, types, sys
    27 import ctypes, os, commands, types, sys
    28 
       
    29 
    28 
    30 if os.name in ("nt", "ce"):
    29 if os.name in ("nt", "ce"):
    31     from _ctypes import LoadLibrary as dlopen
    30     from _ctypes import LoadLibrary as dlopen
    32     from _ctypes import FreeLibrary as dlclose
    31     from _ctypes import FreeLibrary as dlclose
    33 elif os.name == "posix":
    32 elif os.name == "posix":
   181 
   180 
   182     def PrepareRuntimePy(self):
   181     def PrepareRuntimePy(self):
   183         self.python_threads_vars = globals().copy()
   182         self.python_threads_vars = globals().copy()
   184         pyfile = os.path.join(self.workingdir, "runtime.py")
   183         pyfile = os.path.join(self.workingdir, "runtime.py")
   185         hmifile = os.path.join(self.workingdir, "hmi.py")
   184         hmifile = os.path.join(self.workingdir, "hmi.py")
   186         if os.path.exists(pyfile):
       
   187             try:
       
   188                 # TODO handle exceptions in runtime.py
       
   189                 # pyfile may redefine _runtime_cleanup
       
   190                 # or even call _PythonThreadProc itself.
       
   191                 execfile(pyfile, self.python_threads_vars)
       
   192             except:
       
   193                 PLCprint(traceback.format_exc())
       
   194         if os.path.exists(hmifile):
   185         if os.path.exists(hmifile):
   195             try:
   186             try:
   196                 execfile(hmifile, self.python_threads_vars)
   187                 execfile(hmifile, self.python_threads_vars)
       
   188                 if os.path.exists(pyfile):
       
   189                     try:
       
   190                         # TODO handle exceptions in runtime.py
       
   191                         # pyfile may redefine _runtime_cleanup
       
   192                         # or even call _PythonThreadProc itself.
       
   193                         execfile(pyfile, self.python_threads_vars)
       
   194                     except:
       
   195                         PLCprint(traceback.format_exc())
   197                 if self.python_threads_vars.has_key('wx'):
   196                 if self.python_threads_vars.has_key('wx'):
   198                     wx = self.python_threads_vars['wx']
   197                     wx = self.python_threads_vars['wx']
   199                     # try to instanciate the first frame found.
   198                     # try to instanciate the first frame found.
   200                     for name, obj in self.python_threads_vars.iteritems():
   199                     for name, obj in self.python_threads_vars.iteritems():
   201                         # obj is a class
   200                         # obj is a class
   210                             
   209                             
   211                             def OnCloseFrame(evt):
   210                             def OnCloseFrame(evt):
   212                                 wx.MessageBox("Please stop PLC to close")
   211                                 wx.MessageBox("Please stop PLC to close")
   213                             create_frame()
   212                             create_frame()
   214                             break
   213                             break
       
   214             except:
       
   215                 PLCprint(traceback.format_exc())
       
   216         elif os.path.exists(pyfile):
       
   217             try:
       
   218                 # TODO handle exceptions in runtime.py
       
   219                 # pyfile may redefine _runtime_cleanup
       
   220                 # or even call _PythonThreadProc itself.
       
   221                 execfile(pyfile, self.python_threads_vars)
   215             except:
   222             except:
   216                 PLCprint(traceback.format_exc())
   223                 PLCprint(traceback.format_exc())
   217         runtime_begin = self.python_threads_vars.get("_runtime_begin",None)
   224         runtime_begin = self.python_threads_vars.get("_runtime_begin",None)
   218         if runtime_begin is not None:
   225         if runtime_begin is not None:
   219             runtime_begin()
   226             runtime_begin()