PyXR

c:\python24\lib\site-packages\win32\lib \ regutil.py



0001 # Some registry helpers.
0002 import win32api
0003 import win32con
0004 import sys
0005 import os
0006 
0007 error = "Registry utility error"
0008 
0009 # A .py file has a CLSID associated with it (why? - dunno!)
0010 CLSIDPyFile = "{b51df050-06ae-11cf-ad3b-524153480001}"
0011 
0012 RegistryIDPyFile = "Python.File" # The registry "file type" of a .py file
0013 RegistryIDPycFile = "Python.CompiledFile" # The registry "file type" of a .pyc file
0014 
0015 def GetRootKey():
0016         """Retrieves the Registry root in use by Python.
0017         """
0018 # Win32s no longer supported/released.
0019 # if win32ui.IsWin32s():
0020 #         return win32con.HKEY_CLASSES_ROOT
0021 # else:
0022         return win32con.HKEY_LOCAL_MACHINE
0023 
0024 def GetRegistryDefaultValue(subkey, rootkey = None):
0025         """A helper to return the default value for a key in the registry.
0026         """
0027         if rootkey is None: rootkey = GetRootKey()
0028         return win32api.RegQueryValue(rootkey, subkey)
0029 
0030 def SetRegistryDefaultValue(subKey, value, rootkey = None):
0031         """A helper to set the default value for a key in the registry
0032         """
0033         import types
0034         if rootkey is None: rootkey = GetRootKey()
0035         if type(value)==types.StringType:
0036                 typeId = win32con.REG_SZ
0037         elif type(value)==types.IntType:
0038                 typeId = win32con.REG_DWORD
0039         else:
0040                 raise TypeError, "Value must be string or integer - was passed " + str(value)
0041 
0042         win32api.RegSetValue(rootkey, subKey, typeId ,value)
0043         
0044 def BuildDefaultPythonKey():
0045         """Builds a string containing the path to the current registry key.
0046 
0047            The Python registry key contains the Python version.  This function
0048            uses the version of the DLL used by the current process to get the
0049            registry key currently in use.
0050         """
0051 
0052         return "Software\\Python\\PythonCore\\" + sys.winver
0053 
0054 def GetAppPathsKey():
0055         return "Software\\Microsoft\\Windows\\CurrentVersion\\App Paths"
0056 
0057 def RegisterPythonExe(exeFullPath, exeAlias = None, exeAppPath = None):
0058         """Register a .exe file that uses Python.
0059 
0060            Registers the .exe with the OS.  This allows the specified .exe to
0061            be run from the command-line or start button without using the full path,
0062            and also to setup application specific path (ie, os.environ['PATH']).
0063 
0064            Currently the exeAppPath is not supported, so this function is general
0065            purpose, and not specific to Python at all.  Later, exeAppPath may provide
0066            a reasonable default that is used.
0067 
0068            exeFullPath -- The full path to the .exe
0069            exeAlias = None -- An alias for the exe - if none, the base portion
0070                      of the filename is used.
0071            exeAppPath -- Not supported.
0072         """
0073         # Note - Dont work on win32s (but we dont care anymore!)
0074         if exeAppPath:
0075                 raise error, "Do not support exeAppPath argument currently"
0076         if exeAlias is None:
0077                 exeAlias = os.path.basename(exeFullPath)
0078         win32api.RegSetValue(GetRootKey(), GetAppPathsKey() + "\\" + exeAlias, win32con.REG_SZ, exeFullPath)
0079 
0080 def GetRegisteredExe(exeAlias):
0081         """Get a registered .exe
0082         """
0083         return win32api.RegQueryValue(GetRootKey(), GetAppPathsKey() + "\\" + exeAlias)
0084 
0085 def UnregisterPythonExe(exeAlias):
0086         """Unregister a .exe file that uses Python.
0087         """
0088         try:
0089                 win32api.RegDeleteKey(GetRootKey(), GetAppPathsKey() + "\\" + exeAlias)
0090         except win32api.error, (code, fn, details):
0091                 import winerror
0092                 if code!=winerror.ERROR_FILE_NOT_FOUND:
0093                         raise win32api.error, (code, fn, desc)
0094                 return
0095 
0096 def RegisterNamedPath(name, path):
0097         """Register a named path - ie, a named PythonPath entry.
0098         """
0099         keyStr = BuildDefaultPythonKey() + "\\PythonPath"
0100         if name: keyStr = keyStr + "\\" + name
0101         win32api.RegSetValue(GetRootKey(), keyStr, win32con.REG_SZ, path)
0102 
0103 def UnregisterNamedPath(name):
0104         """Unregister a named path - ie, a named PythonPath entry.
0105         """
0106         keyStr = BuildDefaultPythonKey() + "\\PythonPath\\" + name
0107         try:
0108                 win32api.RegDeleteKey(GetRootKey(), keyStr)
0109         except win32api.error, (code, fn, details):
0110                 import winerror
0111                 if code!=winerror.ERROR_FILE_NOT_FOUND:
0112                         raise win32api.error, (code, fn, desc)
0113                 return
0114 
0115 def GetRegisteredNamedPath(name):
0116         """Get a registered named path, or None if it doesnt exist.
0117         """
0118         keyStr = BuildDefaultPythonKey() + "\\PythonPath"
0119         if name: keyStr = keyStr + "\\" + name
0120         try:
0121                 return win32api.RegQueryValue(GetRootKey(), keyStr)
0122         except win32api.error, (code, fn, details):
0123                 import winerror
0124                 if code!=winerror.ERROR_FILE_NOT_FOUND:
0125                         raise win32api.error, (code, fn, details)
0126                 return None
0127 
0128 
0129 def RegisterModule(modName, modPath):
0130         """Register an explicit module in the registry.  This forces the Python import
0131            mechanism to locate this module directly, without a sys.path search.  Thus
0132            a registered module need not appear in sys.path at all.
0133 
0134            modName -- The name of the module, as used by import.
0135            modPath -- The full path and file name of the module.
0136         """
0137         try:
0138                 import os
0139                 os.stat(modPath)
0140         except os.error:
0141                 print "Warning: Registering non-existant module %s" % modPath
0142         win32api.RegSetValue(GetRootKey(), 
0143                              BuildDefaultPythonKey() + "\\Modules\\%s" % modName,
0144                 win32con.REG_SZ, modPath)
0145 
0146 def UnregisterModule(modName):
0147         """Unregister an explicit module in the registry.
0148 
0149            modName -- The name of the module, as used by import.
0150         """
0151         try:
0152                 win32api.RegDeleteKey(GetRootKey(), 
0153                                      BuildDefaultPythonKey() + "\\Modules\\%s" % modName)
0154         except win32api.error, (code, fn, desc):
0155                 import winerror
0156                 if code!=winerror.ERROR_FILE_NOT_FOUND:
0157                         raise win32api.error, (code, fn, desc)
0158 
0159 def GetRegisteredHelpFile(helpDesc):
0160         """Given a description, return the registered entry.
0161         """
0162         try:
0163                 return GetRegistryDefaultValue(BuildDefaultPythonKey() + "\\Help\\" + helpDesc)
0164         except win32api.error:
0165                 try:
0166                         return GetRegistryDefaultValue(BuildDefaultPythonKey() + "\\Help\\" + helpDesc, win32con.HKEY_CURRENT_USER)
0167                 except win32api.error:
0168                         pass
0169         return None
0170 
0171 def RegisterHelpFile(helpFile, helpPath, helpDesc = None, bCheckFile = 1):
0172         """Register a help file in the registry.
0173         
0174              Note that this used to support writing to the Windows Help
0175              key, however this is no longer done, as it seems to be incompatible.
0176 
0177            helpFile -- the base name of the help file.
0178            helpPath -- the path to the help file
0179            helpDesc -- A description for the help file.  If None, the helpFile param is used.
0180            bCheckFile -- A flag indicating if the file existence should be checked.
0181         """
0182         if helpDesc is None: helpDesc = helpFile
0183         fullHelpFile = os.path.join(helpPath, helpFile)
0184         try:
0185                 if bCheckFile: os.stat(fullHelpFile)
0186         except os.error:
0187                 raise ValueError, "Help file does not exist"
0188         # Now register with Python itself.
0189         win32api.RegSetValue(GetRootKey(), 
0190                              BuildDefaultPythonKey() + "\\Help\\%s" % helpDesc, win32con.REG_SZ, fullHelpFile)
0191 
0192 def UnregisterHelpFile(helpFile, helpDesc = None):
0193         """Unregister a help file in the registry.
0194 
0195            helpFile -- the base name of the help file.
0196            helpDesc -- A description for the help file.  If None, the helpFile param is used.
0197         """
0198         key = win32api.RegOpenKey(win32con.HKEY_LOCAL_MACHINE, "Software\\Microsoft\\Windows\\Help", 0, win32con.KEY_ALL_ACCESS)
0199         try:
0200                 try:
0201                         win32api.RegDeleteValue(key, helpFile)
0202                 except win32api.error, (code, fn, desc):
0203                         import winerror
0204                         if code!=winerror.ERROR_FILE_NOT_FOUND:
0205                                 raise win32api.error, (code, fn, desc)
0206         finally:
0207                 win32api.RegCloseKey(key)
0208         
0209         # Now de-register with Python itself.
0210         if helpDesc is None: helpDesc = helpFile
0211         try:
0212                 win32api.RegDeleteKey(GetRootKey(), 
0213                                      BuildDefaultPythonKey() + "\\Help\\%s" % helpDesc)        
0214         except win32api.error, (code, fn, desc):
0215                 import winerror
0216                 if code!=winerror.ERROR_FILE_NOT_FOUND:
0217                         raise win32api.error, (code, fn, desc)
0218 
0219 def RegisterCoreDLL(coredllName = None):
0220         """Registers the core DLL in the registry.
0221 
0222         If no params are passed, the name of the Python DLL used in 
0223         the current process is used and registered.
0224         """
0225         if coredllName is None:
0226                 coredllName = win32api.GetModuleFileName(sys.dllhandle)
0227                 # must exist!
0228         else:
0229                 try:
0230                         os.stat(coredllName)
0231                 except os.error:
0232                         print "Warning: Registering non-existant core DLL %s" % coredllName
0233 
0234         hKey = win32api.RegCreateKey(GetRootKey() , BuildDefaultPythonKey())
0235         try:
0236                 win32api.RegSetValue(hKey, "Dll", win32con.REG_SZ, coredllName)
0237         finally:
0238                 win32api.RegCloseKey(hKey)
0239         # Lastly, setup the current version to point to me.
0240         win32api.RegSetValue(GetRootKey(), "Software\\Python\\PythonCore\\CurrentVersion", win32con.REG_SZ, sys.winver)
0241 
0242 def RegisterFileExtensions(defPyIcon, defPycIcon, runCommand):
0243         """Register the core Python file extensions.
0244         
0245            defPyIcon -- The default icon to use for .py files, in 'fname,offset' format.
0246            defPycIcon -- The default icon to use for .pyc files, in 'fname,offset' format.
0247            runCommand -- The command line to use for running .py files
0248         """
0249         # Register the file extensions.
0250         pythonFileId = RegistryIDPyFile
0251         win32api.RegSetValue(win32con.HKEY_CLASSES_ROOT , ".py", win32con.REG_SZ, pythonFileId)
0252         win32api.RegSetValue(win32con.HKEY_CLASSES_ROOT , pythonFileId , win32con.REG_SZ, "Python File")
0253         win32api.RegSetValue(win32con.HKEY_CLASSES_ROOT , "%s\\CLSID" % pythonFileId , win32con.REG_SZ, CLSIDPyFile)
0254         win32api.RegSetValue(win32con.HKEY_CLASSES_ROOT , "%s\\DefaultIcon" % pythonFileId, win32con.REG_SZ, defPyIcon)
0255         base = "%s\\Shell" % RegistryIDPyFile
0256         win32api.RegSetValue(win32con.HKEY_CLASSES_ROOT , base + "\\Open", win32con.REG_SZ, "Run")
0257         win32api.RegSetValue(win32con.HKEY_CLASSES_ROOT , base + "\\Open\\Command", win32con.REG_SZ, runCommand)
0258 
0259         # Register the .PYC.
0260         pythonFileId = RegistryIDPycFile
0261         win32api.RegSetValue(win32con.HKEY_CLASSES_ROOT , ".pyc", win32con.REG_SZ, pythonFileId)
0262         win32api.RegSetValue(win32con.HKEY_CLASSES_ROOT , pythonFileId , win32con.REG_SZ, "Compiled Python File")
0263         win32api.RegSetValue(win32con.HKEY_CLASSES_ROOT , "%s\\DefaultIcon" % pythonFileId, win32con.REG_SZ, defPycIcon)
0264         base = "%s\\Shell" % pythonFileId
0265         win32api.RegSetValue(win32con.HKEY_CLASSES_ROOT , base + "\\Open", win32con.REG_SZ, "Run")
0266         win32api.RegSetValue(win32con.HKEY_CLASSES_ROOT , base + "\\Open\\Command", win32con.REG_SZ, runCommand)
0267 
0268 def RegisterShellCommand(shellCommand, exeCommand, shellUserCommand = None):
0269         # Last param for "Open" - for a .py file to be executed by the command line
0270         # or shell execute (eg, just entering "foo.py"), the Command must be "Open",
0271         # but you may associate a different name for the right-click menu.
0272         # In our case, normally we have "Open=Run"
0273         base = "%s\\Shell" % RegistryIDPyFile
0274         if shellUserCommand:
0275                 win32api.RegSetValue(win32con.HKEY_CLASSES_ROOT , base + "\\%s" % (shellCommand), win32con.REG_SZ, shellUserCommand)
0276 
0277         win32api.RegSetValue(win32con.HKEY_CLASSES_ROOT , base + "\\%s\\Command" % (shellCommand), win32con.REG_SZ, exeCommand)
0278 
0279 def RegisterDDECommand(shellCommand, ddeApp, ddeTopic, ddeCommand):
0280         base = "%s\\Shell" % RegistryIDPyFile
0281         win32api.RegSetValue(win32con.HKEY_CLASSES_ROOT , base + "\\%s\\ddeexec" % (shellCommand), win32con.REG_SZ, ddeCommand)
0282         win32api.RegSetValue(win32con.HKEY_CLASSES_ROOT , base + "\\%s\\ddeexec\\Application" % (shellCommand), win32con.REG_SZ, ddeApp)
0283         win32api.RegSetValue(win32con.HKEY_CLASSES_ROOT , base + "\\%s\\ddeexec\\Topic" % (shellCommand), win32con.REG_SZ, ddeTopic)
0284 
0285 

Generated by PyXR 0.9.4
SourceForge.net Logo