Skip to content

Commit

Permalink
improve progress info display; fix some bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
finalion committed Jul 13, 2017
1 parent 6136ba7 commit 579e235
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 90 deletions.
2 changes: 1 addition & 1 deletion wquery/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
from .lang import _
from .odds import get_model_byId, get_ord_from_fldname

VERSION = 'V4.0.20170709'
VERSION = 'V4.1.20170714'
CONFIG_FILENAME = '.wqcfg.json'


Expand Down
47 changes: 28 additions & 19 deletions wquery/progress.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,46 +24,55 @@ def __init__(self, mw):
self._levels = 0
self.aborted = False
self.rows_number = 0
self.info_dict = dict()
self._msg_info = defaultdict(dict)
self._msg_count = defaultdict(int)
# Creating progress dialogs
##########################################################################

@pyqtSlot(dict)
def update_labels(self, data):
self.info_dict.update(data)
labels = ''
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.info_dict.get(index, None)
info = self._msg_info.get(index, None)
if not info:
continue
text = info.get('text', None)
if text:
labels += text
if info.type == 'text':
lst.append(info.text)
else:
labels += u"{2} [{0}] {1}".format(
info['service_name'], info['field_name'], info['flag'])
labels += '<br>'
lst.append(u"{2} [{0}] {1}".format(
info.service_name, info.field_name, info.flag))

number_info = ''
words_number, fields_number = \
self.info_dict.get('words_number', 0), \
self.info_dict.get('fields_number', 0)
words_number, fields_number = (
self._msg_count['words_number'], self._msg_count['fields_number'])
if words_number or fields_number:
labels += 45 * '-' + '<br>'
number_info = u'{0} {1} {2}, {3} {4}'.format(
number_info += '<br>' + 45 * '-' + '<br>'
number_info += u'{0} {1} {2}, {3} {4}'.format(
_('QUERIED'), words_number, _(
'WORDS'), fields_number, _('FIELDS')
)
labels += number_info

self.update(labels)
self._win.repaint()
self.update('<br>'.join(lst) + number_info)
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.info_dict.clear()
self._msg_info.clear()
self._msg_count.clear()
self.rows_number = rows
self.aborted = False
self._levels += 1
Expand Down
5 changes: 3 additions & 2 deletions wquery/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
from .service.base import QueryResult, copy_static_file
from .utils import Empty, Queue, wrap_css
from .progress import ProgressManager
from utils.mapdict import MapDict


def inspect_note(note):
Expand Down Expand Up @@ -83,7 +84,7 @@ def query_from_browser(browser):
update_note_fields(note, results)
fields_number += len(results)
progress.update_labels(
{'words_number': i + 1, 'fields_number': fields_number})
MapDict(type='count', words_number=i + 1, fields_number=fields_number))
except InvalidWordException:
showInfo(_("NO_QUERY_WORD"))
promot_choose_css()
Expand Down Expand Up @@ -276,7 +277,7 @@ def start_worker(self, worker):
worker.start()

def start_all_workers(self):
progress.rows_number = len(self.workers)
progress.update_rows(len(self.workers))
for i, worker in enumerate(self.workers.values()):
worker.index = i
worker.start()
Expand Down
34 changes: 14 additions & 20 deletions wquery/service/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,19 +160,22 @@ def active(self, action_label, word):
# if the service instance is LocalService,
# then have to build then index.
if isinstance(self, LocalService):
self.notify({self.work_id: {'text': u'Building %s...' %
os.path.splitext(os.path.basename(self.dict_path))[0]}})
self.notify(MapDict(type='text', index=self.work_id,
text=u'Building %s...' %
os.path.splitext(os.path.basename(self.dict_path))[0]))
self.builder.check_build()

for each in self.exporters:
if action_label == each[0]:
self.notify({self.work_id: {'service_name': self.title,
'field_name': action_label,
'flag': u'->'}})
self.notify(MapDict(type='info', index=self.work_id,
service_name=self.title,
field_name=action_label,
flag=u'->'))
result = each[1]()
self.notify({self.work_id: {'service_name': self.title,
'field_name': action_label,
'flag': u'√'}})
self.notify(MapDict(type='info', index=self.work_id,
service_name=self.title,
field_name=action_label,
flag=u'√'))
return result if result else QueryResult.default() # avoid return None
return QueryResult.default()

Expand Down Expand Up @@ -261,15 +264,12 @@ class MdxService(LocalService):

def __init__(self, dict_path):
super(MdxService, self).__init__(dict_path)
# self.index()
# cache all the static files queried, cache builder
# {'builder':builder, 'files':[...static files list...]}
self.media_cache = defaultdict(set)
self.cache = defaultdict(str)
self.query_interval = 0.01
self.styles = []
self.builder = IndexBuilder(self.dict_path)
self.index_header()
self.builder = IndexBuilder(dict_path)
self.builder.get_header()

@staticmethod
def support(dict_path):
Expand All @@ -282,9 +282,6 @@ def title(self):
else:
return self.builder.meta['title']

def index_header(self):
self.builder.get_header()

@export(u"default", 0)
def fld_whole(self):
html = self.get_html()
Expand Down Expand Up @@ -386,7 +383,7 @@ def __init__(self, dict_path):
super(StardictService, self).__init__(dict_path)
self.query_interval = 0.05
self.builder = Dictionary(self.dict_path, in_memory=False)
self.index_header()
self.builder.get_header()

@staticmethod
def support(dict_path):
Expand All @@ -399,9 +396,6 @@ def title(self):
else:
return self.builder.ifo.bookname.decode('utf-8')

def index_header(self):
self.builder.get_header()

@export(u"default", 0)
def fld_whole(self):
self.builder.check_build()
Expand Down
44 changes: 12 additions & 32 deletions wquery/service/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def __init__(self):

@property
def services(self):
return self.web_services + self.local_services
return self.web_services | self.local_services

# def start_all(self):
# self.fetch_headers()
Expand Down Expand Up @@ -69,7 +69,7 @@ def _get_services_from_files(self, type_, *args):
get service from service packages, available type is
WebService, LocalService
"""
services = []
services = set()
mypath = os.path.dirname(os.path.realpath(__file__))
files = [f for f in os.listdir(mypath)
if f not in ('__init__.py', 'base.py',) and not f.endswith('.pyc')]
Expand All @@ -84,9 +84,12 @@ def _get_services_from_files(self, type_, *args):
if issubclass(cls, type_) and cls not in base_class:
label = getattr(
cls, '__register_label__', cls.__name__)
service = cls(*args)
if service not in services:
services.append(service)
try:
service = cls(*args)
services.add(service)
except Exception:
# exclude the local service whose path has error.
pass
except ImportError:
continue
return services
Expand All @@ -95,41 +98,18 @@ def get_available_web_services(self):
return self._get_services_from_files(WebService)

def get_available_local_services(self):
services = []
services = set()
for each in config.dirs:
for dirpath, dirnames, filenames in os.walk(each):
for filename in filenames:
dict_path = os.path.join(dirpath, filename)
if MdxService.support(dict_path):
services.append(MdxService(dict_path))
services.add(MdxService(dict_path))
if StardictService.support(dict_path):
services.append(StardictService(dict_path))
services.add(StardictService(dict_path))
# support mdx dictionary and stardict format dictionary
# get the customized local services
customed_services = self._get_services_from_files(LocalService, None)
# filter the customized service whose dict path is not available
services.extend([service for service in customed_services
if os.path.exists(service.dict_path)])
services.update(customed_services)
return services

# def fetch_headers(self):
# mw.progress.start(
# immediate=True, label=u"Fetching dictionary information ...")
# index_thread = self.DictHeadIndexer(self)
# index_thread.start()
# while not index_thread.isFinished():
# mw.app.processEvents()
# index_thread.wait(100)
# mw.progress.finish()

# class DictHeadIndexer(QThread):

# def __init__(self, manager):
# QThread.__init__(self)
# self.manager = manager

# def run(self):
# for service in self.manager.local_services:
# mw.progress.update(
# label=u"Fetching dictionary information ...\n{0}".format(os.path.basename(service.dict_path)))
# service.index_header()
39 changes: 23 additions & 16 deletions wquery/ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,11 +145,18 @@ def build(self):
# add description of radio buttons AND ok button
bottom_layout = QHBoxLayout()
about_btn = QPushButton(_('ABOUT'))
home_label = QLabel(
'<a href="https://finalion.github.io/WordQuery/">User Guide</a>')
home_label.setOpenExternalLinks(True)
shop_label = QLabel(
'<a href="https://finalion.github.io/WordQuery/shop.html">Service Shop</a>')
shop_label.setOpenExternalLinks(True)
about_btn.clicked.connect(self.show_about)
btnbox = QDialogButtonBox(QDialogButtonBox.Ok, Qt.Horizontal, self)
btnbox.accepted.connect(self.accept)
bottom_layout.addWidget(about_btn)
bottom_layout.addWidget(QLabel(_("RADIOS_DESC")))
bottom_layout.addWidget(home_label)
bottom_layout.addWidget(shop_label)
bottom_layout.addWidget(btnbox)
self.main_layout.addLayout(bottom_layout)
self.setLayout(self.main_layout)
Expand Down Expand Up @@ -219,7 +226,9 @@ def clear_layout(layout):
self.dicts_layout.addWidget(label1, 0, 0)
self.dicts_layout.addWidget(label2, 0, 1)
self.dicts_layout.addWidget(label3, 0, 2)

maps = config.get_maps(model['id'])
self.radio_group = QButtonGroup()
for i, fld in enumerate(model['flds']):
ord = fld['ord']
name = fld['name']
Expand All @@ -232,6 +241,7 @@ def clear_layout(layout):
self.add_dict_layout(i, fld_name=name)
else:
self.add_dict_layout(i, fld_name=name)

self.setLayout(self.main_layout)
self.resize(widget_size.dialog_width,
(i + 1) * widget_size.map_max_height + widget_size.dialog_height_margin)
Expand Down Expand Up @@ -296,7 +306,6 @@ def fill_field_combo_options(self, field_combo, dict_combo_text, dict_combo_item
field_combo.setFocus(Qt.MouseFocusReason) # MouseFocusReason
else:
field_text = field_combo.currentText()
current_service = None
service_unique = dict_combo_itemdata
current_service = service_manager.get_service(service_unique)
# problem
Expand Down Expand Up @@ -326,14 +335,17 @@ def add_dict_layout(self, i, **kwargs):
kwargs.get('fld_name', ''),
kwargs.get('dict_field', ''),)

fldname_label = QRadioButton(fld_name)
fldname_label.setMinimumSize(widget_size.map_fld_width, 0)
fldname_label.setMaximumSize(widget_size.map_fld_width,
widget_size.map_max_height)
fldname_label.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed)
fldname_label.setCheckable(True)
fldname_label.clicked.connect(self.radio_btn_checked)
fldname_label.setChecked(word_checked)
word_check_btn = QRadioButton(fld_name)
word_check_btn.setMinimumSize(widget_size.map_fld_width, 0)
word_check_btn.setMaximumSize(widget_size.map_fld_width,
widget_size.map_max_height)
word_check_btn.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed)
word_check_btn.setCheckable(True)
word_check_btn.clicked.connect(self.radio_btn_checked)
if i == 0:
word_checked = True
word_check_btn.setChecked(word_checked)
self.radio_group.addButton(word_check_btn)

dict_combo = QComboBox()
dict_combo.setMinimumSize(widget_size.map_dictname_width, 0)
Expand All @@ -353,15 +365,10 @@ def add_dict_layout(self, i, **kwargs):
field_combo.setEditText(dict_field)
self.fill_field_combo_options(field_combo, dict_name, dict_unique)

self.dicts_layout.addWidget(fldname_label, i + 1, 0)
self.dicts_layout.addWidget(word_check_btn, i + 1, 0)
self.dicts_layout.addWidget(dict_combo, i + 1, 1)
self.dicts_layout.addWidget(field_combo, i + 1, 2)

self.setLayout(self.main_layout)
# for osx
# mw.options_dialog.activateWindow()
# mw.options_dialog.raise_()

def _get_combos(self, flag):
# 0 : dict_combox, 1:field_combox
dict_combos = self.findChildren(QComboBox)
Expand Down

0 comments on commit 579e235

Please sign in to comment.