toolbar: remove extraneous whitespace
[git-cola.git] / cola / resources.py
blobca1296a2c47d1ed4472ae27a0cf9cd575abd0c87
1 """Provides the prefix() function for finding cola resources"""
2 from __future__ import division, absolute_import, unicode_literals
3 import os
4 from os.path import dirname
5 import sys
6 import webbrowser
8 from . import core
11 # Default git-cola icon theme
12 _default_icon_theme = 'light'
14 _modpath = core.abspath(core.realpath(__file__))
15 if (
16 os.path.join('share', 'git-cola', 'lib') in _modpath
17 or os.path.join('site-packages', 'cola') in _modpath
19 # this is the release tree
20 # __file__ = '$prefix/share/git-cola/lib/cola/__file__.py'
21 _lib_dir = dirname(dirname(_modpath))
22 _prefix = dirname(dirname(dirname(_lib_dir)))
23 elif os.path.join('pkgs', 'cola') in _modpath:
24 # Windows release tree
25 # __file__ = $installdir/pkgs/cola/resources.py
26 _prefix = dirname(dirname(dirname(_modpath)))
27 else:
28 # this is the source tree
29 # __file__ = '$prefix/cola/__file__.py'
30 _prefix = dirname(dirname(_modpath))
33 def prefix(*args):
34 """Return a path relative to cola's installation prefix"""
35 return os.path.join(_prefix, *args)
38 def command(name):
39 """Return a command sibling to the main program"""
40 bindir = os.path.dirname(sys.argv[0])
41 return os.path.join(bindir, name)
44 def doc(*args):
45 """Return a path relative to cola's /usr/share/doc/ directory"""
46 return os.path.join(_prefix, 'share', 'doc', 'git-cola', *args)
49 def html_docs():
50 """Return the path to the cola html documentation."""
51 # html/index.html only exists after the install-docs target is run.
52 # Fallback to the source tree and lastly git-cola.rst.
53 paths_to_try = (('html', 'index.html'), ('_build', 'html', 'index.html'))
54 for paths in paths_to_try:
55 docdir = doc(*paths)
56 if core.exists(docdir):
57 return docdir
58 return doc('git-cola.rst')
61 def show_html_docs():
62 url = html_docs()
63 webbrowser.open_new_tab('file://' + url)
66 def share(*args):
67 """Return a path relative to cola's /usr/share/ directory"""
68 return prefix('share', 'git-cola', *args)
71 def icon_dir(theme):
72 """Return the path to the icons directory
74 This typically returns share/git-cola/icons within
75 the git-cola installation prefix.
77 When theme is defined then it will return a subdirectory of the icons/
78 directory, e.g. "dark" for the dark icon theme.
80 When theme is set to an absolute directory path, that directory will be
81 returned, which effectively makes git-cola use those icons.
83 """
85 if not theme or theme == _default_icon_theme:
86 icons = share('icons')
87 else:
88 theme_dir = share('icons', theme)
89 if os.path.isabs(theme) and os.path.isdir(theme):
90 icons = theme
91 elif os.path.isdir(theme_dir):
92 icons = theme_dir
93 else:
94 icons = share('icons')
96 return icons
99 def config_home(*args):
100 config = core.getenv(
101 'XDG_CONFIG_HOME', os.path.join(core.expanduser('~'), '.config')
103 return os.path.join(config, 'git-cola', *args)