2018-07-01 10:55:30 +08:00
|
|
|
|
#-*- coding:utf-8 -*-
|
|
|
|
|
|
#
|
2018-07-05 12:38:04 +08:00
|
|
|
|
# Copyright © 2016–2017 ST.Huang <wenhonghuang@gmail.com>
|
2018-07-01 10:55:30 +08:00
|
|
|
|
#
|
2018-07-05 12:38:04 +08:00
|
|
|
|
# Support: Report an issue at https://github.com/sth2018/FastWordQuery/issues
|
2018-07-01 10:55:30 +08:00
|
|
|
|
#
|
|
|
|
|
|
# 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/>.
|
|
|
|
|
|
|
|
|
|
|
|
import inspect
|
|
|
|
|
|
import os
|
|
|
|
|
|
from functools import wraps
|
|
|
|
|
|
|
|
|
|
|
|
from aqt import mw
|
|
|
|
|
|
from aqt.qt import QThread
|
|
|
|
|
|
from aqt.utils import showInfo
|
|
|
|
|
|
from .base import LocalService, MdxService, StardictService, WebService
|
|
|
|
|
|
from ..context import config
|
|
|
|
|
|
from ..utils import MapDict, importlib
|
|
|
|
|
|
|
|
|
|
|
|
|
2018-07-05 12:38:04 +08:00
|
|
|
|
def service_wrap(service, *args):
|
|
|
|
|
|
"""
|
|
|
|
|
|
wrap the service class constructor
|
|
|
|
|
|
"""
|
|
|
|
|
|
def _service():
|
|
|
|
|
|
return service(*args)
|
|
|
|
|
|
return _service
|
|
|
|
|
|
|
2018-07-01 10:55:30 +08:00
|
|
|
|
class ServiceManager(object):
|
2018-07-05 12:38:04 +08:00
|
|
|
|
"""
|
|
|
|
|
|
Query service class manager
|
|
|
|
|
|
"""
|
2018-07-01 10:55:30 +08:00
|
|
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
|
|
self.update_services()
|
|
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
|
def services(self):
|
|
|
|
|
|
return self.web_services | self.local_services
|
|
|
|
|
|
|
|
|
|
|
|
# def start_all(self):
|
|
|
|
|
|
# self.fetch_headers()
|
|
|
|
|
|
# make all local services available
|
|
|
|
|
|
# for service in self.local_services:
|
|
|
|
|
|
# if not service.index(only_header=True):
|
|
|
|
|
|
# self.local_services.remove(service)
|
|
|
|
|
|
|
|
|
|
|
|
def update_services(self):
|
|
|
|
|
|
self.web_services, self.local_custom_services = self._get_services_from_files()
|
|
|
|
|
|
self.local_services = self._get_available_local_services()
|
|
|
|
|
|
# self.fetch_headers()
|
|
|
|
|
|
# combine the customized local services into local services
|
|
|
|
|
|
self.local_services.update(self.local_custom_services)
|
|
|
|
|
|
|
2018-07-05 12:38:04 +08:00
|
|
|
|
def get_service(self, unique):
|
2018-07-01 10:55:30 +08:00
|
|
|
|
# webservice unique: class name
|
|
|
|
|
|
# mdxservice unique: dict filepath
|
|
|
|
|
|
for each in self.services:
|
2018-07-05 12:38:04 +08:00
|
|
|
|
if each.__unique__ == unique:
|
|
|
|
|
|
#cls = getattr(each,"__class__")
|
|
|
|
|
|
return each()
|
2018-07-01 10:55:30 +08:00
|
|
|
|
|
|
|
|
|
|
def get_service_action(self, service, label):
|
|
|
|
|
|
for each in service.fields:
|
|
|
|
|
|
if each.label == label:
|
|
|
|
|
|
return each
|
|
|
|
|
|
|
|
|
|
|
|
def _get_services_from_files(self, *args):
|
|
|
|
|
|
"""
|
|
|
|
|
|
get service from service packages, available type is
|
|
|
|
|
|
WebService, LocalService
|
|
|
|
|
|
"""
|
|
|
|
|
|
web_services, local_custom_services = set(), set()
|
|
|
|
|
|
mypath = os.path.dirname(os.path.realpath(__file__))
|
|
|
|
|
|
files = [f for f in os.listdir(mypath)
|
2018-07-06 12:29:50 +08:00
|
|
|
|
if f not in ('__init__.py', 'base.py', 'manager.py', 'pool.py') and not f.endswith('.pyc') and not os.path.isdir(mypath+os.sep+f)]
|
2018-07-01 10:55:30 +08:00
|
|
|
|
base_class = (WebService, LocalService,
|
|
|
|
|
|
MdxService, StardictService)
|
|
|
|
|
|
|
|
|
|
|
|
for f in files:
|
2018-07-06 12:29:50 +08:00
|
|
|
|
#try:
|
|
|
|
|
|
module = importlib.import_module(
|
|
|
|
|
|
'.%s' % os.path.splitext(f)[0], __package__)
|
|
|
|
|
|
for name, cls in inspect.getmembers(module, predicate=inspect.isclass):
|
|
|
|
|
|
if cls in base_class:
|
|
|
|
|
|
continue
|
|
|
|
|
|
#try:
|
|
|
|
|
|
#service = cls(*args)
|
|
|
|
|
|
service = service_wrap(cls, *args)
|
|
|
|
|
|
service.__unique__ = name
|
|
|
|
|
|
if issubclass(cls, WebService):
|
|
|
|
|
|
web_services.add(service)
|
|
|
|
|
|
# get the customized local services
|
|
|
|
|
|
if issubclass(cls, LocalService):
|
|
|
|
|
|
local_custom_services.add(service)
|
|
|
|
|
|
#except Exception:
|
|
|
|
|
|
# exclude the local service whose path has error.
|
|
|
|
|
|
# pass
|
|
|
|
|
|
#except ImportError:
|
|
|
|
|
|
# continue
|
2018-07-01 10:55:30 +08:00
|
|
|
|
return web_services, local_custom_services
|
|
|
|
|
|
|
|
|
|
|
|
def _get_available_local_services(self):
|
2018-07-05 12:38:04 +08:00
|
|
|
|
|
2018-07-01 10:55:30 +08:00
|
|
|
|
services = set()
|
|
|
|
|
|
for each in config.dirs:
|
|
|
|
|
|
for dirpath, dirnames, filenames in os.walk(each):
|
|
|
|
|
|
for filename in filenames:
|
2018-07-05 12:38:04 +08:00
|
|
|
|
service = None
|
2018-07-01 10:55:30 +08:00
|
|
|
|
dict_path = os.path.join(dirpath, filename)
|
|
|
|
|
|
if MdxService.support(dict_path):
|
2018-07-05 12:38:04 +08:00
|
|
|
|
service = service_wrap(MdxService, dict_path)
|
2018-07-01 10:55:30 +08:00
|
|
|
|
if StardictService.support(dict_path):
|
2018-07-05 12:38:04 +08:00
|
|
|
|
service = service_wrap(StardictService, dict_path)
|
|
|
|
|
|
if service:
|
|
|
|
|
|
service.__unique__ = dict_path
|
|
|
|
|
|
services.add(service)
|
2018-07-01 10:55:30 +08:00
|
|
|
|
# support mdx dictionary and stardict format dictionary
|
|
|
|
|
|
return services
|