_appdirs.py 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. # Copyright (c) 2005-2010 ActiveState Software Inc.
  4. # Copyright (c) 2013 Eddy Petrișor
  5. # flake8: noqa
  6. """
  7. This file is directly from
  8. https://github.com/ActiveState/appdirs/blob/3fe6a83776843a46f20c2e5587afcffe05e03b39/appdirs.py
  9. The license of https://github.com/ActiveState/appdirs copied below:
  10. # This is the MIT license
  11. Copyright (c) 2010 ActiveState Software Inc.
  12. Permission is hereby granted, free of charge, to any person obtaining a
  13. copy of this software and associated documentation files (the
  14. "Software"), to deal in the Software without restriction, including
  15. without limitation the rights to use, copy, modify, merge, publish,
  16. distribute, sublicense, and/or sell copies of the Software, and to
  17. permit persons to whom the Software is furnished to do so, subject to
  18. the following conditions:
  19. The above copyright notice and this permission notice shall be included
  20. in all copies or substantial portions of the Software.
  21. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  22. OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  24. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  25. CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  26. TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  27. SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. """
  29. """Utilities for determining application-specific dirs.
  30. See <https://github.com/ActiveState/appdirs> for details and usage.
  31. """
  32. # Dev Notes:
  33. # - MSDN on where to store app data files:
  34. # http://support.microsoft.com/default.aspx?scid=kb;en-us;310294#XSLTH3194121123120121120120
  35. # - Mac OS X: http://developer.apple.com/documentation/MacOSX/Conceptual/BPFileSystem/index.html
  36. # - XDG spec for Un*x: https://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html
  37. __version__ = "1.4.4"
  38. __version_info__ = tuple(int(segment) for segment in __version__.split("."))
  39. import os
  40. import sys
  41. unicode = str
  42. if sys.platform.startswith("java"):
  43. import platform
  44. os_name = platform.java_ver()[3][0]
  45. if os_name.startswith("Windows"): # "Windows XP", "Windows 7", etc.
  46. system = "win32"
  47. elif os_name.startswith("Mac"): # "Mac OS X", etc.
  48. system = "darwin"
  49. else: # "Linux", "SunOS", "FreeBSD", etc.
  50. # Setting this to "linux2" is not ideal, but only Windows or Mac
  51. # are actually checked for and the rest of the module expects
  52. # *sys.platform* style strings.
  53. system = "linux2"
  54. else:
  55. system = sys.platform
  56. def user_data_dir(appname=None, appauthor=None, version=None, roaming=False):
  57. r"""Return full path to the user-specific data dir for this application.
  58. "appname" is the name of application.
  59. If None, just the system directory is returned.
  60. "appauthor" (only used on Windows) is the name of the
  61. appauthor or distributing body for this application. Typically
  62. it is the owning company name. This falls back to appname. You may
  63. pass False to disable it.
  64. "version" is an optional version path element to append to the
  65. path. You might want to use this if you want multiple versions
  66. of your app to be able to run independently. If used, this
  67. would typically be "<major>.<minor>".
  68. Only applied when appname is present.
  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. Typical user data directories are:
  76. Mac OS X: ~/Library/Application Support/<AppName>
  77. Unix: ~/.local/share/<AppName> # or in $XDG_DATA_HOME, if defined
  78. Win XP (not roaming): C:\Documents and Settings\<username>\Application Data\<AppAuthor>\<AppName>
  79. Win XP (roaming): C:\Documents and Settings\<username>\Local Settings\Application Data\<AppAuthor>\<AppName>
  80. Win 7 (not roaming): C:\Users\<username>\AppData\Local\<AppAuthor>\<AppName>
  81. Win 7 (roaming): C:\Users\<username>\AppData\Roaming\<AppAuthor>\<AppName>
  82. For Unix, we follow the XDG spec and support $XDG_DATA_HOME.
  83. That means, by default "~/.local/share/<AppName>".
  84. """
  85. if system == "win32":
  86. if appauthor is None:
  87. appauthor = appname
  88. const = roaming and "CSIDL_APPDATA" or "CSIDL_LOCAL_APPDATA"
  89. path = os.path.normpath(_get_win_folder(const))
  90. if appname:
  91. if appauthor is not False:
  92. path = os.path.join(path, appauthor, appname)
  93. else:
  94. path = os.path.join(path, appname)
  95. elif system == "darwin":
  96. path = os.path.expanduser("~/Library/Application Support/")
  97. if appname:
  98. path = os.path.join(path, appname)
  99. else:
  100. path = os.getenv("XDG_DATA_HOME", os.path.expanduser("~/.local/share"))
  101. if appname:
  102. path = os.path.join(path, appname)
  103. if appname and version:
  104. path = os.path.join(path, version)
  105. return path
  106. def site_data_dir(appname=None, appauthor=None, version=None, multipath=False):
  107. r"""Return full path to the user-shared data dir for this application.
  108. "appname" is the name of application.
  109. If None, just the system directory is returned.
  110. "appauthor" (only used on Windows) is the name of the
  111. appauthor or distributing body for this application. Typically
  112. it is the owning company name. This falls back to appname. You may
  113. pass False to disable it.
  114. "version" is an optional version path element to append to the
  115. path. You might want to use this if you want multiple versions
  116. of your app to be able to run independently. If used, this
  117. would typically be "<major>.<minor>".
  118. Only applied when appname is present.
  119. "multipath" is an optional parameter only applicable to *nix
  120. which indicates that the entire list of data dirs should be
  121. returned. By default, the first item from XDG_DATA_DIRS is
  122. returned, or '/usr/local/share/<AppName>',
  123. if XDG_DATA_DIRS is not set
  124. Typical site data directories are:
  125. Mac OS X: /Library/Application Support/<AppName>
  126. Unix: /usr/local/share/<AppName> or /usr/share/<AppName>
  127. Win XP: C:\Documents and Settings\All Users\Application Data\<AppAuthor>\<AppName>
  128. Vista: (Fail! "C:\ProgramData" is a hidden *system* directory on Vista.)
  129. Win 7: C:\ProgramData\<AppAuthor>\<AppName> # Hidden, but writeable on Win 7.
  130. For Unix, this is using the $XDG_DATA_DIRS[0] default.
  131. WARNING: Do not use this on Windows. See the Vista-Fail note above for why.
  132. """
  133. if system == "win32":
  134. if appauthor is None:
  135. appauthor = appname
  136. path = os.path.normpath(_get_win_folder("CSIDL_COMMON_APPDATA"))
  137. if appname:
  138. if appauthor is not False:
  139. path = os.path.join(path, appauthor, appname)
  140. else:
  141. path = os.path.join(path, appname)
  142. elif system == "darwin":
  143. path = os.path.expanduser("/Library/Application Support")
  144. if appname:
  145. path = os.path.join(path, appname)
  146. else:
  147. # XDG default for $XDG_DATA_DIRS
  148. # only first, if multipath is False
  149. path = os.getenv(
  150. "XDG_DATA_DIRS", os.pathsep.join(["/usr/local/share", "/usr/share"])
  151. )
  152. pathlist = [
  153. os.path.expanduser(x.rstrip(os.sep)) for x in path.split(os.pathsep)
  154. ]
  155. if appname:
  156. if version:
  157. appname = os.path.join(appname, version)
  158. pathlist = [os.sep.join([x, appname]) for x in pathlist]
  159. if multipath:
  160. path = os.pathsep.join(pathlist)
  161. else:
  162. path = pathlist[0]
  163. return path
  164. if appname and version:
  165. path = os.path.join(path, version)
  166. return path
  167. def user_config_dir(appname=None, appauthor=None, version=None, roaming=False):
  168. r"""Return full path to the user-specific config dir for this application.
  169. "appname" is the name of application.
  170. If None, just the system directory is returned.
  171. "appauthor" (only used on Windows) is the name of the
  172. appauthor or distributing body for this application. Typically
  173. it is the owning company name. This falls back to appname. You may
  174. pass False to disable it.
  175. "version" is an optional version path element to append to the
  176. path. You might want to use this if you want multiple versions
  177. of your app to be able to run independently. If used, this
  178. would typically be "<major>.<minor>".
  179. Only applied when appname is present.
  180. "roaming" (boolean, default False) can be set True to use the Windows
  181. roaming appdata directory. That means that for users on a Windows
  182. network setup for roaming profiles, this user data will be
  183. sync'd on login. See
  184. <http://technet.microsoft.com/en-us/library/cc766489(WS.10).aspx>
  185. for a discussion of issues.
  186. Typical user config directories are:
  187. Mac OS X: ~/Library/Preferences/<AppName>
  188. Unix: ~/.config/<AppName> # or in $XDG_CONFIG_HOME, if defined
  189. Win *: same as user_data_dir
  190. For Unix, we follow the XDG spec and support $XDG_CONFIG_HOME.
  191. That means, by default "~/.config/<AppName>".
  192. """
  193. if system == "win32":
  194. path = user_data_dir(appname, appauthor, None, roaming)
  195. elif system == "darwin":
  196. path = os.path.expanduser("~/Library/Preferences/")
  197. if appname:
  198. path = os.path.join(path, appname)
  199. else:
  200. path = os.getenv("XDG_CONFIG_HOME", os.path.expanduser("~/.config"))
  201. if appname:
  202. path = os.path.join(path, appname)
  203. if appname and version:
  204. path = os.path.join(path, version)
  205. return path
  206. def site_config_dir(appname=None, appauthor=None, version=None, multipath=False):
  207. r"""Return full path to the user-shared data dir for this application.
  208. "appname" is the name of application.
  209. If None, just the system directory is returned.
  210. "appauthor" (only used on Windows) is the name of the
  211. appauthor or distributing body for this application. Typically
  212. it is the owning company name. This falls back to appname. You may
  213. pass False to disable it.
  214. "version" is an optional version path element to append to the
  215. path. You might want to use this if you want multiple versions
  216. of your app to be able to run independently. If used, this
  217. would typically be "<major>.<minor>".
  218. Only applied when appname is present.
  219. "multipath" is an optional parameter only applicable to *nix
  220. which indicates that the entire list of config dirs should be
  221. returned. By default, the first item from XDG_CONFIG_DIRS is
  222. returned, or '/etc/xdg/<AppName>', if XDG_CONFIG_DIRS is not set
  223. Typical site config directories are:
  224. Mac OS X: same as site_data_dir
  225. Unix: /etc/xdg/<AppName> or $XDG_CONFIG_DIRS[i]/<AppName> for each value in
  226. $XDG_CONFIG_DIRS
  227. Win *: same as site_data_dir
  228. Vista: (Fail! "C:\ProgramData" is a hidden *system* directory on Vista.)
  229. For Unix, this is using the $XDG_CONFIG_DIRS[0] default, if multipath=False
  230. WARNING: Do not use this on Windows. See the Vista-Fail note above for why.
  231. """
  232. if system == "win32":
  233. path = site_data_dir(appname, appauthor)
  234. if appname and version:
  235. path = os.path.join(path, version)
  236. elif system == "darwin":
  237. path = os.path.expanduser("/Library/Preferences")
  238. if appname:
  239. path = os.path.join(path, appname)
  240. else:
  241. # XDG default for $XDG_CONFIG_DIRS
  242. # only first, if multipath is False
  243. path = os.getenv("XDG_CONFIG_DIRS", "/etc/xdg")
  244. pathlist = [
  245. os.path.expanduser(x.rstrip(os.sep)) for x in path.split(os.pathsep)
  246. ]
  247. if appname:
  248. if version:
  249. appname = os.path.join(appname, version)
  250. pathlist = [os.sep.join([x, appname]) for x in pathlist]
  251. if multipath:
  252. path = os.pathsep.join(pathlist)
  253. else:
  254. path = pathlist[0]
  255. return path
  256. def user_cache_dir(appname=None, appauthor=None, version=None, opinion=True):
  257. r"""Return full path to the user-specific cache dir for this application.
  258. "appname" is the name of application.
  259. If None, just the system directory is returned.
  260. "appauthor" (only used on Windows) is the name of the
  261. appauthor or distributing body for this application. Typically
  262. it is the owning company name. This falls back to appname. You may
  263. pass False to disable it.
  264. "version" is an optional version path element to append to the
  265. path. You might want to use this if you want multiple versions
  266. of your app to be able to run independently. If used, this
  267. would typically be "<major>.<minor>".
  268. Only applied when appname is present.
  269. "opinion" (boolean) can be False to disable the appending of
  270. "Cache" to the base app data dir for Windows. See
  271. discussion below.
  272. Typical user cache directories are:
  273. Mac OS X: ~/Library/Caches/<AppName>
  274. Unix: ~/.cache/<AppName> (XDG default)
  275. Win XP: C:\Documents and Settings\<username>\Local Settings\Application Data\<AppAuthor>\<AppName>\Cache
  276. Vista: C:\Users\<username>\AppData\Local\<AppAuthor>\<AppName>\Cache
  277. On Windows the only suggestion in the MSDN docs is that local settings go in
  278. the `CSIDL_LOCAL_APPDATA` directory. This is identical to the non-roaming
  279. app data dir (the default returned by `user_data_dir` above). Apps typically
  280. put cache data somewhere *under* the given dir here. Some examples:
  281. ...\Mozilla\Firefox\Profiles\<ProfileName>\Cache
  282. ...\Acme\SuperApp\Cache\1.0
  283. OPINION: This function appends "Cache" to the `CSIDL_LOCAL_APPDATA` value.
  284. This can be disabled with the `opinion=False` option.
  285. """
  286. if system == "win32":
  287. if appauthor is None:
  288. appauthor = appname
  289. path = os.path.normpath(_get_win_folder("CSIDL_LOCAL_APPDATA"))
  290. if appname:
  291. if appauthor is not False:
  292. path = os.path.join(path, appauthor, appname)
  293. else:
  294. path = os.path.join(path, appname)
  295. if opinion:
  296. path = os.path.join(path, "Cache")
  297. elif system == "darwin":
  298. path = os.path.expanduser("~/Library/Caches")
  299. if appname:
  300. path = os.path.join(path, appname)
  301. else:
  302. path = os.getenv("XDG_CACHE_HOME", os.path.expanduser("~/.cache"))
  303. if appname:
  304. path = os.path.join(path, appname)
  305. if appname and version:
  306. path = os.path.join(path, version)
  307. return path
  308. def user_state_dir(appname=None, appauthor=None, version=None, roaming=False):
  309. r"""Return full path to the user-specific state dir for this application.
  310. "appname" is the name of application.
  311. If None, just the system directory is returned.
  312. "appauthor" (only used on Windows) is the name of the
  313. appauthor or distributing body for this application. Typically
  314. it is the owning company name. This falls back to appname. You may
  315. pass False to disable it.
  316. "version" is an optional version path element to append to the
  317. path. You might want to use this if you want multiple versions
  318. of your app to be able to run independently. If used, this
  319. would typically be "<major>.<minor>".
  320. Only applied when appname is present.
  321. "roaming" (boolean, default False) can be set True to use the Windows
  322. roaming appdata directory. That means that for users on a Windows
  323. network setup for roaming profiles, this user data will be
  324. sync'd on login. See
  325. <http://technet.microsoft.com/en-us/library/cc766489(WS.10).aspx>
  326. for a discussion of issues.
  327. Typical user state directories are:
  328. Mac OS X: same as user_data_dir
  329. Unix: ~/.local/state/<AppName> # or in $XDG_STATE_HOME, if defined
  330. Win *: same as user_data_dir
  331. For Unix, we follow this Debian proposal <https://wiki.debian.org/XDGBaseDirectorySpecification#state>
  332. to extend the XDG spec and support $XDG_STATE_HOME.
  333. That means, by default "~/.local/state/<AppName>".
  334. """
  335. if system in ["win32", "darwin"]:
  336. path = user_data_dir(appname, appauthor, None, roaming)
  337. else:
  338. path = os.getenv("XDG_STATE_HOME", os.path.expanduser("~/.local/state"))
  339. if appname:
  340. path = os.path.join(path, appname)
  341. if appname and version:
  342. path = os.path.join(path, version)
  343. return path
  344. def user_log_dir(appname=None, appauthor=None, version=None, opinion=True):
  345. r"""Return full path to the user-specific log dir for this application.
  346. "appname" is the name of application.
  347. If None, just the system directory is returned.
  348. "appauthor" (only used on Windows) is the name of the
  349. appauthor or distributing body for this application. Typically
  350. it is the owning company name. This falls back to appname. You may
  351. pass False to disable it.
  352. "version" is an optional version path element to append to the
  353. path. You might want to use this if you want multiple versions
  354. of your app to be able to run independently. If used, this
  355. would typically be "<major>.<minor>".
  356. Only applied when appname is present.
  357. "opinion" (boolean) can be False to disable the appending of
  358. "Logs" to the base app data dir for Windows, and "log" to the
  359. base cache dir for Unix. See discussion below.
  360. Typical user log directories are:
  361. Mac OS X: ~/Library/Logs/<AppName>
  362. Unix: ~/.cache/<AppName>/log # or under $XDG_CACHE_HOME if defined
  363. Win XP: C:\Documents and Settings\<username>\Local Settings\Application Data\<AppAuthor>\<AppName>\Logs
  364. Vista: C:\Users\<username>\AppData\Local\<AppAuthor>\<AppName>\Logs
  365. On Windows the only suggestion in the MSDN docs is that local settings
  366. go in the `CSIDL_LOCAL_APPDATA` directory. (Note: I'm interested in
  367. examples of what some windows apps use for a logs dir.)
  368. OPINION: This function appends "Logs" to the `CSIDL_LOCAL_APPDATA`
  369. value for Windows and appends "log" to the user cache dir for Unix.
  370. This can be disabled with the `opinion=False` option.
  371. """
  372. if system == "darwin":
  373. path = os.path.join(os.path.expanduser("~/Library/Logs"), appname)
  374. elif system == "win32":
  375. path = user_data_dir(appname, appauthor, version)
  376. version = False
  377. if opinion:
  378. path = os.path.join(path, "Logs")
  379. else:
  380. path = user_cache_dir(appname, appauthor, version)
  381. version = False
  382. if opinion:
  383. path = os.path.join(path, "log")
  384. if appname and version:
  385. path = os.path.join(path, version)
  386. return path
  387. class AppDirs(object):
  388. """Convenience wrapper for getting application dirs."""
  389. def __init__(
  390. self, appname=None, appauthor=None, version=None, roaming=False, multipath=False
  391. ):
  392. self.appname = appname
  393. self.appauthor = appauthor
  394. self.version = version
  395. self.roaming = roaming
  396. self.multipath = multipath
  397. @property
  398. def user_data_dir(self):
  399. return user_data_dir(
  400. self.appname, self.appauthor, version=self.version, roaming=self.roaming
  401. )
  402. @property
  403. def site_data_dir(self):
  404. return site_data_dir(
  405. self.appname, self.appauthor, version=self.version, multipath=self.multipath
  406. )
  407. @property
  408. def user_config_dir(self):
  409. return user_config_dir(
  410. self.appname, self.appauthor, version=self.version, roaming=self.roaming
  411. )
  412. @property
  413. def site_config_dir(self):
  414. return site_config_dir(
  415. self.appname, self.appauthor, version=self.version, multipath=self.multipath
  416. )
  417. @property
  418. def user_cache_dir(self):
  419. return user_cache_dir(self.appname, self.appauthor, version=self.version)
  420. @property
  421. def user_state_dir(self):
  422. return user_state_dir(self.appname, self.appauthor, version=self.version)
  423. @property
  424. def user_log_dir(self):
  425. return user_log_dir(self.appname, self.appauthor, version=self.version)
  426. # ---- internal support stuff
  427. def _get_win_folder_from_registry(csidl_name):
  428. """This is a fallback technique at best. I'm not sure if using the
  429. registry for this guarantees us the correct answer for all CSIDL_*
  430. names.
  431. """
  432. import winreg as _winreg
  433. shell_folder_name = {
  434. "CSIDL_APPDATA": "AppData",
  435. "CSIDL_COMMON_APPDATA": "Common AppData",
  436. "CSIDL_LOCAL_APPDATA": "Local AppData",
  437. }[csidl_name]
  438. key = _winreg.OpenKey(
  439. _winreg.HKEY_CURRENT_USER,
  440. r"Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders",
  441. )
  442. dir, type = _winreg.QueryValueEx(key, shell_folder_name)
  443. return dir
  444. def _get_win_folder_with_pywin32(csidl_name):
  445. from win32com.shell import shell, shellcon
  446. dir = shell.SHGetFolderPath(0, getattr(shellcon, csidl_name), 0, 0)
  447. # Try to make this a unicode path because SHGetFolderPath does
  448. # not return unicode strings when there is unicode data in the
  449. # path.
  450. try:
  451. dir = unicode(dir)
  452. # Downgrade to short path name if have highbit chars. See
  453. # <http://bugs.activestate.com/show_bug.cgi?id=85099>.
  454. has_high_char = False
  455. for c in dir:
  456. if ord(c) > 255:
  457. has_high_char = True
  458. break
  459. if has_high_char:
  460. try:
  461. import win32api
  462. dir = win32api.GetShortPathName(dir)
  463. except ImportError:
  464. pass
  465. except UnicodeError:
  466. pass
  467. return dir
  468. def _get_win_folder_with_ctypes(csidl_name):
  469. import ctypes
  470. csidl_const = {
  471. "CSIDL_APPDATA": 26,
  472. "CSIDL_COMMON_APPDATA": 35,
  473. "CSIDL_LOCAL_APPDATA": 28,
  474. }[csidl_name]
  475. buf = ctypes.create_unicode_buffer(1024)
  476. ctypes.windll.shell32.SHGetFolderPathW(None, csidl_const, None, 0, buf)
  477. # Downgrade to short path name if have highbit chars. See
  478. # <http://bugs.activestate.com/show_bug.cgi?id=85099>.
  479. has_high_char = False
  480. for c in buf:
  481. if ord(c) > 255:
  482. has_high_char = True
  483. break
  484. if has_high_char:
  485. buf2 = ctypes.create_unicode_buffer(1024)
  486. if ctypes.windll.kernel32.GetShortPathNameW(buf.value, buf2, 1024):
  487. buf = buf2
  488. return buf.value
  489. def _get_win_folder_with_jna(csidl_name):
  490. import array
  491. from com.sun import jna
  492. from com.sun.jna.platform import win32
  493. buf_size = win32.WinDef.MAX_PATH * 2
  494. buf = array.zeros("c", buf_size)
  495. shell = win32.Shell32.INSTANCE
  496. shell.SHGetFolderPath(
  497. None,
  498. getattr(win32.ShlObj, csidl_name),
  499. None,
  500. win32.ShlObj.SHGFP_TYPE_CURRENT,
  501. buf,
  502. )
  503. dir = jna.Native.toString(buf.tostring()).rstrip("\0")
  504. # Downgrade to short path name if have highbit chars. See
  505. # <http://bugs.activestate.com/show_bug.cgi?id=85099>.
  506. has_high_char = False
  507. for c in dir:
  508. if ord(c) > 255:
  509. has_high_char = True
  510. break
  511. if has_high_char:
  512. buf = array.zeros("c", buf_size)
  513. kernel = win32.Kernel32.INSTANCE
  514. if kernel.GetShortPathName(dir, buf, buf_size):
  515. dir = jna.Native.toString(buf.tostring()).rstrip("\0")
  516. return dir
  517. if system == "win32":
  518. try:
  519. import win32com.shell
  520. _get_win_folder = _get_win_folder_with_pywin32
  521. except ImportError:
  522. try:
  523. from ctypes import windll
  524. _get_win_folder = _get_win_folder_with_ctypes
  525. except ImportError:
  526. try:
  527. import com.sun.jna
  528. _get_win_folder = _get_win_folder_with_jna
  529. except ImportError:
  530. _get_win_folder = _get_win_folder_from_registry
  531. # ---- self test code
  532. if __name__ == "__main__":
  533. appname = "MyApp"
  534. appauthor = "MyCompany"
  535. props = (
  536. "user_data_dir",
  537. "user_config_dir",
  538. "user_cache_dir",
  539. "user_state_dir",
  540. "user_log_dir",
  541. "site_data_dir",
  542. "site_config_dir",
  543. )
  544. print("-- app dirs %s --" % __version__)
  545. print("-- app dirs (with optional 'version')")
  546. dirs = AppDirs(appname, appauthor, version="1.0")
  547. for prop in props:
  548. print("%s: %s" % (prop, getattr(dirs, prop)))
  549. print("\n-- app dirs (without optional 'version')")
  550. dirs = AppDirs(appname, appauthor)
  551. for prop in props:
  552. print("%s: %s" % (prop, getattr(dirs, prop)))
  553. print("\n-- app dirs (without optional 'appauthor')")
  554. dirs = AppDirs(appname)
  555. for prop in props:
  556. print("%s: %s" % (prop, getattr(dirs, prop)))
  557. print("\n-- app dirs (with disabled 'appauthor')")
  558. dirs = AppDirs(appname, appauthor=False)
  559. for prop in props:
  560. print("%s: %s" % (prop, getattr(dirs, prop)))