PageRenderTime 23ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 1ms

/monitor_batch/pymodules/python2.7/lib/python/clint-0.5.1-py2.7.egg/clint/packages/appdirs.py

https://gitlab.com/pooja043/Globus_Docker_4
Python | 346 lines | 323 code | 10 blank | 13 comment | 1 complexity | 2e09d65a41f6c875e8094081f2f657c9 MD5 | raw file
  1. #!/usr/bin/env python
  2. # Copyright (c) 2005-2010 ActiveState Software Inc.
  3. """Utilities for determining application-specific dirs.
  4. See <http://github.com/ActiveState/appdirs> for details and usage.
  5. """
  6. # Dev Notes:
  7. # - MSDN on where to store app data files:
  8. # http://support.microsoft.com/default.aspx?scid=kb;en-us;310294#XSLTH3194121123120121120120
  9. # - Mac OS X: http://developer.apple.com/documentation/MacOSX/Conceptual/BPFileSystem/index.html
  10. # - XDG spec for Un*x: http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html
  11. __version_info__ = (1, 2, 0)
  12. __version__ = '.'.join(map(str, __version_info__))
  13. import sys
  14. import os
  15. PY3 = sys.version_info[0] == 3
  16. if PY3:
  17. unicode = str
  18. class AppDirsError(Exception):
  19. pass
  20. def user_data_dir(appname, appauthor=None, version=None, roaming=False):
  21. r"""Return full path to the user-specific data dir for this application.
  22. "appname" is the name of application.
  23. "appauthor" (only required and used on Windows) is the name of the
  24. appauthor or distributing body for this application. Typically
  25. it is the owning company name.
  26. "version" is an optional version path element to append to the
  27. path. You might want to use this if you want multiple versions
  28. of your app to be able to run independently. If used, this
  29. would typically be "<major>.<minor>".
  30. "roaming" (boolean, default False) can be set True to use the Windows
  31. roaming appdata directory. That means that for users on a Windows
  32. network setup for roaming profiles, this user data will be
  33. sync'd on login. See
  34. <http://technet.microsoft.com/en-us/library/cc766489(WS.10).aspx>
  35. for a discussion of issues.
  36. Typical user data directories are:
  37. Mac OS X: ~/Library/Application Support/<AppName>
  38. Unix: ~/.config/<appname> # or in $XDG_CONFIG_HOME if defined
  39. Win XP (not roaming): C:\Documents and Settings\<username>\Application Data\<AppAuthor>\<AppName>
  40. Win XP (roaming): C:\Documents and Settings\<username>\Local Settings\Application Data\<AppAuthor>\<AppName>
  41. Win 7 (not roaming): C:\Users\<username>\AppData\Local\<AppAuthor>\<AppName>
  42. Win 7 (roaming): C:\Users\<username>\AppData\Roaming\<AppAuthor>\<AppName>
  43. For Unix, we follow the XDG spec and support $XDG_CONFIG_HOME. We don't
  44. use $XDG_DATA_HOME as that data dir is mostly used at the time of
  45. installation, instead of the application adding data during runtime.
  46. Also, in practice, Linux apps tend to store their data in
  47. "~/.config/<appname>" instead of "~/.local/share/<appname>".
  48. """
  49. if sys.platform.startswith("win"):
  50. if appauthor is None:
  51. raise AppDirsError("must specify 'appauthor' on Windows")
  52. const = roaming and "CSIDL_APPDATA" or "CSIDL_LOCAL_APPDATA"
  53. path = os.path.join(_get_win_folder(const), appauthor, appname)
  54. elif sys.platform == 'darwin':
  55. path = os.path.join(
  56. os.path.expanduser('~/Library/Application Support/'),
  57. appname)
  58. else:
  59. path = os.path.join(
  60. os.getenv('XDG_CONFIG_HOME', os.path.expanduser("~/.config")),
  61. appname.lower())
  62. if version:
  63. path = os.path.join(path, version)
  64. return path
  65. def site_data_dir(appname, appauthor=None, version=None):
  66. """Return full path to the user-shared data dir for this application.
  67. "appname" is the name of application.
  68. "appauthor" (only required and used on Windows) is the name of the
  69. appauthor or distributing body for this application. Typically
  70. it is the owning company name.
  71. "version" is an optional version path element to append to the
  72. path. You might want to use this if you want multiple versions
  73. of your app to be able to run independently. If used, this
  74. would typically be "<major>.<minor>".
  75. Typical user data directories are:
  76. Mac OS X: /Library/Application Support/<AppName>
  77. Unix: /etc/xdg/<appname>
  78. Win XP: C:\Documents and Settings\All Users\Application Data\<AppAuthor>\<AppName>
  79. Vista: (Fail! "C:\ProgramData" is a hidden *system* directory on Vista.)
  80. Win 7: C:\ProgramData\<AppAuthor>\<AppName> # Hidden, but writeable on Win 7.
  81. For Unix, this is using the $XDG_CONFIG_DIRS[0] default.
  82. WARNING: Do not use this on Windows. See the Vista-Fail note above for why.
  83. """
  84. if sys.platform.startswith("win"):
  85. if appauthor is None:
  86. raise AppDirsError("must specify 'appauthor' on Windows")
  87. path = os.path.join(_get_win_folder("CSIDL_COMMON_APPDATA"),
  88. appauthor, appname)
  89. elif sys.platform == 'darwin':
  90. path = os.path.join(
  91. os.path.expanduser('/Library/Application Support'),
  92. appname)
  93. else:
  94. # XDG default for $XDG_CONFIG_DIRS[0]. Perhaps should actually
  95. # *use* that envvar, if defined.
  96. path = "/etc/xdg/"+appname.lower()
  97. if version:
  98. path = os.path.join(path, version)
  99. return path
  100. def user_cache_dir(appname, appauthor=None, version=None, opinion=True):
  101. r"""Return full path to the user-specific cache dir for this application.
  102. "appname" is the name of application.
  103. "appauthor" (only required and used on Windows) is the name of the
  104. appauthor or distributing body for this application. Typically
  105. it is the owning company name.
  106. "version" is an optional version path element to append to the
  107. path. You might want to use this if you want multiple versions
  108. of your app to be able to run independently. If used, this
  109. would typically be "<major>.<minor>".
  110. "opinion" (boolean) can be False to disable the appending of
  111. "Cache" to the base app data dir for Windows. See
  112. discussion below.
  113. Typical user cache directories are:
  114. Mac OS X: ~/Library/Caches/<AppName>
  115. Unix: ~/.cache/<appname> (XDG default)
  116. Win XP: C:\Documents and Settings\<username>\Local Settings\Application Data\<AppAuthor>\<AppName>\Cache
  117. Vista: C:\Users\<username>\AppData\Local\<AppAuthor>\<AppName>\Cache
  118. On Windows the only suggestion in the MSDN docs is that local settings go in
  119. the `CSIDL_LOCAL_APPDATA` directory. This is identical to the non-roaming
  120. app data dir (the default returned by `user_data_dir` above). Apps typically
  121. put cache data somewhere *under* the given dir here. Some examples:
  122. ...\Mozilla\Firefox\Profiles\<ProfileName>\Cache
  123. ...\Acme\SuperApp\Cache\1.0
  124. OPINION: This function appends "Cache" to the `CSIDL_LOCAL_APPDATA` value.
  125. This can be disabled with the `opinion=False` option.
  126. """
  127. if sys.platform.startswith("win"):
  128. if appauthor is None:
  129. raise AppDirsError("must specify 'appauthor' on Windows")
  130. path = os.path.join(_get_win_folder("CSIDL_LOCAL_APPDATA"),
  131. appauthor, appname)
  132. if opinion:
  133. path = os.path.join(path, "Cache")
  134. elif sys.platform == 'darwin':
  135. path = os.path.join(
  136. os.path.expanduser('~/Library/Caches'),
  137. appname)
  138. else:
  139. path = os.path.join(
  140. os.getenv('XDG_CACHE_HOME', os.path.expanduser('~/.cache')),
  141. appname.lower())
  142. if version:
  143. path = os.path.join(path, version)
  144. return path
  145. def user_log_dir(appname, appauthor=None, version=None, opinion=True):
  146. r"""Return full path to the user-specific log dir for this application.
  147. "appname" is the name of application.
  148. "appauthor" (only required and used on Windows) is the name of the
  149. appauthor or distributing body for this application. Typically
  150. it is the owning company name.
  151. "version" is an optional version path element to append to the
  152. path. You might want to use this if you want multiple versions
  153. of your app to be able to run independently. If used, this
  154. would typically be "<major>.<minor>".
  155. "opinion" (boolean) can be False to disable the appending of
  156. "Logs" to the base app data dir for Windows, and "log" to the
  157. base cache dir for Unix. See discussion below.
  158. Typical user cache directories are:
  159. Mac OS X: ~/Library/Logs/<AppName>
  160. Unix: ~/.cache/<appname>/log # or under $XDG_CACHE_HOME if defined
  161. Win XP: C:\Documents and Settings\<username>\Local Settings\Application Data\<AppAuthor>\<AppName>\Logs
  162. Vista: C:\Users\<username>\AppData\Local\<AppAuthor>\<AppName>\Logs
  163. On Windows the only suggestion in the MSDN docs is that local settings
  164. go in the `CSIDL_LOCAL_APPDATA` directory. (Note: I'm interested in
  165. examples of what some windows apps use for a logs dir.)
  166. OPINION: This function appends "Logs" to the `CSIDL_LOCAL_APPDATA`
  167. value for Windows and appends "log" to the user cache dir for Unix.
  168. This can be disabled with the `opinion=False` option.
  169. """
  170. if sys.platform == "darwin":
  171. path = os.path.join(
  172. os.path.expanduser('~/Library/Logs'),
  173. appname)
  174. elif sys.platform == "win32":
  175. path = user_data_dir(appname, appauthor, version); version=False
  176. if opinion:
  177. path = os.path.join(path, "Logs")
  178. else:
  179. path = user_cache_dir(appname, appauthor, version); version=False
  180. if opinion:
  181. path = os.path.join(path, "log")
  182. if version:
  183. path = os.path.join(path, version)
  184. return path
  185. class AppDirs(object):
  186. """Convenience wrapper for getting application dirs."""
  187. def __init__(self, appname, appauthor, version=None, roaming=False):
  188. self.appname = appname
  189. self.appauthor = appauthor
  190. self.version = version
  191. self.roaming = roaming
  192. @property
  193. def user_data_dir(self):
  194. return user_data_dir(self.appname, self.appauthor,
  195. version=self.version, roaming=self.roaming)
  196. @property
  197. def site_data_dir(self):
  198. return site_data_dir(self.appname, self.appauthor,
  199. version=self.version)
  200. @property
  201. def user_cache_dir(self):
  202. return user_cache_dir(self.appname, self.appauthor,
  203. version=self.version)
  204. @property
  205. def user_log_dir(self):
  206. return user_log_dir(self.appname, self.appauthor,
  207. version=self.version)
  208. #---- internal support stuff
  209. def _get_win_folder_from_registry(csidl_name):
  210. """This is a fallback technique at best. I'm not sure if using the
  211. registry for this guarantees us the correct answer for all CSIDL_*
  212. names.
  213. """
  214. import _winreg
  215. shell_folder_name = {
  216. "CSIDL_APPDATA": "AppData",
  217. "CSIDL_COMMON_APPDATA": "Common AppData",
  218. "CSIDL_LOCAL_APPDATA": "Local AppData",
  219. }[csidl_name]
  220. key = _winreg.OpenKey(_winreg.HKEY_CURRENT_USER,
  221. r"Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders")
  222. dir, type = _winreg.QueryValueEx(key, shell_folder_name)
  223. return dir
  224. def _get_win_folder_with_pywin32(csidl_name):
  225. from win32com.shell import shellcon, shell
  226. dir = shell.SHGetFolderPath(0, getattr(shellcon, csidl_name), 0, 0)
  227. # Try to make this a unicode path because SHGetFolderPath does
  228. # not return unicode strings when there is unicode data in the
  229. # path.
  230. try:
  231. dir = unicode(dir)
  232. # Downgrade to short path name if have highbit chars. See
  233. # <http://bugs.activestate.com/show_bug.cgi?id=85099>.
  234. has_high_char = False
  235. for c in dir:
  236. if ord(c) > 255:
  237. has_high_char = True
  238. break
  239. if has_high_char:
  240. try:
  241. import win32api
  242. dir = win32api.GetShortPathName(dir)
  243. except ImportError:
  244. pass
  245. except UnicodeError:
  246. pass
  247. return dir
  248. def _get_win_folder_with_ctypes(csidl_name):
  249. import ctypes
  250. csidl_const = {
  251. "CSIDL_APPDATA": 26,
  252. "CSIDL_COMMON_APPDATA": 35,
  253. "CSIDL_LOCAL_APPDATA": 28,
  254. }[csidl_name]
  255. buf = ctypes.create_unicode_buffer(1024)
  256. ctypes.windll.shell32.SHGetFolderPathW(None, csidl_const, None, 0, buf)
  257. # Downgrade to short path name if have highbit chars. See
  258. # <http://bugs.activestate.com/show_bug.cgi?id=85099>.
  259. has_high_char = False
  260. for c in buf:
  261. if ord(c) > 255:
  262. has_high_char = True
  263. break
  264. if has_high_char:
  265. buf2 = ctypes.create_unicode_buffer(1024)
  266. if ctypes.windll.kernel32.GetShortPathNameW(buf.value, buf2, 1024):
  267. buf = buf2
  268. return buf.value
  269. if sys.platform == "win32":
  270. try:
  271. import win32com.shell
  272. _get_win_folder = _get_win_folder_with_pywin32
  273. except ImportError:
  274. try:
  275. import ctypes
  276. _get_win_folder = _get_win_folder_with_ctypes
  277. except ImportError:
  278. _get_win_folder = _get_win_folder_from_registry
  279. #---- self test code
  280. if __name__ == "__main__":
  281. appname = "MyApp"
  282. appauthor = "MyCompany"
  283. props = ("user_data_dir", "site_data_dir", "user_cache_dir",
  284. "user_log_dir")
  285. print("-- app dirs (without optional 'version')")
  286. dirs = AppDirs(appname, appauthor, version="1.0")
  287. for prop in props:
  288. print("%s: %s" % (prop, getattr(dirs, prop)))
  289. print("\n-- app dirs (with optional 'version')")
  290. dirs = AppDirs(appname, appauthor)
  291. for prop in props:
  292. print("%s: %s" % (prop, getattr(dirs, prop)))