def compile(): # -> bool dprint("enter") dbpath = TARGET_DIR + '/' + DB_FILENAME dicpath = TARGET_DIR + '/' + DIC_FILENAME csvpath = TARGET_DIR + '/' + CSV_FILENAME tmpdicpath = dicpath + '.tmp' from mecabdic import mdedict with SkProfiler("assemble csv"): ok = mdedict.db2csv(csvpath, dbpath) if ok: with SkProfiler("compile dic"): ok = mdedict.csv2dic(tmpdicpath, csvpath, exe=MECAB_EXEPATH, dicdir=UNIDIC_DICPATH) from sakurakit import skfileio if ok: if os.path.exists(dicpath): skfileio.remove(dicpath) os.rename(tmpdicpath, dicpath) elif os.path.exists(tmpdicpath): skfileio.removefile(tmpdicpath) if os.path.exists(csvpath): skfileio.removefile(csvpath) dprint("leave: ok = %s" % ok) return ok
def updateAndRefresh(self): if self._locked: dwarn("locked") return gameId = self.gameId if not gameId: #growl.notify(my.tr("Unknown game. Please try updating the database.")) return if netman.manager().isOnline(): if self.itemId and self.itemId < defs.MIN_NORMAL_GAME_ITEM_ID: dprint("found non-normal game, ignore refs") else: self._locked = True # lock before online access #growl.msg(my.tr("Updating game information")) ok = dataman.manager().updateReferences(gameId) #dm.touchGames() if not ok: growl.notify( my. tr("The game title it not specified. You can click the Edit button to add one." )) #else: #growl.msg(my.tr("Found game information")) self._locked = False # always unlock no matter what happened self.refresh()
def getRioVersion(path): """ @param path unicode @return int """ # Example: [椎名里緒 v2.50] try: line = next(open(path, 'r')) TagSize = 8 # size of 椎名里緒 that contains 8 bytes in sjis if len(line) >= TagSize + 8 and line[0] == '[' and line[ TagSize + 1] == ' ' and line[TagSize + 2] == 'v' and line[TagSize + 4] == '.': major = line[TagSize + 3] minor1 = line[TagSize + 5] minor2 = line[TagSize + 6] if str.isdigit(major): ret = int(major) * 100 if str.isdigit(minor1): ret += int(minor1) * 10 if str.isdigit(minor2): ret += int(minor2) dprint("version = %s" % ret) return ret except Exception: pass return 0
def get(lang): # str -> bool filename = DICS[lang] url = DIC_URL % filename minsize = MIN_DIC_SIZE path = TMP_DIR + '/' + filename targetpath = TARGET_DIR + '/' + filename dprint("enter: url = %s, minsize = %s" % (url, minsize)) #from sakurakit import skfileio #if os.path.exists(path) and skfileio.filesize(path) == size: # dprint("leave: already downloaded") # return True ok = False from sakurakit import skfileio, sknetio with SkProfiler("fetch"): # gzip=True to automatically extract gzip # flush=false to use more memory to reduce disk access if sknetio.getfile(url, path, flush=False, gzip=True): ok = skfileio.filesize(path) > minsize if ok: os.renames(path, targetpath) elif os.path.exists(path): skfileio.removefile(path) dprint("leave: ok = %s" % ok) return ok
def showTranslation(self, text, language, provider, align, timestamp): """ @param text unicode @param language str @param provider str @param align list or None @param timestamp long """ if not settings.global_().isGrimoireTranslationVisible(): return d = self.__d if d.timestamp > timestamp: dprint("translation comes too late, ignored") return if not align: align = None # enforce None else: align = SkValueObject(value=align) d.retainObject(align) #text = text.replace('\n', '<br/>') d.append( partial(GrimoireBean.instance.showTranslation.emit, text, language, provider, align, timestamp))
def makedb(): # -> bool dprint("enter") tmpdic = TMP_DIR + '/' + DIC_FILENAME tmpdb = TMP_DIR + '/' + DB_FILENAME targetdic = TARGET_DIR + '/' + DIC_FILENAME targetdb = TARGET_DIR + '/' + DB_FILENAME from dictdb import edictdb with SkProfiler("create db"): ok = edictdb.makedb(tmpdb, tmpdic) if ok: with SkProfiler("create index"): ok = edictdb.makesurface(tmpdb) from sakurakit import skfileio if ok: skfileio.removefile(targetdb) skfileio.removefile(targetdic) os.rename(tmpdb, targetdb) os.rename(tmpdic, targetdic) else: for it in tmpdb, tmpdic: if os.path.exists(it): skfileio.removefile(it) dprint("leave: ok = %s" % ok) return ok
def _setDictionaries(self): dprint("enter") path = self.dllDirectory # Load default dic, which is required defdic = os.path.join(path, DEFAULT_DIC) ret = self._setBasicDictPath( os.path.join(defdic)) # this cannot fail, or it won't work # This will double the translation time orz. # I should selectively enable the dict that is useful if PROF_DIC_ENABLED: # Load extra dic, which is optional priority = 0 for dic in os.listdir(path): if dic not in (DEFAULT_DIC, USER_DIC): dicpath = os.path.join(path, dic) if os.path.isdir(dicpath): priority += 1 self._setProfDictPath(dicpath, priority) # Load user dic from sakurakit import skfileio userdic = os.path.join(path, USER_DIC) if userdic and os.path.exists( userdic) and not skfileio.emptydir(userdic): self._setUserDictPathA(userdic) dprint("leave: ok = %s" % ret) return ret
def main(argv): """ @param argv [unicode] @return int """ dprint("enter") ok = False if not argv or len(argv) == 1 and argv[0] in ('-h', '--help'): usage() #elif len(argv) != 1: # dwarn("invalid number of parameters") # usage() else: family = argv[-1] quiet = '-q' in argv or '--quiet' in argv try: msg(family) init() ok = run(family) if ok and not quiet: from sakurakit import skos font = FONTS[family] path = os.path.join(FONT_DIR, font['path']) if font['type'] == 'file': path = os.path.dirname(path) skos.open_location(os.path.abspath(path)) except Exception, e: dwarn(e)
def __init__(self, parent=None): super(AppHelpDialog, self).__init__(parent) self.setWindowTitle("%s - %s" % (tr_("Help"), mytr_("Visual Novel Reader"))) self.setHtml(info.renderAppHelp()) self.resize(450, 400) dprint("pass")
def listen(self, time): if time < self.time: # aborted return q = self.q r = self.recognizer while self.enabled: try: from google import googlesr with googlesr.Microphone( device_index=self.deviceIndex) as source: dprint("listen start") r.stopped = False audio = r.listen(source) dprint("listen stop") except Exception, e: dwarn("audio device error", e) q.recognitionFinished.emit() return if time < self.time or self.aborted or not self.enabled: # aborted return if audio and len(audio.data) < MAX_AUDIO_SIZE: skthreads.runasync(partial(self.recognize, audio)) else: q.recognitionFinished.emit() if time < self.time or self.aborted or self.singleShot: return
def start(self): d = self.__d if not d.active: d.active = True self.activeChanged.emit(True) d.thread().requestListen() dprint("pass")
def _fetch(self, *args, **kwargs): """@reimp @return str """ caching = self.cachedir and self.caching if caching: if self.expiretime: expirepath = os.path.join(self.cachedir, self._digest(True, *args, **kwargs) + suffix) data = self._readcache(expirepath) if data: return data path = os.path.join(self.cachedir, self._digest(False, *args, **kwargs) + suffix) if not self.expiretime: data = self._readcache(path) if data: return data dprint('cache MISS') # fetch original response from Amazon data = super(Cacher, self)._fetch(*args, **kwargs) if self.online else None if caching: if data: if self.expiretime: self._writecache(data, expirepath) self._writecache(data, path) else: if self.expiretime: data = self._readcache(path) return data
def dll(self): if not self._dll: try: self._dll = ctypes.WinDLL(self.DLL_MODULE) dprint("D_JK dll is loaded") except Exception, e: dwarn("failed to load D_JK", e)
def _parsedict(data, offset, *args, **kwargs): # str, int; may raise out-of-bound exception # Dictionary type dictType = byteutil.toint(data, offset) # int assert dictType == 3 #System.out.println("词典类型:0x" + Integer.toHexString(data.getInt(offset))) limit = byteutil.toint(data, offset + 4) + offset + 8 # int offsetIndex = offset + 0x1c offsetCompressedDataHeader = byteutil.toint(data, offset + 8) + offsetIndex inflatedWordsIndexLength = byteutil.toint(data, offset + 12) inflatedWordsLength = byteutil.toint(data, offset + 16) inflatedXmlLength = byteutil.toint(data, offset + 20) definitions = (offsetCompressedDataHeader - offsetIndex) / 4 # for debugging only dprint("number of definitions = %i" % definitions) #data.position(offsetCompressedDataHeader + 8) pos = offsetCompressedDataHeader + 8 it = byteutil.toint(data, pos) pos += 4 # skip the index offset #deflateStreams = byteutil.iterint32(data, indexOffset, limit) # [int] deflateStreams = [] #[ int] while (it + pos) < limit: it = byteutil.toint(data, pos) pos += 4 deflateStreams.append(it) inflatedBytes = _inflate(data, deflateStreams, pos) # [byte] if inflatedBytes: return _extract(inflatedBytes, inflatedWordsIndexLength, inflatedWordsIndexLength + inflatedWordsLength, *args, **kwargs)
def stop(self): dprint("enter") d = self.__d w = d.webView w.stop() #w.setContent("") # Not needed dprint("leave")
def __init__(self, parent=None): super(ReferenceHelpDialog, self).__init__(parent) self.setWindowTitle("%s - %s" % (tr_("Help"), mytr_("Game References"))) self.setHtml(info.renderReferenceHelp()) self.resize(500, 375) dprint("pass")
def get(): # -> bool url = DIC_URL minsize = MIN_DIC_SIZE path = TMP_DIR + '/' + DIC_FILENAME path_compressed = path + '.gz' dprint("enter: url = %s, minsize = %s" % (url, minsize)) #from sakurakit import skfileio #if os.path.exists(path) and skfileio.filesize(path) == size: # dprint("leave: already downloaded") # return True ok = False import gzip from sakurakit import skfileio, sknetio with SkProfiler("fetch"): # gzip=True to automatically extract gzip # flush=false to use more memory to reduce disk access if sknetio.getfile(url, path_compressed, flush=False, gzip=False): # Note: gzip=True does not extract gzip, it decompresses the header ... probs? >_< with gzip.open(path_compressed, 'rb') as f_in, open(path, 'wb') as f_out: f_content = f_in.read() f_out.write(f_content) ok = skfileio.filesize(path) > minsize if ok: skfileio.removefile(path_compressed) elif os.path.exists(path): skfileio.removefile(path) skfileio.removefile(path_compressed) # if not ok and os.path.exists(path): # skfileio.removefile(path) dprint("leave: ok = %s" % ok) return ok
def __init__(self, parent=None): super(TermHelpDialog, self).__init__(parent) self.setWindowTitle("%s - %s" % (tr_("Help"), mytr_("Shared Dictionary"))) self.setHtml(info.renderTermHelp()) self.resize(500, 300) dprint("pass")
def dll(self): if not self._dll: try: self._dll = ctypes.WinDLL(self.dllPath) dprint("gts sdk dll is loaded") except Exception, e: dwarn("failed to load gts sdk dll", e)
def makedb(lang): # str -> bool dprint("enter: lang = %s" % lang) dic = DICS[lang] ldpath = LD_DIR + '/' + lang + LD_SUFFIX dbpath = DB_DIR + '/' + lang + DB_SUFFIX tmppath = TMP_DIR + '/' + lang + DB_SUFFIX from sakurakit import skfileio for it in dbpath, tmppath: if os.path.exists(it): skfileio.removefile(it) if not os.path.exists(ldpath): dwarn("leave: ld does not exist: %s" % ldpath) return False ok = False from lingoes.lingoesdb import LingoesDb with SkProfiler("extract"): if LingoesDb(tmppath).create(ldpath, dic['inenc'], dic['outenc']): os.renames(tmppath, dbpath) # renames to create DB_DIR ok = True elif os.path.exists(tmppath): skfileio.removefile(tmppath) if os.path.exists(ldpath): skfileio.removefile(ldpath) dprint("leave: ok = %s" % ok) return ok
def _injectSite(self): t = self.siteAct.isChecked() v = self.tabWidget.currentWidget() if v: dprint(t) v.setSiteEnabled(t) v.injectSite()
def exists(): """ @return bool """ ret = os.path.exists(skpaths.WINDIR + "/AppPatch/AlLayer.dll") dprint("ret = %s" % ret) return ret
def setStaysOnTop(self, value): if value != self.staysOnTop(): dprint("staysOnTop = %s" % value) if value: self.staysOnTopTimer.start() else: self.staysOnTopTimer.stop()
def __init__(self): dprint('enter') super(MainObject, self).__init__() self.__d = _MainObject(self) #MainObject.instance = self dprint('leave')
def setRegionSelectionEnabled(self, t): dprint(t) d = self.__d if d.selectionEnabled != t: d.selectionEnabled = t #d.setRubberBandColor('red' if t else None) d.setRubberBandColor('magenta' if t else None)
def quit(self): if self.hasQuit: return self.hasQuit = True import curtheme curtheme.unload() for w in self.widgets: if w.isVisible(): w.hide() # wait for for done or kill all threads from PySide.QtCore import QThreadPool if QThreadPool.globalInstance().activeThreadCount(): dwarn("warning: wait for active threads") QThreadPool.globalInstance().waitForDone(config.QT_THREAD_TIMEOUT) dprint("leave qthread pool") dprint("send quit signal to qApp") qApp = QCoreApplication.instance() # Make sure settings.sync is the last signal conneced with aboutToQuit #qApp.aboutToQuit.connect(self.settings.sync) skevents.runlater(qApp.quit)
def setScriptEnabled(self, key, t): # str, bool -> if self.scriptEnabled.get(key) != t: self.scriptEnabled[key] = t if self.hasTranslator(key): mt = self.getTranslator(key) dprint("clear cache for %s" % key) mt.clearCache()
def dll(self): if not self._dll: try: self._dll = ctypes.CDLL(ENGINE_DLL) dprint("nova engine dll is loaded") except Exception, e: dwarn("failed to load nova", e)
def _makeurl(self, **params): """ @param params request params @return str See: https://affiliate.dmm.com/api/reference/com/iroiro/ """ if self.service: params['service'] = self.service if self.floor: params['floor'] = self.floor params['api_id'] = self.api_id params['affiliate_id'] = self.affiliate_id params['version'] = self.version params['site'] = self.site params['operation'] = self.operation params['timestamp'] = time.strftime('%Y-%m-%d %H:%M:%S', time.gmtime()) # paramsのハッシュを展開 request = ["%s=%s" % (k, urllib2.quote(self._encodeparam(v))) for k,v in sorted(params.iteritems())] # sorted is needed to make the URL unique for caching ret = self.URL + "?" + "&".join(request) if self.debug: dprint(ret) return ret
def finish(self): dprint('enter') self.writeFile() # write for the last chunk self.stop() growl.notify("[download] %s" % my.tr("clean up")) self.q.finished.emit() dprint('leave')