fix #92
This commit is contained in:
parent
ab9593074b
commit
b07d343dc3
@ -17,29 +17,26 @@
|
|||||||
# You should have received a copy of the GNU General Public License
|
# You should have received a copy of the GNU General Public License
|
||||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
import io
|
||||||
import os
|
import os
|
||||||
import re
|
import re
|
||||||
import io
|
|
||||||
import shutil
|
import shutil
|
||||||
import unicodedata
|
import unicodedata
|
||||||
|
|
||||||
from collections import defaultdict
|
from collections import defaultdict
|
||||||
|
|
||||||
from aqt.qt import *
|
from aqt.qt import *
|
||||||
from aqt.utils import showInfo
|
from aqt.utils import showInfo
|
||||||
|
|
||||||
from ..constants import Template
|
from ..constants import Template
|
||||||
from ..context import config
|
from ..context import config
|
||||||
from ..service import service_pool, QueryResult, copy_static_file
|
from ..libs.snowballstemmer import stemmer
|
||||||
|
from ..service import QueryResult, copy_static_file, service_pool
|
||||||
from ..service.base import LocalService
|
from ..service.base import LocalService
|
||||||
from ..utils import wrap_css
|
from ..utils import wrap_css
|
||||||
from ..libs.snowballstemmer import stemmer
|
|
||||||
|
|
||||||
|
|
||||||
__all__ = [
|
__all__ = [
|
||||||
'InvalidWordException', 'update_note_fields',
|
'InvalidWordException', 'update_note_fields', 'update_note_field',
|
||||||
'update_note_field', 'promot_choose_css', 'add_to_tmpl',
|
'promot_choose_css', 'add_to_tmpl', 'query_flds', 'inspect_note'
|
||||||
'query_flds', 'inspect_note'
|
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
@ -127,19 +124,14 @@ def promot_choose_css(missed_css):
|
|||||||
if not os.path.exists(filename) and not css['file'] in checked:
|
if not os.path.exists(filename) and not css['file'] in checked:
|
||||||
checked.add(css['file'])
|
checked.add(css['file'])
|
||||||
showInfo(
|
showInfo(
|
||||||
Template.miss_css.format(
|
Template.miss_css.format(dict=css['title'], css=css['file']))
|
||||||
dict=css['title'],
|
|
||||||
css=css['file']
|
|
||||||
)
|
|
||||||
)
|
|
||||||
try:
|
try:
|
||||||
filepath = css['dict_path'][:css['dict_path'].rindex(
|
filepath = css['dict_path'][:css['dict_path'].rindex(os.path.
|
||||||
os.path.sep)+1]
|
sep) + 1]
|
||||||
filepath = QFileDialog.getOpenFileName(
|
filepath = QFileDialog.getOpenFileName(
|
||||||
directory=filepath,
|
directory=filepath,
|
||||||
caption=u'Choose css file',
|
caption=u'Choose css file',
|
||||||
filter=u'CSS (*.css)'
|
filter=u'CSS (*.css)')
|
||||||
)
|
|
||||||
if filepath:
|
if filepath:
|
||||||
shutil.copy(filepath, filename)
|
shutil.copy(filepath, filename)
|
||||||
wrap_css(filename)
|
wrap_css(filename)
|
||||||
@ -161,8 +153,10 @@ def add_to_tmpl(note, **kwargs):
|
|||||||
if js and js.strip():
|
if js and js.strip():
|
||||||
addings = js.strip()
|
addings = js.strip()
|
||||||
if addings not in afmt:
|
if addings not in afmt:
|
||||||
if not addings.startswith(u'<script') and not addings.endswith(u'/script>'):
|
if not addings.startswith(u'<script') and not addings.endswith(
|
||||||
addings = u'\n<script type="text/javascript">\n{}\n</script>'.format(addings)
|
u'/script>'):
|
||||||
|
addings = u'\n<script type="text/javascript">\n{}\n</script>'.format(
|
||||||
|
addings)
|
||||||
afmt += addings
|
afmt += addings
|
||||||
if jsfile:
|
if jsfile:
|
||||||
# new_jsfile = u'_' + \
|
# new_jsfile = u'_' + \
|
||||||
@ -237,7 +231,7 @@ def query_flds(note, fileds=None):
|
|||||||
success_num = 0
|
success_num = 0
|
||||||
result = defaultdict(QueryResult)
|
result = defaultdict(QueryResult)
|
||||||
for task in tasks:
|
for task in tasks:
|
||||||
#try:
|
try:
|
||||||
service = services.get(task['k'], None)
|
service = services.get(task['k'], None)
|
||||||
qr = service.active(task['f'], task['w'])
|
qr = service.active(task['f'], task['w'])
|
||||||
if qr:
|
if qr:
|
||||||
@ -245,9 +239,9 @@ def query_flds(note, fileds=None):
|
|||||||
qr['result'] = cloze_deletion(qr['result'], word)
|
qr['result'] = cloze_deletion(qr['result'], word)
|
||||||
result.update({task['i']: qr})
|
result.update({task['i']: qr})
|
||||||
success_num += 1
|
success_num += 1
|
||||||
#except:
|
except Exception as e:
|
||||||
# showInfo(_("NO_QUERY_WORD"))
|
print(_("NO_QUERY_WORD"), e)
|
||||||
# pass
|
pass
|
||||||
|
|
||||||
missed_css = list()
|
missed_css = list()
|
||||||
for service in services.values():
|
for service in services.values():
|
||||||
@ -284,13 +278,15 @@ def cloze_deletion(text, cloze):
|
|||||||
continue
|
continue
|
||||||
word = text[s:e]
|
word = text[s:e]
|
||||||
if _stemmer.stemWord(word).lower() == term:
|
if _stemmer.stemWord(word).lower() == term:
|
||||||
l = len(cloze)
|
ln = len(cloze)
|
||||||
w = word
|
w = word
|
||||||
if w[:l].lower() == cloze.lower():
|
if w[:ln].lower() == cloze.lower():
|
||||||
e = s + l
|
e = s + ln
|
||||||
w = word[:l]
|
w = word[:ln]
|
||||||
result = result[:s+offset] + (config.cloze_str % w) + result[e+offset:]
|
result = result[:s + offset] + (
|
||||||
|
config.cloze_str % w) + result[e + offset:]
|
||||||
offset += len(config.cloze_str) - 2
|
offset += len(config.cloze_str) - 2
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
_stemmer = stemmer('english')
|
_stemmer = stemmer('english')
|
||||||
|
|||||||
@ -19,6 +19,7 @@
|
|||||||
|
|
||||||
import inspect
|
import inspect
|
||||||
import os
|
import os
|
||||||
|
import random
|
||||||
# use ntpath module to ensure the windows-style (e.g. '\\LDOCE.css')
|
# use ntpath module to ensure the windows-style (e.g. '\\LDOCE.css')
|
||||||
# path can be processed on Unix platform.
|
# path can be processed on Unix platform.
|
||||||
# However, anki version on mac platforms doesn't including this package?
|
# However, anki version on mac platforms doesn't including this package?
|
||||||
@ -27,32 +28,33 @@ import re
|
|||||||
import shutil
|
import shutil
|
||||||
import sqlite3
|
import sqlite3
|
||||||
import urllib
|
import urllib
|
||||||
|
import zlib
|
||||||
|
from collections import defaultdict
|
||||||
|
from functools import wraps
|
||||||
|
from hashlib import md5, sha1
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
|
from bs4 import BeautifulSoup
|
||||||
|
|
||||||
|
from aqt import mw
|
||||||
|
from aqt.qt import QMutex, QThread
|
||||||
|
|
||||||
|
from ..context import config
|
||||||
|
from ..lang import _cl
|
||||||
|
from ..libs import MdxBuilder, StardictBuilder
|
||||||
|
from ..utils import MapDict, wrap_css
|
||||||
|
|
||||||
try:
|
try:
|
||||||
import urllib2
|
import urllib2
|
||||||
except:
|
except Exception:
|
||||||
import urllib.request as urllib2
|
import urllib.request as urllib2
|
||||||
|
|
||||||
import zlib
|
|
||||||
import random
|
|
||||||
from collections import defaultdict
|
|
||||||
from functools import wraps
|
|
||||||
from hashlib import md5
|
|
||||||
from hashlib import sha1
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
from cookielib import CookieJar
|
from cookielib import CookieJar
|
||||||
except:
|
except Exception:
|
||||||
from http.cookiejar import CookieJar
|
from http.cookiejar import CookieJar
|
||||||
|
|
||||||
from aqt import mw
|
|
||||||
from aqt.qt import QThread, QMutex
|
|
||||||
from bs4 import BeautifulSoup
|
|
||||||
from ..context import config
|
|
||||||
from ..libs import MdxBuilder, StardictBuilder
|
|
||||||
from ..utils import MapDict, wrap_css
|
|
||||||
from ..lang import _cl
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
import threading as _threading
|
import threading as _threading
|
||||||
@ -69,18 +71,14 @@ __all__ = [
|
|||||||
def get_hex_name(prefix, val, suffix):
|
def get_hex_name(prefix, val, suffix):
|
||||||
''' get sha1 hax name '''
|
''' get sha1 hax name '''
|
||||||
hex_digest = sha1(val.encode('utf-8')).hexdigest().lower()
|
hex_digest = sha1(val.encode('utf-8')).hexdigest().lower()
|
||||||
name = '.'.join([
|
name = '.'.join(['-'.join([prefix, hex_digest[:8], hex_digest[8:16], hex_digest[16:24], hex_digest[24:32], hex_digest[32:], ]), suffix, ])
|
||||||
'-'.join([
|
|
||||||
prefix, hex_digest[:8], hex_digest[8:16],
|
|
||||||
hex_digest[16:24], hex_digest[24:32], hex_digest[32:],
|
|
||||||
]),
|
|
||||||
suffix,
|
|
||||||
])
|
|
||||||
return name
|
return name
|
||||||
|
|
||||||
|
|
||||||
def _is_method_or_func(object):
|
def _is_method_or_func(object):
|
||||||
return inspect.isfunction(object) or inspect.ismethod(object)
|
return inspect.isfunction(object) or inspect.ismethod(object)
|
||||||
|
|
||||||
|
|
||||||
def register(labels):
|
def register(labels):
|
||||||
"""
|
"""
|
||||||
register the dict service with a labels, which will be shown in the dicts list.
|
register the dict service with a labels, which will be shown in the dicts list.
|
||||||
@ -120,6 +118,8 @@ def export(labels):
|
|||||||
export.EXPORT_INDEX += 1
|
export.EXPORT_INDEX += 1
|
||||||
return _deco
|
return _deco
|
||||||
return _with
|
return _with
|
||||||
|
|
||||||
|
|
||||||
export.EXPORT_INDEX = 0
|
export.EXPORT_INDEX = 0
|
||||||
|
|
||||||
|
|
||||||
@ -181,8 +181,9 @@ def with_styles(**styles):
|
|||||||
return _deco
|
return _deco
|
||||||
return _with
|
return _with
|
||||||
|
|
||||||
# bs4 threading lock, overload protection
|
|
||||||
_BS_LOCKS = [_threading.Lock(), _threading.Lock()]
|
_BS_LOCKS = [_threading.Lock(), _threading.Lock()] # bs4 threading lock, overload protection
|
||||||
|
|
||||||
|
|
||||||
def parse_html(html):
|
def parse_html(html):
|
||||||
'''
|
'''
|
||||||
@ -238,6 +239,7 @@ class Service(object):
|
|||||||
@property
|
@property
|
||||||
def unique(self):
|
def unique(self):
|
||||||
return self._unique
|
return self._unique
|
||||||
|
|
||||||
@unique.setter
|
@unique.setter
|
||||||
def unique(self, value):
|
def unique(self, value):
|
||||||
self._unique = value
|
self._unique = value
|
||||||
@ -326,7 +328,7 @@ class WebService(Service):
|
|||||||
if response.info().get('Content-Encoding') == 'gzip':
|
if response.info().get('Content-Encoding') == 'gzip':
|
||||||
data = zlib.decompress(data, 16 + zlib.MAX_WBITS)
|
data = zlib.decompress(data, 16 + zlib.MAX_WBITS)
|
||||||
return data
|
return data
|
||||||
except:
|
except Exception:
|
||||||
return u''
|
return u''
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
@ -340,7 +342,7 @@ class WebService(Service):
|
|||||||
'(KHTML, like Gecko) Chrome/31.0.1623.0 Safari/537.36'
|
'(KHTML, like Gecko) Chrome/31.0.1623.0 Safari/537.36'
|
||||||
}).content)
|
}).content)
|
||||||
return True
|
return True
|
||||||
except Exception as e:
|
except Exception:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
class TinyDownloadError(ValueError):
|
class TinyDownloadError(ValueError):
|
||||||
@ -434,7 +436,7 @@ class WebService(Service):
|
|||||||
try:
|
try:
|
||||||
value_error.payload = response.read()
|
value_error.payload = response.read()
|
||||||
response.close()
|
response.close()
|
||||||
except:
|
except Exception:
|
||||||
pass
|
pass
|
||||||
raise value_error
|
raise value_error
|
||||||
|
|
||||||
@ -475,7 +477,7 @@ class WebService(Service):
|
|||||||
f.write(payload)
|
f.write(payload)
|
||||||
f.close()
|
f.close()
|
||||||
return True
|
return True
|
||||||
except:
|
except Exception:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
@ -516,7 +518,7 @@ class LocalService(Service):
|
|||||||
def _get_builer(key, func=None):
|
def _get_builer(key, func=None):
|
||||||
LocalService._mutex_builder.lock()
|
LocalService._mutex_builder.lock()
|
||||||
key = md5(str(key).encode('utf-8')).hexdigest()
|
key = md5(str(key).encode('utf-8')).hexdigest()
|
||||||
if not func is None:
|
if not(func is None):
|
||||||
if not LocalService._mdx_builders[key]:
|
if not LocalService._mdx_builders[key]:
|
||||||
worker = _DictBuildWorker(func)
|
worker = _DictBuildWorker(func)
|
||||||
worker.start()
|
worker.start()
|
||||||
@ -582,10 +584,10 @@ class MdxService(LocalService):
|
|||||||
jsfile = re.findall(r'<script .*?src=[\'\"](.+?)[\'\"]', html, re.DOTALL)
|
jsfile = re.findall(r'<script .*?src=[\'\"](.+?)[\'\"]', html, re.DOTALL)
|
||||||
return QueryResult(result=html, js=u'\n'.join(js), jsfile=jsfile)
|
return QueryResult(result=html, js=u'\n'.join(js), jsfile=jsfile)
|
||||||
|
|
||||||
|
|
||||||
def _get_definition_mdx(self):
|
def _get_definition_mdx(self):
|
||||||
"""according to the word return mdx dictionary page"""
|
"""according to the word return mdx dictionary page"""
|
||||||
content = self.builder.mdx_lookup(self.word, ignorecase=config.ignore_mdx_wordcase)
|
ignorecase = config.ignore_mdx_wordcase and (self.word != self.word.lower() or self.word != self.word.upper())
|
||||||
|
content = self.builder.mdx_lookup(self.word, ignorecase=ignorecase)
|
||||||
str_content = ""
|
str_content = ""
|
||||||
if len(content) > 0:
|
if len(content) > 0:
|
||||||
for c in content:
|
for c in content:
|
||||||
@ -596,7 +598,8 @@ class MdxService(LocalService):
|
|||||||
def _get_definition_mdd(self, word):
|
def _get_definition_mdd(self, word):
|
||||||
"""according to the keyword(param word) return the media file contents"""
|
"""according to the keyword(param word) return the media file contents"""
|
||||||
word = word.replace('/', '\\')
|
word = word.replace('/', '\\')
|
||||||
content = self.builder.mdd_lookup(word, ignorecase=config.ignore_mdx_wordcase)
|
ignorecase = config.ignore_mdx_wordcase and (word != word.lower() or word != word.upper())
|
||||||
|
content = self.builder.mdd_lookup(word, ignorecase=ignorecase)
|
||||||
if len(content) > 0:
|
if len(content) > 0:
|
||||||
return [content[0]]
|
return [content[0]]
|
||||||
else:
|
else:
|
||||||
@ -620,7 +623,7 @@ class MdxService(LocalService):
|
|||||||
f.write(bytes_list[0])
|
f.write(bytes_list[0])
|
||||||
return savepath
|
return savepath
|
||||||
except sqlite3.OperationalError as e:
|
except sqlite3.OperationalError as e:
|
||||||
#showInfo(str(e))
|
print(e)
|
||||||
pass
|
pass
|
||||||
return ''
|
return ''
|
||||||
|
|
||||||
@ -705,12 +708,14 @@ class MdxService(LocalService):
|
|||||||
shutil.copy(src_fn, savepath)
|
shutil.copy(src_fn, savepath)
|
||||||
return savepath
|
return savepath
|
||||||
else:
|
else:
|
||||||
bytes_list = self.builder.mdd_lookup(filepath_in_mdx, ignorecase=config.ignore_mdx_wordcase)
|
ignorecase = config.ignore_mdx_wordcase and (filepath_in_mdx != filepath_in_mdx.lower() or filepath_in_mdx != filepath_in_mdx.upper())
|
||||||
|
bytes_list = self.builder.mdd_lookup(filepath_in_mdx, ignorecase=ignorecase)
|
||||||
if bytes_list:
|
if bytes_list:
|
||||||
with open(savepath, 'wb') as f:
|
with open(savepath, 'wb') as f:
|
||||||
f.write(bytes_list[0])
|
f.write(bytes_list[0])
|
||||||
return savepath
|
return savepath
|
||||||
except sqlite3.OperationalError as e:
|
except sqlite3.OperationalError as e:
|
||||||
|
print('save default file error', e)
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def save_media_files(self, data):
|
def save_media_files(self, data):
|
||||||
@ -788,7 +793,7 @@ class QueryResult(MapDict):
|
|||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
super(QueryResult, self).__init__(*args, **kwargs)
|
super(QueryResult, self).__init__(*args, **kwargs)
|
||||||
# avoid return None
|
# avoid return None
|
||||||
if self['result'] == None:
|
if self['result'] is None:
|
||||||
self['result'] = ""
|
self['result'] = ""
|
||||||
|
|
||||||
def set_styles(self, **kwargs):
|
def set_styles(self, **kwargs):
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user