git-cola v2.11
[git-cola.git] / cola / resources.py
blob0cfac463a8eae5bb2c925c129b4f41fba95d5a66
1 """Provides the prefix() function for finding cola resources"""
2 from __future__ import division, absolute_import, unicode_literals
4 import os
5 import webbrowser
6 from os.path import dirname
8 from . import core
11 # Default git-cola icon theme
12 _default_icon_theme = 'light'
14 _modpath = core.abspath(__file__)
15 if os.path.join('share', 'git-cola', 'lib') in _modpath:
16 # this is the release tree
17 # __file__ = '$prefix/share/git-cola/lib/cola/__file__.py'
18 _lib_dir = dirname(dirname(_modpath))
19 _prefix = dirname(dirname(dirname(_lib_dir)))
20 elif os.path.join('pkgs', 'cola') in _modpath:
21 # Windows release tree
22 # __file__ = $installdir/pkgs/cola/resources.py
23 _prefix = dirname(dirname(dirname(_modpath)))
24 else:
25 # this is the source tree
26 # __file__ = '$prefix/cola/__file__.py'
27 _prefix = dirname(dirname(_modpath))
30 def prefix(*args):
31 """Return a path relative to cola's installation prefix"""
32 return os.path.join(_prefix, *args)
35 def doc(*args):
36 """Return a path relative to cola's /usr/share/doc/ directory"""
37 return os.path.join(_prefix, 'share', 'doc', 'git-cola', *args)
40 def html_docs():
41 """Return the path to the cola html documentation."""
42 # html/index.html only exists after the install-docs target is run.
43 # Fallback to the source tree and lastly git-cola.rst.
44 paths_to_try = (('html', 'index.html'),
45 ('_build', 'html', 'index.html'))
46 for paths in paths_to_try:
47 docdir = doc(*paths)
48 if core.exists(docdir):
49 return docdir
50 return doc('git-cola.rst')
53 def show_html_docs():
54 url = html_docs()
55 webbrowser.open_new_tab('file://' + url)
58 def share(*args):
59 """Return a path relative to cola's /usr/share/ directory"""
60 return prefix('share', 'git-cola', *args)
63 def icon_dir(theme):
64 """Return the path to the icons directory
66 This typically returns share/git-cola/icons within
67 the git-cola installation prefix.
69 When theme is defined then it will return a subdirectory of the icons/
70 directory, e.g. "dark" for the dark icon theme.
72 When theme is set to an absolute directory path, that directory will be
73 returned, which effectively makes git-cola use those icons.
75 """
77 if not theme or theme == _default_icon_theme:
78 icons = share('icons')
79 else:
80 theme_dir = share('icons', theme)
81 if os.path.isabs(theme) and os.path.isdir(theme):
82 icons = theme
83 elif os.path.isdir(theme_dir):
84 icons = theme_dir
85 else:
86 icons = share('icons')
88 return icons
91 def config_home(*args):
92 config = core.getenv('XDG_CONFIG_HOME',
93 os.path.join(core.expanduser('~'), '.config'))
94 return os.path.join(config, 'git-cola', *args)