diff --git a/2.0.x.zip b/2.0.x.zip index e7a8ba6..0343cc9 100644 Binary files a/2.0.x.zip and b/2.0.x.zip differ diff --git a/src/fastwq/progress.py b/src/fastwq/progress.py index 9f28d2e..ce18cbe 100644 --- a/src/fastwq/progress.py +++ b/src/fastwq/progress.py @@ -25,40 +25,25 @@ class ProgressManager(object): self.blockUpdates = False self._win = None self._levels = 0 - self.aborted = False - self.rows_number = 0 - self._msg_info = defaultdict(dict) self._msg_count = defaultdict(int) # Creating progress dialogs ########################################################################## - @pyqtSlot(dict) def update_labels(self, data): if self.abort(): return - + if data.type == 'count': self._msg_count.update(data) else: - self._msg_info[data.index] = data - - lst = [] - for index in range(self.rows_number): - info = self._msg_info.get(index, None) - if not info: - continue - if info.type == 'text': - lst.append(info.text) - else: - lst.append(u"{2} [{0}] {1}".format( - info.service_name, info.field_name, info.flag)) + return number_info = '' words_number, fields_number, fails_number = ( - self._msg_count['words_number'], + self._msg_count['words_number'], self._msg_count['fields_number'], self._msg_count['fails_number'] - ) + ) if words_number or fields_number: number_info += _('QUERIED') + '
' + 45 * '-' number_info += u'
{0}: {1}{2}'.format( @@ -68,24 +53,17 @@ class ProgressManager(object): number_info += u'
{0}: {1}{2}'.format( _('FAILURE'), fails_number, _('WORDS')) - self.update('
'.join(lst) + number_info, value=words_number) + self.update(label=number_info, value=words_number) self._win.adjustSize() def update_title(self, title): self._win.setWindowTitle(title) - def update_rows(self, number): - self.rows_number = number - self._msg_info.clear() - def reset_count(self): self._msg_count.clear() def start(self, max=0, min=0, label=None, parent=None, immediate=False, rows=0): - self._msg_info.clear() self._msg_count.clear() - self.rows_number = rows - self.aborted = False self._levels += 1 if self._levels > 1: return diff --git a/src/fastwq/query.py b/src/fastwq/query.py index 80fd1c6..32df18d 100644 --- a/src/fastwq/query.py +++ b/src/fastwq/query.py @@ -35,13 +35,16 @@ from .progress import ProgressManager from .service import service_manager, QueryResult, copy_static_file from .utils import Empty, MapDict, Queue, wrap_css + + def inspect_note(note): - ''' + """ inspect the note, and get necessary input parameters return word_ord: field index of the word in current note return word: the word return maps: dicts map of current note - ''' + """ + maps = config.get_maps(note.model()['id']) for i, m in enumerate(maps): if m.get('word_checked', False): @@ -58,8 +61,11 @@ def inspect_note(note): word = purify_word(note.fields[word_ord]) return word_ord, word, maps -# 工作线程 + class QueryThread(QThread): + """ + Query Worker Thread + """ progress_update = pyqtSignal(dict) note_flush = pyqtSignal(object) @@ -87,7 +93,7 @@ class QueryThread(QThread): if self.manager: if self.manager.update(note, results, success_num): self.note_flush.emit(note) - # 更新进度 + # Update progress window infomation self.progress_update.emit( MapDict( type='count', @@ -99,9 +105,11 @@ class QueryThread(QThread): except InvalidWordException: showInfo(_("NO_QUERY_WORD")) - class QueryWorkerManager(object): + """ + Query Worker Thread Manager + """ def __init__(self): self.workers = [] @@ -122,7 +130,6 @@ class QueryWorkerManager(object): self.get_worker() self.total = self.queue.qsize() - progress.update_rows(len(self.workers)) for worker in self.workers: worker.start() @@ -169,8 +176,11 @@ def handle_flush(note): note.flush() -# 浏览界面查找 def query_from_browser(browser): + """ + Query word from Browser + """ + if not browser: return @@ -184,8 +194,12 @@ def query_from_browser(browser): query_all(notes) browser.model.reset() -# 编辑界面查找 + def query_from_editor_all_fields(editor): + """ + Query word fileds from Editor + """ + if not editor or not editor.note: return @@ -193,9 +207,12 @@ def query_from_editor_all_fields(editor): editor.setNote(editor.note, focus=True) editor.saveNow() + +def query_all(notes): + """ + Query maps word fileds + """ -# 查找所有 -def query_all(notes): if len(notes) == 0: return @@ -215,7 +232,12 @@ def query_all(notes): work_manager.clean() service_pool.clean(); + def update_note_fields(note, results): + """ + Update query result to note fields, return updated fields count. + """ + if not results or not note or len(results) == 0: return count = 0 @@ -227,6 +249,10 @@ def update_note_fields(note, results): def update_note_field(note, fld_index, fld_result): + """ + Update single field, if result is valid then return 1, else return 0 + """ + result, js, jsfile = fld_result.result, fld_result.js, fld_result.jsfile # js process: add to template of the note model add_to_tmpl(note, js=js, jsfile=jsfile) @@ -287,7 +313,11 @@ def add_to_tmpl(note, **kwargs): class InvalidWordException(Exception): """Invalid word exception""" -def query_all_flds(note, progress_update=None): +def query_all_flds(note): + """ + Query all fields of single note + """ + word_ord, word, maps = inspect_note(note) if not word: raise InvalidWordException @@ -315,7 +345,6 @@ def query_all_flds(note, progress_update=None): for task in tasks: try: service = services.get(task['k'], None) - service.set_notifier(progress_update, task['i']) qr = service.active(task['f'], task['w']) if qr: result.update({task['i']: qr}) @@ -329,9 +358,11 @@ def query_all_flds(note, progress_update=None): return result, success_num -# 服务实例对象池 + class ServicePool(object): - + """ + Service instance pool + """ def __init__(self): self.pools = {} @@ -358,6 +389,6 @@ class ServicePool(object): self.pools = {} -progress = ProgressManager(mw) -work_manager = QueryWorkerManager() -service_pool = ServicePool() +progress = ProgressManager(mw) # progress window +work_manager = QueryWorkerManager() # Query Worker Thread Manager +service_pool = ServicePool() # Service Instance Pool Manager diff --git a/src/fastwq/service/base.py b/src/fastwq/service/base.py index e924bfc..0715e71 100644 --- a/src/fastwq/service/base.py +++ b/src/fastwq/service/base.py @@ -159,33 +159,14 @@ class Service(object): # if the service instance is LocalService, # then have to build then index. if isinstance(self, LocalService): - self.notify(MapDict(type='text', index=self.work_id, - text=u'Building %s...' % self._filename)) if isinstance(self, MdxService) or isinstance(self, StardictService): self.builder.check_build() for each in self.exporters: if action_label == each[0]: - self.notify(MapDict(type='info', index=self.work_id, - service_name=self.title, - field_name=action_label, - flag=u'->')) - result = each[1]() - self.notify(MapDict(type='info', index=self.work_id, - service_name=self.title, - field_name=action_label, - flag=u'√')) - return result + return each[1]() return QueryResult.default() - def set_notifier(self, progress_update, index): - self.notify_signal = progress_update - self.work_id = index - - def notify(self, data): - if self.notify_signal: - self.notify_signal.emit(data) - @staticmethod def get_anki_label(filename, type_): formats = {'audio': u'[sound:{0}]',