Commits

Jason S committed df3b884

got md5 processing working

Comments (0)

Files changed (6)

 syntax: glob
 .cache
-submit
+submit
+**.pyc
 @author: jmsachs
 '''
 
+import bookcasedb
+import time
+import sys
+
 if __name__ == '__main__':
-    pass
+    processor = bookcasedb.Processor(sys.argv[1])
+    processor.start()
+    try:
+        while True:
+            time.sleep(1)
+    except KeyboardInterrupt:
+        processor.stop()
+    processor.join()

bookcasedb/__init__.py

+from processor import Processor

bookcasedb/md5plus.py

         return (self.h.digest(), self.hx.digest())
     def hexdigest(self):
         return (self.h.hexdigest(), self.hx.hexdigest())
+    @staticmethod
+    def processFile(f):
+        h = Md5Plus()
+        while True:
+            buf = f.read(65536)
+            if len(buf) == 0:
+                break
+            h.update(buf)
+        h.flush()
+        return h
         
 if __name__ == '__main__':
     g = mwc1616()
     print hsha512.hexdigest()
     
     print p.hexdigest()
-    print h.hexdigest()
+    print h.hexdigest()
+    
+    with open('/Users/jmsachs/Documents/python/bookcasedb/submit/Screen shot 2012-12-31 at 6.54.22 PM.png','rb') as f:
+        print Md5Plus.processFile(f).hexdigest()
+        

bookcasedb/processor.py

+'''
+Created on May 25, 2013
+
+@author: jmsachs
+'''
+
+from watchdog.observers import Observer
+from watchdog.events import FileSystemEventHandler
+import time
+import md5plus
+
+class NewFileHandler(FileSystemEventHandler):
+    def __init__(self,f):
+        self.f = f
+    def on_modified(self, event):
+        if not event.is_directory:
+            self.f(event.src_path)
+#    def on_any_event(self, event):
+#        print event
+
+class Processor(Observer):
+    def __init__(self, path):
+        Observer.__init__(self)
+        self.path = path
+        def f(path):
+            self.onNewFile(path)
+        self.handler = NewFileHandler(f)
+        self.schedule(self.handler, self.path, recursive=True)
+    def onNewFile(self, filepath):
+        print "new file at %s" % filepath
+        with open(filepath,'rb') as f:
+            h = md5plus.Md5Plus.processFile(f)
+            print h.hexdigest()
+        print "finished processing %s" % filepath

sandbox/watchdogtest.py

 class MyEventHandler(FileSystemEventHandler):
     def __init__(self):
         pass
-    def on_created(self, event):
+    def on_modified(self, event):
         if not event.is_directory:
             print "e=", event
             print "file created"