Starts upgrading observer pattern. Have to update the update method now

This commit is contained in:
2012-08-28 22:12:06 +02:00
parent 4886c7e1ca
commit 13e2041e95
4 changed files with 18 additions and 26 deletions

View File

@@ -31,7 +31,7 @@ class FacemovieThread(threading.Thread, Observable, Observer):
"""
threading.Thread.__init__(self)
Observable.__init__(self)
Observer.__init__(self, "Facemovie")
Observer.__init__(self, "Application")
self.stop_process = False
@@ -54,7 +54,7 @@ class FacemovieThread(threading.Thread, Observable, Observer):
self.my_logger.debug("Facemovie is going to stop")
self.stop_process = True
self.notify(["STOP"])
self.notify(["Lib", ["STOP"]])
else:
self.console_logger.debug("Unrecognized system command")
self.my_logger.debug("Unrecognized system command")
@@ -65,14 +65,14 @@ class FacemovieThread(threading.Thread, Observable, Observer):
#self.console_logger.debug(message)
self.my_logger.debug(message)
# notify gui about small updates
self.notify(["STATUS", message[0], message[1]])
self.notify(["Lib", ["STATUS", message[0], message[1]]])
# checking for fatal error
if message[0] == "Error":
self.console_logger.debug("Fatal Error detected")
self.my_logger.debug("Fatal Error detected")
self.stop_process = True
self.notify(["STOP"])
self.notify(["Lib", ["STOP"]])
else:
self.console_logger.debug("Unrecognized command")
@@ -81,35 +81,34 @@ class FacemovieThread(threading.Thread, Observable, Observer):
self.my_logger.debug(message)
def run(self):
# FIXME : Quite ugly way of doing. Find better!
if not self.stop_process:
self.my_logger.debug("Listing pictures")
self.notify(["PROGRESS", "Listing pictures", 0.0])
self.notify(["Interface", ["PROGRESS", "Listing pictures", 0.0]])
self.facemovie.list_guys()
if not self.stop_process:
self.my_logger.debug("Detecting Faces")
self.notify(["PROGRESS", "Detecting Faces", 0.2])
self.notify(["Interface", ["PROGRESS", "Detecting Faces", 0.2]])
self.facemovie.prepare_faces() # I want to search for the faces, and characteristics of the images
if not self.stop_process:
self.my_logger.debug("Calculating video requirements")
self.notify(["PROGRESS", "Calculating video requirements", 0.6])
self.notify(["Interface", ["PROGRESS", "Calculating video requirements", 0.6]])
self.facemovie.find_final_dimensions() # finds output size for desired mode.
if not self.stop_process:
self.my_logger.debug("Generating movie")
self.notify(["PROGRESS", "Generating movie", 0.8])
self.notify(["Interface", ["PROGRESS", "Generating movie", 0.8]])
self.facemovie.save_movie()
self.my_logger.debug("Movie saved")
self.notify(["PROGRESS", "Movie saved, Finished!", 1.0])
self.notify(["Interface", ["PROGRESS", "Movie saved, Finished!", 1.0]])
# updating status to avoid remanent messages
self.notify(["STATUS", " ", 1.0])
self.notify(["Interface", ["STATUS", " ", 1.0]])
if not self.stop_process:
self.my_logger.debug("Thread terminated")
if self.stop_process:
self.notify(["PROGRESS", "Process cancelled!", 1.0])
self.notify(["Interface", ["PROGRESS", "Process cancelled!", 1.0]])

View File

@@ -40,7 +40,7 @@ class FaceMovie(object, Observable, Observer):
:type face_param: string
"""
Observable.__init__(self) # used to send notifications to process
Observer.__init__(self, "Library") # used to receive notification to stop
Observer.__init__(self, "Lib") # used to receive notification to stop
self.console_logger = logging.getLogger('ConsoleLog') # Used to send messages to the console
self.my_logger = logging.getLogger('FileLog') # Used to save events into a file
@@ -183,9 +183,9 @@ class FaceMovie(object, Observable, Observer):
# notifying the Observers
try:
message = message_root + " %d / %d" % (num, den)
self.notify([message, self.percent(num, den)])
self.notify(["Application", [message, self.percent(num, den)]])
except (ArithmeticError, ZeroDivisionError):
self.notify(["Error", 0])
self.notify(["Application", ["Error", 0]])
def clean_guys(self):
"""

View File

@@ -37,7 +37,7 @@ class IvolutionWindow(IvolutionTemplate, Observer, Observable):
Overrides init frame IvolutionTemplate
"""
IvolutionTemplate.__init__(self, parent)
Observer.__init__(self, title)
Observer.__init__(self, "Interface")
Observable.__init__(self)
# Sets up logging capability
@@ -108,7 +108,7 @@ class IvolutionWindow(IvolutionTemplate, Observer, Observable):
"""
self.my_logger.debug("Stop pressed")
self.console_logger.debug("Stop pressed")
self.notify(["STOP"]) # Asking the Facemovie to stop
self.notify(["Application", ["STOP"]]) # Asking the Facemovie to stop
self.process_running = False
#self.on_exit(event) # Finally shuts down the interface
@@ -155,7 +155,7 @@ class IvolutionWindow(IvolutionTemplate, Observer, Observable):
Called when the IvolutionWindow is closed, or File/Exit is called.
"""
# Clean up code for saving application state should be added here.
self.notify(["STOP"]) # Asking the Facemovie to stop
self.notify(["Application", ["STOP"]]) # Asking the Facemovie to stop
self.process_running = False
self.Close(True) # Close the frame.

View File

@@ -1,7 +1,7 @@
"""
.. module:: Notifier
:platform: Unix, Windows
:synopsis: Implements a simple Observer/Observable pattern for communication between between Facemovie thread and Ivolution GUI
:synopsis: Implements a simple Observer/Observable pattern for communication between between Facemovie thread and Ivolution GUI
.. moduleauthor:: Julien Lengrand-Lambert <jlengrand@gmail.com>
@@ -76,10 +76,3 @@ class Observable():
for observer in self.obs_collection:
#print "sent %s to %s" %(message, str(observer))
observer.update(message)
def set_val(self, val=1):
"""
"""
self.val += val
self.notify(str(self.val))