anki-word-query/addons/fastwq/service/manager.py

122 lines
4.4 KiB
Python
Raw Normal View History

2018-07-01 10:55:30 +08:00
#-*- coding:utf-8 -*-
#
2018-07-27 17:57:00 +08:00
# Copyright (C) 2018 sthoo <sth201807@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/>.
2018-07-27 14:51:18 +08:00
import sys
2018-07-01 10:55:30 +08:00
import inspect
import os
2018-07-14 17:07:47 +08:00
from hashlib import md5
2018-07-01 10:55:30 +08:00
from .base import LocalService, MdxService, StardictService, WebService, service_wrap
2018-07-01 10:55:30 +08:00
from ..context import config
2018-07-14 21:37:21 +08:00
from ..utils import importlib
2018-07-01 10:55:30 +08:00
2018-07-27 14:51:18 +08:00
reload(sys)
sys.setdefaultencoding('utf8')
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):
2018-07-08 18:00:58 +08:00
return self.web_services + self.local_services
2018-07-01 10:55:30 +08:00
def update_services(self):
2018-08-12 11:26:57 +08:00
self.mdx_services, self.star_dict_services = self._get_available_local_services()
2018-07-01 10:55:30 +08:00
self.web_services, self.local_custom_services = self._get_services_from_files()
# combine the customized local services into local services
2018-08-12 11:26:57 +08:00
self.local_services = self.mdx_services + self.star_dict_services + self.local_custom_services
2018-07-01 10:55:30 +08:00
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
2018-08-12 11:26:57 +08:00
# mdxservice unique: md5 of dict filepath
2018-07-01 10:55:30 +08:00
for each in self.services:
2018-07-05 12:38:04 +08:00
if each.__unique__ == unique:
2018-07-14 17:07:47 +08:00
service = each()
service.unique = unique
return service
2018-07-01 10:55:30 +08:00
def _get_services_from_files(self, *args):
"""
get service from service packages, available type is
WebService, LocalService
"""
2018-07-14 21:21:31 +08:00
service_path = u'dict'
2018-07-08 18:00:58 +08:00
web_services, local_custom_services = list(), list()
2018-07-14 22:03:58 +08:00
mypath = os.path.join(os.path.dirname(os.path.realpath(__file__)), service_path)
2018-08-12 11:26:57 +08:00
files = [
f for f in os.listdir(mypath) \
if f not in ('__init__.py') and \
not f.endswith('.pyc') and \
not os.path.isdir(mypath+os.sep+f)
]
base_class = (
WebService,
LocalService,
MdxService,
StardictService
)
2018-07-01 10:55:30 +08:00
for f in files:
#try:
2018-07-14 21:21:31 +08:00
module = importlib.import_module(
u'.%s.%s' % (service_path, os.path.splitext(f)[0]),
__package__
)
2018-08-12 11:26:57 +08:00
for name, clazz in inspect.getmembers(module, predicate=inspect.isclass):
if clazz in base_class:
continue
2018-08-12 11:26:57 +08:00
service = service_wrap(clazz, *args)
service.__unique__ = name
2018-08-12 11:26:57 +08:00
if issubclass(clazz, WebService):
2018-07-08 18:00:58 +08:00
web_services.append(service)
# get the customized local services
2018-08-12 11:26:57 +08:00
if issubclass(clazz, LocalService):
2018-07-08 18:00:58 +08:00
local_custom_services.append(service)
2018-07-01 10:55:30 +08:00
return web_services, local_custom_services
def _get_available_local_services(self):
2018-07-06 21:12:41 +08:00
'''
available local dictionary services
'''
2018-08-12 11:26:57 +08:00
mdx_services = list()
star_dict_services = list()
2018-07-01 10:55:30 +08:00
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)
2018-07-06 22:37:00 +08:00
#MDX
if MdxService.check(dict_path):
service = service_wrap(MdxService, dict_path)
2018-07-14 17:07:47 +08:00
service.__unique__ = md5(dict_path).hexdigest()
2018-08-12 11:26:57 +08:00
mdx_services.append(service)
2018-07-06 22:37:00 +08:00
#Stardict
if StardictService.check(dict_path):
service = service_wrap(StardictService, dict_path)
2018-07-14 17:07:47 +08:00
service.__unique__ = md5(dict_path).hexdigest()
2018-08-12 11:26:57 +08:00
star_dict_services.append(service)
2018-07-01 10:55:30 +08:00
# support mdx dictionary and stardict format dictionary
2018-08-12 11:26:57 +08:00
return mdx_services, star_dict_services