dev: format code using "garden fmt" (black)
[git-cola.git] / cola / resources.py
blobeca99817706e9ea1d211e4a11fe4e50291d1ba48
1 """Functions for finding cola resources"""
2 from __future__ import absolute_import, division, print_function, unicode_literals
3 import os
4 from os.path import dirname
5 import webbrowser
7 from . import core
8 from . import compat
11 # Default git-cola icon theme
12 _default_icon_theme = 'light'
14 _resources = core.abspath(core.realpath(__file__))
15 _package = os.path.dirname(_resources)
17 if _package.endswith(os.path.join('site-packages', 'cola')):
18 # Unix release tree
19 # __file__ = '$prefix/lib/pythonX.Y/site-packages/cola/__file__.py'
20 # _package = '$prefix/lib/pythonX.Y/site-packages/cola'
21 _prefix = dirname(dirname(dirname(dirname(_package))))
22 elif _package.endswith(os.path.join('pkgs', 'cola')):
23 # Windows release tree
24 # __file__ = $installdir/pkgs/cola
25 _prefix = dirname(dirname(_package))
26 else:
27 # this is the source tree
28 # __file__ = '$prefix/cola/__file__.py'
29 _prefix = dirname(_package)
32 def get_prefix():
33 """Return the installation prefix"""
34 return _prefix
37 def prefix(*args):
38 """Return a path relative to cola's installation prefix"""
39 return os.path.join(get_prefix(), *args)
42 def command(name):
43 """Return a command from the bin/ directory"""
44 if compat.WIN32:
45 # Check for "${name}.exe" on Windows.
46 exe_path = prefix('bin', '%s.exe' % name)
47 scripts_exe_path = prefix('Scripts', '%s.exe' % name)
48 scripts_path = prefix('Scripts', name)
49 path = prefix('bin', name)
51 if core.exists(exe_path):
52 result = exe_path
53 elif core.exists(scripts_exe_path):
54 result = scripts_exe_path
55 elif core.exists(scripts_path):
56 result = scripts_path
57 else:
58 result = path
59 else:
60 result = prefix('bin', name)
61 return result
64 def doc(*args):
65 """Return a path relative to cola's /usr/share/doc/ directory"""
66 return share('doc', 'git-cola', *args)
69 def i18n(*args):
70 """Return a path relative to cola's i18n locale directory, eg. cola/i18n"""
71 return package_data('i18n', *args)
74 def html_docs():
75 """Return the path to the cola html documentation."""
76 # html/index.html only exists after the install-docs target is run.
77 # Fallback to the source tree and lastly git-cola.rst.
78 paths_to_try = (('html', 'index.html'), ('_build', 'html', 'index.html'))
79 for paths in paths_to_try:
80 docdir = doc(*paths)
81 if core.exists(docdir):
82 return docdir
83 return doc('git-cola.rst')
86 def show_html_docs():
87 """Open the HTML documentation in a browser"""
88 url = html_docs()
89 webbrowser.open_new_tab('file://' + url)
92 def share(*args):
93 """Return a path relative to cola's /usr/share/ directory"""
94 return prefix('share', *args)
97 def package_data(*args):
98 """Return a path relative to cola's Python modules"""
99 return os.path.join(_package, *args)
102 def package_command(*args):
103 """Return a path relative to cola's private bin/ directory"""
104 return package_data('bin', *args)
107 def icon_dir(theme):
108 """Return the icons directory for the specified theme
110 This returns the ``icons`` directory inside the ``cola`` Python package.
111 When theme is defined then it will return a subdirectory of the icons/
112 directory, e.g. "dark" for the dark icon theme.
114 When theme is set to an absolute directory path, that directory will be
115 returned, which effectively makes git-cola use those icons.
117 if not theme or theme == _default_icon_theme:
118 icons = package_data('icons')
119 else:
120 theme_dir = package_data('icons', theme)
121 if os.path.isabs(theme) and os.path.isdir(theme):
122 icons = theme
123 elif os.path.isdir(theme_dir):
124 icons = theme_dir
125 else:
126 icons = package_data('icons')
128 return icons
131 def xdg_config_home(*args):
132 """Return the XDG_CONFIG_HOME configuration directory, eg. ~/.config"""
133 config = core.getenv(
134 'XDG_CONFIG_HOME', os.path.join(core.expanduser('~'), '.config')
136 return os.path.join(config, *args)
139 def xdg_data_home(*args):
140 """Return the XDG_DATA_HOME configuration directory, eg. ~/.local/share"""
141 config = core.getenv(
142 'XDG_DATA_HOME', os.path.join(core.expanduser('~'), '.local', 'share')
144 return os.path.join(config, *args)
147 def xdg_data_dirs():
148 """Return the current set of XDG data directories
150 Returns the values from $XDG_DATA_DIRS when defined in the environment.
151 If $XDG_DATA_DIRS is either not set or empty, a value equal to
152 /usr/local/share:/usr/share is used.
154 paths = []
155 xdg_data_home_dir = xdg_data_home()
156 if os.path.isdir(xdg_data_home_dir):
157 paths.append(xdg_data_home_dir)
159 xdg_data_dirs_env = core.getenv('XDG_DATA_DIRS', '')
160 if not xdg_data_dirs_env:
161 xdg_data_dirs_env = '/usr/local/share:/usr/share'
162 paths.extend(path for path in xdg_data_dirs_env.split(':') if os.path.isdir(path))
163 return paths
166 def find_first(subpath, paths, validate=os.path.isfile):
167 """Return the first `subpath` found in the specified directory paths"""
168 if os.path.isabs(subpath):
169 return subpath
170 for path in paths:
171 candidate = os.path.join(path, subpath)
172 if validate(candidate):
173 return candidate
174 # Nothing was found so return None.
175 return None
178 def config_home(*args):
179 """Return git-cola's configuration directory, eg. ~/.config/git-cola"""
180 return xdg_config_home('git-cola', *args)