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