diff mercurial/windows.py @ 16807:80142f385af9

win32: move lookupreg() to windows.py lookupreg() doesn't use the win32 API directly any more, it uses the Python standard library module _winreg.
author Adrian Buehlmann <adrian@cadifra.com>
date Sun, 27 May 2012 11:29:52 +0200
parents e34106fa0dc3
children 0cb55b5c19a3
line wrap: on
line diff
--- a/mercurial/windows.py	Sun May 27 11:29:45 2012 +0200
+++ b/mercurial/windows.py	Sun May 27 11:29:52 2012 +0200
@@ -6,14 +6,13 @@
 # GNU General Public License version 2 or any later version.
 
 from i18n import _
-import osutil
-import errno, msvcrt, os, re, sys
+import osutil, encoding
+import errno, msvcrt, os, re, sys, _winreg
 
 import win32
 executablepath = win32.executablepath
 getuser = win32.getuser
 hidewindow = win32.hidewindow
-lookupreg = win32.lookupreg
 makedir = win32.makedir
 nlinks = win32.nlinks
 oslink = win32.oslink
@@ -316,4 +315,25 @@
     def cacheable(self):
         return False
 
+def lookupreg(key, valname=None, scope=None):
+    ''' Look up a key/value name in the Windows registry.
+
+    valname: value name. If unspecified, the default value for the key
+    is used.
+    scope: optionally specify scope for registry lookup, this can be
+    a sequence of scopes to look up in order. Default (CURRENT_USER,
+    LOCAL_MACHINE).
+    '''
+    if scope is None:
+        scope = (_winreg.HKEY_CURRENT_USER, _winreg.HKEY_LOCAL_MACHINE)
+    elif not isinstance(scope, (list, tuple)):
+        scope = (scope,)
+    for s in scope:
+        try:
+            val = _winreg.QueryValueEx(_winreg.OpenKey(s, key), valname)[0]
+            # never let a Unicode string escape into the wild
+            return encoding.tolocal(val.encode('UTF-8'))
+        except EnvironmentError:
+            pass
+
 expandglobs = True