1 """Functions for finding cola resources"""
3 from os
.path
import dirname
10 # Default git-cola icon theme
11 _default_icon_theme
= 'light'
13 _resources
= core
.abspath(core
.realpath(__file__
))
14 _package
= os
.path
.dirname(_resources
)
16 if _package
.endswith(os
.path
.join('site-packages', 'cola')):
18 # __file__ = '$prefix/lib/pythonX.Y/site-packages/cola/__file__.py'
19 # _package = '$prefix/lib/pythonX.Y/site-packages/cola'
20 _prefix
= dirname(dirname(dirname(dirname(_package
))))
21 elif _package
.endswith(os
.path
.join('pkgs', 'cola')):
22 # Windows release tree
23 # __file__ = $installdir/pkgs/cola
24 _prefix
= dirname(dirname(_package
))
26 # this is the source tree
27 # __file__ = '$prefix/cola/__file__.py'
28 _prefix
= dirname(_package
)
32 """Return the installation prefix"""
37 """Return a path relative to cola's installation prefix"""
38 return os
.path
.join(get_prefix(), *args
)
42 """Return a command from the bin/ directory"""
44 # Check for "${name}.exe" on Windows.
45 exe_path
= prefix('bin', '%s.exe' % name
)
46 scripts_exe_path
= prefix('Scripts', '%s.exe' % name
)
47 scripts_path
= prefix('Scripts', name
)
48 path
= prefix('bin', name
)
50 if core
.exists(exe_path
):
52 elif core
.exists(scripts_exe_path
):
53 result
= scripts_exe_path
54 elif core
.exists(scripts_path
):
59 result
= prefix('bin', name
)
64 """Return a path relative to cola's /usr/share/doc/ directory"""
65 return share('doc', 'git-cola', *args
)
69 """Return a path relative to cola's i18n locale directory, eg. cola/i18n"""
70 return package_data('i18n', *args
)
74 """Return the path to the cola html documentation."""
75 # html/index.html only exists after the install-docs target is run.
76 # Fallback to the source tree and lastly git-cola.rst.
77 paths_to_try
= (('html', 'index.html'), ('_build', 'html', 'index.html'))
78 for paths
in paths_to_try
:
80 if core
.exists(docdir
):
82 return doc('git-cola.rst')
86 """Open the HTML documentation in a browser"""
88 webbrowser
.open_new_tab('file://' + url
)
92 """Return a path relative to cola's /usr/share/ directory"""
93 return prefix('share', *args
)
96 def package_data(*args
):
97 """Return a path relative to cola's Python modules"""
98 return os
.path
.join(_package
, *args
)
101 def package_command(*args
):
102 """Return a path relative to cola's private bin/ directory"""
103 return package_data('bin', *args
)
107 """Return the icons directory for the specified theme
109 This returns the ``icons`` directory inside the ``cola`` Python package.
110 When theme is defined then it will return a subdirectory of the icons/
111 directory, e.g. "dark" for the dark icon theme.
113 When theme is set to an absolute directory path, that directory will be
114 returned, which effectively makes git-cola use those icons.
116 if not theme
or theme
== _default_icon_theme
:
117 icons
= package_data('icons')
119 theme_dir
= package_data('icons', theme
)
120 if os
.path
.isabs(theme
) and os
.path
.isdir(theme
):
122 elif os
.path
.isdir(theme_dir
):
125 icons
= package_data('icons')
130 def xdg_config_home(*args
):
131 """Return the XDG_CONFIG_HOME configuration directory, eg. ~/.config"""
132 config
= core
.getenv(
133 'XDG_CONFIG_HOME', os
.path
.join(core
.expanduser('~'), '.config')
135 return os
.path
.join(config
, *args
)
138 def xdg_data_home(*args
):
139 """Return the XDG_DATA_HOME configuration directory, eg. ~/.local/share"""
140 config
= core
.getenv(
141 'XDG_DATA_HOME', os
.path
.join(core
.expanduser('~'), '.local', 'share')
143 return os
.path
.join(config
, *args
)
147 """Return the current set of XDG data directories
149 Returns the values from $XDG_DATA_DIRS when defined in the environment.
150 If $XDG_DATA_DIRS is either not set or empty, a value equal to
151 /usr/local/share:/usr/share is used.
154 xdg_data_home_dir
= xdg_data_home()
155 if os
.path
.isdir(xdg_data_home_dir
):
156 paths
.append(xdg_data_home_dir
)
158 xdg_data_dirs_env
= core
.getenv('XDG_DATA_DIRS', '')
159 if not xdg_data_dirs_env
:
160 xdg_data_dirs_env
= '/usr/local/share:/usr/share'
161 paths
.extend(path
for path
in xdg_data_dirs_env
.split(':') if os
.path
.isdir(path
))
165 def find_first(subpath
, paths
, validate
=os
.path
.isfile
):
166 """Return the first `subpath` found in the specified directory paths"""
167 if os
.path
.isabs(subpath
):
170 candidate
= os
.path
.join(path
, subpath
)
171 if validate(candidate
):
173 # Nothing was found so return None.
177 def config_home(*args
):
178 """Return git-cola's configuration directory, eg. ~/.config/git-cola"""
179 return xdg_config_home('git-cola', *args
)