diff mercurial/util_win32.py @ 6006:3c9dbb743d20

merge: add registry look up bits to tool search
author Matt Mackall <mpm@selenic.com>
date Sun, 03 Feb 2008 19:29:05 -0600
parents 821fc5c0656c
children e45de0f47215
line wrap: on
line diff
--- a/mercurial/util_win32.py	Sun Feb 03 19:29:05 2008 -0600
+++ b/mercurial/util_win32.py	Sun Feb 03 19:29:05 2008 -0600
@@ -187,6 +187,37 @@
         return details[0] != winerror.ERROR_INVALID_PARAMETER
     return True
 
+def lookup_reg(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).
+    '''
+    try:
+        from _winreg import HKEY_CURRENT_USER, HKEY_LOCAL_MACHINE, \
+            QueryValueEx, OpenKey
+    except ImportError:
+        return None
+
+    def query_val(scope, key):
+        try:
+            keyhandle = OpenKey(scope, key)
+            return QueryValueEx(keyhandle, valname)[0]
+        except EnvironmentError:
+            return None
+
+    if scope is None:
+        scope = (HKEY_CURRENT_USER, HKEY_LOCAL_MACHINE)
+    elif not isinstance(scope, (list, tuple)):
+        scope = (scope,)
+    for s in scope:
+        val = query_val(s, key, valname)
+        if val is not None:
+            return val
+
 def system_rcpath_win32():
     '''return default os-specific hgrc search path'''
     proc = win32api.GetCurrentProcess()