1 # Copyright (c) 2008 David Aguilar
2 """This handles saving complex settings such as bookmarks, etc.
4 from __future__
import division
, absolute_import
, unicode_literals
11 from cola
import resources
31 with core
.xopen(path
, 'rt') as fp
:
32 return mkdict(json
.load(fp
))
33 except: # bad path or json
37 def write_json(values
, path
):
39 parent
= os
.path
.dirname(path
)
40 if not core
.isdir(parent
):
42 with core
.xopen(path
, 'wt') as fp
:
43 json
.dump(values
, fp
, indent
=4)
45 sys
.stderr
.write('git-cola: error writing "%s"\n' % path
)
48 class Settings(object):
49 _file
= resources
.config_home('settings')
50 bookmarks
= property(lambda self
: mklist(self
.values
['bookmarks']))
51 gui_state
= property(lambda self
: mkdict(self
.values
['gui_state']))
52 recent
= property(lambda self
: mklist(self
.values
['recent']))
54 def __init__(self
, verify
=git
.is_git_worktree
):
55 """Load existing settings if they exist"""
63 def remove_missing(self
):
64 missing_bookmarks
= []
67 for bookmark
in self
.bookmarks
:
68 if not self
.verify(bookmark
):
69 missing_bookmarks
.append(bookmark
)
71 for bookmark
in missing_bookmarks
:
73 self
.bookmarks
.remove(bookmark
)
77 for recent
in self
.recent
:
78 if not self
.verify(recent
):
79 missing_recent
.append(recent
)
81 for recent
in missing_recent
:
83 self
.recent
.remove(recent
)
87 def add_bookmark(self
, bookmark
):
88 """Adds a bookmark to the saved settings"""
89 if bookmark
not in self
.bookmarks
:
90 self
.bookmarks
.append(bookmark
)
92 def remove_bookmark(self
, bookmark
):
93 """Remove a bookmark"""
94 if bookmark
in self
.bookmarks
:
95 self
.bookmarks
.remove(bookmark
)
97 def remove_recent(self
, entry
):
98 """Removes an item from the recent items list"""
99 if entry
in self
.recent
:
100 self
.recent
.remove(entry
)
102 def add_recent(self
, entry
):
103 if entry
in self
.recent
:
104 self
.recent
.remove(entry
)
105 self
.recent
.insert(0, entry
)
106 if len(self
.recent
) >= 8:
113 write_json(self
.values
, self
.path())
116 self
.values
.update(self
.asdict())
117 self
.remove_missing()
121 if core
.exists(path
):
122 return read_json(path
)
123 # We couldn't find ~/.config/git-cola, try ~/.cola
125 path
= os
.path
.join(core
.expanduser('~'), '.cola')
126 if core
.exists(path
):
127 json_values
= read_json(path
)
128 # Keep only the entries we care about
129 for key
in self
.values
:
131 values
[key
] = json_values
[key
]
136 def reload_recent(self
):
137 values
= self
.asdict()
138 self
.values
['recent'] = mklist(values
.get('recent', []))
140 def save_gui_state(self
, gui
):
141 """Saves settings for a cola view"""
143 self
.gui_state
[name
] = mkdict(gui
.export_state())
146 def get_gui_state(self
, gui
):
147 """Returns the saved state for a gui"""
149 state
= mkdict(self
.gui_state
[gui
.name()])
151 state
= self
.gui_state
[gui
.name()] = {}
155 class Session(Settings
):
156 """Store per-session settings"""
158 _sessions_dir
= resources
.config_home('sessions')
160 repo
= property(lambda self
: self
.values
['repo'])
162 def __init__(self
, session_id
, repo
=None):
163 Settings
.__init
__(self
)
164 self
.session_id
= session_id
165 self
.values
.update({'repo': repo
})
168 return os
.path
.join(self
._sessions
_dir
, self
.session_id
)
172 if core
.exists(path
):
173 self
.values
.update(read_json(path
))