File: winpaths.py

package info (click to toggle)
anki 2.1.15%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 6,240 kB
  • sloc: python: 27,663; javascript: 589; xml: 67; sh: 51; makefile: 45
file content (168 lines) | stat: -rw-r--r-- 4,722 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# -*- coding: utf-8 -*-

"""
System File Locations
Retrieves common system path names on Windows XP/Vista
Depends only on ctypes, and retrieves path locations in Unicode
"""

import ctypes
from ctypes import windll, wintypes

__license__ = "MIT"
__version__ = "0.2"
__author__ = "Ryan Ginstrom"
__description__ = "Retrieves common Windows system paths as Unicode strings"

class PathConstants(object):
    """
    Define constants here to avoid dependency on shellcon.
    Put it in a class to avoid polluting namespace
    """

    CSIDL_DESKTOP = 0
    CSIDL_PROGRAMS = 2
    CSIDL_PERSONAL = 5
    CSIDL_FAVORITES = 6
    CSIDL_STARTUP = 7
    CSIDL_RECENT = 8
    CSIDL_SENDTO = 9
    CSIDL_BITBUCKET = 10
    CSIDL_STARTMENU = 11
    CSIDL_MYDOCUMENTS = 12
    CSIDL_MYMUSIC = 13
    CSIDL_MYVIDEO = 14
    CSIDL_DESKTOPDIRECTORY = 16
    CSIDL_DRIVES = 17
    CSIDL_NETWORK = 18
    CSIDL_NETHOOD = 19
    CSIDL_FONTS = 20
    CSIDL_TEMPLATES = 21
    CSIDL_COMMON_STARTMENU = 22
    CSIDL_COMMON_PROGRAMS = 23
    CSIDL_COMMON_STARTUP = 24
    CSIDL_COMMON_DESKTOPDIRECTORY = 25
    CSIDL_APPDATA = 26
    CSIDL_PRINTHOOD = 27
    CSIDL_LOCAL_APPDATA = 28
    CSIDL_ALTSTARTUP = 29
    CSIDL_COMMON_ALTSTARTUP = 30
    CSIDL_COMMON_FAVORITES = 31
    CSIDL_INTERNET_CACHE = 32
    CSIDL_COOKIES = 33
    CSIDL_HISTORY = 34
    CSIDL_COMMON_APPDATA = 35
    CSIDL_WINDOWS = 36
    CSIDL_SYSTEM = 37
    CSIDL_PROGRAM_FILES = 38
    CSIDL_MYPICTURES = 39
    CSIDL_PROFILE = 40
    CSIDL_SYSTEMX86 = 41
    CSIDL_PROGRAM_FILESX86 = 42
    CSIDL_PROGRAM_FILES_COMMON = 43
    CSIDL_PROGRAM_FILES_COMMONX86 = 44
    CSIDL_COMMON_TEMPLATES = 45
    CSIDL_COMMON_DOCUMENTS = 46
    CSIDL_COMMON_ADMINTOOLS = 47
    CSIDL_ADMINTOOLS  = 48
    CSIDL_CONNECTIONS = 49
    CSIDL_COMMON_MUSIC = 53
    CSIDL_COMMON_PICTURES = 54
    CSIDL_COMMON_VIDEO = 55
    CSIDL_RESOURCES = 56
    CSIDL_RESOURCES_LOCALIZED = 57
    CSIDL_COMMON_OEM_LINKS = 58
    CSIDL_CDBURN_AREA = 59
    # 60 unused
    CSIDL_COMPUTERSNEARME = 61

class WinPathsException(Exception):
    pass

def _err_unless_zero(result):
    if result == 0:
        return result
    else:
        raise WinPathsException("Failed to retrieve windows path: %s" % result)

_SHGetFolderPath = windll.shell32.SHGetFolderPathW
_SHGetFolderPath.argtypes = [wintypes.HWND,
                            ctypes.c_int,
                            wintypes.HANDLE,
                            wintypes.DWORD, wintypes.LPCWSTR]
_SHGetFolderPath.restype = _err_unless_zero

def _get_path_buf(csidl):
    path_buf = ctypes.create_unicode_buffer(wintypes.MAX_PATH)
    result = _SHGetFolderPath(0, csidl, 0, 0, path_buf)
    return path_buf.value

def get_local_appdata():
    return _get_path_buf(PathConstants.CSIDL_LOCAL_APPDATA)

def get_appdata():
    return _get_path_buf(PathConstants.CSIDL_APPDATA)

def get_desktop():
    return _get_path_buf(PathConstants.CSIDL_DESKTOP)

def get_programs():
    """current user -> Start menu -> Programs"""
    return _get_path_buf(PathConstants.CSIDL_PROGRAMS)

def get_admin_tools():
    """current user -> Start menu -> Programs -> Admin tools"""
    return _get_path_buf(PathConstants.CSIDL_ADMINTOOLS)

def get_common_admin_tools():
    """all users -> Start menu -> Programs -> Admin tools"""
    return _get_path_buf(PathConstants.CSIDL_COMMON_ADMINTOOLS)

def get_common_appdata():
    return _get_path_buf(PathConstants.CSIDL_COMMON_APPDATA)

def get_common_documents():
    return _get_path_buf(PathConstants.CSIDL_COMMON_DOCUMENTS)

def get_cookies():
    return _get_path_buf(PathConstants.CSIDL_COOKIES)

def get_history():
    return _get_path_buf(PathConstants.CSIDL_HISTORY)

def get_internet_cache():
    return _get_path_buf(PathConstants.CSIDL_INTERNET_CACHE)

def get_my_pictures():
    """Get the user's My Pictures folder"""
    return _get_path_buf(PathConstants.CSIDL_MYPICTURES)

def get_personal():
    """AKA 'My Documents'"""
    return _get_path_buf(PathConstants.CSIDL_PERSONAL)

get_my_documents = get_personal

def get_program_files():
    return _get_path_buf(PathConstants.CSIDL_PROGRAM_FILES)

def get_program_files_common():
    return _get_path_buf(PathConstants.CSIDL_PROGRAM_FILES_COMMON)

def get_system():
    """Use with care and discretion"""
    return _get_path_buf(PathConstants.CSIDL_SYSTEM)

def get_windows():
    """Use with care and discretion"""
    return _get_path_buf(PathConstants.CSIDL_WINDOWS)

def get_favorites():
    return _get_path_buf(PathConstants.CSIDL_FAVORITES)

def get_startup():
    """current user -> start menu -> programs -> startup"""
    return _get_path_buf(PathConstants.CSIDL_STARTUP)

def get_recent():
    return _get_path_buf(PathConstants.CSIDL_RECENT)