1 """Save settings, bookmarks, etc.
3 from __future__
import division
, absolute_import
, unicode_literals
10 from . import resources
29 with core
.xopen(path
, 'rt') as fp
:
30 return mkdict(json
.load(fp
))
31 except: # bad path or json
35 def write_json(values
, path
):
37 parent
= os
.path
.dirname(path
)
38 if not core
.isdir(parent
):
40 with core
.xopen(path
, 'wt') as fp
:
41 json
.dump(values
, fp
, indent
=4)
43 sys
.stderr
.write('git-cola: error writing "%s"\n' % path
)
46 class Settings(object):
47 _file
= resources
.config_home('settings')
48 bookmarks
= property(lambda self
: mklist(self
.values
['bookmarks']))
49 gui_state
= property(lambda self
: mkdict(self
.values
['gui_state']))
50 recent
= property(lambda self
: mklist(self
.values
['recent']))
52 def __init__(self
, verify
=git
.is_git_worktree
):
53 """Load existing settings if they exist"""
61 def remove_missing(self
):
62 missing_bookmarks
= []
65 for bookmark
in self
.bookmarks
:
66 if not self
.verify(bookmark
['path']):
67 missing_bookmarks
.append(bookmark
)
69 for bookmark
in missing_bookmarks
:
71 self
.bookmarks
.remove(bookmark
)
75 for recent
in self
.recent
:
76 if not self
.verify(recent
['path']):
77 missing_recent
.append(recent
)
79 for recent
in missing_recent
:
81 self
.recent
.remove(recent
)
85 def add_bookmark(self
, path
, name
):
86 """Adds a bookmark to the saved settings"""
87 bookmark
= {'path': path
, 'name': name
}
88 if bookmark
not in self
.bookmarks
:
89 self
.bookmarks
.append(bookmark
)
91 def remove_bookmark(self
, path
, name
):
92 """Remove a bookmark"""
93 bookmark
= {'path': path
, 'name': name
}
95 self
.bookmarks
.remove(bookmark
)
99 def rename_entry(self
, entries
, path
, name
, new_name
):
100 entry
= {'name': name
, 'path': path
}
102 index
= entries
.index(entry
)
106 if all([item
['name'] != new_name
for item
in entries
]):
107 entries
[index
]['name'] = new_name
112 def rename_bookmark(self
, path
, name
, new_name
):
113 return self
.rename_entry(self
.bookmarks
, path
, name
, new_name
)
115 def add_recent(self
, path
):
117 index
= [recent
['path'] for recent
in self
.recent
].index(path
)
118 entry
= self
.recent
.pop(index
)
119 except (IndexError, ValueError):
121 'name': os
.path
.basename(path
),
124 self
.recent
.insert(0, entry
)
126 if len(self
.recent
) >= 8:
129 def remove_recent(self
, path
):
130 """Removes an item from the recent items list"""
132 index
= [recent
.get('path', '') for recent
in self
.recent
].index(path
)
136 self
.recent
.pop(index
)
140 def rename_recent(self
, path
, name
, new_name
):
141 return self
.rename_entry(self
.recent
, path
, name
, new_name
)
147 write_json(self
.values
, self
.path())
150 self
.values
.update(self
.asdict())
151 self
.upgrade_settings()
152 self
.remove_missing()
154 def upgrade_settings(self
):
155 """Upgrade git-cola settings"""
156 # Upgrade bookmarks to the new dict-based bookmarks format.
157 if self
.bookmarks
and not isinstance(self
.bookmarks
[0], dict):
158 bookmarks
= [dict(name
=os
.path
.basename(path
), path
=path
)
159 for path
in self
.bookmarks
]
160 self
.values
['bookmarks'] = bookmarks
162 if self
.recent
and not isinstance(self
.recent
[0], dict):
163 recent
= [dict(name
=os
.path
.basename(path
), path
=path
)
164 for path
in self
.recent
]
165 self
.values
['recent'] = recent
169 if core
.exists(path
):
170 return read_json(path
)
171 # We couldn't find ~/.config/git-cola, try ~/.cola
173 path
= os
.path
.join(core
.expanduser('~'), '.cola')
174 if core
.exists(path
):
175 json_values
= read_json(path
)
176 # Keep only the entries we care about
177 for key
in self
.values
:
179 values
[key
] = json_values
[key
]
184 def reload_recent(self
):
185 values
= self
.asdict()
186 self
.values
['recent'] = mklist(values
.get('recent', []))
188 def save_gui_state(self
, gui
):
189 """Saves settings for a cola view"""
191 self
.gui_state
[name
] = mkdict(gui
.export_state())
194 def get_gui_state(self
, gui
):
195 """Returns the saved state for a gui"""
197 state
= mkdict(self
.gui_state
[gui
.name()])
199 state
= self
.gui_state
[gui
.name()] = {}
203 class Session(Settings
):
204 """Store per-session settings"""
206 _sessions_dir
= resources
.config_home('sessions')
208 repo
= property(lambda self
: self
.values
['repo'])
210 def __init__(self
, session_id
, repo
=None):
211 Settings
.__init
__(self
)
212 self
.session_id
= session_id
213 self
.values
.update({'repo': repo
})
216 return os
.path
.join(self
._sessions
_dir
, self
.session_id
)
220 if core
.exists(path
):
221 self
.values
.update(read_json(path
))