# A demo plugin for Google Desktop Search (aka GDS) # by Hernan Foffani hfoffani@gmail.com # # This plugin acts as a COM server for GDS by handling the files we want # to index and as COM client when telling GDS about us and when we send # the file content back to GDS. No error processing is done to keep the # demo as readable as possible. # # To register the addin, simply execute: # gdsPlugin.py # This will install the COM server, and call the GDS methods # for registering the Plugin in GDS # # To unregister completely: # gdsPlugin.py --unregister # # To debug, execute # gdsPlugin.py --debug # then open Pythonwin, and select "Tools->Trace Collector Debugging Tool" # Restart GDS, and you should see some output. All the print statements # inside the COM server will be showed here too. # # The file extensions of the demo are invented for testing purposes. # Copy some textfile with a known content to C:\foobar.hernan wait a few # seconds and search for it in GDS. You should see that file at the top. from win32com.client import gencache import pywintypes import pythoncom # Read file content and metadata. import os, sys, time def processFile(path): mtime = os.stat(path).st_mtime content = file(path).read() return path, mtime, content # Specifics for GDS GDS_ICON = "no icon" GDS_FEXT = "hmf" # The file extensions that we index. # With the following call we obtain get a Python module that corresponds # to a COM object. The GUID corresponds to Google Desktop Search. # We use that object like a any other standard python module. gds = gencache.EnsureModule('{3D056FE7-EA8E-481A-B18F-0B02EBF6B3C1}', 0, 1, 0) # The following class IS our COM Server. Please DO NO reuse the same # GUID. You can create a GUID in python interpreter doing: # >>> import pythoncom; pythoncom.CreateGuid() TITLE = "PyGDS.SimpleAnytext" MYGUID = '{01C58A3B-18DB-4A5E-8F3E-3154C48DF4DE}' DESCRIPTION = "A text indexer in Python for GDS." class GdsIndexer: _public_methods_ = [ 'HandleFile' ] _reg_clsid_ = MYGUID _reg_clsctx_ = pythoncom.CLSCTX_INPROC_SERVER _reg_desc_ = DESCRIPTION _reg_progid_ = TITLE _reg_policy_spec_ = "win32com.server.policy.EventHandlerPolicy" def HandleFile(self, full_path_to_file, event_factory): print "HandleFile: ", full_path_to_file path, mtime, content = processFile(full_path_to_file) sendData(path, mtime, content, event_factory) return 0 # Send the file content and metadata to GDS. def sendData(path, mtime, content, event_factory): # Create the event gdsEvent = event_factory.CreateEvent(MYGUID, "Google.Desktop.TextFile"); # Fill the required properties. gdsEvent.AddProperty("format", "text/plain"); gdsEvent.AddProperty("content", content); gdsEvent.AddProperty("uri", path); gdsEvent.AddProperty("last_modified_time", pywintypes.Time(mtime)); # Send the event gdsEvent.Send(0x01); # COM server says hello to Google Desktop Search def DllRegisterServer(): reg = gds.GoogleDesktopSearchRegister() compDesc = ["Title",TITLE, "Description",DESCRIPTION, "Icon",GDS_ICON] registration = reg.RegisterComponent(MYGUID, compDesc) ret = registration.RegisterExtension(GDS_FEXT) # COM server says goodbye to Google Desktop Search def DllUnregisterServer(): reg = gds.GoogleDesktopSearchRegister() ret = reg.UnregisterComponent(MYGUID) if __name__ == '__main__': # 1st. (un)register the COM sever... import win32com.server.register win32com.server.register.UseCommandLine(GdsIndexer) # ... later (un)register the plugin in GDS if "--unregister" in sys.argv: DllUnregisterServer() else: DllRegisterServer()