/usr/share/git-cola/lib/cola/settings.py is in git-cola 1.9.3-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 | # Copyright (c) 2008 David Aguilar
"""This handles saving complex settings such as bookmarks, etc.
"""
import os
import sys
from cola import core
from cola import git
from cola import resources
from cola.compat import json
def mkdict(obj):
if type(obj) is dict:
return obj
else:
return {}
def mklist(obj):
if type(obj) is list:
return obj
else:
return []
class Settings(object):
_file = resources.config_home('settings')
bookmarks = property(lambda self: mklist(self.values['bookmarks']))
gui_state = property(lambda self: mkdict(self.values['gui_state']))
recent = property(lambda self: mklist(self.values['recent']))
def __init__(self, verify=git.is_git_worktree):
"""Load existing settings if they exist"""
self.values = {
'bookmarks': [],
'gui_state': {},
'recent': [],
}
self.verify = verify
self.load()
self.remove_missing()
def remove_missing(self):
missing_bookmarks = []
missing_recent = []
for bookmark in self.bookmarks:
if not self.verify(bookmark):
missing_bookmarks.append(bookmark)
for bookmark in missing_bookmarks:
try:
self.bookmarks.remove(bookmark)
except:
pass
for recent in self.recent:
if not self.verify(recent):
missing_recent.append(recent)
for recent in missing_recent:
try:
self.recent.remove(recent)
except:
pass
def add_bookmark(self, bookmark):
"""Adds a bookmark to the saved settings"""
if bookmark not in self.bookmarks:
self.bookmarks.append(bookmark)
def remove_bookmark(self, bookmark):
"""Removes a bookmark from the saved settings"""
if bookmark in self.bookmarks:
self.bookmarks.remove(bookmark)
def add_recent(self, entry):
if entry in self.recent:
self.recent.remove(entry)
self.recent.insert(0, entry)
if len(self.recent) > 8:
self.recent.pop()
def path(self):
return self._file
def save(self):
path = self.path()
try:
parent = os.path.dirname(path)
if not core.isdir(parent):
core.makedirs(parent)
with core.xopen(path, 'wb') as fp:
json.dump(self.values, fp, indent=4)
except:
sys.stderr.write('git-cola: error writing "%s"\n' % path)
def load(self):
self.values.update(self._load())
def _load(self):
path = self.path()
if not core.exists(path):
return self._load_dot_cola()
try:
fp = core.xopen(path, 'rb')
return mkdict(json.load(fp))
except: # bad json
return {}
def reload_recent(self):
values = self._load()
self.values['recent'] = mklist(values.get('recent', []))
def _load_dot_cola(self):
values = {}
path = os.path.join(core.expanduser('~'), '.cola')
if not core.exists(path):
return {}
try:
with core.xopen(path, 'r') as fp:
json_values = json.load(fp)
except: # bad json
return {}
# Keep only the entries we care about
for key in self.values:
try:
values[key] = json_values[key]
except KeyError:
pass
return values
def save_gui_state(self, gui):
"""Saves settings for a cola view"""
name = gui.name()
self.gui_state[name] = mkdict(gui.export_state())
self.save()
def get_gui_state(self, gui):
"""Returns the state for a gui"""
try:
state = mkdict(self.gui_state[gui.name()])
except KeyError:
state = self.gui_state[gui.name()] = {}
return state
|