Commits

uniqx committed e442c70

scripts are now read and executed from ram instead of reading the file once again for every execution.

  • Participants
  • Parent commits cc5cd1b

Comments (0)

Files changed (1)

 import re
 import sys
 import time
-import shutil
 import tarfile
 
 #
-# shared object definition
-#
-
-#class PyldInterface:
-#
-#  def __init__(self):
-#    intervals = []
-#
-#  def setInterval(self,interval,callback):
-#    intervals.append({'callback':callback})
-#
-#  def clear_all(self):
-#    del intervals[:]
-
-#
 # main application
 #
 
 NAME
        pyld - Python Live Daemon
 
-SYPNOSIS
+SYNOPSIS
        pyld [-i INTERVAL] [-n] [-r RECORD] FILE...
 
 DESCRIPTION
        pyld is a live coding tool for python. It is designed to run as a daemon
        so you may use your favourite editor for coding. pyld executes a python
-       script and monitors it constatnly. When a change to the script file is
+       script and monitors it constantly. When a change to the script file is
        detected pyld will attempt to monkey-patch the running script. Also a
-       copy of the script will be added to a version history if changes are
-       detected.
+       copy of the script will be added to a version history archive if changes
+       are detected.
 
        FILE
               The file(s) which should be executed and monitored for changes.
               result in 10 scans per second. default: 0.33
 
       -n, --no-record
-              Instructs pyled to do not record your live session to a tar file.
+              Instructs pyld to do not record your live session to a tar file.
               If -r is set, the -n option will be ignored.
 
       -r RECORD, --record RECORD
 
 
 def check_file(handle):
+  '''checks if a file has changed and calls refresh_file if necessary.
+
+  :param dict handle: meta data for a monitored python script
+  '''
 
   if os.path.exists(handle['path']):
     #print handle['path'], 'file exists'
     handle['init'] = False
 
 def refresh_file(handle,mtime):
-  # execute
+  '''refresh script file and execute it.
+
+  :param dict handle: meta data for a monitored python script
+  :param number mtime: new modification time
+  '''
+
+  # unlad if already initialized
   if handle['init']:
     __name__ = '__pyld_unload__'
-    execfile('.pyld.last.'+os.path.split(handle['path'])[-1])
+    exec(handle['content'])
     __name__ = '__main__'
-  else:
+
+  # (re)read the file
+  read_file(handle)
+
+  # initialize if not yet initialized
+  if not handle['init']:
     __name__ = '__pyld_init__'
-    execfile(handle['path'])
+    exec(handle['content'])
     __name__ = '__main__'
     handle['init'] = True
+
+  # exec reload
   __name__ = '__pyld_reload__'
-  execfile(handle['path'])
+  exec(handle['content'])
   __name__ = '__main__'
+
+  # update modification time
   handle['mtime'] = mtime
+
   # archive
   if arch_path != '':
     try:
         tar.close()
       except:
         pass
-  # last copy
-  shutil.copyfile(handle['path'],'.pyld.last.'+os.path.split(handle['path'])[-1])
 
 
+def read_file(handle):
+
+  with open(handle['path'],'r') as f:
+    handle['content']=''
+    for line in f:
+      handle['content'] += line
+  f.closed
 
 if __name__ == '__main__':
 
 
-  # handles for the monitored filed
+  # handles for the monitored files
   observe_handles = []
 
   # scan interval for file changes