Beispiel #1
0
    def saveTerms(self):
        if not self.scriptTimes:
            return
        if not self.saveMutex.tryLock():
            dwarn("retry later due to thread contention")
            self.rebuildCacheLater(queued=True)
            return

        saveTime = time()
        skthreads.runsync(partial(self._saveTerms, saveTime))
        self.saveMutex.unlock()
Beispiel #2
0
    def __init__(self, q, data, async):
        """
    @param  data  [dataman.Comment|dataman.Term]
    @param  async  bool
    """
        self._createUi(q)
        #canvas.setContentsMargins(0,0,0,0)
        #q.setContentsMargins(0,0,0,0)
        #skqss.class_(canvas, 'texture')

        if async:
            skthreads.runsync(partial(self._plot, data))
        else:
            self._plot(data)
Beispiel #3
0
  def _apply(self):
    sel = self.wizard().selection()
    man = self.wizard().manager()

    tasks = []
    if sel.readerChecked != man.isReaderActivated():
      tasks.append(partial(
        man.setReaderActivated, sel.readerChecked))
    if sel.playerChecked != man.isPlayerActivated():
      tasks.append(partial(
        man.setPlayerActivated, sel.playerChecked))
    if tasks:
      self.wizard().hide()
      skthreads.runsync(partial(
        map, apply, tasks))
      self.wizard().show()
Beispiel #4
0
 def appId(self):
   """
   @return  str or None
   """
   if not self._appId:
     self._appId = skthreads.runsync(bingauth.getappid)
   return self._appId
Beispiel #5
0
 def renderJapanese(self, text):
   """
   @param  text  unicode
   @return  unicode
   """
   ret = ""
   feature = GrimoireBean.instance.lookupFeature(text)
   if self._renderMutex.tryLock():
     ret = skthreads.runsync(partial(
         dictman.manager().renderJapanese, text, exact=True,feature=feature)) # do exact match for speed
     self._renderMutex.unlock()
   else:
     dwarn("ignore thread contention")
   return ret
Beispiel #6
0
 def updateAvatar(self, token):
     """
 @param  token  unicode
 @return  bool  whether succeeded
 """
     path = rc.avatar_image_path(token)
     if os.path.exists(path):
         return True
     if not self.__d.enabled:
         return False
     path_tmp = path + TMP_SUFFIX
     if os.path.exists(path_tmp):
         return False
     skfileio.touchfile(path_tmp)
     ok = skthreads.runsync(partial(_saveavatar, token, path_tmp))
     if ok and skfileio.rename(path_tmp, path):
         return True
     else:
         skfileio.removefile(path_tmp)
         return False
Beispiel #7
0
class MachineTranslator(Translator):

    splitsSentences = False  # bool

    #_CACHE_LENGTH = 10 # length of the translation to cache

    #_DELIM_SET = _PARAGRAPH_SET # set of deliminators
    #_DELIM_RE = _PARAGRAPH_RE   # rx of deliminators

    def __init__(self, abortSignal=None):
        super(MachineTranslator, self).__init__()
        self.cache = TranslationCache()  # public overall translation cache
        self._cache = TranslationCache(
        )  # private translation cache for internal translation
        self.abortSignal = abortSignal  # QtCore.Signal abort translation

    def clearCache(self):
        """@reimp"""
        self.cache.clear()
        self._cache.clear()

    def __cachedtr(self, text, async, tr, **kwargs):
        """
    @param  text  unicode
    @param  async  bool
    @param  tr  function(unicode text, str to, str fr)
    @return  unicode or None
    """
        #if len(text) > self._CACHE_LENGTH:
        #  return skthreads.runsync(partial(
        #       tr, text, **kwargs),
        #       abortSignal=self.abortSignal,
        #       parent=self.parent) if async else tr(text, **kwargs)
        return self._cache.get(text) or (self._cache.update(
            text,
            skthreads.runsync(
                partial(tr, text, **kwargs),
                abortSignal=self.abortSignal,
            ) if async else tr(text, **kwargs)))
Beispiel #8
0
      }
      it = d.findRetranslator(it, to=to, fr=fr) or it
      if it.key == 'eztrans':
        kw['ehndEnabled'] = ehndEnabled if ehndEnabled is not None else d.ehndEnabled
      if scriptEnabled != False:
        if it.key == 'retr':
          kw['scriptEnabled1'] = scriptEnabled or d.getScriptEnabled(it.first.key)
          kw['scriptEnabled2'] = scriptEnabled or d.getScriptEnabled(it.second.key)
        else:
          kw['scriptEnabled'] = scriptEnabled or d.getScriptEnabled(it.key)
      if emit or not it.onlineRequired or not it.asyncSupported:
        ret = it.translate(text, **kw)
      else: # not emit and asyncSupported
        kw['async'] = False # force using single thread
        ret = skthreads.runsync(partial(it.translate, text, **kw),
          abortSignal=self.abortionRequested,
        )
    if ret and ret[0] and d.rubyEnabled and rubyEnabled == False:
      t = richutil.removeRuby(ret[0])
      ret = t, ret[1], ret[2]
    return ret or (None, None, None)

  def translateApply(self, func, text, fr='ja', to='', keepsNewLine=None, mark=None, scriptEnabled=None, ehndEnabled=None, rubyEnabled=None, context='', **kwargs):
    """Specialized for textman
    @param  func  function(unicode sub, unicode lang, unicode provider)
    @param  text  unicode
    @param* fr  unicode  language
    @param* mark  bool or None
    @param* kwargs  pass to func
    """
    if not features.MACHINE_TRANSLATION or not text:
Beispiel #9
0
      return self
    else:
      dwarn("failed to lock mutex due to contention")
      return
  def __exit__(self, *err):
    if self.locked:
      READ_MUTEX.unlock()

def readtext(path, lang, async=False):
  """
  @param  path  unicode
  @param  lang  unicode
  @param* async  bool
  @return  unicode
  """
  return skthreads.runsync(partial(_readtext_async, path, lang)) if async else _readtext_sync(path, lang)

def readtexts(path, lang, async=False):
  """
  @param  path  unicode
  @param  lang  unicode
  @param* async  bool
  @return  [unicode]
  """
  return skthreads.runsync(partial(_readtexts_async, path, lang)) if async else _readtexts_sync(path, lang)

def _readtext_async(*args, **kwargs):
  with SkCoInitializer():
    return _readtext_sync(*args, **kwargs)

def _readtexts_async(*args, **kwargs):