_appdirs.py 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643
  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 sys
  40. import os
  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('XDG_DATA_DIRS',
  150. os.pathsep.join(['/usr/local/share', '/usr/share']))
  151. pathlist = [os.path.expanduser(x.rstrip(os.sep)) for x in path.split(os.pathsep)]
  152. if appname:
  153. if version:
  154. appname = os.path.join(appname, version)
  155. pathlist = [os.sep.join([x, appname]) for x in pathlist]
  156. if multipath:
  157. path = os.pathsep.join(pathlist)
  158. else:
  159. path = pathlist[0]
  160. return path
  161. if appname and version:
  162. path = os.path.join(path, version)
  163. return path
  164. def user_config_dir(appname=None, appauthor=None, version=None, roaming=False):
  165. r"""Return full path to the user-specific config dir for this application.
  166. "appname" is the name of application.
  167. If None, just the system directory is returned.
  168. "appauthor" (only used on Windows) is the name of the
  169. appauthor or distributing body for this application. Typically
  170. it is the owning company name. This falls back to appname. You may
  171. pass False to disable it.
  172. "version" is an optional version path element to append to the
  173. path. You might want to use this if you want multiple versions
  174. of your app to be able to run independently. If used, this
  175. would typically be "<major>.<minor>".
  176. Only applied when appname is present.
  177. "roaming" (boolean, default False) can be set True to use the Windows
  178. roaming appdata directory. That means that for users on a Windows
  179. network setup for roaming profiles, this user data will be
  180. sync'd on login. See
  181. <http://technet.microsoft.com/en-us/library/cc766489(WS.10).aspx>
  182. for a discussion of issues.
  183. Typical user config directories are:
  184. Mac OS X: ~/Library/Preferences/<AppName>
  185. Unix: ~/.config/<AppName> # or in $XDG_CONFIG_HOME, if defined
  186. Win *: same as user_data_dir
  187. For Unix, we follow the XDG spec and support $XDG_CONFIG_HOME.
  188. That means, by default "~/.config/<AppName>".
  189. """
  190. if system == "win32":
  191. path = user_data_dir(appname, appauthor, None, roaming)
  192. elif system == 'darwin':
  193. path = os.path.expanduser('~/Library/Preferences/')
  194. if appname:
  195. path = os.path.join(path, appname)
  196. else:
  197. path = os.getenv('XDG_CONFIG_HOME', os.path.expanduser("~/.config"))
  198. if appname:
  199. path = os.path.join(path, appname)
  200. if appname and version:
  201. path = os.path.join(path, version)
  202. return path
  203. def site_config_dir(appname=None, appauthor=None, version=None, multipath=False):
  204. r"""Return full path to the user-shared data dir for this application.
  205. "appname" is the name of application.
  206. If None, just the system directory is returned.
  207. "appauthor" (only used on Windows) is the name of the
  208. appauthor or distributing body for this application. Typically
  209. it is the owning company name. This falls back to appname. You may
  210. pass False to disable it.
  211. "version" is an optional version path element to append to the
  212. path. You might want to use this if you want multiple versions
  213. of your app to be able to run independently. If used, this
  214. would typically be "<major>.<minor>".
  215. Only applied when appname is present.
  216. "multipath" is an optional parameter only applicable to *nix
  217. which indicates that the entire list of config dirs should be
  218. returned. By default, the first item from XDG_CONFIG_DIRS is
  219. returned, or '/etc/xdg/<AppName>', if XDG_CONFIG_DIRS is not set
  220. Typical site config directories are:
  221. Mac OS X: same as site_data_dir
  222. Unix: /etc/xdg/<AppName> or $XDG_CONFIG_DIRS[i]/<AppName> for each value in
  223. $XDG_CONFIG_DIRS
  224. Win *: same as site_data_dir
  225. Vista: (Fail! "C:\ProgramData" is a hidden *system* directory on Vista.)
  226. For Unix, this is using the $XDG_CONFIG_DIRS[0] default, if multipath=False
  227. WARNING: Do not use this on Windows. See the Vista-Fail note above for why.
  228. """
  229. if system == 'win32':
  230. path = site_data_dir(appname, appauthor)
  231. if appname and version:
  232. path = os.path.join(path, version)
  233. elif system == 'darwin':
  234. path = os.path.expanduser('/Library/Preferences')
  235. if appname:
  236. path = os.path.join(path, appname)
  237. else:
  238. # XDG default for $XDG_CONFIG_DIRS
  239. # only first, if multipath is False
  240. path = os.getenv('XDG_CONFIG_DIRS', '/etc/xdg')
  241. pathlist = [os.path.expanduser(x.rstrip(os.sep)) for x in path.split(os.pathsep)]
  242. if appname:
  243. if version:
  244. appname = os.path.join(appname, version)
  245. pathlist = [os.sep.join([x, appname]) for x in pathlist]
  246. if multipath:
  247. path = os.pathsep.join(pathlist)
  248. else:
  249. path = pathlist[0]
  250. return path
  251. def user_cache_dir(appname=None, appauthor=None, version=None, opinion=True):
  252. r"""Return full path to the user-specific cache dir for this application.
  253. "appname" is the name of application.
  254. If None, just the system directory is returned.
  255. "appauthor" (only used on Windows) is the name of the
  256. appauthor or distributing body for this application. Typically
  257. it is the owning company name. This falls back to appname. You may
  258. pass False to disable it.
  259. "version" is an optional version path element to append to the
  260. path. You might want to use this if you want multiple versions
  261. of your app to be able to run independently. If used, this
  262. would typically be "<major>.<minor>".
  263. Only applied when appname is present.
  264. "opinion" (boolean) can be False to disable the appending of
  265. "Cache" to the base app data dir for Windows. See
  266. discussion below.
  267. Typical user cache directories are:
  268. Mac OS X: ~/Library/Caches/<AppName>
  269. Unix: ~/.cache/<AppName> (XDG default)
  270. Win XP: C:\Documents and Settings\<username>\Local Settings\Application Data\<AppAuthor>\<AppName>\Cache
  271. Vista: C:\Users\<username>\AppData\Local\<AppAuthor>\<AppName>\Cache
  272. On Windows the only suggestion in the MSDN docs is that local settings go in
  273. the `CSIDL_LOCAL_APPDATA` directory. This is identical to the non-roaming
  274. app data dir (the default returned by `user_data_dir` above). Apps typically
  275. put cache data somewhere *under* the given dir here. Some examples:
  276. ...\Mozilla\Firefox\Profiles\<ProfileName>\Cache
  277. ...\Acme\SuperApp\Cache\1.0
  278. OPINION: This function appends "Cache" to the `CSIDL_LOCAL_APPDATA` value.
  279. This can be disabled with the `opinion=False` option.
  280. """
  281. if system == "win32":
  282. if appauthor is None:
  283. appauthor = appname
  284. path = os.path.normpath(_get_win_folder("CSIDL_LOCAL_APPDATA"))
  285. if appname:
  286. if appauthor is not False:
  287. path = os.path.join(path, appauthor, appname)
  288. else:
  289. path = os.path.join(path, appname)
  290. if opinion:
  291. path = os.path.join(path, "Cache")
  292. elif system == 'darwin':
  293. path = os.path.expanduser('~/Library/Caches')
  294. if appname:
  295. path = os.path.join(path, appname)
  296. else:
  297. path = os.getenv('XDG_CACHE_HOME', os.path.expanduser('~/.cache'))
  298. if appname:
  299. path = os.path.join(path, appname)
  300. if appname and version:
  301. path = os.path.join(path, version)
  302. return path
  303. def user_state_dir(appname=None, appauthor=None, version=None, roaming=False):
  304. r"""Return full path to the user-specific state dir for this application.
  305. "appname" is the name of application.
  306. If None, just the system directory is returned.
  307. "appauthor" (only used on Windows) is the name of the
  308. appauthor or distributing body for this application. Typically
  309. it is the owning company name. This falls back to appname. You may
  310. pass False to disable it.
  311. "version" is an optional version path element to append to the
  312. path. You might want to use this if you want multiple versions
  313. of your app to be able to run independently. If used, this
  314. would typically be "<major>.<minor>".
  315. Only applied when appname is present.
  316. "roaming" (boolean, default False) can be set True to use the Windows
  317. roaming appdata directory. That means that for users on a Windows
  318. network setup for roaming profiles, this user data will be
  319. sync'd on login. See
  320. <http://technet.microsoft.com/en-us/library/cc766489(WS.10).aspx>
  321. for a discussion of issues.
  322. Typical user state directories are:
  323. Mac OS X: same as user_data_dir
  324. Unix: ~/.local/state/<AppName> # or in $XDG_STATE_HOME, if defined
  325. Win *: same as user_data_dir
  326. For Unix, we follow this Debian proposal <https://wiki.debian.org/XDGBaseDirectorySpecification#state>
  327. to extend the XDG spec and support $XDG_STATE_HOME.
  328. That means, by default "~/.local/state/<AppName>".
  329. """
  330. if system in ["win32", "darwin"]:
  331. path = user_data_dir(appname, appauthor, None, roaming)
  332. else:
  333. path = os.getenv('XDG_STATE_HOME', os.path.expanduser("~/.local/state"))
  334. if appname:
  335. path = os.path.join(path, appname)
  336. if appname and version:
  337. path = os.path.join(path, version)
  338. return path
  339. def user_log_dir(appname=None, appauthor=None, version=None, opinion=True):
  340. r"""Return full path to the user-specific log dir for this application.
  341. "appname" is the name of application.
  342. If None, just the system directory is returned.
  343. "appauthor" (only used on Windows) is the name of the
  344. appauthor or distributing body for this application. Typically
  345. it is the owning company name. This falls back to appname. You may
  346. pass False to disable it.
  347. "version" is an optional version path element to append to the
  348. path. You might want to use this if you want multiple versions
  349. of your app to be able to run independently. If used, this
  350. would typically be "<major>.<minor>".
  351. Only applied when appname is present.
  352. "opinion" (boolean) can be False to disable the appending of
  353. "Logs" to the base app data dir for Windows, and "log" to the
  354. base cache dir for Unix. See discussion below.
  355. Typical user log directories are:
  356. Mac OS X: ~/Library/Logs/<AppName>
  357. Unix: ~/.cache/<AppName>/log # or under $XDG_CACHE_HOME if defined
  358. Win XP: C:\Documents and Settings\<username>\Local Settings\Application Data\<AppAuthor>\<AppName>\Logs
  359. Vista: C:\Users\<username>\AppData\Local\<AppAuthor>\<AppName>\Logs
  360. On Windows the only suggestion in the MSDN docs is that local settings
  361. go in the `CSIDL_LOCAL_APPDATA` directory. (Note: I'm interested in
  362. examples of what some windows apps use for a logs dir.)
  363. OPINION: This function appends "Logs" to the `CSIDL_LOCAL_APPDATA`
  364. value for Windows and appends "log" to the user cache dir for Unix.
  365. This can be disabled with the `opinion=False` option.
  366. """
  367. if system == "darwin":
  368. path = os.path.join(
  369. os.path.expanduser('~/Library/Logs'),
  370. appname)
  371. elif system == "win32":
  372. path = user_data_dir(appname, appauthor, version)
  373. version = False
  374. if opinion:
  375. path = os.path.join(path, "Logs")
  376. else:
  377. path = user_cache_dir(appname, appauthor, version)
  378. version = False
  379. if opinion:
  380. path = os.path.join(path, "log")
  381. if appname and version:
  382. path = os.path.join(path, version)
  383. return path
  384. class AppDirs(object):
  385. """Convenience wrapper for getting application dirs."""
  386. def __init__(self, appname=None, appauthor=None, version=None,
  387. roaming=False, multipath=False):
  388. self.appname = appname
  389. self.appauthor = appauthor
  390. self.version = version
  391. self.roaming = roaming
  392. self.multipath = multipath
  393. @property
  394. def user_data_dir(self):
  395. return user_data_dir(self.appname, self.appauthor,
  396. version=self.version, roaming=self.roaming)
  397. @property
  398. def site_data_dir(self):
  399. return site_data_dir(self.appname, self.appauthor,
  400. version=self.version, multipath=self.multipath)
  401. @property
  402. def user_config_dir(self):
  403. return user_config_dir(self.appname, self.appauthor,
  404. version=self.version, roaming=self.roaming)
  405. @property
  406. def site_config_dir(self):
  407. return site_config_dir(self.appname, self.appauthor,
  408. version=self.version, multipath=self.multipath)
  409. @property
  410. def user_cache_dir(self):
  411. return user_cache_dir(self.appname, self.appauthor,
  412. version=self.version)
  413. @property
  414. def user_state_dir(self):
  415. return user_state_dir(self.appname, self.appauthor,
  416. version=self.version)
  417. @property
  418. def user_log_dir(self):
  419. return user_log_dir(self.appname, self.appauthor,
  420. version=self.version)
  421. #---- internal support stuff
  422. def _get_win_folder_from_registry(csidl_name):
  423. """This is a fallback technique at best. I'm not sure if using the
  424. registry for this guarantees us the correct answer for all CSIDL_*
  425. names.
  426. """
  427. import winreg as _winreg
  428. shell_folder_name = {
  429. "CSIDL_APPDATA": "AppData",
  430. "CSIDL_COMMON_APPDATA": "Common AppData",
  431. "CSIDL_LOCAL_APPDATA": "Local AppData",
  432. }[csidl_name]
  433. key = _winreg.OpenKey(
  434. _winreg.HKEY_CURRENT_USER,
  435. r"Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders"
  436. )
  437. dir, type = _winreg.QueryValueEx(key, shell_folder_name)
  438. return dir
  439. def _get_win_folder_with_pywin32(csidl_name):
  440. from win32com.shell import shellcon, shell
  441. dir = shell.SHGetFolderPath(0, getattr(shellcon, csidl_name), 0, 0)
  442. # Try to make this a unicode path because SHGetFolderPath does
  443. # not return unicode strings when there is unicode data in the
  444. # path.
  445. try:
  446. dir = unicode(dir)
  447. # Downgrade to short path name if have highbit chars. See
  448. # <http://bugs.activestate.com/show_bug.cgi?id=85099>.
  449. has_high_char = False
  450. for c in dir:
  451. if ord(c) > 255:
  452. has_high_char = True
  453. break
  454. if has_high_char:
  455. try:
  456. import win32api
  457. dir = win32api.GetShortPathName(dir)
  458. except ImportError:
  459. pass
  460. except UnicodeError:
  461. pass
  462. return dir
  463. def _get_win_folder_with_ctypes(csidl_name):
  464. import ctypes
  465. csidl_const = {
  466. "CSIDL_APPDATA": 26,
  467. "CSIDL_COMMON_APPDATA": 35,
  468. "CSIDL_LOCAL_APPDATA": 28,
  469. }[csidl_name]
  470. buf = ctypes.create_unicode_buffer(1024)
  471. ctypes.windll.shell32.SHGetFolderPathW(None, csidl_const, None, 0, buf)
  472. # Downgrade to short path name if have highbit chars. See
  473. # <http://bugs.activestate.com/show_bug.cgi?id=85099>.
  474. has_high_char = False
  475. for c in buf:
  476. if ord(c) > 255:
  477. has_high_char = True
  478. break
  479. if has_high_char:
  480. buf2 = ctypes.create_unicode_buffer(1024)
  481. if ctypes.windll.kernel32.GetShortPathNameW(buf.value, buf2, 1024):
  482. buf = buf2
  483. return buf.value
  484. def _get_win_folder_with_jna(csidl_name):
  485. import array
  486. from com.sun import jna
  487. from com.sun.jna.platform import win32
  488. buf_size = win32.WinDef.MAX_PATH * 2
  489. buf = array.zeros('c', buf_size)
  490. shell = win32.Shell32.INSTANCE
  491. shell.SHGetFolderPath(None, getattr(win32.ShlObj, csidl_name), None, win32.ShlObj.SHGFP_TYPE_CURRENT, buf)
  492. dir = jna.Native.toString(buf.tostring()).rstrip("\0")
  493. # Downgrade to short path name if have highbit chars. See
  494. # <http://bugs.activestate.com/show_bug.cgi?id=85099>.
  495. has_high_char = False
  496. for c in dir:
  497. if ord(c) > 255:
  498. has_high_char = True
  499. break
  500. if has_high_char:
  501. buf = array.zeros('c', buf_size)
  502. kernel = win32.Kernel32.INSTANCE
  503. if kernel.GetShortPathName(dir, buf, buf_size):
  504. dir = jna.Native.toString(buf.tostring()).rstrip("\0")
  505. return dir
  506. if system == "win32":
  507. try:
  508. import win32com.shell
  509. _get_win_folder = _get_win_folder_with_pywin32
  510. except ImportError:
  511. try:
  512. from ctypes import windll
  513. _get_win_folder = _get_win_folder_with_ctypes
  514. except ImportError:
  515. try:
  516. import com.sun.jna
  517. _get_win_folder = _get_win_folder_with_jna
  518. except ImportError:
  519. _get_win_folder = _get_win_folder_from_registry
  520. #---- self test code
  521. if __name__ == "__main__":
  522. appname = "MyApp"
  523. appauthor = "MyCompany"
  524. props = ("user_data_dir",
  525. "user_config_dir",
  526. "user_cache_dir",
  527. "user_state_dir",
  528. "user_log_dir",
  529. "site_data_dir",
  530. "site_config_dir")
  531. print("-- app dirs %s --" % __version__)
  532. print("-- app dirs (with optional 'version')")
  533. dirs = AppDirs(appname, appauthor, version="1.0")
  534. for prop in props:
  535. print("%s: %s" % (prop, getattr(dirs, prop)))
  536. print("\n-- app dirs (without optional 'version')")
  537. dirs = AppDirs(appname, appauthor)
  538. for prop in props:
  539. print("%s: %s" % (prop, getattr(dirs, prop)))
  540. print("\n-- app dirs (without optional 'appauthor')")
  541. dirs = AppDirs(appname)
  542. for prop in props:
  543. print("%s: %s" % (prop, getattr(dirs, prop)))
  544. print("\n-- app dirs (with disabled 'appauthor')")
  545. dirs = AppDirs(appname, appauthor=False)
  546. for prop in props:
  547. print("%s: %s" % (prop, getattr(dirs, prop)))