def removeModule(manager, moduleName): """Physically deletes a module from the hard disk Returns True if module couldn't be found, False otherwise""" mod_name = Sword.SWBuf(moduleName) s = manager.config.getSections() module = s.find(mod_name) # we use to have != s.end, but that crashes... if mod_name in s: manager.deleteModule(moduleName) file_buf = Sword.SWBuf("File") mod_second = module.value()[1] fileBegin = mod_second.lower_bound(file_buf) fileEnd = mod_second.upper_bound(file_buf) entry = mod_second.find(Sword.SWBuf("AbsoluteDataPath")) modDir = entry.value()[1].c_str() modDir = removeTrailingSlash(modDir) if fileBegin != fileEnd: # remove each file while fileBegin != fileEnd: modFile = modDir modFile += "/" modFile += fileBegin.value()[1].c_str() # remove file FileMgr.removeFile(modFile.c_str()) fileBegin += 1 else: #remove all files in DataPath directory FileMgr.removeDir(modDir) # BM: this could be a bit ticklish... # I hope I have copied the correct behaviour # find and remove .conf file try: items = os.listdir(manager.configPath) except OSError: pass else: baseModFile = manager.configPath baseModFile = removeTrailingSlash(baseModFile) for item in items: modFile = baseModFile + "/" modFile += item config = Sword.SWConfig(modFile) if mod_name in config.getSections(): del config FileMgr.removeFile(modFile) return False return True
def __init__(self, reference_iter, strongs=False, morph=False, module='KJV', markup=Sword.FMT_PLAIN, render=''): """ Initialize. """ markup = Sword.MarkupFilterMgr(markup) # We don't own this or it will segfault. markup.thisown = False self._library = Sword.SWMgr(markup) self._library.setGlobalOption("Headings", "On") self._library.setGlobalOption("Cross-references", "Off") if strongs: self._library.setGlobalOption("Strong's Numbers", "On") else: self._library.setGlobalOption("Strong's Numbers", "Off") if morph: self._library.setGlobalOption("Morphological Tags", "On") else: self._library.setGlobalOption("Morphological Tags", "Off") # Strings for finding the heading. self._head_str = Sword.SWBuf('Heading') self._preverse_str = Sword.SWBuf('Preverse') self._canon_str = Sword.SWBuf('canonical') self._module = self._library.getModule(module) self._key = self._module.getKey() if render.lower() == 'raw': self._render_func = self._module.getRawEntry elif render.lower() == 'render_raw': self._fix_space_regx = re.compile(r'([^\.:\?!])\s+') self._fix_end_regx = re.compile(r'\s+([\.:\?!,;])') self._fix_start_tag_regx = re.compile(r'(<[npi]>)\s*') self._fix_end_tag_regx = re.compile(r'\s*(</[npi]>)') self._upper_divname_regx = re.compile(r'(\w+)([\'s]*)') self._render_func = \ lambda: self._parse_raw(self._module.getRawEntry(), strongs, morph) else: self._render_func = self._module.renderText self._ref_iter = reference_iter
def __init__(self, privatePath="./", sr=None): self.statusReporter = sr self.privatePath = privatePath self.transport = None self.passive = False self.term = False if self.privatePath: self.privatePath = removeTrailingSlash(self.privatePath) confPath = privatePath + "/InstallMgr.conf" FileMgr.createParent(confPath) installConf = Sword.SWConfig(confPath) #SectionMap::iterator sourcesSection; #ConfigEntMap::iterator sourceBegin; #ConfigEntMap::iterator sourceEnd; self.sources = {} self.setFTPPassive(installConf.get("General", "PassiveFTP") == "false") sourcesSection = installConf.getSections().find(Sword.SWBuf("Sources")) if sourcesSection != installConf.getSections().end(): ftp_source = Sword.SWBuf("FTPSource") ss = sourcesSection.value()[1] sourceBegin = ss.lower_bound(ftp_source) sourceEnd = ss.upper_bound(ftp_source) while sourceBegin != sourceEnd: install_source = InstallSource("FTP", sourceBegin.value()[1].c_str()) self.sources[install_source.caption] = install_source parent = privatePath + "/" + install_source.source + "/file" FileMgr.createParent(parent) install_source.localShadow = privatePath + "/" + install_source.source sourceBegin += 1 self.defaultMods = set() sourcesSection = installConf.getSections().find(Sword.SWBuf("General")) general = sourcesSection.value()[1] if sourcesSection != installConf.getSections().end(): default_mod = Sword.SWBuf("DefaultMod") sourceBegin = general.lower_bound(default_mod) sourceEnd = general.upper_bound(default_mod) while sourceBegin != sourceEnd: self.defaultMods.add(sourceBegin.value()[1].c_str()) sourceBegin += 1
def installModule(self, destMgr, fromLocation, modName, install_source=None): """Install a module from destMgr to a location Returns -1 on aborted, 0 on success and 1 on error """ aborted = False cipher = False print destMgr, fromLocation, modName, install_source #printf("***** InstallMgr::installModule\n") #if fromLocation: # printf("***** fromLocation: %s \n", fromLocation) #printf("***** modName: %s \n", modName) if install_source: sourceDir = self.privatePath + "/" + install_source.source else: sourceDir = fromLocation sourceDir = removeTrailingSlash(sourceDir) sourceDir += '/' # do this with False at the end to stop the augmenting mgr = Sword.SWMgr(sourceDir, True, None, False, False) module = mgr.config.getSections().find(Sword.SWBuf(modName)) if module != mgr.config.getSections().end(): mod_second = module.value()[1] entry = mod_second.find(Sword.SWBuf("CipherKey")) if entry != mod_second.end(): cipher = True # This first check is a method to allow a module to specify each # file that needs to be copied file_buf = Sword.SWBuf("File") file_value = mod_second fileEnd = file_value.upper_bound(file_buf) fileBegin = file_value.lower_bound(file_buf) print fileEnd == fileBegin if (fileBegin != fileEnd): # copy each file if (install_source): while (fileBegin != fileEnd): swbuf = fileBegin.value()[1] src = swbuf.c_str() # ftp each file first buffer = sourceDir + src if self.ftpCopy(install_source, src, buffer): aborted = True break # user aborted fileBegin += 1 fileBegin = mod_second.lower_bound(file_buf) if not aborted: # DO THE INSTALL while fileBegin != fileEnd: sourcePath = sourceDir sourcePath += fileBegin.value()[1].c_str() dest = destMgr.prefixPath dest = removeTrailingSlash(dest) dest += '/' dest += fileBegin.value()[1].c_str() FileMgr.copyFile(sourcePath, dest) fileBegin += 1 # --------------- if install_source: fileBegin = mod_second.lower_bound(file_buf) while (fileBegin != fileEnd): # delete each tmp ftp file buffer = sourceDir + fileBegin.value()[1].c_str() FileMgr.removeFile(buffer.c_str()) fileBegin += 1 # This is the REAL install code, the above code I don't think has # ever been used # # Copy all files in DataPath directory # else: entry = mod_second.find(Sword.SWBuf("AbsoluteDataPath")) if (entry != mod_second.end()): absolutePath = entry.value()[1].c_str() relativePath = absolutePath entry = mod_second.find(Sword.SWBuf("PrefixPath")) if (entry != mod_second.end()): relativePath = relativePath[entry.value()[1].size():] else: relativePath = relativePath[len(mgr.prefixPath):] printf("***** mgr.prefixPath: %s", mgr.prefixPath) printf("***** destMgr.prefixPath: %s", destMgr.prefixPath) printf("***** absolutePath: %s", absolutePath) printf("***** relativePath: %s", relativePath) if install_source: if self.ftpCopy(install_source, relativePath, absolutePath, True): aborted = True # user aborted if not aborted: destPath = (destMgr.prefixPath or "") + relativePath FileMgr.copyDir(absolutePath, destPath) if install_source: # delete tmp ftp files mgr.deleteModule(modName) FileMgr.removeDir(absolutePath) if not aborted: confDir = sourceDir + "mods.d/" try: items = os.listdir(confDir) except OSError: pass else: for item in items: modFile = confDir modFile += item config = Sword.SWConfig(modFile) if config.getSections().find(Sword.SWBuf(modName)) != \ config.getSections().end(): targetFile = destMgr.configPath or "" #; //"./mods.d/"; targetFile = removeTrailingSlash(targetFile) targetFile += "/" targetFile += item FileMgr.copyFile(modFile, targetFile) if (cipher): if self.getCipherCode(modName, config): # An error has occurred with getting # cipher code # This removes the module # Is this wise? newDest = Sword.SWMgr(destMgr.prefixPath) self.removeModule(newDest, modName) aborted = True else: config.Save() FileMgr.copyFile(modFile, targetFile) del config if aborted: return -1 return 0 return 1