change config file name and format.

This commit is contained in:
St.Huang 2018-07-13 22:09:07 +08:00
parent c40f99856a
commit 90c22c7a97
3 changed files with 84 additions and 65 deletions

View File

@ -23,8 +23,8 @@ from .constants import VERSION
from .utils import get_icon from .utils import get_icon
CONFIG_FILENAME = '_fwqcfg.json' #Config File Path CONFIG_FILENAME = '.fastwqcfg.json' #Config File Path
APP_ICON = get_icon('wqicon.png') #Addon Icon APP_ICON = get_icon('wqicon.png') #Addon Icon
class Config(object): class Config(object):

View File

@ -382,17 +382,17 @@ def query_all_flds(note):
if skip and len(note.fields[i]) != 0: if skip and len(note.fields[i]) != 0:
continue continue
#normal #normal
dict_name = each.get('dict', '').strip()
dict_field = each.get('dict_field', '').strip()
dict_unique = each.get('dict_unique', '').strip() dict_unique = each.get('dict_unique', '').strip()
if dict_name and dict_name not in _sl('NOT_DICT_FIELD') and dict_field: dict_fld_ord = each.get('dict_fld_ord', -1)
fld_ord = each.get('fld_ord', -1)
if dict_unique and dict_fld_ord != -1 and fld_ord != -1:
s = services.get(dict_unique, None) s = services.get(dict_unique, None)
if s is None: if s is None:
s = service_pool.get(dict_unique) s = service_pool.get(dict_unique)
if s.support: if s.support:
services[dict_unique] = s services[dict_unique] = s
if s and s.support: if s and s.support:
tasks.append({'k': dict_unique, 'w': word, 'f': dict_field, 'i': i}) tasks.append({'k': dict_unique, 'w': word, 'f': dict_fld_ord, 'i': fld_ord})
success_num = 0 success_num = 0
result = defaultdict(QueryResult) result = defaultdict(QueryResult)

View File

@ -338,7 +338,9 @@ class OptionsDialog(Dialog):
self.build_mappings_layout(self.current_model) self.build_mappings_layout(self.current_model)
def build_mappings_layout(self, model): def build_mappings_layout(self, model):
'''
build dictionaryfields etc
'''
def clear_layout(layout): def clear_layout(layout):
if layout is not None: if layout is not None:
while layout.count(): while layout.count():
@ -366,13 +368,15 @@ class OptionsDialog(Dialog):
name = fld['name'] name = fld['name']
if maps: if maps:
for j, each in enumerate(maps): for j, each in enumerate(maps):
if each.get('fld_ord', -1) == ord: if each.get('fld_ord', -1) == ord or each.get('fld_name', '') == name:
self.add_dict_layout(j, fld_name=name, **each) each['fld_name'] = name
each['fld_ord'] = ord
self.add_dict_layout(j, **each)
break break
else: else:
self.add_dict_layout(i, fld_name=name) self.add_dict_layout(i, fld_name=name, fld_ord=ord)
else: else:
self.add_dict_layout(i, fld_name=name) self.add_dict_layout(i, fld_name=name, fld_ord=ord)
#self.setLayout(self.main_layout) #self.setLayout(self.main_layout)
self.resize(WIDGET_SIZE.dialog_width, self.resize(WIDGET_SIZE.dialog_width,
@ -380,6 +384,9 @@ class OptionsDialog(Dialog):
self.save() self.save()
def show_models(self): def show_models(self):
'''
show choose note type window
'''
edit = QtGui.QPushButton(anki.lang._("Manage"), edit = QtGui.QPushButton(anki.lang._("Manage"),
clicked=lambda: aqt.models.Models(mw, self)) clicked=lambda: aqt.models.Models(mw, self))
ret = StudyDeck(mw, names=lambda: sorted(mw.col.models.allNames()), ret = StudyDeck(mw, names=lambda: sorted(mw.col.models.allNames()),
@ -392,7 +399,7 @@ class OptionsDialog(Dialog):
u'%s [%s]' % (_('CHOOSE_NOTE_TYPES'), ret.name)) u'%s [%s]' % (_('CHOOSE_NOTE_TYPES'), ret.name))
return model return model
def fill_dict_combo_options(self, dict_combo, current_text): def fill_dict_combo_options(self, dict_combo, current_unique):
'''setup dict combo box''' '''setup dict combo box'''
dict_combo.clear() dict_combo.clear()
#dict_combo.addItem(_('NOT_DICT_FIELD')) #dict_combo.addItem(_('NOT_DICT_FIELD'))
@ -424,15 +431,15 @@ class OptionsDialog(Dialog):
def set_dict_combo_index(): def set_dict_combo_index():
#dict_combo.setCurrentIndex(-1) #dict_combo.setCurrentIndex(-1)
dict_combo.setCurrentIndex(0) dict_combo.setCurrentIndex(0)
if current_text: if current_unique:
for i in range(dict_combo.count()): for i in range(dict_combo.count()):
if dict_combo.itemText(i) == current_text: if dict_combo.itemData(i) == current_unique:
dict_combo.setCurrentIndex(i) dict_combo.setCurrentIndex(i)
break break
set_dict_combo_index() set_dict_combo_index()
def fill_field_combo_options(self, field_combo, dict_combo_text, dict_combo_itemdata, field_combo_text): def fill_field_combo_options(self, field_combo, dict_combo_text, dict_combo_itemdata, dict_fld_name, dict_fld_ord):
'''setup field combobox''' '''setup field combobox'''
field_combo.clear() field_combo.clear()
field_combo.setEditable(False) field_combo.setEditable(False)
@ -440,7 +447,7 @@ class OptionsDialog(Dialog):
# field_combo.setEnabled(False) # field_combo.setEnabled(False)
#el #el
if dict_combo_text in _sl('MDX_SERVER'): if dict_combo_text in _sl('MDX_SERVER'):
text = field_combo_text if field_combo_text else 'http://' text = dict_fld_name if dict_fld_name else 'http://'
field_combo.setEditable(True) field_combo.setEditable(True)
field_combo.setEditText(text) field_combo.setEditText(text)
field_combo.setFocus(QtCore.Qt.MouseFocusReason) # MouseFocusReason field_combo.setFocus(QtCore.Qt.MouseFocusReason) # MouseFocusReason
@ -451,24 +458,32 @@ class OptionsDialog(Dialog):
field_combo.setCurrentIndex(0) field_combo.setCurrentIndex(0)
if service and service.support and service.fields: if service and service.support and service.fields:
for i, each in enumerate(service.fields): for i, each in enumerate(service.fields):
field_combo.addItem(each) field_combo.addItem(each, userData=i)
if each == field_combo_text: if each == dict_fld_name or i == dict_fld_ord:
field_combo.setCurrentIndex(i) field_combo.setCurrentIndex(i)
service_pool.put(service) service_pool.put(service)
def add_dict_layout(self, i, **kwargs): def add_dict_layout(self, i, **kwargs):
""" """
kwargs: add dictionary fields row
word_checked dict fld_name dict_field
""" """
word_checked = i == 0 word_checked = i == 0
dict_name, dict_unique, fld_name, dict_field, ignore, skip = (
kwargs.get('dict', ''), fld_name, fld_ord = (
kwargs.get('dict_unique', ''), kwargs.get('fld_name', ''), #笔记类型的字段名
kwargs.get('fld_name', ''), kwargs.get('fld_ord', ''), #笔记类型的字段编号
kwargs.get('dict_field', ''), )
kwargs.get('ignore', False),
kwargs.get('skip_valued', False), dict_name, dict_unique, dict_fld_name, dict_fld_ord = (
kwargs.get('dict_name', ''), #字典名
kwargs.get('dict_unique', ''), #字典ID
kwargs.get('dict_fld_name', ''), #对应字典的字段名
kwargs.get('dcit_fld_ord', 0) #对应字典的字段编号
)
ignore, skip = (
kwargs.get('ignore', False), #忽略标志
kwargs.get('skip_valued', False), #略过有值项标志
) )
# check # check
word_check_btn = QtGui.QRadioButton(fld_name) word_check_btn = QtGui.QRadioButton(fld_name)
@ -488,23 +503,23 @@ class OptionsDialog(Dialog):
QtCore.Qt.TabFocus | QtCore.Qt.ClickFocus | QtCore.Qt.StrongFocus | QtCore.Qt.WheelFocus QtCore.Qt.TabFocus | QtCore.Qt.ClickFocus | QtCore.Qt.StrongFocus | QtCore.Qt.WheelFocus
) )
dict_combo.setEnabled(not word_checked and not ignore) dict_combo.setEnabled(not word_checked and not ignore)
self.fill_dict_combo_options(dict_combo, dict_name) self.fill_dict_combo_options(dict_combo, dict_unique)
dict_unique = dict_combo.itemData(dict_combo.currentIndex()) dict_unique = dict_combo.itemData(dict_combo.currentIndex())
# field combox # field combox
field_combo = QtGui.QComboBox() field_combo = QtGui.QComboBox()
field_combo.setMinimumSize(WIDGET_SIZE.map_dictfield_width, 0) field_combo.setMinimumSize(WIDGET_SIZE.map_dictfield_width, 0)
field_combo.setEnabled(not word_checked and not ignore) field_combo.setEnabled(not word_checked and not ignore)
self.fill_field_combo_options(field_combo, dict_name, dict_unique, dict_field) self.fill_field_combo_options(field_combo, dict_name, dict_unique, dict_fld_name, dict_fld_ord)
# ignore # ignore
check_ignore = QtGui.QCheckBox(_("NOT_DICT_FIELD")) ignore_check_btn = QtGui.QCheckBox(_("NOT_DICT_FIELD"))
check_ignore.setEnabled(not word_checked) ignore_check_btn.setEnabled(not word_checked)
check_ignore.setChecked(ignore) ignore_check_btn.setChecked(ignore)
# Skip valued # Skip valued
check_skip = QtGui.QCheckBox(_("SKIP_VALUED")) skip_check_btn = QtGui.QCheckBox(_("SKIP_VALUED"))
check_skip.setEnabled(not word_checked and not ignore) skip_check_btn.setEnabled(not word_checked and not ignore)
check_skip.setChecked(skip) skip_check_btn.setChecked(skip)
# events # events
# word # word
@ -516,15 +531,15 @@ class OptionsDialog(Dialog):
self.___last_checkeds___[i].setEnabled(not ignore) self.___last_checkeds___[i].setEnabled(not ignore)
word_checked = word_check_btn.isChecked() word_checked = word_check_btn.isChecked()
check_ignore.setEnabled(not word_checked) ignore_check_btn.setEnabled(not word_checked)
ignore = check_ignore.isChecked() ignore = ignore_check_btn.isChecked()
dict_combo.setEnabled(not word_checked and not ignore) dict_combo.setEnabled(not word_checked and not ignore)
field_combo.setEnabled(not word_checked and not ignore) field_combo.setEnabled(not word_checked and not ignore)
check_skip.setEnabled(not word_checked and not ignore) skip_check_btn.setEnabled(not word_checked and not ignore)
if word_checked: if word_checked:
self.___last_checkeds___ = [ self.___last_checkeds___ = [
check_ignore, dict_combo, ignore_check_btn, dict_combo,
field_combo, check_skip field_combo, skip_check_btn
] ]
word_check_btn.clicked.connect(radio_btn_checked) word_check_btn.clicked.connect(radio_btn_checked)
if word_checked: if word_checked:
@ -532,11 +547,11 @@ class OptionsDialog(Dialog):
radio_btn_checked() radio_btn_checked()
# ignor # ignor
def ignore_check_changed(): def ignore_check_changed():
ignore = not check_ignore.isChecked() ignore = not ignore_check_btn.isChecked()
dict_combo.setEnabled(ignore) dict_combo.setEnabled(ignore)
field_combo.setEnabled(ignore) field_combo.setEnabled(ignore)
check_skip.setEnabled(ignore) skip_check_btn.setEnabled(ignore)
check_ignore.clicked.connect(ignore_check_changed) ignore_check_btn.clicked.connect(ignore_check_changed)
# dict # dict
def dict_combo_changed(index): def dict_combo_changed(index):
'''dict combo box index changed''' '''dict combo box index changed'''
@ -544,40 +559,44 @@ class OptionsDialog(Dialog):
field_combo, field_combo,
dict_combo.currentText(), dict_combo.currentText(),
dict_combo.itemData(index), dict_combo.itemData(index),
field_combo.currentText() field_combo.currentText(),
field_combo.itemData(field_combo.currentIndex())
) )
dict_combo.currentIndexChanged.connect(dict_combo_changed) dict_combo.currentIndexChanged.connect(dict_combo_changed)
self.dicts_layout.addWidget(word_check_btn, i + 1, 0) self.dicts_layout.addWidget(word_check_btn, i + 1, 0)
self.dicts_layout.addWidget(check_ignore, i + 1, 1) self.dicts_layout.addWidget(ignore_check_btn, i + 1, 1)
self.dicts_layout.addWidget(dict_combo, i + 1, 2) self.dicts_layout.addWidget(dict_combo, i + 1, 2)
self.dicts_layout.addWidget(field_combo, i + 1, 3) self.dicts_layout.addWidget(field_combo, i + 1, 3)
self.dicts_layout.addWidget(check_skip, i + 1, 4) self.dicts_layout.addWidget(skip_check_btn, i + 1, 4)
self.___options___.append([ self.___options___.append({
word_check_btn, 'model': {'fld_name': fld_name, 'fld_ord': fld_ord},
dict_combo, 'word_check_btn': word_check_btn,
field_combo, 'dict_combo': dict_combo,
check_ignore, 'field_combo': field_combo,
check_skip 'ignore_check_btn': ignore_check_btn,
]) 'skip_check_btn': skip_check_btn
})
def save(self): def save(self):
'''save config to file'''
if not self.current_model: if not self.current_model:
return return
data = dict() data = dict()
maps = [ maps = []
{ for row in self.___options___:
"word_checked": x[0].isChecked(), maps.append({
"dict": x[1].currentText().strip(), 'fld_name': row['model']['fld_name'],
"dict_unique": x[1].itemData(x[1].currentIndex()) if x[1].itemData(x[1].currentIndex()) else '', 'fld_ord': row['model']['fld_ord'],
"dict_field": x[2].currentText().strip(), 'word_checked': row['word_check_btn'].isChecked(),
"fld_ord": get_ord_from_fldname(self.current_model, x[0].text()), 'dict_name': row['dict_combo'].currentText().strip(),
'ignore': x[3].isChecked(), 'dict_unique': row['dict_combo'].itemData(row['dict_combo'].currentIndex()),
'skip_valued': x[4].isChecked(), 'dict_fld_name': row['field_combo'].currentText().strip(),
} 'dict_fld_ord': row['field_combo'].itemData(row['field_combo'].currentIndex()),
for x in self.___options___ 'ignore': row['ignore_check_btn'].isChecked(),
] 'skip_valued': row['skip_check_btn'].isChecked()
})
current_model_id = str(self.current_model['id']) current_model_id = str(self.current_model['id'])
data[current_model_id] = maps data[current_model_id] = maps
data['last_model'] = self.current_model['id'] data['last_model'] = self.current_model['id']