refactoring
This commit is contained in:
parent
7f3b7ffd3b
commit
cc288141f3
@ -32,7 +32,7 @@ from .constants import Endpoint, Template
|
|||||||
from .context import config
|
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, QueryResult, copy_static_file
|
from .service import service_manager, service_pool, QueryResult, copy_static_file
|
||||||
from .utils import Empty, MapDict, Queue, wrap_css
|
from .utils import Empty, MapDict, Queue, wrap_css
|
||||||
|
|
||||||
|
|
||||||
@ -257,15 +257,16 @@ def update_note_field(note, fld_index, fld_result):
|
|||||||
def promot_choose_css():
|
def promot_choose_css():
|
||||||
for local_service in service_manager.local_services:
|
for local_service in service_manager.local_services:
|
||||||
try:
|
try:
|
||||||
missed_css = local_service.missed_css.pop()
|
service = service_pool.get(local_service.__unique__)
|
||||||
|
missed_css = service.missed_css.pop()
|
||||||
showInfo(Template.miss_css.format(
|
showInfo(Template.miss_css.format(
|
||||||
dict=local_service.title, css=missed_css))
|
dict=service.title, css=missed_css))
|
||||||
filepath = QFileDialog.getOpenFileName(
|
filepath = QFileDialog.getOpenFileName(
|
||||||
caption=u'Choose css file', filter=u'CSS (*.css)')
|
caption=u'Choose css file', filter=u'CSS (*.css)')
|
||||||
if filepath:
|
if filepath:
|
||||||
shutil.copy(filepath, u'_' + missed_css)
|
shutil.copy(filepath, u'_' + missed_css)
|
||||||
wrap_css(u'_' + missed_css)
|
wrap_css(u'_' + missed_css)
|
||||||
local_service.missed_css.clear()
|
service.missed_css.clear()
|
||||||
|
|
||||||
except KeyError as e:
|
except KeyError as e:
|
||||||
pass
|
pass
|
||||||
@ -343,36 +344,3 @@ def query_all_flds(note):
|
|||||||
service_pool.put(service)
|
service_pool.put(service)
|
||||||
|
|
||||||
return result, success_num
|
return result, success_num
|
||||||
|
|
||||||
|
|
||||||
class ServicePool(object):
|
|
||||||
"""
|
|
||||||
Service instance pool
|
|
||||||
"""
|
|
||||||
def __init__(self):
|
|
||||||
self.pools = {}
|
|
||||||
|
|
||||||
def get(self, unique):
|
|
||||||
queue = self.pools.get(unique, None)
|
|
||||||
if queue:
|
|
||||||
try:
|
|
||||||
return queue.get(True, timeout=0.1)
|
|
||||||
except Empty:
|
|
||||||
pass
|
|
||||||
|
|
||||||
return service_manager.get_service(unique)
|
|
||||||
|
|
||||||
def put(self, service):
|
|
||||||
unique = service.unique
|
|
||||||
queue = self.pools.get(unique, None)
|
|
||||||
if queue == None:
|
|
||||||
queue = Queue()
|
|
||||||
self.pools[unique] = queue
|
|
||||||
|
|
||||||
queue.put(service)
|
|
||||||
|
|
||||||
def clean(self):
|
|
||||||
self.pools = {}
|
|
||||||
|
|
||||||
|
|
||||||
service_pool = ServicePool() # Service Instance Pool Manager
|
|
||||||
|
|||||||
@ -18,6 +18,8 @@
|
|||||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
from .manager import ServiceManager
|
from .manager import ServiceManager
|
||||||
|
from .pool import ServicePool
|
||||||
from .base import QueryResult, copy_static_file
|
from .base import QueryResult, copy_static_file
|
||||||
|
|
||||||
service_manager = ServiceManager()
|
service_manager = ServiceManager() # Service Manager
|
||||||
|
service_pool = ServicePool(service_manager) # Service Instance Pool Manager
|
||||||
@ -1,8 +1,8 @@
|
|||||||
#-*- coding:utf-8 -*-
|
#-*- coding:utf-8 -*-
|
||||||
#
|
#
|
||||||
# Copyright © 2016–2017 Liang Feng <finalion@gmail.com>
|
# Copyright © 2016–2017 ST.Huang <wenhonghuang@gmail.com>
|
||||||
#
|
#
|
||||||
# Support: Report an issue at https://github.com/finalion/WordQuery/issues
|
# Support: Report an issue at https://github.com/sth2018/FastWordQuery/issues
|
||||||
#
|
#
|
||||||
# This program is free software: you can redistribute it and/or modify
|
# 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
|
# it under the terms of the GNU General Public License as published by
|
||||||
@ -29,7 +29,18 @@ from ..context import config
|
|||||||
from ..utils import MapDict, importlib
|
from ..utils import MapDict, importlib
|
||||||
|
|
||||||
|
|
||||||
|
def service_wrap(service, *args):
|
||||||
|
"""
|
||||||
|
wrap the service class constructor
|
||||||
|
"""
|
||||||
|
def _service():
|
||||||
|
return service(*args)
|
||||||
|
return _service
|
||||||
|
|
||||||
class ServiceManager(object):
|
class ServiceManager(object):
|
||||||
|
"""
|
||||||
|
Query service class manager
|
||||||
|
"""
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.update_services()
|
self.update_services()
|
||||||
@ -52,13 +63,13 @@ class ServiceManager(object):
|
|||||||
# combine the customized local services into local services
|
# combine the customized local services into local services
|
||||||
self.local_services.update(self.local_custom_services)
|
self.local_services.update(self.local_custom_services)
|
||||||
|
|
||||||
def get_service(self, unique, *args):
|
def get_service(self, unique):
|
||||||
# webservice unique: class name
|
# webservice unique: class name
|
||||||
# mdxservice unique: dict filepath
|
# mdxservice unique: dict filepath
|
||||||
for each in self.services:
|
for each in self.services:
|
||||||
if each.unique == unique:
|
if each.__unique__ == unique:
|
||||||
cls = getattr(each,"__class__")
|
#cls = getattr(each,"__class__")
|
||||||
return cls(*args)
|
return each()
|
||||||
|
|
||||||
def get_service_action(self, service, label):
|
def get_service_action(self, service, label):
|
||||||
for each in service.fields:
|
for each in service.fields:
|
||||||
@ -73,7 +84,7 @@ class ServiceManager(object):
|
|||||||
web_services, local_custom_services = set(), set()
|
web_services, local_custom_services = set(), set()
|
||||||
mypath = os.path.dirname(os.path.realpath(__file__))
|
mypath = os.path.dirname(os.path.realpath(__file__))
|
||||||
files = [f for f in os.listdir(mypath)
|
files = [f for f in os.listdir(mypath)
|
||||||
if f not in ('__init__.py', 'base.py', 'manager.py') and not f.endswith('.pyc')]
|
if f not in ('__init__.py', 'base.py', 'manager.py', 'pool.py') and not f.endswith('.pyc')]
|
||||||
base_class = (WebService, LocalService,
|
base_class = (WebService, LocalService,
|
||||||
MdxService, StardictService)
|
MdxService, StardictService)
|
||||||
|
|
||||||
@ -84,29 +95,36 @@ class ServiceManager(object):
|
|||||||
for name, cls in inspect.getmembers(module, predicate=inspect.isclass):
|
for name, cls in inspect.getmembers(module, predicate=inspect.isclass):
|
||||||
if cls in base_class:
|
if cls in base_class:
|
||||||
continue
|
continue
|
||||||
try:
|
#try:
|
||||||
service = cls(*args)
|
#service = cls(*args)
|
||||||
|
service = service_wrap(cls, *args)
|
||||||
|
service.__unique__ = name
|
||||||
if issubclass(cls, WebService):
|
if issubclass(cls, WebService):
|
||||||
web_services.add(service)
|
web_services.add(service)
|
||||||
# get the customized local services
|
# get the customized local services
|
||||||
if issubclass(cls, LocalService):
|
if issubclass(cls, LocalService):
|
||||||
local_custom_services.add(service)
|
local_custom_services.add(service)
|
||||||
except Exception:
|
#except Exception:
|
||||||
# exclude the local service whose path has error.
|
# exclude the local service whose path has error.
|
||||||
pass
|
# pass
|
||||||
except ImportError:
|
except ImportError:
|
||||||
continue
|
continue
|
||||||
return web_services, local_custom_services
|
return web_services, local_custom_services
|
||||||
|
|
||||||
def _get_available_local_services(self):
|
def _get_available_local_services(self):
|
||||||
|
|
||||||
services = set()
|
services = set()
|
||||||
for each in config.dirs:
|
for each in config.dirs:
|
||||||
for dirpath, dirnames, filenames in os.walk(each):
|
for dirpath, dirnames, filenames in os.walk(each):
|
||||||
for filename in filenames:
|
for filename in filenames:
|
||||||
|
service = None
|
||||||
dict_path = os.path.join(dirpath, filename)
|
dict_path = os.path.join(dirpath, filename)
|
||||||
if MdxService.support(dict_path):
|
if MdxService.support(dict_path):
|
||||||
services.add(MdxService(dict_path))
|
service = service_wrap(MdxService, dict_path)
|
||||||
if StardictService.support(dict_path):
|
if StardictService.support(dict_path):
|
||||||
services.add(StardictService(dict_path))
|
service = service_wrap(StardictService, dict_path)
|
||||||
|
if service:
|
||||||
|
service.__unique__ = dict_path
|
||||||
|
services.add(service)
|
||||||
# support mdx dictionary and stardict format dictionary
|
# support mdx dictionary and stardict format dictionary
|
||||||
return services
|
return services
|
||||||
|
|||||||
51
src/fastwq/service/pool.py
Normal file
51
src/fastwq/service/pool.py
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
#-*- coding:utf-8 -*-
|
||||||
|
#
|
||||||
|
# Copyright © 2016–2017 ST.Huang <wenhonghuang@gmail.com>
|
||||||
|
#
|
||||||
|
# Support: Report an issue at https://github.com/sth2018/FastWordQuery/issues
|
||||||
|
#
|
||||||
|
# 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
|
||||||
|
# any later version; http://www.gnu.org/copyleft/gpl.html.
|
||||||
|
#
|
||||||
|
# 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 ..utils import Empty, Queue
|
||||||
|
|
||||||
|
|
||||||
|
class ServicePool(object):
|
||||||
|
"""
|
||||||
|
Service instance pool
|
||||||
|
"""
|
||||||
|
def __init__(self, manager):
|
||||||
|
self.pools = {}
|
||||||
|
self.manager = manager
|
||||||
|
|
||||||
|
def get(self, unique):
|
||||||
|
queue = self.pools.get(unique, None)
|
||||||
|
if queue:
|
||||||
|
try:
|
||||||
|
return queue.get(True, timeout=0.1)
|
||||||
|
except Empty:
|
||||||
|
pass
|
||||||
|
|
||||||
|
return self.manager.get_service(unique)
|
||||||
|
|
||||||
|
def put(self, service):
|
||||||
|
unique = service.unique
|
||||||
|
queue = self.pools.get(unique, None)
|
||||||
|
if queue == None:
|
||||||
|
queue = Queue()
|
||||||
|
self.pools[unique] = queue
|
||||||
|
|
||||||
|
queue.put(service)
|
||||||
|
|
||||||
|
def clean(self):
|
||||||
|
self.pools = {}
|
||||||
@ -32,7 +32,7 @@ from aqt.utils import shortcut, showInfo
|
|||||||
from .constants import VERSION, Endpoint, Template
|
from .constants import VERSION, Endpoint, Template
|
||||||
from .context import APP_ICON, config
|
from .context import APP_ICON, config
|
||||||
from .lang import _, _sl
|
from .lang import _, _sl
|
||||||
from .service import service_manager
|
from .service import service_manager, service_pool
|
||||||
from .utils import MapDict, get_icon, get_model_byId, get_ord_from_fldname
|
from .utils import MapDict, get_icon, get_model_byId, get_ord_from_fldname
|
||||||
|
|
||||||
DICT_COMBOS, DICT_FILED_COMBOS, ALL_COMBOS = [0, 1, 2]
|
DICT_COMBOS, DICT_FILED_COMBOS, ALL_COMBOS = [0, 1, 2]
|
||||||
@ -340,15 +340,21 @@ class OptionsDialog(QDialog):
|
|||||||
def fill_dict_combo_options(self, dict_combo, current_text):
|
def fill_dict_combo_options(self, dict_combo, current_text):
|
||||||
dict_combo.clear()
|
dict_combo.clear()
|
||||||
dict_combo.addItem(_('NOT_DICT_FIELD'))
|
dict_combo.addItem(_('NOT_DICT_FIELD'))
|
||||||
|
|
||||||
dict_combo.insertSeparator(dict_combo.count())
|
dict_combo.insertSeparator(dict_combo.count())
|
||||||
for service in service_manager.local_services:
|
for cls in service_manager.local_services:
|
||||||
# combo_data.insert("data", each.label)
|
# combo_data.insert("data", each.label)
|
||||||
|
service = service_pool.get(cls.__unique__)
|
||||||
dict_combo.addItem(
|
dict_combo.addItem(
|
||||||
service.title, userData=service.unique)
|
service.title, userData=service.unique)
|
||||||
|
service_pool.put(service)
|
||||||
|
|
||||||
dict_combo.insertSeparator(dict_combo.count())
|
dict_combo.insertSeparator(dict_combo.count())
|
||||||
for service in service_manager.web_services:
|
for cls in service_manager.web_services:
|
||||||
|
service = service_pool.get(cls.__unique__)
|
||||||
dict_combo.addItem(
|
dict_combo.addItem(
|
||||||
service.title, userData=service.unique)
|
service.title, userData=service.unique)
|
||||||
|
service_pool.put(service)
|
||||||
|
|
||||||
def set_dict_combo_index():
|
def set_dict_combo_index():
|
||||||
dict_combo.setCurrentIndex(-1)
|
dict_combo.setCurrentIndex(-1)
|
||||||
@ -371,13 +377,14 @@ class OptionsDialog(QDialog):
|
|||||||
else:
|
else:
|
||||||
field_text = field_combo.currentText()
|
field_text = field_combo.currentText()
|
||||||
service_unique = dict_combo_itemdata
|
service_unique = dict_combo_itemdata
|
||||||
current_service = service_manager.get_service(service_unique)
|
current_service = service_pool.get(service_unique)
|
||||||
# problem
|
# problem
|
||||||
if current_service and current_service.fields:
|
if current_service and current_service.fields:
|
||||||
for each in current_service.fields:
|
for each in current_service.fields:
|
||||||
field_combo.addItem(each)
|
field_combo.addItem(each)
|
||||||
if each == field_text:
|
if each == field_text:
|
||||||
field_combo.setEditText(field_text)
|
field_combo.setEditText(field_text)
|
||||||
|
service_pool.put(current_service)
|
||||||
|
|
||||||
def radio_btn_checked(self):
|
def radio_btn_checked(self):
|
||||||
rbs = self.findChildren(QRadioButton)
|
rbs = self.findChildren(QRadioButton)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user