Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

regutil.py 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. # Some registry helpers.
  2. import os
  3. import sys
  4. import win32api
  5. import win32con
  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 BuildDefaultPythonKey():
  12. """Builds a string containing the path to the current registry key.
  13. The Python registry key contains the Python version. This function
  14. uses the version of the DLL used by the current process to get the
  15. registry key currently in use.
  16. """
  17. return "Software\\Python\\PythonCore\\" + sys.winver
  18. def GetRootKey():
  19. """Retrieves the Registry root in use by Python."""
  20. keyname = BuildDefaultPythonKey()
  21. try:
  22. k = win32api.RegOpenKey(win32con.HKEY_CURRENT_USER, keyname)
  23. k.close()
  24. return win32con.HKEY_CURRENT_USER
  25. except win32api.error:
  26. return win32con.HKEY_LOCAL_MACHINE
  27. def GetRegistryDefaultValue(subkey, rootkey=None):
  28. """A helper to return the default value for a key in the registry."""
  29. if rootkey is None:
  30. rootkey = GetRootKey()
  31. return win32api.RegQueryValue(rootkey, subkey)
  32. def SetRegistryDefaultValue(subKey, value, rootkey=None):
  33. """A helper to set the default value for a key in the registry"""
  34. if rootkey is None:
  35. rootkey = GetRootKey()
  36. if type(value) == str:
  37. typeId = win32con.REG_SZ
  38. elif type(value) == int:
  39. typeId = win32con.REG_DWORD
  40. else:
  41. raise TypeError("Value must be string or integer - was passed " + repr(value))
  42. win32api.RegSetValue(rootkey, subKey, typeId, value)
  43. def GetAppPathsKey():
  44. return "Software\\Microsoft\\Windows\\CurrentVersion\\App Paths"
  45. def RegisterPythonExe(exeFullPath, exeAlias=None, exeAppPath=None):
  46. """Register a .exe file that uses Python.
  47. Registers the .exe with the OS. This allows the specified .exe to
  48. be run from the command-line or start button without using the full path,
  49. and also to setup application specific path (ie, os.environ['PATH']).
  50. Currently the exeAppPath is not supported, so this function is general
  51. purpose, and not specific to Python at all. Later, exeAppPath may provide
  52. a reasonable default that is used.
  53. exeFullPath -- The full path to the .exe
  54. exeAlias = None -- An alias for the exe - if none, the base portion
  55. of the filename is used.
  56. exeAppPath -- Not supported.
  57. """
  58. # Note - Dont work on win32s (but we dont care anymore!)
  59. if exeAppPath:
  60. raise error("Do not support exeAppPath argument currently")
  61. if exeAlias is None:
  62. exeAlias = os.path.basename(exeFullPath)
  63. win32api.RegSetValue(
  64. GetRootKey(), GetAppPathsKey() + "\\" + exeAlias, win32con.REG_SZ, exeFullPath
  65. )
  66. def GetRegisteredExe(exeAlias):
  67. """Get a registered .exe"""
  68. return win32api.RegQueryValue(GetRootKey(), GetAppPathsKey() + "\\" + exeAlias)
  69. def UnregisterPythonExe(exeAlias):
  70. """Unregister a .exe file that uses Python."""
  71. try:
  72. win32api.RegDeleteKey(GetRootKey(), GetAppPathsKey() + "\\" + exeAlias)
  73. except win32api.error as exc:
  74. import winerror
  75. if exc.winerror != winerror.ERROR_FILE_NOT_FOUND:
  76. raise
  77. return
  78. def RegisterNamedPath(name, path):
  79. """Register a named path - ie, a named PythonPath entry."""
  80. keyStr = BuildDefaultPythonKey() + "\\PythonPath"
  81. if name:
  82. keyStr = keyStr + "\\" + name
  83. win32api.RegSetValue(GetRootKey(), keyStr, win32con.REG_SZ, path)
  84. def UnregisterNamedPath(name):
  85. """Unregister a named path - ie, a named PythonPath entry."""
  86. keyStr = BuildDefaultPythonKey() + "\\PythonPath\\" + name
  87. try:
  88. win32api.RegDeleteKey(GetRootKey(), keyStr)
  89. except win32api.error as 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. keyStr = BuildDefaultPythonKey() + "\\PythonPath"
  97. if name:
  98. keyStr = keyStr + "\\" + name
  99. try:
  100. return win32api.RegQueryValue(GetRootKey(), keyStr)
  101. except win32api.error as 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(
  119. GetRootKey(),
  120. BuildDefaultPythonKey() + "\\Modules\\%s" % modName,
  121. win32con.REG_SZ,
  122. modPath,
  123. )
  124. def UnregisterModule(modName):
  125. """Unregister an explicit module in the registry.
  126. modName -- The name of the module, as used by import.
  127. """
  128. try:
  129. win32api.RegDeleteKey(
  130. GetRootKey(), BuildDefaultPythonKey() + "\\Modules\\%s" % modName
  131. )
  132. except win32api.error as exc:
  133. import winerror
  134. if exc.winerror != winerror.ERROR_FILE_NOT_FOUND:
  135. raise
  136. def GetRegisteredHelpFile(helpDesc):
  137. """Given a description, return the registered entry."""
  138. try:
  139. return GetRegistryDefaultValue(BuildDefaultPythonKey() + "\\Help\\" + helpDesc)
  140. except win32api.error:
  141. try:
  142. return GetRegistryDefaultValue(
  143. BuildDefaultPythonKey() + "\\Help\\" + helpDesc,
  144. win32con.HKEY_CURRENT_USER,
  145. )
  146. except win32api.error:
  147. pass
  148. return None
  149. def RegisterHelpFile(helpFile, helpPath, helpDesc=None, bCheckFile=1):
  150. """Register a help file in the registry.
  151. Note that this used to support writing to the Windows Help
  152. key, however this is no longer done, as it seems to be incompatible.
  153. helpFile -- the base name of the help file.
  154. helpPath -- the path to the help file
  155. helpDesc -- A description for the help file. If None, the helpFile param is used.
  156. bCheckFile -- A flag indicating if the file existence should be checked.
  157. """
  158. if helpDesc is None:
  159. helpDesc = helpFile
  160. fullHelpFile = os.path.join(helpPath, helpFile)
  161. try:
  162. if bCheckFile:
  163. os.stat(fullHelpFile)
  164. except os.error:
  165. raise ValueError("Help file does not exist")
  166. # Now register with Python itself.
  167. win32api.RegSetValue(
  168. GetRootKey(),
  169. BuildDefaultPythonKey() + "\\Help\\%s" % helpDesc,
  170. win32con.REG_SZ,
  171. fullHelpFile,
  172. )
  173. def UnregisterHelpFile(helpFile, helpDesc=None):
  174. """Unregister a help file in the registry.
  175. helpFile -- the base name of the help file.
  176. helpDesc -- A description for the help file. If None, the helpFile param is used.
  177. """
  178. key = win32api.RegOpenKey(
  179. win32con.HKEY_LOCAL_MACHINE,
  180. "Software\\Microsoft\\Windows\\Help",
  181. 0,
  182. win32con.KEY_ALL_ACCESS,
  183. )
  184. try:
  185. try:
  186. win32api.RegDeleteValue(key, helpFile)
  187. except win32api.error as exc:
  188. import winerror
  189. if exc.winerror != winerror.ERROR_FILE_NOT_FOUND:
  190. raise
  191. finally:
  192. win32api.RegCloseKey(key)
  193. # Now de-register with Python itself.
  194. if helpDesc is None:
  195. helpDesc = helpFile
  196. try:
  197. win32api.RegDeleteKey(
  198. GetRootKey(), BuildDefaultPythonKey() + "\\Help\\%s" % helpDesc
  199. )
  200. except win32api.error as exc:
  201. import winerror
  202. if exc.winerror != winerror.ERROR_FILE_NOT_FOUND:
  203. raise
  204. def RegisterCoreDLL(coredllName=None):
  205. """Registers the core DLL in the registry.
  206. If no params are passed, the name of the Python DLL used in
  207. the current process is used and registered.
  208. """
  209. if coredllName is None:
  210. coredllName = win32api.GetModuleFileName(sys.dllhandle)
  211. # must exist!
  212. else:
  213. try:
  214. os.stat(coredllName)
  215. except os.error:
  216. print("Warning: Registering non-existant core DLL %s" % coredllName)
  217. hKey = win32api.RegCreateKey(GetRootKey(), BuildDefaultPythonKey())
  218. try:
  219. win32api.RegSetValue(hKey, "Dll", win32con.REG_SZ, coredllName)
  220. finally:
  221. win32api.RegCloseKey(hKey)
  222. # Lastly, setup the current version to point to me.
  223. win32api.RegSetValue(
  224. GetRootKey(),
  225. "Software\\Python\\PythonCore\\CurrentVersion",
  226. win32con.REG_SZ,
  227. sys.winver,
  228. )
  229. def RegisterFileExtensions(defPyIcon, defPycIcon, runCommand):
  230. """Register the core Python file extensions.
  231. defPyIcon -- The default icon to use for .py files, in 'fname,offset' format.
  232. defPycIcon -- The default icon to use for .pyc files, in 'fname,offset' format.
  233. runCommand -- The command line to use for running .py files
  234. """
  235. # Register the file extensions.
  236. pythonFileId = RegistryIDPyFile
  237. win32api.RegSetValue(
  238. win32con.HKEY_CLASSES_ROOT, ".py", win32con.REG_SZ, pythonFileId
  239. )
  240. win32api.RegSetValue(
  241. win32con.HKEY_CLASSES_ROOT, pythonFileId, win32con.REG_SZ, "Python File"
  242. )
  243. win32api.RegSetValue(
  244. win32con.HKEY_CLASSES_ROOT,
  245. "%s\\CLSID" % pythonFileId,
  246. win32con.REG_SZ,
  247. CLSIDPyFile,
  248. )
  249. win32api.RegSetValue(
  250. win32con.HKEY_CLASSES_ROOT,
  251. "%s\\DefaultIcon" % pythonFileId,
  252. win32con.REG_SZ,
  253. defPyIcon,
  254. )
  255. base = "%s\\Shell" % RegistryIDPyFile
  256. win32api.RegSetValue(
  257. win32con.HKEY_CLASSES_ROOT, base + "\\Open", win32con.REG_SZ, "Run"
  258. )
  259. win32api.RegSetValue(
  260. win32con.HKEY_CLASSES_ROOT,
  261. base + "\\Open\\Command",
  262. win32con.REG_SZ,
  263. runCommand,
  264. )
  265. # Register the .PYC.
  266. pythonFileId = RegistryIDPycFile
  267. win32api.RegSetValue(
  268. win32con.HKEY_CLASSES_ROOT, ".pyc", win32con.REG_SZ, pythonFileId
  269. )
  270. win32api.RegSetValue(
  271. win32con.HKEY_CLASSES_ROOT,
  272. pythonFileId,
  273. win32con.REG_SZ,
  274. "Compiled Python File",
  275. )
  276. win32api.RegSetValue(
  277. win32con.HKEY_CLASSES_ROOT,
  278. "%s\\DefaultIcon" % pythonFileId,
  279. win32con.REG_SZ,
  280. defPycIcon,
  281. )
  282. base = "%s\\Shell" % pythonFileId
  283. win32api.RegSetValue(
  284. win32con.HKEY_CLASSES_ROOT, base + "\\Open", win32con.REG_SZ, "Run"
  285. )
  286. win32api.RegSetValue(
  287. win32con.HKEY_CLASSES_ROOT,
  288. base + "\\Open\\Command",
  289. win32con.REG_SZ,
  290. runCommand,
  291. )
  292. def RegisterShellCommand(shellCommand, exeCommand, shellUserCommand=None):
  293. # Last param for "Open" - for a .py file to be executed by the command line
  294. # or shell execute (eg, just entering "foo.py"), the Command must be "Open",
  295. # but you may associate a different name for the right-click menu.
  296. # In our case, normally we have "Open=Run"
  297. base = "%s\\Shell" % RegistryIDPyFile
  298. if shellUserCommand:
  299. win32api.RegSetValue(
  300. win32con.HKEY_CLASSES_ROOT,
  301. base + "\\%s" % (shellCommand),
  302. win32con.REG_SZ,
  303. shellUserCommand,
  304. )
  305. win32api.RegSetValue(
  306. win32con.HKEY_CLASSES_ROOT,
  307. base + "\\%s\\Command" % (shellCommand),
  308. win32con.REG_SZ,
  309. exeCommand,
  310. )
  311. def RegisterDDECommand(shellCommand, ddeApp, ddeTopic, ddeCommand):
  312. base = "%s\\Shell" % RegistryIDPyFile
  313. win32api.RegSetValue(
  314. win32con.HKEY_CLASSES_ROOT,
  315. base + "\\%s\\ddeexec" % (shellCommand),
  316. win32con.REG_SZ,
  317. ddeCommand,
  318. )
  319. win32api.RegSetValue(
  320. win32con.HKEY_CLASSES_ROOT,
  321. base + "\\%s\\ddeexec\\Application" % (shellCommand),
  322. win32con.REG_SZ,
  323. ddeApp,
  324. )
  325. win32api.RegSetValue(
  326. win32con.HKEY_CLASSES_ROOT,
  327. base + "\\%s\\ddeexec\\Topic" % (shellCommand),
  328. win32con.REG_SZ,
  329. ddeTopic,
  330. )