Guest

Untitled

By: a guest on Nov 27th, 2008  |  syntax: Python  |  size: 2.64 KB  |  hits: 200  |  expires: Never
download  |  raw  |  embed  |  report abuse
Copied
  1. #! /usr/bin/env python
  2.  
  3. import gio
  4. import gtk
  5. import sqlite3
  6.  
  7. class Monitor(object):
  8.         def __init__(self, file):
  9.                
  10.                 # Setup Database
  11.                 self.db = sqlite3.connect(':memory:')
  12. #               self.db = sqlite3.connect('test.db')
  13.                
  14.                 self.create_table()
  15.                
  16.                 # Delete Everything that is in the database
  17.                 self.delete_all()
  18.                
  19.                 if self.is_directory(file):
  20.                         self.add_directory(file)
  21.                        
  22.         def add_directory(self, file):
  23.                 # Add the Change Listener
  24.                 mon = file.monitor_directory()
  25.                 mon.connect("changed", self.changed_event)
  26.                
  27.                 file.enumerate_children_async("*", self.enumerate_children_callback)
  28.                
  29.         def enumerate_children_callback(self, file, result):
  30.                 print file
  31.                 file_infos = file.enumerate_children_finish(result)
  32.                 print file_infos
  33.                 for file_info in file_infos:
  34.                         new_file = file.resolve_relative_path(file_info.get_name())
  35.                         if file_info.get_file_type() == gio.FILE_TYPE_DIRECTORY:
  36.                                 self.add_directory(new_file)
  37.                         else:
  38.                                 self.insert(new_file.get_basename(), new_file.get_path())
  39.                                
  40.         def insert(self, name, path):
  41.                 cursor = self.db.cursor()
  42. #               print "Name " + name
  43. #               print "Path " + path
  44.                
  45.                 cursor.execute('INSERT INTO files (name, path) VALUES ("%s", "%s")' % (name, path))
  46.                 self.db.commit()
  47.                
  48.         def delete(self, path):
  49.                 cursor = self.db.cursor()
  50. #               print "Path " + path
  51.                
  52.                 cursor.execute('DELETE FROM files WHERE path = "%s"' % (path))
  53.                 self.db.commit()
  54.                
  55.         def create_table(self):
  56.                 cursor = self.db.cursor()
  57.                 cursor.execute("CREATE TABLE files (id INTEGER PRIMARY KEY AUTOINCREMENT, name VARCHAR(255), path VARCHAR(255))")
  58.                 self.db.commit()
  59.        
  60.         def delete_all(self):
  61.                 cursor = self.db.cursor()
  62.                 cursor.execute("DELETE FROM files")
  63.                 self.db.commit()
  64.                
  65.         def is_directory(self, file):
  66.                 if file.query_exists():
  67.                         info = file.query_info("standard::type")
  68.                        
  69.                         # Check to make sure that the root file is a directory
  70.                         if info.get_file_type() == gio.FILE_TYPE_DIRECTORY:
  71.                                 return True
  72.                         else:
  73.                                 return False
  74.                                
  75.         def changed_event(self, monitor, file, unknown, event):
  76.                 info = None
  77.                
  78.                 if file.query_exists():
  79.                         info = file.query_info("standard::type")
  80.                        
  81.                         if info.get_file_type() == gio.FILE_TYPE_DIRECTORY:
  82.                                 mon = file.monitor_directory()
  83.                                 mon.connect("changed", self.changed_event)
  84.                        
  85.                 name = file.get_basename()
  86.                 if name.find('.gedit-save') < 0 and name.find('~') < 0:
  87. #                       print file.get_uri()
  88. #                       print event    
  89.        
  90.                         if gio.FILE_MONITOR_EVENT_CREATED == event:
  91.                                 self.insert(name, file.get_uri())
  92.                         elif gio.FILE_MONITOR_EVENT_DELETED == event:
  93.                                 self.delete(file.get_uri())
  94.  
  95. if __name__ == '__main__':
  96.         mon = Monitor(gio.File('file_folder'))
  97.         gtk.main()