i18n: update git-cola.pot for v1.8.5
[git-cola.git] / cola / settings.py
blob4cc029ed45f3260a4a859d3eab6609a9f21752fb
1 # Copyright (c) 2008 David Aguilar
2 """This handles saving complex settings such as bookmarks, etc.
3 """
5 import os
6 import sys
7 try:
8 import simplejson
9 json = simplejson
10 except ImportError:
11 import json
13 from cola import core
14 from cola import git
15 from cola import xdg
18 def mkdict(obj):
19 if type(obj) is dict:
20 return obj
21 else:
22 return {}
25 def mklist(obj):
26 if type(obj) is list:
27 return obj
28 else:
29 return []
32 class Settings(object):
33 _file = xdg.config_home('settings')
35 def __init__(self, verify=git.is_git_worktree):
36 """Load existing settings if they exist"""
37 self.values = {
38 'bookmarks': [],
39 'gui_state': {},
40 'recent': [],
42 self.verify = verify
43 self.load()
44 self.remove_missing()
47 def remove_missing(self):
48 missing_bookmarks = []
49 missing_recent = []
51 for bookmark in self.bookmarks:
52 if not self.verify(core.encode(bookmark)):
53 missing_bookmarks.append(bookmark)
55 for bookmark in missing_bookmarks:
56 try:
57 self.bookmarks.remove(bookmark)
58 except:
59 pass
61 for recent in self.recent:
62 if not self.verify(core.encode(recent)):
63 missing_recent.append(recent)
65 for recent in missing_recent:
66 try:
67 self.recent.remove(recent)
68 except:
69 pass
71 # properties
72 def _get_bookmarks(self):
73 return mklist(self.values['bookmarks'])
75 def _get_gui_state(self):
76 return mkdict(self.values['gui_state'])
78 def _get_recent(self):
79 return mklist(self.values['recent'])
81 bookmarks = property(_get_bookmarks)
82 gui_state = property(_get_gui_state)
83 recent = property(_get_recent)
85 def add_bookmark(self, bookmark):
86 """Adds a bookmark to the saved settings"""
87 if bookmark not in self.bookmarks:
88 self.bookmarks.append(bookmark)
90 def remove_bookmark(self, bookmark):
91 """Removes a bookmark from the saved settings"""
92 if bookmark in self.bookmarks:
93 self.bookmarks.remove(bookmark)
95 def add_recent(self, entry):
96 if entry in self.recent:
97 self.recent.remove(entry)
98 self.recent.insert(0, entry)
99 if len(self.recent) > 8:
100 self.recent.pop()
102 def path(self):
103 return self._file
105 def save(self):
106 path = self.path()
107 try:
108 parent = os.path.dirname(path)
109 if not os.path.isdir(parent):
110 os.makedirs(parent)
111 fp = open(path, 'wb')
112 json.dump(self.values, fp, indent=4)
113 fp.close()
114 except:
115 sys.stderr.write('git-cola: error writing "%s"\n' % path)
117 def load(self):
118 self.values.update(self._load())
120 def _load(self):
121 path = self.path()
122 if not os.path.exists(path):
123 return self._load_dot_cola()
124 try:
125 fp = open(path, 'rb')
126 return mkdict(json.load(fp))
127 except: # bad json
128 return {}
130 def reload_recent(self):
131 values = self._load()
132 self.values['recent'] = mklist(values.get('recent', []))
134 def _load_dot_cola(self):
135 values = {}
136 path = os.path.join(os.path.expanduser('~'), '.cola')
137 if not os.path.exists(path):
138 return {}
139 try:
140 fp = open(path, 'rb')
141 json_values = json.load(fp)
142 fp.close()
143 except: # bad json
144 return {}
146 # Keep only the entries we care about
147 for key in self.values:
148 try:
149 values[key] = json_values[key]
150 except KeyError:
151 pass
153 return values
155 def save_gui_state(self, gui):
156 """Saves settings for a cola view"""
157 name = gui.name()
158 self.gui_state[name] = mkdict(gui.export_state())
159 self.save()
161 def get_gui_state(self, gui):
162 """Returns the state for a gui"""
163 try:
164 state = mkdict(self.gui_state[gui.name()])
165 except KeyError:
166 state = self.gui_state[gui.name()] = {}
167 return state