diff options
author | Shubham Saini <shubham6405@gmail.com> | 2018-12-11 10:01:23 +0000 |
---|---|---|
committer | Shubham Saini <shubham6405@gmail.com> | 2018-12-11 10:01:23 +0000 |
commit | 68df54d6629ec019142eb149dd037774f2d11e7c (patch) | |
tree | 345bc22d46b4e01a4ba8303b94278952a4ed2b9e /venv/lib/python3.7/site-packages/pip-10.0.1-py3.7.egg/pip/_internal/utils/appdirs.py |
First commit
Diffstat (limited to 'venv/lib/python3.7/site-packages/pip-10.0.1-py3.7.egg/pip/_internal/utils/appdirs.py')
-rw-r--r-- | venv/lib/python3.7/site-packages/pip-10.0.1-py3.7.egg/pip/_internal/utils/appdirs.py | 258 |
1 files changed, 258 insertions, 0 deletions
diff --git a/venv/lib/python3.7/site-packages/pip-10.0.1-py3.7.egg/pip/_internal/utils/appdirs.py b/venv/lib/python3.7/site-packages/pip-10.0.1-py3.7.egg/pip/_internal/utils/appdirs.py new file mode 100644 index 0000000..0eb87ca --- /dev/null +++ b/venv/lib/python3.7/site-packages/pip-10.0.1-py3.7.egg/pip/_internal/utils/appdirs.py | |||
@@ -0,0 +1,258 @@ | |||
1 | """ | ||
2 | This code was taken from https://github.com/ActiveState/appdirs and modified | ||
3 | to suit our purposes. | ||
4 | """ | ||
5 | from __future__ import absolute_import | ||
6 | |||
7 | import os | ||
8 | import sys | ||
9 | |||
10 | from pip._vendor.six import PY2, text_type | ||
11 | |||
12 | from pip._internal.compat import WINDOWS, expanduser | ||
13 | |||
14 | |||
15 | def user_cache_dir(appname): | ||
16 | r""" | ||
17 | Return full path to the user-specific cache dir for this application. | ||
18 | |||
19 | "appname" is the name of application. | ||
20 | |||
21 | Typical user cache directories are: | ||
22 | macOS: ~/Library/Caches/<AppName> | ||
23 | Unix: ~/.cache/<AppName> (XDG default) | ||
24 | Windows: C:\Users\<username>\AppData\Local\<AppName>\Cache | ||
25 | |||
26 | On Windows the only suggestion in the MSDN docs is that local settings go | ||
27 | in the `CSIDL_LOCAL_APPDATA` directory. This is identical to the | ||
28 | non-roaming app data dir (the default returned by `user_data_dir`). Apps | ||
29 | typically put cache data somewhere *under* the given dir here. Some | ||
30 | examples: | ||
31 | ...\Mozilla\Firefox\Profiles\<ProfileName>\Cache | ||
32 | ...\Acme\SuperApp\Cache\1.0 | ||
33 | |||
34 | OPINION: This function appends "Cache" to the `CSIDL_LOCAL_APPDATA` value. | ||
35 | """ | ||
36 | if WINDOWS: | ||
37 | # Get the base path | ||
38 | path = os.path.normpath(_get_win_folder("CSIDL_LOCAL_APPDATA")) | ||
39 | |||
40 | # When using Python 2, return paths as bytes on Windows like we do on | ||
41 | # other operating systems. See helper function docs for more details. | ||
42 | if PY2 and isinstance(path, text_type): | ||
43 | path = _win_path_to_bytes(path) | ||
44 | |||
45 | # Add our app name and Cache directory to it | ||
46 | path = os.path.join(path, appname, "Cache") | ||
47 | elif sys.platform == "darwin": | ||
48 | # Get the base path | ||
49 | path = expanduser("~/Library/Caches") | ||
50 | |||
51 | # Add our app name to it | ||
52 | path = os.path.join(path, appname) | ||
53 | else: | ||
54 | # Get the base path | ||
55 | path = os.getenv("XDG_CACHE_HOME", expanduser("~/.cache")) | ||
56 | |||
57 | # Add our app name to it | ||
58 | path = os.path.join(path, appname) | ||
59 | |||
60 | return path | ||
61 | |||
62 | |||
63 | def user_data_dir(appname, roaming=False): | ||
64 | r""" | ||
65 | Return full path to the user-specific data dir for this application. | ||
66 | |||
67 | "appname" is the name of application. | ||
68 | If None, just the system directory is returned. | ||
69 | "roaming" (boolean, default False) can be set True to use the Windows | ||
70 | roaming appdata directory. That means that for users on a Windows | ||
71 | network setup for roaming profiles, this user data will be | ||
72 | sync'd on login. See | ||
73 | <http://technet.microsoft.com/en-us/library/cc766489(WS.10).aspx> | ||
74 | for a discussion of issues. | ||
75 | |||
76 | Typical user data directories are: | ||
77 | macOS: ~/Library/Application Support/<AppName> | ||
78 | if it exists, else ~/.config/<AppName> | ||
79 | Unix: ~/.local/share/<AppName> # or in | ||
80 | $XDG_DATA_HOME, if defined | ||
81 | Win XP (not roaming): C:\Documents and Settings\<username>\ ... | ||
82 | ...Application Data\<AppName> | ||
83 | Win XP (roaming): C:\Documents and Settings\<username>\Local ... | ||
84 | ...Settings\Application Data\<AppName> | ||
85 | Win 7 (not roaming): C:\\Users\<username>\AppData\Local\<AppName> | ||
86 | Win 7 (roaming): C:\\Users\<username>\AppData\Roaming\<AppName> | ||
87 | |||
88 | For Unix, we follow the XDG spec and support $XDG_DATA_HOME. | ||
89 | That means, by default "~/.local/share/<AppName>". | ||
90 | """ | ||
91 | if WINDOWS: | ||
92 | const = roaming and "CSIDL_APPDATA" or "CSIDL_LOCAL_APPDATA" | ||
93 | path = os.path.join(os.path.normpath(_get_win_folder(const)), appname) | ||
94 | elif sys.platform == "darwin": | ||
95 | path = os.path.join( | ||
96 | expanduser('~/Library/Application Support/'), | ||
97 | appname, | ||
98 | ) if os.path.isdir(os.path.join( | ||
99 | expanduser('~/Library/Application Support/'), | ||
100 | appname, | ||
101 | ) | ||
102 | ) else os.path.join( | ||
103 | expanduser('~/.config/'), | ||
104 | appname, | ||
105 | ) | ||
106 | else: | ||
107 | path = os.path.join( | ||
108 | os.getenv('XDG_DATA_HOME', expanduser("~/.local/share")), | ||
109 | appname, | ||
110 | ) | ||
111 | |||
112 | return path | ||
113 | |||
114 | |||
115 | def user_config_dir(appname, roaming=True): | ||
116 | """Return full path to the user-specific config dir for this application. | ||
117 | |||
118 | "appname" is the name of application. | ||
119 | If None, just the system directory is returned. | ||
120 | "roaming" (boolean, default True) can be set False to not use the | ||
121 | Windows roaming appdata directory. That means that for users on a | ||
122 | Windows network setup for roaming profiles, this user data will be | ||
123 | sync'd on login. See | ||
124 | <http://technet.microsoft.com/en-us/library/cc766489(WS.10).aspx> | ||
125 | for a discussion of issues. | ||
126 | |||
127 | Typical user data directories are: | ||
128 | macOS: same as user_data_dir | ||
129 | Unix: ~/.config/<AppName> | ||
130 | Win *: same as user_data_dir | ||
131 | |||
132 | For Unix, we follow the XDG spec and support $XDG_CONFIG_HOME. | ||
133 | That means, by default "~/.config/<AppName>". | ||
134 | """ | ||
135 | if WINDOWS: | ||
136 | path = user_data_dir(appname, roaming=roaming) | ||
137 | elif sys.platform == "darwin": | ||
138 | path = user_data_dir(appname) | ||
139 | else: | ||
140 | path = os.getenv('XDG_CONFIG_HOME', expanduser("~/.config")) | ||
141 | path = os.path.join(path, appname) | ||
142 | |||
143 | return path | ||
144 | |||
145 | |||
146 | # for the discussion regarding site_config_dirs locations | ||
147 | # see <https://github.com/pypa/pip/issues/1733> | ||
148 | def site_config_dirs(appname): | ||
149 | r"""Return a list of potential user-shared config dirs for this application. | ||
150 | |||
151 | "appname" is the name of application. | ||
152 | |||
153 | Typical user config directories are: | ||
154 | macOS: /Library/Application Support/<AppName>/ | ||
155 | Unix: /etc or $XDG_CONFIG_DIRS[i]/<AppName>/ for each value in | ||
156 | $XDG_CONFIG_DIRS | ||
157 | Win XP: C:\Documents and Settings\All Users\Application ... | ||
158 | ...Data\<AppName>\ | ||
159 | Vista: (Fail! "C:\ProgramData" is a hidden *system* directory | ||
160 | on Vista.) | ||
161 | Win 7: Hidden, but writeable on Win 7: | ||
162 | C:\ProgramData\<AppName>\ | ||
163 | """ | ||
164 | if WINDOWS: | ||
165 | path = os.path.normpath(_get_win_folder("CSIDL_COMMON_APPDATA")) | ||
166 | pathlist = [os.path.join(path, appname)] | ||
167 | elif sys.platform == 'darwin': | ||
168 | pathlist = [os.path.join('/Library/Application Support', appname)] | ||
169 | else: | ||
170 | # try looking in $XDG_CONFIG_DIRS | ||
171 | xdg_config_dirs = os.getenv('XDG_CONFIG_DIRS', '/etc/xdg') | ||
172 | if xdg_config_dirs: | ||
173 | pathlist = [ | ||
174 | os.path.join(expanduser(x), appname) | ||
175 | for x in xdg_config_dirs.split(os.pathsep) | ||
176 | ] | ||
177 | else: | ||
178 | pathlist = [] | ||
179 | |||
180 | # always look in /etc directly as well | ||
181 | pathlist.append('/etc') | ||
182 | |||
183 | return pathlist | ||
184 | |||
185 | |||
186 | # -- Windows support functions -- | ||
187 | |||
188 | def _get_win_folder_from_registry(csidl_name): | ||
189 | """ | ||
190 | This is a fallback technique at best. I'm not sure if using the | ||
191 | registry for this guarantees us the correct answer for all CSIDL_* | ||
192 | names. | ||
193 | """ | ||
194 | import _winreg | ||
195 | |||
196 | shell_folder_name = { | ||
197 | "CSIDL_APPDATA": "AppData", | ||
198 | "CSIDL_COMMON_APPDATA": "Common AppData", | ||
199 | "CSIDL_LOCAL_APPDATA": "Local AppData", | ||
200 | }[csidl_name] | ||
201 | |||
202 | key = _winreg.OpenKey( | ||
203 | _winreg.HKEY_CURRENT_USER, | ||
204 | r"Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" | ||
205 | ) | ||
206 | directory, _type = _winreg.QueryValueEx(key, shell_folder_name) | ||
207 | return directory | ||
208 | |||
209 | |||
210 | def _get_win_folder_with_ctypes(csidl_name): | ||
211 | csidl_const = { | ||
212 | "CSIDL_APPDATA": 26, | ||
213 | "CSIDL_COMMON_APPDATA": 35, | ||
214 | "CSIDL_LOCAL_APPDATA": 28, | ||
215 | }[csidl_name] | ||
216 | |||
217 | buf = ctypes.create_unicode_buffer(1024) | ||
218 | ctypes.windll.shell32.SHGetFolderPathW(None, csidl_const, None, 0, buf) | ||
219 | |||
220 | # Downgrade to short path name if have highbit chars. See | ||
221 | # <http://bugs.activestate.com/show_bug.cgi?id=85099>. | ||
222 | has_high_char = False | ||
223 | for c in buf: | ||
224 | if ord(c) > 255: | ||
225 | has_high_char = True | ||
226 | break | ||
227 | if has_high_char: | ||
228 | buf2 = ctypes.create_unicode_buffer(1024) | ||
229 | if ctypes.windll.kernel32.GetShortPathNameW(buf.value, buf2, 1024): | ||
230 | buf = buf2 | ||
231 | |||
232 | return buf.value | ||
233 | |||
234 | |||
235 | if WINDOWS: | ||
236 | try: | ||
237 | import ctypes | ||
238 | _get_win_folder = _get_win_folder_with_ctypes | ||
239 | except ImportError: | ||
240 | _get_win_folder = _get_win_folder_from_registry | ||
241 | |||
242 | |||
243 | def _win_path_to_bytes(path): | ||
244 | """Encode Windows paths to bytes. Only used on Python 2. | ||
245 | |||
246 | Motivation is to be consistent with other operating systems where paths | ||
247 | are also returned as bytes. This avoids problems mixing bytes and Unicode | ||
248 | elsewhere in the codebase. For more details and discussion see | ||
249 | <https://github.com/pypa/pip/issues/3463>. | ||
250 | |||
251 | If encoding using ASCII and MBCS fails, return the original Unicode path. | ||
252 | """ | ||
253 | for encoding in ('ASCII', 'MBCS'): | ||
254 | try: | ||
255 | return path.encode(encoding) | ||
256 | except (UnicodeEncodeError, LookupError): | ||
257 | pass | ||
258 | return path | ||