示例#1
0
def indexShouldBeMade(dictionary):
    """Check if index exists and is up to date.

    Return True if dictionary must be indexed.
    """

    filePath = dictionary.getPath()
    fileName = os.path.basename(filePath)

    dictLocalHome = os.path.join(info.LOCAL_HOME,
                                 info.PLAIN_DICT_DIR,
                                 fileName)

    dictGlobalHome = os.path.join(info.GLOBAL_HOME,
                                  info.PLAIN_DICT_DIR,
                                  fileName)

    if not os.path.exists(os.path.join(dictGlobalHome, 'data', 'index.xml')) \
           and not os.path.exists(os.path.join(dictLocalHome, 'data',
                                               'index.xml')):
        return True

    debugLog(INFO, "Old checksum: %s" % dictionary.getChecksum())
    newChecksum = util.getMD5Sum(filePath)
    debugLog(INFO, "New checksum: %s" % newChecksum)

    return dictionary.getChecksum() != newChecksum
示例#2
0
def indexShouldBeMade(dictionary):
    """Check if index exists and is up to date.

    Return True if dictionary must be indexed.
    """

    filePath = dictionary.getPath()
    fileName = os.path.basename(filePath)

    dictLocalHome = os.path.join(info.LOCAL_HOME, info.PLAIN_DICT_DIR,
                                 fileName)

    dictGlobalHome = os.path.join(info.GLOBAL_HOME, info.PLAIN_DICT_DIR,
                                  fileName)

    if not os.path.exists(os.path.join(dictGlobalHome, 'data', 'index.xml')) \
           and not os.path.exists(os.path.join(dictLocalHome, 'data',
                                               'index.xml')):
        return True

    debugLog(INFO, "Old checksum: %s" % dictionary.getChecksum())
    newChecksum = util.getMD5Sum(filePath)
    debugLog(INFO, "New checksum: %s" % newChecksum)

    return dictionary.getChecksum() != newChecksum
示例#3
0
   def stop(self):
      """Close file handle"""

      try:
         debugLog(DEBUG, "Closing file %s" % self.filePath)
         self.fd.close()
      except:
         pass
示例#4
0
文件: parser.py 项目: idiles/opendict
   def stop(self):
      """Close file handle"""

      try:
         debugLog(DEBUG, "Closing file %s" % self.filePath)
         self.fd.close()
      except:
         pass
示例#5
0
   def search(self, word):
      """Lookup word"""

      _start = time.time()

      result = meta.SearchResult()

      word_lowered = word.lower()
      
      if self.definitions is None:
         self.definitions = self.dict.getdeflist()
         self.definitions.sort()

      words = []

      for definition in self.definitions:
         if definition.lower().startswith(word_lowered):
            words.append(definition)

      html = []

      html.append("<html><head>")
      html.append("<meta http-equiv=\"Content-Type\" " \
                  "content=\"text/html; charset=%s\">" \
                  % str(self.getEncoding()))
      html.append("<head><body>")

      (orig, translation) = self._getTranslation(word)

      if not translation:
         if len(words):
            debugLog(DEBUG, "Retrying search...")
            _word = words[0]
            orig, translation = self._getTranslation(_word)
            if not translation:
               result.setError(errortype.NOT_FOUND)
         else:
            result.setError(errortype.NOT_FOUND)
            translation = ""

      html.append("<table width=\"100%\"><tr>")
      html.append("<td bgcolor=\"%s\">" % WORD_BG)
      html.append("<b>%s</b></td></tr>" % orig)
      html.append("<tr><td>")
      html.append("<p>%s</p>" % translation)
      html.append("</td></tr></table>")
      html.append("</body></html>")

      result.setTranslation("".join(html))
      result.setWordList(words)

      debugLog(DEBUG, "DictParser: Search took % f seconds" \
            % (time.time() - _start))

      return result
示例#6
0
文件: parser.py 项目: idiles/opendict
   def search(self, word):
      """Lookup word"""

      _start = time.time()

      result = meta.SearchResult()

      word_lowered = word.lower()
      
      if self.definitions is None:
         self.definitions = self.dict.getdeflist()
         self.definitions.sort()

      words = []

      for definition in self.definitions:
         if definition.lower().startswith(word_lowered):
            words.append(definition)

      html = []

      html.append("<html><head>")
      html.append("<meta http-equiv=\"Content-Type\" " \
                  "content=\"text/html; charset=%s\">" \
                  % str(self.getEncoding()))
      html.append("<head><body>")

      (orig, translation) = self._getTranslation(word)

      if not translation:
         if len(words):
            debugLog(DEBUG, "Retrying search...")
            _word = words[0]
            orig, translation = self._getTranslation(_word)
            if not translation:
               result.setError(errortype.NOT_FOUND)
         else:
            result.setError(errortype.NOT_FOUND)
            translation = ""

      html.append("<table width=\"100%\"><tr>")
      html.append("<td bgcolor=\"%s\">" % WORD_BG)
      html.append("<b>%s</b></td></tr>" % orig)
      html.append("<tr><td>")
      html.append("<p>%s</p>" % translation)
      html.append("</td></tr></table>")
      html.append("</body></html>")

      result.setTranslation("".join(html))
      result.setWordList(words)

      debugLog(DEBUG, "DictParser: Search took % f seconds" \
            % (time.time() - _start))

      return result
示例#7
0
   def start(self):
      """Open file handle"""

      debugLog(DEBUG, "Opening file %s" % self.filePath)
      self.fd = open(self.filePath)
示例#8
0
   def search(self, word):
      """Lookup word"""

      _start = time.time()

      word_lowered = word.lower()

      encodedIndex = {}
      for literal in self.index:
         encodedIndex[literal.encode(self.getEncoding())] = \
                      self.index.get(literal)

      #
      # Seek to the beginning of the block
      #
      position = 0L
      if word_lowered[:2] in encodedIndex.keys():
         position = encodedIndex[word_lowered[:2]]

      debugLog(DEBUG, "Index: %s->%d" % (word_lowered[:2], position))
      debugLog(DEBUG, "MovaParser: Seeking to %d" % position)
      self.fd.seek(position)

      html = []

      html.append("<html><head>")
      html.append("<meta http-equiv=\"Content-Type\" " \
                  "content=\"text/html; charset=%s\">" \
                  % str(self.getEncoding()))
      html.append("<head><body>")

      found = False
      words = []

      result = meta.SearchResult()

      # DEBUG
      _linesRead = 0

      for line in self.fd.xreadlines():
         _linesRead += 1
         line = line.strip()
         try:
            orig, trans = line.split("  ", 1)
         except:
            continue

         if line.lower().startswith(word_lowered):   
            
            if not orig.lower().startswith(word_lowered):
               break
            
            if orig.lower() == word_lowered and not found:
               found = True
               self._appendTranslation(html, orig, trans)               
               
            words.append(orig)
            if len(words) == 1:
               suggestedWord = orig
               suggestedTrans = trans
         elif len(words):
            break

      debugLog(DEBUG, "%d lines scanned" % _linesRead)
      
      if not found:
         if words:
            self._appendTranslation(html, suggestedWord, suggestedTrans)
         else:
            result.setError(errortype.NOT_FOUND)

      html.append("</font></body></html>")

      try:
         translation = "".join(html)
      except:
         result.setError(errortype.INVALID_ENCOFING)
         translation = ""
      
      result.setTranslation(translation)
      result.setWordList(words)

      debugLog(DEBUG, "MovaParser: Search took %f seconds" \
               % (time.time() - _start))

      return result
示例#9
0
   def search(self, word):
      """Lookup word"""

      _start = time.time()

      word_lowered = word.lower()

      encodedIndex = {}
      for literal in self.index:
         encodedIndex[literal.encode(self.getEncoding())] = \
                      self.index.get(literal)

      #
      # Seek to the beginning of the block
      #
      position = 0L
      if word_lowered[:2] in encodedIndex.keys():
         position = encodedIndex[word_lowered[:2]]

      debugLog(DEBUG, "Index: %s->%d" % (word_lowered[:2], position))
      debugLog(DEBUG, "SlowoParser: Seeking to %d" % position)
      
      self.fd.seek(position)

      html = []

      html.append("<html><head>")
      html.append("<meta http-equiv=\"Content-Type\" " \
                  "content=\"text/html; charset=%s\">" \
                  % str(self.getEncoding()))
      html.append("<head><body>")

      found = False
      words = []

      result = meta.SearchResult()

      # DEBUG
      _linesRead = 0

      for line in self.fd.xreadlines():
         _linesRead += 1
         line = line.strip()
         try:
            orig = ""
            end = ""
            try:
                orig, end = line.split('=', 1)
            except ValueError, e:
                systemLog(ERROR, '%s (line %s)' % (e, line))
            orig = orig.strip()
            chunks = end.split(';')

            translation = ["<ul>"]
            for chunk in chunks:
               comment = []
               trans = chunk.split('//')
               
               if len(trans) > 1:
                  comment = trans[1:]

               trans = trans[:1]
                  
               trans = "".join(trans).strip()
               comment = "".join(comment).strip()
               
               if len(trans) and len(comment) != 0:
                  translation.append("<li>%s (<i>%s</i>)</li>" \
                                     % (trans, comment))
               elif len(trans):
                  translation.append("<li>%s</li>" % trans)

            translation.append("</ul>")

            translation = "".join(translation)

         except:
示例#10
0
            
            if not orig.lower().startswith(word_lowered):
               break
            
            if orig.lower() == word_lowered and not found:
               found = True
               self._appendTranslation(html, orig, translation)               
               
            words.append(orig)
            if len(words) == 1:
               suggestedWord = orig
               suggestedTrans = translation
         elif len(words):
            break

      debugLog(DEBUG, "%d lines scanned" % _linesRead)
      
      if not found:
         if words:
            self._appendTranslation(html, suggestedWord, suggestedTrans)
         else:
            result.setError(errortype.NOT_FOUND)

      html.append("</font></body></html>")

      try:
         translation = "".join(html)
      except:
         result.setError(errortype.INVALID_ENCOFING)
         translation = ""
      
示例#11
0
文件: parser.py 项目: idiles/opendict
   def start(self):
      """Open file handle"""

      debugLog(DEBUG, "Opening file %s" % self.filePath)
      self.fd = open(self.filePath)
示例#12
0
文件: parser.py 项目: idiles/opendict
   def search(self, word):
      """Lookup word"""

      _start = time.time()

      word_lowered = word.lower()

      encodedIndex = {}
      for literal in self.index:
         encodedIndex[literal.encode(self.getEncoding())] = \
                      self.index.get(literal)

      #
      # Seek to the beginning of the block
      #
      position = 0L
      if word_lowered[:2] in encodedIndex.keys():
         position = encodedIndex[word_lowered[:2]]

      debugLog(DEBUG, "Index: %s->%d" % (word_lowered[:2], position))
      debugLog(DEBUG, "MovaParser: Seeking to %d" % position)
      self.fd.seek(position)

      html = []

      html.append("<html><head>")
      html.append("<meta http-equiv=\"Content-Type\" " \
                  "content=\"text/html; charset=%s\">" \
                  % str(self.getEncoding()))
      html.append("<head><body>")

      found = False
      words = []

      result = meta.SearchResult()

      # DEBUG
      _linesRead = 0

      for line in self.fd.xreadlines():
         _linesRead += 1
         line = line.strip()
         try:
            orig, trans = line.split("  ", 1)
         except:
            continue

         if line.lower().startswith(word_lowered):   
            
            if not orig.lower().startswith(word_lowered):
               break
            
            if orig.lower() == word_lowered and not found:
               found = True
               self._appendTranslation(html, orig, trans)               
               
            words.append(orig)
            if len(words) == 1:
               suggestedWord = orig
               suggestedTrans = trans
         elif len(words):
            break

      debugLog(DEBUG, "%d lines scanned" % _linesRead)
      
      if not found:
         if words:
            self._appendTranslation(html, suggestedWord, suggestedTrans)
         else:
            result.setError(errortype.NOT_FOUND)

      html.append("</font></body></html>")

      try:
         translation = "".join(html)
      except:
         result.setError(errortype.INVALID_ENCOFING)
         translation = ""
      
      result.setTranslation(translation)
      result.setWordList(words)

      debugLog(DEBUG, "MovaParser: Search took %f seconds" \
               % (time.time() - _start))

      return result
示例#13
0
文件: parser.py 项目: idiles/opendict
   def search(self, word):
      """Lookup word"""

      _start = time.time()

      word_lowered = word.lower()

      encodedIndex = {}
      for literal in self.index:
         encodedIndex[literal.encode(self.getEncoding())] = \
                      self.index.get(literal)

      #
      # Seek to the beginning of the block
      #
      position = 0L
      if word_lowered[:2] in encodedIndex.keys():
         position = encodedIndex[word_lowered[:2]]

      debugLog(DEBUG, "Index: %s->%d" % (word_lowered[:2], position))
      debugLog(DEBUG, "SlowoParser: Seeking to %d" % position)
      
      self.fd.seek(position)

      html = []

      html.append("<html><head>")
      html.append("<meta http-equiv=\"Content-Type\" " \
                  "content=\"text/html; charset=%s\">" \
                  % str(self.getEncoding()))
      html.append("<head><body>")

      found = False
      words = []

      result = meta.SearchResult()

      # DEBUG
      _linesRead = 0

      for line in self.fd.xreadlines():
         _linesRead += 1
         line = line.strip()
         try:
            try:
                orig, end = line.split('=', 1)
            except ValueError, e:
                systemLog(ERROR, '%s (line %s)' % (e, line))
            orig = orig.strip()
            chunks = end.split(';')

            translation = ["<ul>"]
            for chunk in chunks:
               comment = []
               trans = chunk.split('//')
               
               if len(trans) > 1:
                  comment = trans[1:]

               trans = trans[:1]
                  
               trans = "".join(trans).strip()
               comment = "".join(comment).strip()
               
               if len(trans) and len(comment) != 0:
                  translation.append("<li>%s (<i>%s</i>)</li>" \
                                     % (trans, comment))
               elif len(trans):
                  translation.append("<li>%s</li>" % trans)

            translation.append("</ul>")

            translation = "".join(translation)

         except:
示例#14
0
文件: parser.py 项目: idiles/opendict
            
            if not orig.lower().startswith(word_lowered):
               break
            
            if orig.lower() == word_lowered and not found:
               found = True
               self._appendTranslation(html, orig, translation)               
               
            words.append(orig)
            if len(words) == 1:
               suggestedWord = orig
               suggestedTrans = translation
         elif len(words):
            break

      debugLog(DEBUG, "%d lines scanned" % _linesRead)
      
      if not found:
         if words:
            self._appendTranslation(html, suggestedWord, suggestedTrans)
         else:
            result.setError(errortype.NOT_FOUND)

      html.append("</font></body></html>")

      try:
         translation = "".join(html)
      except:
         result.setError(errortype.INVALID_ENCOFING)
         translation = ""
      
示例#15
0
    def search(self, word):
        """Lookup word"""

        _start = time.time()

        word_lowered = word.lower()

        encodedIndex = {}
        for literal in self.index:
            encodedIndex[literal.encode(self.getEncoding())] = \
                         self.index.get(literal)

        #
        # Seek to the beginning of the block
        #
        position = 0
        if word_lowered[:2] in list(encodedIndex.keys()):
            position = encodedIndex[word_lowered[:2]]

        debugLog(DEBUG, "Index: %s->%d" % (word_lowered[:2], position))
        debugLog(DEBUG, "SlowoParser: Seeking to %d" % position)

        self.fd.seek(position)

        html = []

        html.append("<html><head>")
        html.append("<meta http-equiv=\"Content-Type\" " \
                    "content=\"text/html; charset=%s\">" \
                    % str(self.getEncoding()))
        html.append("<head><body>")

        found = False
        words = []

        result = meta.SearchResult()

        # DEBUG
        _linesRead = 0

        for line in self.fd:
            _linesRead += 1
            line = line.strip()
            try:
                orig = ""
                end = ""
                try:
                    orig, end = line.split('=', 1)
                except ValueError as e:
                    systemLog(ERROR, '%s (line %s)' % (e, line))
                orig = orig.strip()
                chunks = end.split(';')

                translation = ["<ul>"]
                for chunk in chunks:
                    comment = []
                    trans = chunk.split('//')

                    if len(trans) > 1:
                        comment = trans[1:]

                    trans = trans[:1]

                    trans = "".join(trans).strip()
                    comment = "".join(comment).strip()

                    if len(trans) and len(comment) != 0:
                        translation.append("<li>%s (<i>%s</i>)</li>" \
                                           % (trans, comment))
                    elif len(trans):
                        translation.append("<li>%s</li>" % trans)

                translation.append("</ul>")

                translation = "".join(translation)

            except:
                traceback.print_exc()
                continue

            if line.lower().startswith(word_lowered):

                if not orig.lower().startswith(word_lowered):
                    break

                if orig.lower() == word_lowered and not found:
                    found = True
                    self._appendTranslation(html, orig, translation)

                words.append(orig)
                if len(words) == 1:
                    suggestedWord = orig
                    suggestedTrans = translation
            elif len(words):
                break

        debugLog(DEBUG, "%d lines scanned" % _linesRead)

        if not found:
            if words:
                self._appendTranslation(html, suggestedWord, suggestedTrans)
            else:
                result.setError(errortype.NOT_FOUND)

        html.append("</font></body></html>")

        try:
            translation = "".join(html)
        except:
            result.setError(errortype.INVALID_ENCOFING)
            translation = ""

        result.setTranslation(translation)
        result.setWordList(words)

        debugLog(DEBUG, "SlowoParser: search took %f seconds" \
              % (time.time() - _start))

        return result