1
yomichan-anki/yomi_base/preference_data.py

261 lines
9.5 KiB
Python
Raw Normal View History

2011-08-28 18:01:32 +00:00
# -*- coding: utf-8 -*-
2011-10-27 15:22:26 +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/>.
from PyQt4 import QtCore, QtGui
from xml.dom import minidom
import os
2011-08-28 18:01:32 +00:00
class Preferences:
def __init__(self):
self.clear()
def clear(self):
self.generalFindUpdates = True
self.generalReadingsStrip = False
self.generalRecentLoad = True
self.generalRecentFiles = list()
self.generalRecentMax = 10
self.uiContentFontFamily = 'Arial'
self.uiContentFontSize = 12
self.uiContentColorFg = QtGui.QColor(QtCore.Qt.black).rgba()
self.uiContentColorBg = QtGui.QColor(QtCore.Qt.white).rgba()
self.uiContentWordWrap = False
self.uiReaderState = None
self.uiReaderPosition = None
self.uiReaderSize = None
self.searchScanMax = 16
self.searchResultMax = 32
self.searchGroupByExp = True
self.ankiFields = dict()
self.ankiTags = list()
2012-12-24 02:11:14 +00:00
self.ankiDeck = unicode()
2012-12-24 00:02:00 +00:00
self.ankiModel = unicode()
2011-08-28 18:01:32 +00:00
def load(self, filename=None):
if not filename:
filename = self.defaultFilename()
try:
doc = minidom.parse(filename)
except:
return False
root = doc.documentElement
if root.nodeName != 'yomichan':
return False
self.clear()
for general in root.getElementsByTagName('general'):
2011-10-08 16:32:51 +00:00
self.generalRecentLoad = self.readAttrBool(general, 'recentLoad', self.generalRecentLoad)
self.generalReadingsStrip = self.readAttrBool(general, 'readingsStrip', self.generalReadingsStrip)
self.generalFindUpdates = self.readAttrBool(general, 'findUpdates', self.generalFindUpdates)
2011-08-28 18:01:32 +00:00
for recent in general.getElementsByTagName('recent'):
2011-10-08 16:32:51 +00:00
path = self.readAttrStr(recent, 'path')
position = self.readAttrInt(recent, 'position')
2011-08-28 18:01:32 +00:00
if path and os.path.isfile(path):
self.generalRecentFiles.append((path, position))
for ui in root.getElementsByTagName('ui'):
for content in ui.getElementsByTagName('content'):
2011-10-08 16:32:51 +00:00
self.uiContentFontFamily = self.readAttrStr(content, 'fontFamily', self.uiContentFontFamily)
self.uiContentFontSize = self.readAttrInt(content, 'fontSize', self.uiContentFontSize)
self.uiContentColorFg = self.readAttrInt(content, 'colorFg', self.uiContentColorFg)
self.uiContentColorBg = self.readAttrInt(content, 'colorBg', self.uiContentColorBg)
self.uiContentWordWrap = self.readAttrBool(content, 'wordWrap', self.uiContentWordWrap)
2011-08-28 18:01:32 +00:00
for reader in ui.getElementsByTagName('reader'):
2011-10-08 16:32:51 +00:00
self.uiReaderState = self.readAttrStr(reader, 'state', self.uiReaderState)
self.uiReaderPosition = self.readAttrIntTuple(reader, 'position', self.uiReaderPosition)
self.uiReaderSize = self.readAttrIntTuple(reader, 'size', self.uiReaderSize)
2011-08-28 18:01:32 +00:00
for search in root.getElementsByTagName('search'):
2011-10-08 16:32:51 +00:00
self.searchScanMax = self.readAttrInt(search, 'scanMax', self.searchScanMax)
self.searchResultMax = self.readAttrInt(search, 'resultMax', self.searchResultMax)
self.searchGroupByExp = self.readAttrBool(search, 'groupByExp', self.searchGroupByExp)
2011-08-28 18:01:32 +00:00
for anki in root.getElementsByTagName('anki'):
2012-12-24 02:11:14 +00:00
self.ankiDeck = self.readAttrStr(anki, 'deck', unicode())
2012-12-24 00:02:00 +00:00
self.ankiModel = self.readAttrStr(anki, 'model', unicode())
2011-08-28 18:01:32 +00:00
for tag in anki.getElementsByTagName('tag'):
2011-10-08 16:32:51 +00:00
value = self.readAttrStr(tag, 'value', unicode())
2011-08-28 18:01:32 +00:00
self.ankiTags.append(value)
for model in anki.getElementsByTagName('model'):
for field in model.getElementsByTagName('field'):
2011-10-08 16:32:51 +00:00
name = self.readAttrStr(field, 'name')
value = self.readAttrStr(field, 'value')
2011-08-28 18:01:32 +00:00
if name and value:
self.ankiFields[name] = value
return True
def save(self, filename=None):
if not filename:
filename = self.defaultFilename()
directory = os.path.split(filename)[0]
if not os.path.isdir(directory):
try:
os.makedirs(directory)
except OSError:
return False
doc = minidom.Document()
root = doc.createElement('yomichan')
doc.appendChild(root)
general = doc.createElement('general')
root.appendChild(general)
2011-10-08 16:32:51 +00:00
self.writeAttrBool(general, 'recentLoad', self.generalRecentLoad)
self.writeAttrBool(general, 'readingsStrip', self.generalReadingsStrip)
self.writeAttrBool(general, 'findUpdates', self.generalFindUpdates)
2011-08-28 18:01:32 +00:00
for path, position in self.generalRecentFiles:
recent = doc.createElement('recent')
general.appendChild(recent)
2011-10-08 16:32:51 +00:00
self.writeAttrStr(recent, 'path', path)
self.writeAttrInt(recent, 'position', position)
2011-08-28 18:01:32 +00:00
ui = doc.createElement('ui')
root.appendChild(ui)
content = doc.createElement('content')
ui.appendChild(content)
2011-10-08 16:32:51 +00:00
self.writeAttrStr(content, 'fontFamily', self.uiContentFontFamily)
self.writeAttrInt(content, 'fontSize', self.uiContentFontSize)
self.writeAttrInt(content, 'colorFg', self.uiContentColorFg)
self.writeAttrInt(content, 'colorBg', self.uiContentColorBg)
self.writeAttrBool(content, 'wordWrap', self.uiContentWordWrap)
2011-08-28 18:01:32 +00:00
reader = doc.createElement('reader')
ui.appendChild(reader)
2011-10-08 16:32:51 +00:00
self.writeAttrStr(reader, 'state', self.uiReaderState)
self.writeAttrIntTuple(reader, 'position', self.uiReaderPosition)
self.writeAttrIntTuple(reader, 'size', self.uiReaderSize)
2011-08-28 18:01:32 +00:00
search = doc.createElement('search')
root.appendChild(search)
2011-10-08 16:32:51 +00:00
self.writeAttrInt(search, 'scanMax', self.searchScanMax)
self.writeAttrInt(search, 'resultMax', self.searchResultMax)
self.writeAttrBool(search, 'groupByExp', self.searchGroupByExp)
2011-08-28 18:01:32 +00:00
anki = doc.createElement('anki')
root.appendChild(anki)
2012-12-24 02:11:14 +00:00
self.writeAttrStr(anki, 'deck', self.ankiDeck)
2012-12-24 00:02:00 +00:00
self.writeAttrStr(anki, 'model', self.ankiModel)
2011-08-28 18:01:32 +00:00
for value in self.ankiTags:
tag = doc.createElement('tag')
anki.appendChild(tag)
2011-10-08 16:32:51 +00:00
self.writeAttrStr(tag, 'value', value)
2011-08-28 18:01:32 +00:00
if self.ankiFields:
model = doc.createElement('model')
anki.appendChild(model)
for name, value in self.ankiFields.items():
field = doc.createElement('field')
model.appendChild(field)
2011-10-08 16:32:51 +00:00
self.writeAttrStr(field, 'name', name)
self.writeAttrStr(field, 'value', value)
2011-08-28 18:01:32 +00:00
try:
with open(filename, 'w') as fp:
fp.write(doc.toprettyxml(encoding='utf-8'))
except IOError:
return False
return True
def defaultFilename(self):
return os.path.expanduser('~/.yomichan/preferences.xml')
def filePosition(self, filename):
results = filter(lambda x: x[0] == filename, self.generalRecentFiles)
return results[0][1] if results else 0
def recentFiles(self):
return map(lambda x: x[0], self.generalRecentFiles)
def updateFactTags(self, tags):
if tags in self.ankiTags:
self.ankiTags.remove(tags)
self.ankiTags.insert(0, tags)
def updateRecentFile(self, filename, position):
self.generalRecentFiles = filter(lambda x: x[0] != filename, self.generalRecentFiles)
self.generalRecentFiles.insert(0, (filename, position))
self.generalRecentFiles = self.generalRecentFiles[:self.generalRecentMax]
def clearRecentFiles(self):
self.generalRecentFiles = list()
2011-10-08 16:32:51 +00:00
def readAttrStr(self, node, name, default=None):
2011-08-28 18:01:32 +00:00
return node.getAttribute(name) or default
2011-10-08 16:32:51 +00:00
def readAttrBool(self, node, name, default=False):
value = self.readAttrStr(node, name)
2011-08-28 18:01:32 +00:00
return value.lower() == 'true' if value else default
2011-10-08 16:32:51 +00:00
def readAttrInt(self, node, name, default=0):
value = self.readAttrStr(node, name)
2011-08-28 18:01:32 +00:00
return int(value) if value else default
2011-10-08 16:32:51 +00:00
def readAttrIntTuple(self, node, name, default=None):
value = self.readAttrStr(node, name)
2011-08-28 18:01:32 +00:00
return tuple([int(v) for v in value.split(',')]) if value else default
2011-10-08 16:32:51 +00:00
def writeAttrStr(self, node, name, value):
2011-08-28 18:01:32 +00:00
if value != None:
node.setAttribute(name, value)
2011-10-08 16:32:51 +00:00
def writeAttrBool(self, node, name, value):
2011-08-28 18:01:32 +00:00
if value != None:
node.setAttribute(name, ['false', 'true'][bool(value)])
2011-10-08 16:32:51 +00:00
def writeAttrInt(self, node, name, value):
2011-08-28 18:01:32 +00:00
if value != None:
node.setAttribute(name, str(value))
2011-10-08 16:32:51 +00:00
def writeAttrIntTuple(self, node, name, value):
2011-08-28 18:01:32 +00:00
if value != None:
node.setAttribute(name, ','.join([str(v) for v in value]))