fix bug in processing local service missed .css files.

This commit is contained in:
St.Huang 2018-07-11 01:24:16 +08:00
parent 1e046b5b24
commit 99601197c1
2 changed files with 48 additions and 22 deletions

View File

@ -33,10 +33,10 @@ from .context import config
from .lang import _, _sl from .lang import _, _sl
from .progress import ProgressWindow from .progress import ProgressWindow
from .service import service_manager, service_pool, QueryResult, copy_static_file from .service import service_manager, service_pool, QueryResult, copy_static_file
from .service.base import LocalService
from .utils import Empty, MapDict, Queue, wrap_css from .utils import Empty, MapDict, Queue, wrap_css
def inspect_note(note): def inspect_note(note):
""" """
inspect the note, and get necessary input parameters inspect the note, and get necessary input parameters
@ -88,9 +88,9 @@ class QueryThread(QThread):
continue continue
try: try:
results, success_num = query_all_flds(note) results, success_num, missed_css = query_all_flds(note)
if not self.exit and self.manager: if not self.exit and self.manager:
if self.manager.update(note, results, success_num): if self.manager.update(note, results, success_num, missed_css):
self.note_flush.emit(note) self.note_flush.emit(note)
except InvalidWordException: except InvalidWordException:
# only show error info on single query # only show error info on single query
@ -117,6 +117,7 @@ class QueryWorkerManager(object):
self.counter = 0 self.counter = 0
self.fails = 0 self.fails = 0
self.fields = 0 self.fields = 0
self.missed_css = list()
def get_worker(self): def get_worker(self):
worker = QueryThread(self) worker = QueryThread(self)
@ -137,7 +138,7 @@ class QueryWorkerManager(object):
worker = self.get_worker() worker = self.get_worker()
worker.run() worker.run()
def update(self, note, results, success_num): def update(self, note, results, success_num, missed_css):
self.mutex.lock() self.mutex.lock()
if success_num > 0: if success_num > 0:
self.counter += 1 self.counter += 1
@ -145,6 +146,7 @@ class QueryWorkerManager(object):
self.fails += 1 self.fails += 1
val = update_note_fields(note, results) val = update_note_fields(note, results)
self.fields += val self.fields += val
self.missed_css += missed_css
self.mutex.unlock() self.mutex.unlock()
if self.total > 1: if self.total > 1:
return val > 0 return val > 0
@ -236,7 +238,7 @@ def query_all(notes):
work_manager.join() work_manager.join()
#progress.finish() #progress.finish()
promot_choose_css() promot_choose_css(work_manager.missed_css)
tooltip(u'{0} {1} {2}, {3} {4}'.format(_('UPDATED'), work_manager.counter, _('CARDS'), work_manager.fields, _('FIELDS'))) tooltip(u'{0} {1} {2}, {3} {4}'.format(_('UPDATED'), work_manager.counter, _('CARDS'), work_manager.fields, _('FIELDS')))
#work_manager.clean() #work_manager.clean()
service_pool.clean() service_pool.clean()
@ -277,21 +279,33 @@ def update_note_field(note, fld_index, fld_result):
return 0 return 0
def promot_choose_css(): def promot_choose_css(missed_css):
for local_service in service_manager.local_services: '''
Choose missed css file and copy to user folder
'''
checked = set()
for css in missed_css:
filename = u'_' + css['file']
if not os.path.exists(filename) and not css['file'] in checked:
checked.add(css['file'])
showInfo(
Template.miss_css.format(
dict = css['title'],
css = css['file']
)
)
try: try:
service = service_pool.get(local_service.__unique__) filepath = css['dict_path'][:css['dict_path'].rindex(os.path.sep)+1]
missed_css = service.missed_css.pop()
showInfo(Template.miss_css.format(
dict=service.title, css=missed_css))
filepath = QFileDialog.getOpenFileName( filepath = QFileDialog.getOpenFileName(
caption=u'Choose css file', filter=u'CSS (*.css)') directory = filepath,
caption = u'Choose css file',
filter = u'CSS (*.css)'
)
if filepath: if filepath:
shutil.copy(filepath, u'_' + missed_css) shutil.copy(filepath, filename)
wrap_css(u'_' + missed_css) wrap_css(filename)
service.missed_css.clear()
except KeyError as e: except KeyError:
pass pass
@ -366,7 +380,15 @@ def query_all_flds(note):
# showInfo(_("NO_QUERY_WORD")) # showInfo(_("NO_QUERY_WORD"))
# pass # pass
missed_css = list()
for service in services.values(): for service in services.values():
if isinstance(service, LocalService):
for css in service.missed_css:
missed_css.append({
'dict_path': service.dict_path,
'title': service.title,
'file': css
})
service_pool.put(service) service_pool.put(service)
return result, success_num return result, success_num, missed_css

View File

@ -468,6 +468,10 @@ class LocalService(Service):
def _filename(self): def _filename(self):
return os.path.splitext(os.path.basename(self.dict_path))[0] return os.path.splitext(os.path.basename(self.dict_path))[0]
def active(self, action_label, word):
self.missed_css.clear()
return super(LocalService, self).active(action_label, word)
class MdxService(LocalService): class MdxService(LocalService):
""" """