Skip to content

Instantly share code, notes, and snippets.

@denkiwakame
Created August 24, 2014 13:43
Show Gist options
  • Save denkiwakame/4d07fc3382fc98c83b55 to your computer and use it in GitHub Desktop.
Save denkiwakame/4d07fc3382fc98c83b55 to your computer and use it in GitHub Desktop.
watchdog stop() は外からコールしてjoinしないといけなかった
import ParamJsonGenerator
import time
import os,sys
import cgi
from watchdog.events import FileSystemEventHandler
from watchdog.observers import Observer
def DEBUG(msg):
sys.stderr.write("".join(["[DEBUG] ", msg, "\n"]))
class CaptureStatus():
def __init__(self, cams):
self.cams = cams
self.status = {} # key: int
for cam in cams:
self.status[cam] = 'wait'
# { 0:wait
# 1:wait
# 2:wait }
def showStatus(self):
print self.status
def setStatus(self,key,status):
self.status[key] = status
# True if all cameras are exit
def is_all_exit(self):
for key, val in self.status.iteritems():
if val != 'exit':
return False
return True
class ChangeHandler(FileSystemEventHandler):
def __init__(self, capture_status):
self.capture_status = capture_status
def on_created(self, event):
if event.is_directory:
return
if self.getext(event.src_path) in ('.json'):
# /tmp/cam0/cam0_capture.json
camnum = os.path.dirname(event.src_path)[-1]
self.capture_status.setStatus( int(camnum), 'capturing')
DEBUG('%s has been created.' % event.src_path)
self.capture_status.showStatus()
def on_modified(self, event):
if event.is_directory:
return
if self.getext(event.src_path) in ('.json'):
DEBUG('%s has been modified.' % event.src_path)
#raise Exception, 'thread terminates'
def on_moved(self, event):
pass
#raise Exception, 'thread terminates'
def on_deleted(self, event):
if event.is_directory:
return
if self.getext(event.src_path) in ('.json'):
camnum = os.path.dirname(event.src_path)[-1]
self.capture_status.setStatus( int(camnum), 'exit')
self.capture_status.showStatus()
DEBUG('%s has been deleted.' % event.src_path)
if self.capture_status.is_all_exit():
raise Exception, 'watch termiantes'
@staticmethod
def getext(filename):
return os.path.splitext(filename)[-1].lower()
def main():
BASEDIR = './monitor'
BASE_IMG_DIR = './data/'
cams = [0]
capture_status = CaptureStatus(cams)
observer = Observer()
event_handler = ChangeHandler(capture_status)
observer.schedule(event_handler,BASEDIR,recursive=True)
observer.start()
cams = [0,1,2]
for cam in cams:
pj_generator = ParamJsonGenerator.ParamJsonGenerator({
'json_dir' : BASEDIR + '/',
'img_dir' : BASE_IMG_DIR,
'fname' : 'test',
'fext' : '.pgm',
'cam_num' : cam,
'set_num' : 0
})
pj_generator.save_json()
# try:
# while True:
# time.sleep(1)
# print capture_sta.showStatus()
# if capture_sta.is_all_exit():
# observer.stop()
# break
# except KeyboardInterrupt:
# observer.stop()# observer.join()
if __name__ in '__main__':
main()
print 'EXITED'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment