PageRenderTime 27ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/Python/Lib/site-packages/win32/lib/regutil.py

https://gitlab.com/orvi2014/rcs-db-ext
Python | 283 lines | 247 code | 12 blank | 24 comment | 14 complexity | 5cb5c65c6ca89ebd204707fa4877c813 MD5 | raw file
  1. # Some registry helpers.
  2. import win32api
  3. import win32con
  4. import sys
  5. import os
  6. error = "Registry utility error"
  7. # A .py file has a CLSID associated with it (why? - dunno!)
  8. CLSIDPyFile = "{b51df050-06ae-11cf-ad3b-524153480001}"
  9. RegistryIDPyFile = "Python.File" # The registry "file type" of a .py file
  10. RegistryIDPycFile = "Python.CompiledFile" # The registry "file type" of a .pyc file
  11. def GetRootKey():
  12. """Retrieves the Registry root in use by Python.
  13. """
  14. # Win32s no longer supported/released.
  15. # if win32ui.IsWin32s():
  16. # return win32con.HKEY_CLASSES_ROOT
  17. # else:
  18. return win32con.HKEY_LOCAL_MACHINE
  19. def GetRegistryDefaultValue(subkey, rootkey = None):
  20. """A helper to return the default value for a key in the registry.
  21. """
  22. if rootkey is None: rootkey = GetRootKey()
  23. return win32api.RegQueryValue(rootkey, subkey)
  24. def SetRegistryDefaultValue(subKey, value, rootkey = None):
  25. """A helper to set the default value for a key in the registry
  26. """
  27. if rootkey is None: rootkey = GetRootKey()
  28. if type(value)==str:
  29. typeId = win32con.REG_SZ
  30. elif type(value)==int:
  31. typeId = win32con.REG_DWORD
  32. else:
  33. raise TypeError("Value must be string or integer - was passed " + repr(value))
  34. win32api.RegSetValue(rootkey, subKey, typeId ,value)
  35. def BuildDefaultPythonKey():
  36. """Builds a string containing the path to the current registry key.
  37. The Python registry key contains the Python version. This function
  38. uses the version of the DLL used by the current process to get the
  39. registry key currently in use.
  40. """
  41. return "Software\\Python\\PythonCore\\" + sys.winver
  42. def GetAppPathsKey():
  43. return "Software\\Microsoft\\Windows\\CurrentVersion\\App Paths"
  44. def RegisterPythonExe(exeFullPath, exeAlias = None, exeAppPath = None):
  45. """Register a .exe file that uses Python.
  46. Registers the .exe with the OS. This allows the specified .exe to
  47. be run from the command-line or start button without using the full path,
  48. and also to setup application specific path (ie, os.environ['PATH']).
  49. Currently the exeAppPath is not supported, so this function is general
  50. purpose, and not specific to Python at all. Later, exeAppPath may provide
  51. a reasonable default that is used.
  52. exeFullPath -- The full path to the .exe
  53. exeAlias = None -- An alias for the exe - if none, the base portion
  54. of the filename is used.
  55. exeAppPath -- Not supported.
  56. """
  57. # Note - Dont work on win32s (but we dont care anymore!)
  58. if exeAppPath:
  59. raise error("Do not support exeAppPath argument currently")
  60. if exeAlias is None:
  61. exeAlias = os.path.basename(exeFullPath)
  62. win32api.RegSetValue(GetRootKey(), GetAppPathsKey() + "\\" + exeAlias, win32con.REG_SZ, exeFullPath)
  63. def GetRegisteredExe(exeAlias):
  64. """Get a registered .exe
  65. """
  66. return win32api.RegQueryValue(GetRootKey(), GetAppPathsKey() + "\\" + exeAlias)
  67. def UnregisterPythonExe(exeAlias):
  68. """Unregister a .exe file that uses Python.
  69. """
  70. try:
  71. win32api.RegDeleteKey(GetRootKey(), GetAppPathsKey() + "\\" + exeAlias)
  72. except win32api.error, exc:
  73. import winerror
  74. if exc.winerror!=winerror.ERROR_FILE_NOT_FOUND:
  75. raise
  76. return
  77. def RegisterNamedPath(name, path):
  78. """Register a named path - ie, a named PythonPath entry.
  79. """
  80. keyStr = BuildDefaultPythonKey() + "\\PythonPath"
  81. if name: keyStr = keyStr + "\\" + name
  82. win32api.RegSetValue(GetRootKey(), keyStr, win32con.REG_SZ, path)
  83. def UnregisterNamedPath(name):
  84. """Unregister a named path - ie, a named PythonPath entry.
  85. """
  86. keyStr = BuildDefaultPythonKey() + "\\PythonPath\\" + name
  87. try:
  88. win32api.RegDeleteKey(GetRootKey(), keyStr)
  89. except win32api.error, exc:
  90. import winerror
  91. if exc.winerror!=winerror.ERROR_FILE_NOT_FOUND:
  92. raise
  93. return
  94. def GetRegisteredNamedPath(name):
  95. """Get a registered named path, or None if it doesnt exist.
  96. """
  97. keyStr = BuildDefaultPythonKey() + "\\PythonPath"
  98. if name: keyStr = keyStr + "\\" + name
  99. try:
  100. return win32api.RegQueryValue(GetRootKey(), keyStr)
  101. except win32api.error, exc:
  102. import winerror
  103. if exc.winerror!=winerror.ERROR_FILE_NOT_FOUND:
  104. raise
  105. return None
  106. def RegisterModule(modName, modPath):
  107. """Register an explicit module in the registry. This forces the Python import
  108. mechanism to locate this module directly, without a sys.path search. Thus
  109. a registered module need not appear in sys.path at all.
  110. modName -- The name of the module, as used by import.
  111. modPath -- The full path and file name of the module.
  112. """
  113. try:
  114. import os
  115. os.stat(modPath)
  116. except os.error:
  117. print "Warning: Registering non-existant module %s" % modPath
  118. win32api.RegSetValue(GetRootKey(),
  119. BuildDefaultPythonKey() + "\\Modules\\%s" % modName,
  120. win32con.REG_SZ, modPath)
  121. def UnregisterModule(modName):
  122. """Unregister an explicit module in the registry.
  123. modName -- The name of the module, as used by import.
  124. """
  125. try:
  126. win32api.RegDeleteKey(GetRootKey(),
  127. BuildDefaultPythonKey() + "\\Modules\\%s" % modName)
  128. except win32api.error, exc:
  129. import winerror
  130. if exc.winerror!=winerror.ERROR_FILE_NOT_FOUND:
  131. raise
  132. def GetRegisteredHelpFile(helpDesc):
  133. """Given a description, return the registered entry.
  134. """
  135. try:
  136. return GetRegistryDefaultValue(BuildDefaultPythonKey() + "\\Help\\" + helpDesc)
  137. except win32api.error:
  138. try:
  139. return GetRegistryDefaultValue(BuildDefaultPythonKey() + "\\Help\\" + helpDesc, win32con.HKEY_CURRENT_USER)
  140. except win32api.error:
  141. pass
  142. return None
  143. def RegisterHelpFile(helpFile, helpPath, helpDesc = None, bCheckFile = 1):
  144. """Register a help file in the registry.
  145. Note that this used to support writing to the Windows Help
  146. key, however this is no longer done, as it seems to be incompatible.
  147. helpFile -- the base name of the help file.
  148. helpPath -- the path to the help file
  149. helpDesc -- A description for the help file. If None, the helpFile param is used.
  150. bCheckFile -- A flag indicating if the file existence should be checked.
  151. """
  152. if helpDesc is None: helpDesc = helpFile
  153. fullHelpFile = os.path.join(helpPath, helpFile)
  154. try:
  155. if bCheckFile: os.stat(fullHelpFile)
  156. except os.error:
  157. raise ValueError("Help file does not exist")
  158. # Now register with Python itself.
  159. win32api.RegSetValue(GetRootKey(),
  160. BuildDefaultPythonKey() + "\\Help\\%s" % helpDesc, win32con.REG_SZ, fullHelpFile)
  161. def UnregisterHelpFile(helpFile, helpDesc = None):
  162. """Unregister a help file in the registry.
  163. helpFile -- the base name of the help file.
  164. helpDesc -- A description for the help file. If None, the helpFile param is used.
  165. """
  166. key = win32api.RegOpenKey(win32con.HKEY_LOCAL_MACHINE, "Software\\Microsoft\\Windows\\Help", 0, win32con.KEY_ALL_ACCESS)
  167. try:
  168. try:
  169. win32api.RegDeleteValue(key, helpFile)
  170. except win32api.error, exc:
  171. import winerror
  172. if exc.winerror!=winerror.ERROR_FILE_NOT_FOUND:
  173. raise
  174. finally:
  175. win32api.RegCloseKey(key)
  176. # Now de-register with Python itself.
  177. if helpDesc is None: helpDesc = helpFile
  178. try:
  179. win32api.RegDeleteKey(GetRootKey(),
  180. BuildDefaultPythonKey() + "\\Help\\%s" % helpDesc)
  181. except win32api.error, exc:
  182. import winerror
  183. if exc.winerror!=winerror.ERROR_FILE_NOT_FOUND:
  184. raise
  185. def RegisterCoreDLL(coredllName = None):
  186. """Registers the core DLL in the registry.
  187. If no params are passed, the name of the Python DLL used in
  188. the current process is used and registered.
  189. """
  190. if coredllName is None:
  191. coredllName = win32api.GetModuleFileName(sys.dllhandle)
  192. # must exist!
  193. else:
  194. try:
  195. os.stat(coredllName)
  196. except os.error:
  197. print "Warning: Registering non-existant core DLL %s" % coredllName
  198. hKey = win32api.RegCreateKey(GetRootKey() , BuildDefaultPythonKey())
  199. try:
  200. win32api.RegSetValue(hKey, "Dll", win32con.REG_SZ, coredllName)
  201. finally:
  202. win32api.RegCloseKey(hKey)
  203. # Lastly, setup the current version to point to me.
  204. win32api.RegSetValue(GetRootKey(), "Software\\Python\\PythonCore\\CurrentVersion", win32con.REG_SZ, sys.winver)
  205. def RegisterFileExtensions(defPyIcon, defPycIcon, runCommand):
  206. """Register the core Python file extensions.
  207. defPyIcon -- The default icon to use for .py files, in 'fname,offset' format.
  208. defPycIcon -- The default icon to use for .pyc files, in 'fname,offset' format.
  209. runCommand -- The command line to use for running .py files
  210. """
  211. # Register the file extensions.
  212. pythonFileId = RegistryIDPyFile
  213. win32api.RegSetValue(win32con.HKEY_CLASSES_ROOT , ".py", win32con.REG_SZ, pythonFileId)
  214. win32api.RegSetValue(win32con.HKEY_CLASSES_ROOT , pythonFileId , win32con.REG_SZ, "Python File")
  215. win32api.RegSetValue(win32con.HKEY_CLASSES_ROOT , "%s\\CLSID" % pythonFileId , win32con.REG_SZ, CLSIDPyFile)
  216. win32api.RegSetValue(win32con.HKEY_CLASSES_ROOT , "%s\\DefaultIcon" % pythonFileId, win32con.REG_SZ, defPyIcon)
  217. base = "%s\\Shell" % RegistryIDPyFile
  218. win32api.RegSetValue(win32con.HKEY_CLASSES_ROOT , base + "\\Open", win32con.REG_SZ, "Run")
  219. win32api.RegSetValue(win32con.HKEY_CLASSES_ROOT , base + "\\Open\\Command", win32con.REG_SZ, runCommand)
  220. # Register the .PYC.
  221. pythonFileId = RegistryIDPycFile
  222. win32api.RegSetValue(win32con.HKEY_CLASSES_ROOT , ".pyc", win32con.REG_SZ, pythonFileId)
  223. win32api.RegSetValue(win32con.HKEY_CLASSES_ROOT , pythonFileId , win32con.REG_SZ, "Compiled Python File")
  224. win32api.RegSetValue(win32con.HKEY_CLASSES_ROOT , "%s\\DefaultIcon" % pythonFileId, win32con.REG_SZ, defPycIcon)
  225. base = "%s\\Shell" % pythonFileId
  226. win32api.RegSetValue(win32con.HKEY_CLASSES_ROOT , base + "\\Open", win32con.REG_SZ, "Run")
  227. win32api.RegSetValue(win32con.HKEY_CLASSES_ROOT , base + "\\Open\\Command", win32con.REG_SZ, runCommand)
  228. def RegisterShellCommand(shellCommand, exeCommand, shellUserCommand = None):
  229. # Last param for "Open" - for a .py file to be executed by the command line
  230. # or shell execute (eg, just entering "foo.py"), the Command must be "Open",
  231. # but you may associate a different name for the right-click menu.
  232. # In our case, normally we have "Open=Run"
  233. base = "%s\\Shell" % RegistryIDPyFile
  234. if shellUserCommand:
  235. win32api.RegSetValue(win32con.HKEY_CLASSES_ROOT , base + "\\%s" % (shellCommand), win32con.REG_SZ, shellUserCommand)
  236. win32api.RegSetValue(win32con.HKEY_CLASSES_ROOT , base + "\\%s\\Command" % (shellCommand), win32con.REG_SZ, exeCommand)
  237. def RegisterDDECommand(shellCommand, ddeApp, ddeTopic, ddeCommand):
  238. base = "%s\\Shell" % RegistryIDPyFile
  239. win32api.RegSetValue(win32con.HKEY_CLASSES_ROOT , base + "\\%s\\ddeexec" % (shellCommand), win32con.REG_SZ, ddeCommand)
  240. win32api.RegSetValue(win32con.HKEY_CLASSES_ROOT , base + "\\%s\\ddeexec\\Application" % (shellCommand), win32con.REG_SZ, ddeApp)
  241. win32api.RegSetValue(win32con.HKEY_CLASSES_ROOT , base + "\\%s\\ddeexec\\Topic" % (shellCommand), win32con.REG_SZ, ddeTopic)