You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

227 lines
7.4 KiB

4 years ago
  1. .. image:: https://secure.travis-ci.org/ActiveState/appdirs.png
  2. :target: http://travis-ci.org/ActiveState/appdirs
  3. the problem
  4. ===========
  5. What directory should your app use for storing user data? If running on Mac OS X, you
  6. should use::
  7. ~/Library/Application Support/<AppName>
  8. If on Windows (at least English Win XP) that should be::
  9. C:\Documents and Settings\<User>\Application Data\Local Settings\<AppAuthor>\<AppName>
  10. or possibly::
  11. C:\Documents and Settings\<User>\Application Data\<AppAuthor>\<AppName>
  12. for `roaming profiles <http://bit.ly/9yl3b6>`_ but that is another story.
  13. On Linux (and other Unices) the dir, according to the `XDG
  14. spec <http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html>`_, is::
  15. ~/.local/share/<AppName>
  16. ``appdirs`` to the rescue
  17. =========================
  18. This kind of thing is what the ``appdirs`` module is for. ``appdirs`` will
  19. help you choose an appropriate:
  20. - user data dir (``user_data_dir``)
  21. - user config dir (``user_config_dir``)
  22. - user cache dir (``user_cache_dir``)
  23. - site data dir (``site_data_dir``)
  24. - site config dir (``site_config_dir``)
  25. - user log dir (``user_log_dir``)
  26. and also:
  27. - is a single module so other Python packages can include their own private copy
  28. - is slightly opinionated on the directory names used. Look for "OPINION" in
  29. documentation and code for when an opinion is being applied.
  30. some example output
  31. ===================
  32. On Mac OS X::
  33. >>> from appdirs import *
  34. >>> appname = "SuperApp"
  35. >>> appauthor = "Acme"
  36. >>> user_data_dir(appname, appauthor)
  37. '/Users/trentm/Library/Application Support/SuperApp'
  38. >>> site_data_dir(appname, appauthor)
  39. '/Library/Application Support/SuperApp'
  40. >>> user_cache_dir(appname, appauthor)
  41. '/Users/trentm/Library/Caches/SuperApp'
  42. >>> user_log_dir(appname, appauthor)
  43. '/Users/trentm/Library/Logs/SuperApp'
  44. On Windows 7::
  45. >>> from appdirs import *
  46. >>> appname = "SuperApp"
  47. >>> appauthor = "Acme"
  48. >>> user_data_dir(appname, appauthor)
  49. 'C:\\Users\\trentm\\AppData\\Local\\Acme\\SuperApp'
  50. >>> user_data_dir(appname, appauthor, roaming=True)
  51. 'C:\\Users\\trentm\\AppData\\Roaming\\Acme\\SuperApp'
  52. >>> user_cache_dir(appname, appauthor)
  53. 'C:\\Users\\trentm\\AppData\\Local\\Acme\\SuperApp\\Cache'
  54. >>> user_log_dir(appname, appauthor)
  55. 'C:\\Users\\trentm\\AppData\\Local\\Acme\\SuperApp\\Logs'
  56. On Linux::
  57. >>> from appdirs import *
  58. >>> appname = "SuperApp"
  59. >>> appauthor = "Acme"
  60. >>> user_data_dir(appname, appauthor)
  61. '/home/trentm/.local/share/SuperApp
  62. >>> site_data_dir(appname, appauthor)
  63. '/usr/local/share/SuperApp'
  64. >>> site_data_dir(appname, appauthor, multipath=True)
  65. '/usr/local/share/SuperApp:/usr/share/SuperApp'
  66. >>> user_cache_dir(appname, appauthor)
  67. '/home/trentm/.cache/SuperApp'
  68. >>> user_log_dir(appname, appauthor)
  69. '/home/trentm/.cache/SuperApp/log'
  70. >>> user_config_dir(appname)
  71. '/home/trentm/.config/SuperApp'
  72. >>> site_config_dir(appname)
  73. '/etc/xdg/SuperApp'
  74. >>> os.environ['XDG_CONFIG_DIRS'] = '/etc:/usr/local/etc'
  75. >>> site_config_dir(appname, multipath=True)
  76. '/etc/SuperApp:/usr/local/etc/SuperApp'
  77. ``AppDirs`` for convenience
  78. ===========================
  79. ::
  80. >>> from appdirs import AppDirs
  81. >>> dirs = AppDirs("SuperApp", "Acme")
  82. >>> dirs.user_data_dir
  83. '/Users/trentm/Library/Application Support/SuperApp'
  84. >>> dirs.site_data_dir
  85. '/Library/Application Support/SuperApp'
  86. >>> dirs.user_cache_dir
  87. '/Users/trentm/Library/Caches/SuperApp'
  88. >>> dirs.user_log_dir
  89. '/Users/trentm/Library/Logs/SuperApp'
  90. Per-version isolation
  91. =====================
  92. If you have multiple versions of your app in use that you want to be
  93. able to run side-by-side, then you may want version-isolation for these
  94. dirs::
  95. >>> from appdirs import AppDirs
  96. >>> dirs = AppDirs("SuperApp", "Acme", version="1.0")
  97. >>> dirs.user_data_dir
  98. '/Users/trentm/Library/Application Support/SuperApp/1.0'
  99. >>> dirs.site_data_dir
  100. '/Library/Application Support/SuperApp/1.0'
  101. >>> dirs.user_cache_dir
  102. '/Users/trentm/Library/Caches/SuperApp/1.0'
  103. >>> dirs.user_log_dir
  104. '/Users/trentm/Library/Logs/SuperApp/1.0'
  105. appdirs Changelog
  106. =================
  107. appdirs 1.4.3
  108. -------------
  109. - [PR #76] Python 3.6 invalid escape sequence deprecation fixes
  110. - Fix for Python 3.6 support
  111. appdirs 1.4.2
  112. -------------
  113. - [PR #84] Allow installing without setuptools
  114. - [PR #86] Fix string delimiters in setup.py description
  115. - Add Python 3.6 support
  116. appdirs 1.4.1
  117. -------------
  118. - [issue #38] Fix _winreg import on Windows Py3
  119. - [issue #55] Make appname optional
  120. appdirs 1.4.0
  121. -------------
  122. - [PR #42] AppAuthor is now optional on Windows
  123. - [issue 41] Support Jython on Windows, Mac, and Unix-like platforms. Windows
  124. support requires `JNA <https://github.com/twall/jna>`_.
  125. - [PR #44] Fix incorrect behaviour of the site_config_dir method
  126. appdirs 1.3.0
  127. -------------
  128. - [Unix, issue 16] Conform to XDG standard, instead of breaking it for
  129. everybody
  130. - [Unix] Removes gratuitous case mangling of the case, since \*nix-es are
  131. usually case sensitive, so mangling is not wise
  132. - [Unix] Fixes the utterly wrong behaviour in ``site_data_dir``, return result
  133. based on XDG_DATA_DIRS and make room for respecting the standard which
  134. specifies XDG_DATA_DIRS is a multiple-value variable
  135. - [Issue 6] Add ``*_config_dir`` which are distinct on nix-es, according to
  136. XDG specs; on Windows and Mac return the corresponding ``*_data_dir``
  137. appdirs 1.2.0
  138. -------------
  139. - [Unix] Put ``user_log_dir`` under the *cache* dir on Unix. Seems to be more
  140. typical.
  141. - [issue 9] Make ``unicode`` work on py3k.
  142. appdirs 1.1.0
  143. -------------
  144. - [issue 4] Add ``AppDirs.user_log_dir``.
  145. - [Unix, issue 2, issue 7] appdirs now conforms to `XDG base directory spec
  146. <http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html>`_.
  147. - [Mac, issue 5] Fix ``site_data_dir()`` on Mac.
  148. - [Mac] Drop use of 'Carbon' module in favour of hardcoded paths; supports
  149. Python3 now.
  150. - [Windows] Append "Cache" to ``user_cache_dir`` on Windows by default. Use
  151. ``opinion=False`` option to disable this.
  152. - Add ``appdirs.AppDirs`` convenience class. Usage:
  153. >>> dirs = AppDirs("SuperApp", "Acme", version="1.0")
  154. >>> dirs.user_data_dir
  155. '/Users/trentm/Library/Application Support/SuperApp/1.0'
  156. - [Windows] Cherry-pick Komodo's change to downgrade paths to the Windows short
  157. paths if there are high bit chars.
  158. - [Linux] Change default ``user_cache_dir()`` on Linux to be singular, e.g.
  159. "~/.superapp/cache".
  160. - [Windows] Add ``roaming`` option to ``user_data_dir()`` (for use on Windows only)
  161. and change the default ``user_data_dir`` behaviour to use a *non*-roaming
  162. profile dir (``CSIDL_LOCAL_APPDATA`` instead of ``CSIDL_APPDATA``). Why? Because
  163. a large roaming profile can cause login speed issues. The "only syncs on
  164. logout" behaviour can cause surprises in appdata info.
  165. appdirs 1.0.1 (never released)
  166. ------------------------------
  167. Started this changelog 27 July 2010. Before that this module originated in the
  168. `Komodo <http://www.activestate.com/komodo>`_ product as ``applib.py`` and then
  169. as `applib/location.py
  170. <http://github.com/ActiveState/applib/blob/master/applib/location.py>`_ (used by
  171. `PyPM <http://code.activestate.com/pypm/>`_ in `ActivePython
  172. <http://www.activestate.com/activepython>`_). This is basically a fork of
  173. applib.py 1.0.1 and applib/location.py 1.0.1.