dag: Use a slightly thinner edge thickness
[git-cola.git] / cola / settings.py
blob0c02c7298e8e90a789e0c7060c80fb7dda91ff45
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
14 class Settings(object):
15 _file = '~/.config/git-cola/settings'
17 def __init__(self):
18 """Load existing settings if they exist"""
19 self.values = {}
20 self.load()
22 # properties
23 def _get_bookmarks(self):
24 try:
25 bookmarks = self.values['bookmarks']
26 except KeyError:
27 bookmarks = self.values['bookmarks'] = []
28 return bookmarks
30 def _get_gui_state(self):
31 try:
32 gui_state = self.values['gui_state']
33 except KeyError:
34 gui_state = self.values['gui_state'] = {}
35 return gui_state
37 bookmarks = property(_get_bookmarks)
38 gui_state = property(_get_gui_state)
40 def add_bookmark(self, bookmark):
41 """Adds a bookmark to the saved settings"""
42 if bookmark not in self.bookmarks:
43 self.bookmarks.append(bookmark)
45 def remove_bookmark(self, bookmark):
46 """Removes a bookmark from the saved settings"""
47 if bookmark in self.bookmarks:
48 self.bookmarks.remove(bookmark)
50 def path(self):
51 return os.path.expanduser(Settings._file)
53 def save(self):
54 path = self.path()
55 try:
56 parent = os.path.dirname(path)
57 if not os.path.isdir(parent):
58 os.makedirs(parent)
60 fp = open(path, 'wb')
61 json.dump(self.values, fp, indent=4)
62 fp.close()
63 except:
64 sys.stderr.write('git-cola: error writing "%s"\n' % path)
66 def load(self):
67 path = self.path()
68 if not os.path.exists(path):
69 self.load_dot_cola(path)
70 return
71 try:
72 fp = open(path, 'rb')
73 self.values = json.load(fp)
74 except: # bad json
75 pass
77 def load_dot_cola(self, path):
78 path = os.path.join(os.path.expanduser('~'), '.cola')
79 if not os.path.exists(path):
80 return
81 try:
82 fp = open(path, 'rb')
83 values = json.load(fp)
84 fp.close()
85 except: # bad json
86 return
87 for key in ('bookmarks', 'gui_state'):
88 try:
89 self.values[key] = values[key]
90 except KeyError:
91 pass
93 def save_gui_state(self, gui):
94 """Saves settings for a cola view"""
95 name = gui.name()
96 state = gui.export_state()
97 self.gui_state[name] = state
98 self.save()
100 def get_gui_state(self, gui):
101 """Returns the state for a gui"""
102 try:
103 state = self.gui_state[gui.name()]
104 except KeyError:
105 state = self.gui_state[gui.name()] = {}
106 return state