1 """Functions for finding cola resources"""
2 from __future__
import absolute_import
, division
, print_function
, unicode_literals
4 from os
.path
import dirname
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')):
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
))
27 # this is the source tree
28 # __file__ = '$prefix/cola/__file__.py'
29 _prefix
= dirname(_package
)
33 """Return the installation prefix"""
38 """Return a path relative to cola's installation prefix"""
39 return os
.path
.join(get_prefix(), *args
)
43 """Return a command from the bin/ directory"""
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
):
53 elif core
.exists(scripts_exe_path
):
54 result
= scripts_exe_path
55 elif core
.exists(scripts_path
):
60 result
= prefix('bin', name
)
65 """Return a path relative to cola's /usr/share/doc/ directory"""
66 return share('doc', 'git-cola', *args
)
70 """Return a path relative to cola's i18n locale directory, eg. cola/i18n"""
71 return package_data('i18n', *args
)
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
:
81 if core
.exists(docdir
):
83 return doc('git-cola.rst')
87 """Open the HTML documentation in a browser"""
89 webbrowser
.open_new_tab('file://' + url
)
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
)
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')
120 theme_dir
= package_data('icons', theme
)
121 if os
.path
.isabs(theme
) and os
.path
.isdir(theme
):
123 elif os
.path
.isdir(theme_dir
):
126 icons
= package_data('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
)
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.
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
))
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
):
171 candidate
= os
.path
.join(path
, subpath
)
172 if validate(candidate
):
174 # Nothing was found so 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
)