Auto detect LDOCE6 path
This commit is contained in:
parent
44c6244154
commit
cbc89f4910
@ -21,7 +21,6 @@ from anki.hooks import addHook
|
||||
|
||||
############## other config here ##################
|
||||
shortcut = 'Ctrl+Q'
|
||||
LDOCE6_PATH = u'D:\\mdx_server\\mdx\\LDOCE6.mdx'
|
||||
###################################################
|
||||
|
||||
def start_here():
|
||||
@ -34,7 +33,6 @@ def start_here():
|
||||
import fastwq
|
||||
|
||||
fastwq.config.read()
|
||||
fastwq.config.LDOCE6_PATH = LDOCE6_PATH
|
||||
fastwq.my_shortcut = shortcut
|
||||
if not fastwq.have_setup:
|
||||
fastwq.have_setup = True
|
||||
|
||||
@ -37,7 +37,6 @@ class Config(object):
|
||||
"""
|
||||
|
||||
_CONFIG_FILENAME = u'fastwqcfg.json' #Config File Path
|
||||
LDOCE6_PATH = u''
|
||||
|
||||
def __init__(self, window):
|
||||
self.path = u'_' + self._CONFIG_FILENAME
|
||||
|
||||
@ -75,14 +75,22 @@ class OptionsDialog(Dialog):
|
||||
'local': [], #本地词典
|
||||
'web': [] #网络词典
|
||||
}
|
||||
for cls in service_manager.local_services:
|
||||
service = service_pool.get(cls.__unique__)
|
||||
for clazz in service_manager.local_services:
|
||||
service = service_pool.get(clazz.__unique__)
|
||||
if service and service.support:
|
||||
self.dict_services['local'].append(service)
|
||||
for cls in service_manager.web_services:
|
||||
service = service_pool.get(cls.__unique__)
|
||||
self.dict_services['local'].append({
|
||||
'title': service.title,
|
||||
'unique': service.unique
|
||||
})
|
||||
service_pool.put(service)
|
||||
for clazz in service_manager.web_services:
|
||||
service = service_pool.get(clazz.__unique__)
|
||||
if service and service.support:
|
||||
self.dict_services['web'].append(service)
|
||||
self.dict_services['web'].append({
|
||||
'title': service.title,
|
||||
'unique': service.unique
|
||||
})
|
||||
service_pool.put(service)
|
||||
# emit finished
|
||||
self._signal.emit('after_build')
|
||||
|
||||
@ -474,7 +482,7 @@ class TabContent(QWidget):
|
||||
|
||||
# local dict service
|
||||
for service in services['local']:
|
||||
dict_combo.addItem(service.title, userData=service.unique)
|
||||
dict_combo.addItem(service['title'], userData=service['unique'])
|
||||
|
||||
# hr
|
||||
if len(services['local']) > 0:
|
||||
@ -482,7 +490,7 @@ class TabContent(QWidget):
|
||||
|
||||
# web dict service
|
||||
for service in services['web']:
|
||||
dict_combo.addItem(service.title, userData=service.unique)
|
||||
dict_combo.addItem(service['title'], userData=service['unique'])
|
||||
|
||||
def set_dict_combo_index():
|
||||
#dict_combo.setCurrentIndex(-1)
|
||||
|
||||
@ -1,7 +1,6 @@
|
||||
#-*- coding:utf-8 -*-
|
||||
import re
|
||||
from ..base import *
|
||||
from ...context import config
|
||||
|
||||
VOICE_PATTERN = r'<a href="sound://([\w/]+\w*\.mp3)"><img src="img/spkr_%s.png"></a>'
|
||||
MAPPINGS = [
|
||||
@ -15,7 +14,16 @@ LANG_TO_REGEXPS = {lang: regexps for lang, regexps in MAPPINGS}
|
||||
class Ldoce6(MdxService):
|
||||
|
||||
def __init__(self):
|
||||
super(Ldoce6, self).__init__(config.LDOCE6_PATH)
|
||||
from ...service import service_manager, service_pool
|
||||
dict_path = ''
|
||||
for clazz in service_manager.mdx_services:
|
||||
service = service_pool.get(clazz.__unique__)
|
||||
title = service.builder._title if service and service.support else u''
|
||||
service_pool.put(service)
|
||||
if title.startswith(u'LDOCE6'):
|
||||
dict_path = service.dict_path
|
||||
break
|
||||
super(Ldoce6, self).__init__(dict_path)
|
||||
|
||||
@property
|
||||
def title(self):
|
||||
|
||||
@ -43,35 +43,21 @@ class ServiceManager(object):
|
||||
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.mdx_services, self.star_dict_services = self._get_available_local_services()
|
||||
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 = self.local_services + self.local_custom_services
|
||||
self.local_services = self.mdx_services + self.star_dict_services + self.local_custom_services
|
||||
|
||||
def get_service(self, unique):
|
||||
# webservice unique: class name
|
||||
# mdxservice unique: dict filepath
|
||||
# mdxservice unique: md5 of dict filepath
|
||||
for each in self.services:
|
||||
if each.__unique__ == unique:
|
||||
#cls = getattr(each,"__class__")
|
||||
service = each()
|
||||
service.unique = unique
|
||||
return service
|
||||
|
||||
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
|
||||
@ -80,41 +66,42 @@ class ServiceManager(object):
|
||||
service_path = u'dict'
|
||||
web_services, local_custom_services = list(), list()
|
||||
mypath = os.path.join(os.path.dirname(os.path.realpath(__file__)), service_path)
|
||||
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)
|
||||
|
||||
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
|
||||
)
|
||||
for f in files:
|
||||
#try:
|
||||
module = importlib.import_module(
|
||||
u'.%s.%s' % (service_path, os.path.splitext(f)[0]),
|
||||
__package__
|
||||
)
|
||||
for name, cls in inspect.getmembers(module, predicate=inspect.isclass):
|
||||
if cls in base_class:
|
||||
for name, clazz in inspect.getmembers(module, predicate=inspect.isclass):
|
||||
if clazz in base_class:
|
||||
continue
|
||||
#try:
|
||||
#service = cls(*args)
|
||||
service = service_wrap(cls, *args)
|
||||
service = service_wrap(clazz, *args)
|
||||
service.__unique__ = name
|
||||
if issubclass(cls, WebService):
|
||||
if issubclass(clazz, WebService):
|
||||
web_services.append(service)
|
||||
# get the customized local services
|
||||
if issubclass(cls, LocalService):
|
||||
if issubclass(clazz, LocalService):
|
||||
local_custom_services.append(service)
|
||||
#except Exception:
|
||||
# exclude the local service whose path has error.
|
||||
# pass
|
||||
#except ImportError:
|
||||
# continue
|
||||
return web_services, local_custom_services
|
||||
|
||||
def _get_available_local_services(self):
|
||||
'''
|
||||
available local dictionary services
|
||||
'''
|
||||
local_services = list()
|
||||
mdx_services = list()
|
||||
star_dict_services = list()
|
||||
for each in config.dirs:
|
||||
for dirpath, dirnames, filenames in os.walk(each):
|
||||
for filename in filenames:
|
||||
@ -124,11 +111,11 @@ class ServiceManager(object):
|
||||
if MdxService.check(dict_path):
|
||||
service = service_wrap(MdxService, dict_path)
|
||||
service.__unique__ = md5(dict_path).hexdigest()
|
||||
local_services.append(service)
|
||||
mdx_services.append(service)
|
||||
#Stardict
|
||||
if StardictService.check(dict_path):
|
||||
service = service_wrap(StardictService, dict_path)
|
||||
service.__unique__ = md5(dict_path).hexdigest()
|
||||
local_services.append(service)
|
||||
star_dict_services.append(service)
|
||||
# support mdx dictionary and stardict format dictionary
|
||||
return local_services
|
||||
return mdx_services, star_dict_services
|
||||
|
||||
@ -18,19 +18,17 @@
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
from anki.hooks import addHook
|
||||
from . import common as fastwq
|
||||
from .context import config
|
||||
|
||||
|
||||
############## other config here ##################
|
||||
shortcut = 'Ctrl+Q'
|
||||
LDOCE6_PATH = u'D:\\mdx_server\\mdx\\LDOCE6.mdx'
|
||||
###################################################
|
||||
|
||||
|
||||
def start_here():
|
||||
from . import common as fastwq
|
||||
from .context import config
|
||||
config.read()
|
||||
config.LDOCE6_PATH = LDOCE6_PATH
|
||||
fastwq.my_shortcut = shortcut
|
||||
if not fastwq.have_setup:
|
||||
fastwq.have_setup = True
|
||||
|
||||
@ -37,7 +37,6 @@ class Config(object):
|
||||
"""
|
||||
|
||||
_CONFIG_FILENAME = 'fastwqcfg.json' #Config File Path
|
||||
LDOCE6_PATH = ''
|
||||
|
||||
def __init__(self, window):
|
||||
self.path = u'_' + self._CONFIG_FILENAME
|
||||
|
||||
@ -75,14 +75,22 @@ class OptionsDialog(Dialog):
|
||||
'local': [], #本地词典
|
||||
'web': [] #网络词典
|
||||
}
|
||||
for cls in service_manager.local_services:
|
||||
service = service_pool.get(cls.__unique__)
|
||||
for clazz in service_manager.local_services:
|
||||
service = service_pool.get(clazz.__unique__)
|
||||
if service and service.support:
|
||||
self.dict_services['local'].append(service)
|
||||
for cls in service_manager.web_services:
|
||||
service = service_pool.get(cls.__unique__)
|
||||
self.dict_services['local'].append({
|
||||
'title': service.title,
|
||||
'unique': service.unique
|
||||
})
|
||||
service_pool.put(service)
|
||||
for clazz in service_manager.web_services:
|
||||
service = service_pool.get(clazz.__unique__)
|
||||
if service and service.support:
|
||||
self.dict_services['web'].append(service)
|
||||
self.dict_services['web'].append({
|
||||
'title': service.title,
|
||||
'unique': service.unique
|
||||
})
|
||||
service_pool.put(service)
|
||||
# emit finished
|
||||
self._signal.emit('after_build')
|
||||
|
||||
@ -474,7 +482,7 @@ class TabContent(QWidget):
|
||||
|
||||
# local dict service
|
||||
for service in services['local']:
|
||||
dict_combo.addItem(service.title, userData=service.unique)
|
||||
dict_combo.addItem(service['title'], userData=service['unique'])
|
||||
|
||||
# hr
|
||||
if len(services['local']) > 0:
|
||||
@ -482,7 +490,7 @@ class TabContent(QWidget):
|
||||
|
||||
# web dict service
|
||||
for service in services['web']:
|
||||
dict_combo.addItem(service.title, userData=service.unique)
|
||||
dict_combo.addItem(service['title'], userData=service['unique'])
|
||||
|
||||
def set_dict_combo_index():
|
||||
#dict_combo.setCurrentIndex(-1)
|
||||
|
||||
@ -1,7 +1,6 @@
|
||||
#-*- coding:utf-8 -*-
|
||||
import re
|
||||
from ..base import *
|
||||
from ...context import config
|
||||
|
||||
|
||||
VOICE_PATTERN = r'<a href="sound://([\w/]+\w*\.mp3)"><img src="img/spkr_%s.png"></a>'
|
||||
@ -16,7 +15,16 @@ LANG_TO_REGEXPS = {lang: regexps for lang, regexps in MAPPINGS}
|
||||
class Ldoce6(MdxService):
|
||||
|
||||
def __init__(self):
|
||||
super(Ldoce6, self).__init__(config.LDOCE6_PATH)
|
||||
from ...service import service_manager, service_pool
|
||||
dict_path = ''
|
||||
for clazz in service_manager.mdx_services:
|
||||
service = service_pool.get(clazz.__unique__)
|
||||
title = service.builder._title if service and service.support else u''
|
||||
service_pool.put(service)
|
||||
if title.startswith(u'LDOCE6'):
|
||||
dict_path = service.dict_path
|
||||
break
|
||||
super(Ldoce6, self).__init__(dict_path)
|
||||
|
||||
@property
|
||||
def title(self):
|
||||
|
||||
@ -17,7 +17,6 @@
|
||||
# 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 sys
|
||||
import inspect
|
||||
import os
|
||||
from hashlib import md5
|
||||
@ -39,35 +38,21 @@ class ServiceManager(object):
|
||||
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.mdx_services, self.star_dict_services = self._get_available_local_services()
|
||||
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 = self.local_services + self.local_custom_services
|
||||
self.local_services = self.mdx_services + self.star_dict_services + self.local_custom_services
|
||||
|
||||
def get_service(self, unique):
|
||||
# webservice unique: class name
|
||||
# mdxservice unique: dict filepath
|
||||
# mdxservice unique: md5 of dict filepath
|
||||
for each in self.services:
|
||||
if each.__unique__ == unique:
|
||||
#cls = getattr(each,"__class__")
|
||||
service = each()
|
||||
service.unique = unique
|
||||
return service
|
||||
|
||||
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
|
||||
@ -76,41 +61,42 @@ class ServiceManager(object):
|
||||
service_path = u'dict'
|
||||
web_services, local_custom_services = list(), list()
|
||||
mypath = os.path.join(os.path.dirname(os.path.realpath(__file__)), service_path)
|
||||
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)
|
||||
|
||||
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
|
||||
)
|
||||
for f in files:
|
||||
#try:
|
||||
module = importlib.import_module(
|
||||
u'.%s.%s' % (service_path, os.path.splitext(f)[0]),
|
||||
__package__
|
||||
)
|
||||
for name, cls in inspect.getmembers(module, predicate=inspect.isclass):
|
||||
if cls in base_class:
|
||||
for name, clazz in inspect.getmembers(module, predicate=inspect.isclass):
|
||||
if clazz in base_class:
|
||||
continue
|
||||
#try:
|
||||
#service = cls(*args)
|
||||
service = service_wrap(cls, *args)
|
||||
service = service_wrap(clazz, *args)
|
||||
service.__unique__ = name
|
||||
if issubclass(cls, WebService):
|
||||
if issubclass(clazz, WebService):
|
||||
web_services.append(service)
|
||||
# get the customized local services
|
||||
if issubclass(cls, LocalService):
|
||||
if issubclass(clazz, LocalService):
|
||||
local_custom_services.append(service)
|
||||
#except Exception:
|
||||
# exclude the local service whose path has error.
|
||||
# pass
|
||||
#except ImportError:
|
||||
# continue
|
||||
return web_services, local_custom_services
|
||||
|
||||
def _get_available_local_services(self):
|
||||
'''
|
||||
available local dictionary services
|
||||
'''
|
||||
local_services = list()
|
||||
mdx_services = list()
|
||||
star_dict_services = list()
|
||||
for each in config.dirs:
|
||||
for dirpath, dirnames, filenames in os.walk(each):
|
||||
for filename in filenames:
|
||||
@ -120,11 +106,11 @@ class ServiceManager(object):
|
||||
if MdxService.check(dict_path):
|
||||
service = service_wrap(MdxService, dict_path)
|
||||
service.__unique__ = md5(str(dict_path).encode('utf-8')).hexdigest()
|
||||
local_services.append(service)
|
||||
mdx_services.append(service)
|
||||
#Stardict
|
||||
if StardictService.check(dict_path):
|
||||
service = service_wrap(StardictService, dict_path)
|
||||
service.__unique__ = md5(str(dict_path).encode('utf-8')).hexdigest()
|
||||
local_services.append(service)
|
||||
star_dict_services.append(service)
|
||||
# support mdx dictionary and stardict format dictionary
|
||||
return local_services
|
||||
return mdx_services, star_dict_services
|
||||
|
||||
Loading…
Reference in New Issue
Block a user