gitcmds: fix pylint violations
[git-cola.git] / cola / models / prefs.py
blob0d828c51a05037fadc0cd6e7501bb732a6e43b04
1 from __future__ import division, absolute_import, unicode_literals
2 import sys
4 from .. import core
5 from .. import hidpi
6 from .. import observable
7 from .. import utils
8 from ..cmd import Command
11 AUTOCOMPLETE_PATHS = 'cola.autocompletepaths'
12 AUTOTEMPLATE = 'cola.autoloadcommittemplate'
13 BACKGROUND_EDITOR = 'cola.backgroundeditor'
14 BLAME_VIEWER = 'cola.blameviewer'
15 BOLD_HEADERS = 'cola.boldheaders'
16 CHECK_CONFLICTS = 'cola.checkconflicts'
17 CHECK_PUBLISHED_COMMITS = 'cola.checkpublishedcommits'
18 COMMENT_CHAR = 'core.commentchar'
19 DIFFCONTEXT = 'gui.diffcontext'
20 DIFFTOOL = 'diff.tool'
21 DISPLAY_UNTRACKED = 'gui.displayuntracked'
22 EDITOR = 'gui.editor'
23 EXPANDTAB = 'cola.expandtab'
24 FONTDIFF = 'cola.fontdiff'
25 HIDPI = 'cola.hidpi'
26 HISTORY_BROWSER = 'gui.historybrowser'
27 ICON_THEME = 'cola.icontheme'
28 LINEBREAK = 'cola.linebreak'
29 MAXRECENT = 'cola.maxrecent'
30 MERGE_DIFFSTAT = 'merge.diffstat'
31 MERGE_KEEPBACKUP = 'merge.keepbackup'
32 MERGE_SUMMARY = 'merge.summary'
33 MERGE_VERBOSITY = 'merge.verbosity'
34 MERGETOOL = 'merge.tool'
35 RESIZE_BROWSER_COLUMNS = 'cola.resizebrowsercolumns'
36 SAFE_MODE = 'cola.safemode'
37 SAVEWINDOWSETTINGS = 'cola.savewindowsettings'
38 SHOW_PATH = 'cola.showpath'
39 SORT_BOOKMARKS = 'cola.sortbookmarks'
40 SPELL_CHECK = 'cola.spellcheck'
41 STATUS_INDENT = 'cola.statusindent'
42 STATUS_SHOW_TOTALS = 'cola.statusshowtotals'
43 THEME = 'cola.theme'
44 TABWIDTH = 'cola.tabwidth'
45 TEXTWIDTH = 'cola.textwidth'
46 USER_EMAIL = 'user.email'
47 USER_NAME = 'user.name'
50 class Defaults(object):
51 """Read-only class for holding defaults that get overridden"""
53 # These should match Git's defaults for git-defined values.
54 autotemplate = False
55 blame_viewer = 'git gui blame'
56 bold_headers = False
57 check_conflicts = True
58 check_published_commits = True
59 comment_char = '#'
60 display_untracked = True
61 diff_context = 5
62 difftool = 'xxdiff'
63 editor = 'gvim'
64 expandtab = False
65 history_browser = 'gitk'
66 icon_theme = 'default'
67 linebreak = True
68 maxrecent = 8
69 mergetool = difftool
70 merge_diffstat = True
71 merge_keep_backup = True
72 merge_summary = True
73 merge_verbosity = 2
74 resize_browser_columns = False
75 save_window_settings = True
76 safe_mode = False
77 autocomplete_paths = True
78 show_path = True
79 sort_bookmarks = True
80 spellcheck = False
81 tabwidth = 8
82 textwidth = 72
83 theme = 'default'
84 hidpi = hidpi.Option.AUTO
85 status_indent = False
86 status_show_totals = False
89 def blame_viewer(context):
90 """Return the configured "blame" viewer"""
91 default = Defaults.blame_viewer
92 return context.cfg.get(BLAME_VIEWER, default=default)
95 def bold_headers(context):
96 """Should we bold the Status column headers?"""
97 return context.cfg.get(BOLD_HEADERS, default=Defaults.bold_headers)
100 def check_conflicts(context):
101 """Should we check for merge conflict markers in unmerged files?"""
102 return context.cfg.get(CHECK_CONFLICTS, default=Defaults.check_conflicts)
105 def check_published_commits(context):
106 """Should we check for published commits when amending?"""
107 return context.cfg.get(
108 CHECK_PUBLISHED_COMMITS, default=Defaults.check_published_commits
112 def display_untracked(context):
113 """Should we display untracked files?"""
114 return context.cfg.get(DISPLAY_UNTRACKED, default=Defaults.display_untracked)
117 def editor(context):
118 """Return the configured editor"""
119 app = context.cfg.get(EDITOR, default=Defaults.editor)
120 return _remap_editor(app)
123 def background_editor(context):
124 """Return the configured non-blocking background editor"""
125 app = context.cfg.get(BACKGROUND_EDITOR, default=editor(context))
126 return _remap_editor(app)
129 def _remap_editor(app):
130 """Remap a configured editorinto a visual editor name"""
131 # We do this for vim users because this configuration is convenient.
132 return {'vim': 'gvim -f'}.get(app, app)
135 def comment_char(context):
136 """Return the configured git commit comment character"""
137 return context.cfg.get(COMMENT_CHAR, default=Defaults.comment_char)
140 def default_history_browser():
141 """Return the default history browser (e.g. git-dag, gitk)"""
142 if utils.is_win32():
143 # On Windows, a sensible default is "python git-cola dag"
144 # which is different than `gitk` below, but is preferred
145 # because we don't have to guess paths.
146 git_cola = sys.argv[0].replace('\\', '/')
147 python = sys.executable.replace('\\', '/')
148 cwd = core.getcwd().replace('\\', '/')
149 argv = [python, git_cola, 'dag', '--repo', cwd]
150 argv = core.prep_for_subprocess(argv)
151 default = core.list2cmdline(argv)
152 else:
153 # The `gitk` script can be launched as-is on unix
154 default = Defaults.history_browser
155 return default
158 def history_browser(context):
159 """Return the configured history browser"""
160 default = default_history_browser()
161 return context.cfg.get(HISTORY_BROWSER, default=default)
164 def linebreak(context):
165 """Should we word-wrap lines in the commit message editor?"""
166 return context.cfg.get(LINEBREAK, default=Defaults.linebreak)
169 def maxrecent(context):
170 """Return the configured maximum number of Recent Repositories"""
171 value = Defaults.maxrecent
172 if context:
173 value = context.cfg.get(MAXRECENT, default=value)
174 return value
177 def spellcheck(context):
178 """Should we spellcheck commit messages?"""
179 return context.cfg.get(SPELL_CHECK, default=Defaults.spellcheck)
182 def expandtab(context):
183 """Should we expand tabs in commit messages?"""
184 return context.cfg.get(EXPANDTAB, default=Defaults.expandtab)
187 def sort_bookmarks(context):
188 """Should we sort bookmarks by name?"""
189 return context.cfg.get(SORT_BOOKMARKS, default=Defaults.sort_bookmarks)
192 def tabwidth(context):
193 """Return the configured tab width in the commit message editor"""
194 return context.cfg.get(TABWIDTH, default=Defaults.tabwidth)
197 def textwidth(context):
198 """Return the configured text width for word wrapping commit messages"""
199 return context.cfg.get(TEXTWIDTH, default=Defaults.textwidth)
202 def status_indent(context):
203 """Should we indent items in the status widget?"""
204 return context.cfg.get(STATUS_INDENT, default=Defaults.status_indent)
207 def status_show_totals(context):
208 """Should we display count totals in the status widget headers?"""
209 return context.cfg.get(STATUS_SHOW_TOTALS, default=Defaults.status_show_totals)
212 class PreferencesModel(observable.Observable):
213 """Interact with repo-local and user-global git config preferences"""
215 message_config_updated = 'config_updated'
217 def __init__(self, context):
218 observable.Observable.__init__(self)
219 self.context = context
220 self.config = context.cfg
222 def set_config(self, source, config, value):
223 """Set a configuration value"""
224 if source == 'repo':
225 self.config.set_repo(config, value)
226 else:
227 self.config.set_user(config, value)
228 message = self.message_config_updated
229 self.notify_observers(message, source, config, value)
231 def get_config(self, source, config):
232 """Get a configured value"""
233 if source == 'repo':
234 value = self.config.get_repo(config)
235 else:
236 value = self.config.get(config)
237 return value
240 class SetConfig(Command):
241 """Store a gitconfig value"""
243 UNDOABLE = True
245 def __init__(self, model, source, config, value):
246 self.source = source
247 self.config = config
248 self.value = value
249 self.old_value = None
250 self.model = model
252 def do(self):
253 """Modify the model and store the updated configuration"""
254 self.old_value = self.model.get_config(self.source, self.config)
255 self.model.set_config(self.source, self.config, self.value)
257 def undo(self):
258 """Restore the configuration change to its original value"""
259 if self.old_value is None:
260 return
261 self.model.set_config(self.source, self.config, self.old_value)