2011-08-28 18:01:32 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2011-10-27 15:22:26 +00:00
|
|
|
|
2013-11-10 02:49:31 +00:00
|
|
|
# Copyright (C) 2013 Alex Yatskov
|
2011-08-28 18:01:32 +00:00
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
|
2013-11-10 19:46:50 +00:00
|
|
|
import codecs
|
|
|
|
import json
|
|
|
|
import operator
|
2013-11-10 02:49:31 +00:00
|
|
|
import os
|
2011-08-28 18:01:32 +00:00
|
|
|
|
|
|
|
|
2013-11-10 19:46:50 +00:00
|
|
|
class Preferences(object):
|
2011-08-28 18:01:32 +00:00
|
|
|
def __init__(self):
|
2013-11-10 19:46:50 +00:00
|
|
|
self.filename = os.path.expanduser('~/.yomichan.json')
|
|
|
|
self.defaults = os.path.join(os.path.dirname(__file__), 'defaults.json')
|
|
|
|
self.settings = dict()
|
2011-08-28 18:01:32 +00:00
|
|
|
|
|
|
|
|
2013-11-10 19:46:50 +00:00
|
|
|
def __getitem__(self, name):
|
|
|
|
return self.settings.get(name)
|
2011-08-28 18:01:32 +00:00
|
|
|
|
|
|
|
|
2013-11-10 19:46:50 +00:00
|
|
|
def __setitem__(self, name, value):
|
|
|
|
self.settings[name] = value
|
2011-08-28 18:01:32 +00:00
|
|
|
|
|
|
|
|
2013-11-10 19:46:50 +00:00
|
|
|
def load(self):
|
|
|
|
with codecs.open(self.defaults, 'rb', 'utf-8') as fp:
|
|
|
|
self.settings = json.load(fp)
|
2011-08-28 18:01:32 +00:00
|
|
|
|
|
|
|
try:
|
2013-11-10 19:46:50 +00:00
|
|
|
if os.path.exists(self.filename):
|
|
|
|
with codecs.open(self.filename, 'rb', 'utf-8') as fp:
|
|
|
|
self.settings.update(json.load(fp))
|
|
|
|
except ValueError:
|
|
|
|
pass
|
2011-08-28 18:01:32 +00:00
|
|
|
|
|
|
|
|
2013-11-10 19:46:50 +00:00
|
|
|
def save(self):
|
|
|
|
with codecs.open(self.filename, 'wb', 'utf-8') as fp:
|
|
|
|
json.dump(self.settings, fp, indent=4, sort_keys=True)
|
2011-08-28 18:01:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
def filePosition(self, filename):
|
2013-11-10 19:46:50 +00:00
|
|
|
matches = filter(lambda f: f['path'] != filename, self['recentFiles'])
|
|
|
|
return 0 if len(matches) == 0 else matches[0]['position']
|
2011-08-28 18:01:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
def recentFiles(self):
|
2013-11-10 19:46:50 +00:00
|
|
|
return map(operator.itemgetter('path'), self['recentFiles'])
|
2011-08-28 18:01:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
def updateFactTags(self, tags):
|
2013-11-10 19:46:50 +00:00
|
|
|
if tags in self['tags']:
|
|
|
|
self['tags'].remove(tags)
|
|
|
|
self['tags'].insert(0, tags)
|
2011-08-28 18:01:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
def updateRecentFile(self, filename, position):
|
2013-11-10 19:46:50 +00:00
|
|
|
self['recentFiles'] = filter(lambda f: f['path'] != filename, self['recentFiles'])
|
|
|
|
self['recentFiles'].insert(0, {'path': filename, 'position': position})
|
2011-08-28 18:01:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
def clearRecentFiles(self):
|
2013-11-10 19:46:50 +00:00
|
|
|
self['recentFiles'] = list()
|