1
2
3 """Functions for getting system/language/user dependent paths on windows.
4
5 All path names returned by the functions of this module are unicode strings.
6 """
7
8 __all__ = [
9 'HKCU', 'HKLM',
10 'SHELL_FOLDERS',
11 'USER_SHELL_FOLDERS',
12 'expandvars',
13 'get_appdata',
14 'get_common_shellfolders',
15 'get_homedir',
16 'get_sharedconf',
17 'get_shellfolders',
18 'get_userconf',
19 'get_windir'
20 ]
21
22 __module__ = "winpaths"
23 __author__ = "Christopher Arndt"
24 __version__ = "0.1"
25 __revision__ = "$Rev$"
26 __date__ = "$Date$"
27 __copyright__ = "Python license"
28
29
30 import _winreg, os
31
32 SHELL_FOLDERS = \
33 r'Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders'
34 USER_SHELL_FOLDERS = \
35 r'Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders'
36 HKCU = _winreg.HKEY_CURRENT_USER
37 HKLM = _winreg.HKEY_LOCAL_MACHINE
38
39
41 return os.environ.get(m.group(1), m.group(0))
42
43 _env_rx = None
45 """Expand environment variables of form %var%.
46
47 Unknown variables are left unchanged.
48 """
49
50 global _env_rx
51
52 if '%' not in s:
53 return s
54 if _env_rx is None:
55 import re
56 _env_rx = re.compile(r'%([^|<>=^%]+)%')
57 return _env_rx.sub(_substenv, s)
58
60 """Return registry value specified by key, subkey, and name.
61
62 Environment variables in values of type REG_EXPAND_SZ are expanded
63 if possible.
64 """
65
66 key = _winreg.OpenKey(key, subkey)
67 try:
68 ret = _winreg.QueryValueEx(key, name)
69 except WindowsError:
70 return None
71 else:
72 key.Close()
73 if ret[1] == _winreg.REG_EXPAND_SZ:
74 return expandvars(ret[0])
75 else:
76 return ret[0]
77
79 """Return a windows registry value from the CURRENT_USER branch."""
80
81 return _get_reg_value(HKCU, key, name)
82
84 """Return a windows registry value from the LOCAL_MACHINE branch."""
85
86 return _get_reg_value(HKLM, key, name)
87
88
93
95 """Return mapping of shell folder names (all users) to paths."""
96
97 return get_shellfolders(branch=HKLM)
98
103
105 """Return path to shared configuration data for 'prog' from 'vendor'.
106
107 Additional arguments are appended via os.path.join().
108
109 See also: get_user_conf()
110 """
111
112 return os.path.join(
113 _get_reg_machine_value(SHELL_FOLDERS, 'Common AppData'),
114 vendor, prog, *args
115 )
116
118 """Return mapping of shell folder names (current user) to paths."""
119
120 key = _winreg.OpenKey(branch, key)
121 folders = {}
122 i = 0
123 while True:
124 try:
125 ret = _winreg.EnumValue(key, i)
126 if ret[2] == _winreg.REG_EXPAND_SZ:
127 folders[ret[0]] = expandvars(ret[1])
128 else:
129 folders[ret[0]] = ret[1]
130 except WindowsError:
131 break
132 i +=1
133 key.Close()
134 return folders
135
137 """Return path to user configuration data for 'prog' from 'vendor'.
138
139 Additional arguments are appended via os.path.join(), e.g.
140 use like this:
141
142 optionsfn = get_userconf("ACME Soft", "Exploder", "Options.xml")
143 """
144
145 return os.path.join(get_appdata(), vendor, prog, *args)
146
148 """Convenience function to get path to windows installation directory."""
149
150 return unicode(os.environ["WINDIR"])
151