column_provider.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. # A sample shell column provider
  2. # Mainly ported from MSDN article:
  3. # Using Shell Column Handlers for Detailed File Information,
  4. # Raymond Chen, Microsoft Corporation, February 2000
  5. #
  6. # To demostrate:
  7. # * Execute this script to register the namespace.
  8. # * Open Windows Explorer
  9. # * Right-click an explorer column header - select "More"
  10. # * Locate column 'pyc size' or 'pyo size', and add it to the view.
  11. # This handler is providing that column data.
  12. import sys, os, stat
  13. import pythoncom
  14. from win32com.shell import shell, shellcon
  15. import commctrl
  16. import winerror
  17. from win32com.server.util import wrap
  18. from pywintypes import IID
  19. IPersist_Methods = ["GetClassID"]
  20. IColumnProvider_Methods = IPersist_Methods + \
  21. ["Initialize", "GetColumnInfo", "GetItemData"]
  22. class ColumnProvider:
  23. _reg_progid_ = "Python.ShellExtension.ColumnProvider"
  24. _reg_desc_ = "Python Sample Shell Extension (Column Provider)"
  25. _reg_clsid_ = IID("{0F14101A-E05E-4070-BD54-83DFA58C3D68}")
  26. _com_interfaces_ = [pythoncom.IID_IPersist,
  27. shell.IID_IColumnProvider,
  28. ]
  29. _public_methods_ = IColumnProvider_Methods
  30. # IPersist
  31. def GetClassID(self):
  32. return self._reg_clsid_
  33. # IColumnProvider
  34. def Initialize(self, colInit):
  35. flags, reserved, name = colInit
  36. print "ColumnProvider initializing for file", name
  37. def GetColumnInfo(self, index):
  38. # We support exactly 2 columns - 'pyc size' and 'pyo size'
  39. if index in [0,1]:
  40. # As per the MSDN sample, use our CLSID as the fmtid
  41. if index==0:
  42. ext = ".pyc"
  43. else:
  44. ext = ".pyo"
  45. title = ext + " size"
  46. description = "Size of compiled %s file" % ext
  47. col_id = (self._reg_clsid_, # fmtid
  48. index) # pid
  49. col_info = (
  50. col_id, # scid
  51. pythoncom.VT_I4, # vt
  52. commctrl.LVCFMT_RIGHT, # fmt
  53. 20, #cChars
  54. shellcon.SHCOLSTATE_TYPE_INT | \
  55. shellcon.SHCOLSTATE_SECONDARYUI, # csFlags
  56. title,
  57. description)
  58. return col_info
  59. return None # Indicate no more columns.
  60. def GetItemData(self, colid, colData):
  61. fmt_id, pid = colid
  62. fmt_id==self._reg_clsid_
  63. flags, attr, reserved, ext, name = colData
  64. if ext.lower() not in [".py", ".pyw"]:
  65. return None
  66. if pid==0:
  67. ext = ".pyc"
  68. else:
  69. ext = ".pyo"
  70. check_file = os.path.splitext(name)[0] + ext
  71. try:
  72. st = os.stat(check_file)
  73. return st[stat.ST_SIZE]
  74. except OSError:
  75. # No file
  76. return None
  77. def DllRegisterServer():
  78. import _winreg
  79. # Special ColumnProvider key
  80. key = _winreg.CreateKey(_winreg.HKEY_CLASSES_ROOT,
  81. "Folder\\ShellEx\\ColumnHandlers\\" + \
  82. str(ColumnProvider._reg_clsid_ ))
  83. _winreg.SetValueEx(key, None, 0, _winreg.REG_SZ, ColumnProvider._reg_desc_)
  84. print ColumnProvider._reg_desc_, "registration complete."
  85. def DllUnregisterServer():
  86. import _winreg
  87. try:
  88. key = _winreg.DeleteKey(_winreg.HKEY_CLASSES_ROOT,
  89. "Folder\\ShellEx\\ColumnHandlers\\" + \
  90. str(ColumnProvider._reg_clsid_) )
  91. except WindowsError, details:
  92. import errno
  93. if details.errno != errno.ENOENT:
  94. raise
  95. print ColumnProvider._reg_desc_, "unregistration complete."
  96. if __name__=='__main__':
  97. from win32com.server import register
  98. register.UseCommandLine(ColumnProvider,
  99. finalize_register = DllRegisterServer,
  100. finalize_unregister = DllUnregisterServer)