dag: add word wrapping to the diff widget
[git-cola.git] / cola / resources.py
blob2451ebb8d482a1d64c068c16cb734432613932a9
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 config_home(*args):
140 """Return git-cola's configuration directory, eg. ~/.config/git-cola"""
141 return xdg_config_home('git-cola', *args)