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
def savePlainConfiguration(dictionary): """Write configuration to disk""" from lib import dicttype if not dictionary.getType() in dicttype.plainTypes: systemLog(ERROR, "Request to write configuration to %s of type %s" \ % (dictionary.getName(), dictionary.getType())) return md5sum = util.getMD5Sum(dictionary.getPath()) dictDir = dictionary.getConfigDir() doc = xmltools.generatePlainDictConfig( name=dictionary.getName(), format=dictionary.getType().getIdName(), version=dictionary.getVersion(), authors=dictionary.getAuthors(), path=dictionary.getPath(), md5=md5sum, encoding=dictionary.getEncoding(), description=dictionary.getDescription(), licence=dictionary.getLicenceFile()) xmltools.writePlainDictConfig(doc, os.path.join(dictDir, 'conf', 'config.xml'))
def savePlainConfiguration(dictionary): """Write configuration to disk""" from lib import dicttype if not dictionary.getType() in dicttype.plainTypes: systemLog(ERROR, "Request to write configuration to %s of type %s" \ % (dictionary.getName(), dictionary.getType())) return md5sum = util.getMD5Sum(dictionary.getPath()) dictDir = dictionary.getConfigDir() doc = xmltools.generatePlainDictConfig(name=dictionary.getName(), format=dictionary.getType().getIdName(), version=dictionary.getVersion(), authors=dictionary.getAuthors(), path=dictionary.getPath(), md5=md5sum, encoding=dictionary.getEncoding(), description=dictionary.getDescription(), licence=dictionary.getLicenceFile()) xmltools.writePlainDictConfig(doc, os.path.join(dictDir, 'conf', 'config.xml'))
fd.close() if stopped: return if not error: error = downloader.getErrorMessage() if error: systemLog(ERROR, error) title = _("Unable to download") errorwin.showErrorMessage(title, error) return md5sum = util.getMD5Sum(localPath) # # Check checksum # if md5sum != dictInfo.getChecksum(): title = _("File is damaged") msg = _("Downloaded file is damaged and cannot be installed. " \ "Please try again.") errorwin.showErrorMessage(title, msg) return # # Remove old version if exists # if dictInfo.getName() in self.app.dictionaries.keys():
# Create directories try: os.mkdir(dictDir) os.mkdir(os.path.join(dictDir, info.__PLAIN_DICT_CONFIG_DIR)) os.mkdir(os.path.join(dictDir, info.__PLAIN_DICT_FILE_DIR)) os.mkdir(os.path.join(dictDir, info._PLAIN_DICT_DATA_DIR)) except Exception, e: print "ERROR Unable to create dicrectories, aborted (%s)" % e try: shutil.rmtree(dictDir) except Exception, e: print "ERROR Unable to remove directories (%s)" % e # Determine info dictFormat = dictType.getIdName() md5sum = util.getMD5Sum(filePath) # Write configuration doc = xmltools.generatePlainDictConfig(name=dictionaryName, format=dictFormat, version=None, authors={}, path=filePath, md5=md5sum, encoding='UTF-8', description=None) xmltools.writePlainDictConfig(doc, os.path.join(dictDir, 'conf', 'config.xml')) return dictDir
try: os.mkdir(dictDir) os.mkdir(os.path.join(dictDir, info.__PLAIN_DICT_CONFIG_DIR)) os.mkdir(os.path.join(dictDir, info.__PLAIN_DICT_FILE_DIR)) os.mkdir(os.path.join(dictDir, info._PLAIN_DICT_DATA_DIR)) except Exception, e: print "ERROR Unable to create dicrectories, aborted (%s)" % e try: shutil.rmtree(dictDir) except Exception, e: print "ERROR Unable to remove directories (%s)" % e # Determine info dictFormat = dictType.getIdName() md5sum = util.getMD5Sum(filePath) # Write configuration doc = xmltools.generatePlainDictConfig(name=dictionaryName, format=dictFormat, version=None, authors={}, path=filePath, md5=md5sum, encoding='UTF-8', description=None) xmltools.writePlainDictConfig(doc, os.path.join(dictDir, 'conf', 'config.xml'))
def installPlainDictionary(filePath): """Install plain dictionary and return directory path""" if not os.path.exists(filePath): raise Exception(_("File %s does not exist") % filePath) if not os.path.isfile(filePath): raise Exception(_("%s is not a file") % filePath) util.makeDirectories() fileName = os.path.basename(filePath) dictionaryName = os.path.splitext(fileName)[0] dictDir = os.path.join(info.LOCAL_HOME, info.__DICT_DIR, info.__PLAIN_DICT_DIR, fileName) # Check existance if os.path.exists(dictDir): raise Exception(_("Dictionary \"%s\" is already installed") \ % dictionaryName) extention = os.path.splitext(fileName)[1][1:] dictType = None # Determine type for t in dicttype.supportedTypes: for ext in t.getFileExtentions(): if ext.lower() == extention.lower(): dictType = t break if not dictType: raise Exception("Dictionary type for '%s' still unknown! " \ "This may be internal error." % fileName) # Create directories try: os.mkdir(dictDir) os.mkdir(os.path.join(dictDir, info.__PLAIN_DICT_CONFIG_DIR)) os.mkdir(os.path.join(dictDir, info.__PLAIN_DICT_FILE_DIR)) os.mkdir(os.path.join(dictDir, info._PLAIN_DICT_DATA_DIR)) except Exception as e: print("ERROR Unable to create dicrectories, aborted (%s)" % e) try: shutil.rmtree(dictDir) except Exception as e: print("ERROR Unable to remove directories (%s)" % e) # Determine info dictFormat = dictType.getIdName() md5sum = util.getMD5Sum(filePath) # Write configuration doc = xmltools.generatePlainDictConfig(name=dictionaryName, format=dictFormat, version=None, authors={}, path=filePath, md5=md5sum, encoding='UTF-8', description=None) xmltools.writePlainDictConfig(doc, os.path.join(dictDir, 'conf', 'config.xml')) return dictDir
def _fetchAddon(self, dictInfo): """Fetch add-on using progress bar""" downloadsDir = os.path.join(info.LOCAL_HOME, 'downloads') if not os.path.exists(downloadsDir): os.mkdir(downloadsDir) localPath = os.path.join(downloadsDir, os.path.basename(dictInfo.getLocation())) title = _("Downloading %s...") % dictInfo.getName() progressDialog = wx.ProgressDialog(title, '', maximum=100, parent=self, style=wx.PD_CAN_ABORT | wx.PD_APP_MODAL) keepGoing = True error = None downloader = util.DownloadThread(dictInfo.getLocation()) stopped = False try: fd = open(localPath, 'wb') downloader.start() while keepGoing and not downloader.finished(): keepGoing = progressDialog.Update(downloader.getPercentage(), downloader.getMessage()) if not keepGoing: stopped = True break chunk = downloader.getBytes() fd.write(chunk) time.sleep(0.1) bytes = downloader.getBytes() if len(bytes): fd.write(bytes) progressDialog.Destroy() downloader.stop() except Exception as e: traceback.print_exc() progressDialog.Destroy() error = "Unable to fetch \"%s\" from %s: %s" \ % (dictInfo.getName(), dictInfo.getLocation(), e) systemLog(ERROR, error) fd.close() if stopped: return if not error: error = downloader.getErrorMessage() if error: systemLog(ERROR, error) title = _("Unable to download") errorwin.showErrorMessage(title, error) return md5sum = util.getMD5Sum(localPath) # # Check checksum # if md5sum != dictInfo.getChecksum(): title = _("File is damaged") msg = _("Downloaded file is damaged and cannot be installed. " \ "Please try again.") errorwin.showErrorMessage(title, msg) return # # Remove old version if exists # if dictInfo.getName() in list(self.app.dictionaries.keys()): try: dictInstance = self.app.dictionaries.get(dictInfo.getName()) if dictInstance.getType() == dicttype.PLUGIN: installer.removePluginDictionary(dictInstance) else: installer.removePlainDictionary(dictInstance) except Exception as e: traceback.print_exc() title = _("Error") msg = _("Unable to remove old version of \"%s\". " "Error occured: \"<i>%s</i>\". New version " "cannot be installed without removing old one.") \ % (dictInstance.getName(), e) errorwin.showErrorMessage(title, msg) return # # Install # try: inst = installer.Installer(self.mainWin, self.app.config) inst.install(localPath) if self.installedList.FindString( dictInfo.getName()) == wx.NOT_FOUND: index = self.installedList.Insert(dictInfo.getName(), 0) self.installedList.Check(0) self.app.config.activedict.add(dictInfo.getName()) self.app.config.activedict.save() # FIXME: Code-wasting. Separated duplicated code into # functions. except Exception as e: traceback.print_exc() title = _("Unable to install") msg = _("Unable to install dictionary \"%s\".") \ % dictInfo.getName() errorwin.showErrorMessage(title, msg) return self.availableList.DeleteItem(self.currentAvailItemSelection) del self.addons[dictInfo.getName()] self.buttonInstall.Disable() self.clearInfo() self.disableInfo()