You are definitively right.
So, I commented out the concerned line in the Finder class. Then, i wrote 2 classes that allow dynamic rebinding triggered by file modification.
So, they monitor the source directory and reload a class file every time you save it. I like it this way because it does not depend on the editor you’re using.
I’m not an experienced python programmer, so feel free to comment
# FileChangeNotifier.py #
import os
import thread
import time
class FileChangeNotifier(object):
def __init__(self, path, callback, interval=0.5):
self.path_to_scan = path
self.files = {}
self.callback = callback
self.interval = interval
self.is_monitoring = False
def __stat(self, filename):
stat=os.stat(filename)
return str(stat[6]) + '_' + str(stat[8])
def __walkCallback(self, args, directory, files):
for file_name in files:
if file_name.endswith('.py'):
filename = directory + '/' + file_name
if self.files.has_key(filename):
nrepr = self.__stat(filename)
if not nrepr == self.files[filename]:
self.files[filename]=self.__stat(filename)
self.callback(filename)
else :
self.files[filename]=self.__stat(filename)
def __monitor(self):
while self.is_monitoring :
os.path.walk(self.path_to_scan, self.__walkCallback, '')
time.sleep(self.interval)
def startMonitor(self):
self.is_monitoring = True
thread.start_new_thread(self.__monitor, ())
def stopMonitor(self):
self.is_monitoring = False
#########################
# ClassUpdater.py #
from direct.showbase import Finder
from FileChangeNotifier import FileChangeNotifier
class ClassUpdater(object):
def __onFileChange(self, filename):
try:
Finder.rebindClass(None, filename)
except Exception, ex:
print 'Exception while rebinding the class :',ex
def __init__(self, directory, interval):
self.fcn = FileChangeNotifier(directory, self.__onFileChange, interval)
def start(self):
self.fcn.startMonitor()
def stop(self):
self.fcn.stopMonitor()
#########################
# Example of use (go.py) :
from ClassUpdater import ClassUpdater
from MyWorld import MyWorld
w = MyWorld()
c = ClassUpdater('.', 1)
c.start()
run()
I open a shell, start ppython, type “execfile(‘go.py’)” that execute the above script, and everything is fine.
Thanks a lot David, this is even better that what I was expected.
Is there a place where I could place this stuff in the wiki ? I also have a VC8 project compiling pview that could be useful, regarding to how many people ask how to use Panda in C++
(edit : fixed a “print” bug)