Merge branch 'configdlg'
authorUrban Wallasch <urban.wallasch@freenet.de>
Tue, 15 Jun 2021 15:58:28 +0000 (17:58 +0200)
committerUrban Wallasch <urban.wallasch@freenet.de>
Tue, 15 Jun 2021 15:58:28 +0000 (17:58 +0200)
1  2 
jiten-pai.py

diff --cc jiten-pai.py
index a501e2a3967b8d1c96a36411bfcde2e40be76d4d,0c7a3f6b815677bc3eb9957eb5499619ad2abd73..753ba7a88162744815fea2367b029789a3c413f4
@@@ -436,17 -841,20 +841,26 @@@ class jpMainWindow(QMainWindow)
              elif self.engopt_word.isChecked():
                  term = '\W' + term + '\W'
          # result limiting
-         max_res = self.genopt_limit.value() if self.genopt_limit.isEnabled() else 0
+         limit = self.genopt_limit.value() if self.genopt_limit.isEnabled() else 0
          # perform lookup
          QApplication.processEvents()
-         result = dict_lookup(cfg['dict'], term, mode, max_res)
-         #
+         result = []
+         if self.genopt_dict.isChecked():
+             dic = self.genopt_dictsel.itemData(self.genopt_dictsel.currentIndex())
+             result = dict_lookup(dic, term, mode, limit)
+         else:
+             for d in cfg['dicts']:
+                 r = dict_lookup(d[1], term, mode, limit)
+                 result.extend(r)
+                 limit -= len(r)
+                 if limit == 0:
+                     limit = -1
 +        self.result_group.setTitle('Search results: %d' % len(result))
-         self.result_pane.setHtml('')
-         self.result_pane.setEnabled(True);
 +        # bail early on empty result
 +        if 0 == len(result):
++            self.result_pane.setHtml('')
++            self.result_pane.setEnabled(True);
 +            return
          # format result
          term = self.search_box.lineEdit().text()
          re_term = re.compile(kata2hira(term), re.IGNORECASE)
              html.append(' %s<br>\n' % res[2])
          html.append('</div>')
          self.result_pane.setHtml(''.join(html))
--        self.result_group.setTitle('Search results: %d' % len(result))
          self.result_pane.setEnabled(True)
  
-     def kbd_copy(self):
-         self.clipboard.setText(self.result_pane.textCursor().selectedText())
-     def kbd_paste(self):
-         self.search_box.lineEdit().setText(self.clipboard.text())
-         self.search_box.setFocus()
  
  ############################################################
  # dictionary lookup
  # 〆日 [しめび] /(n) time limit/closing day/settlement day (payment)/deadline/
  # ハート /(n) heart/(P)/
  
- def dict_lookup(dict_fname, pattern, mode, max_res=0):
+ def dict_lookup(dict_fname, pattern, mode, limit=0):
      result = []
      cnt = 0
-     with open(dict_fname) as dict_file:
-         try:
-             re_pattern = re.compile(pattern, re.IGNORECASE)
-         except:
-             return result
-         for line in dict_file:
-             if max_res and cnt >= max_res:
-                 break
+     try:
+         with open(dict_fname) as dict_file:
 -            re_pattern = re.compile(pattern, re.IGNORECASE)
 +            try:
-                 # manually splitting the line is actually faster than regex
-                 p1 = line.split('[', 1)
-                 if len(p1) < 2:
-                     p1 = line.split('/', 1)
-                     p2 = ['', p1[1]]
-                 else:
-                     p2 = p1[1].split(']', 1)
-                 term = p1[0].strip()
-                 hira = p2[0].strip()
-                 trans = ' ' + p2[1].lstrip('/ ').rstrip(' \t\r\n').replace('/', '; ')
++                re_pattern = re.compile(pattern, re.IGNORECASE)
 +            except:
-                 continue
-             if (mode == ScanMode.JAP and (re_pattern.search(kata2hira(term)) or re_pattern.search(hira))) \
-             or (mode == ScanMode.ENG and re_pattern.search(trans)):
-                 result.append([term, hira, trans])
-                 cnt += 1
++                return result
+             for line in dict_file:
+                 if limit and cnt >= limit:
+                     break
+                 try:
+                     # manually splitting the line is actually faster than regex
+                     p1 = line.split('[', 1)
+                     if len(p1) < 2:
+                         p1 = line.split('/', 1)
+                         p2 = ['', p1[1]]
+                     else:
+                         p2 = p1[1].split(']', 1)
+                     term = p1[0].strip()
+                     hira = p2[0].strip()
+                     trans = ' ' + p2[1].lstrip('/ ').rstrip(' \t\r\n').replace('/', '; ')
+                 except:
+                     continue
+                 if (mode == ScanMode.JAP and (re_pattern.search(kata2hira(term)) or re_pattern.search(hira))) \
+                 or (mode == ScanMode.ENG and re_pattern.search(trans)):
+                     result.append([term, hira, trans])
+                     cnt += 1
+     except Exception as e:
+         eprint(dict_fname, str(e))
      return result