def OnCopyTheme(self, event): dialog = wx.TextEntryDialog(self, _("Please type a name for your new theme"), _("New Theme"), "") if dialog.ShowModal() == wx.ID_OK: themedir = os.path.join(settings.AppDir, "themes") filename = string.replace(fileutils.MakeFileName2(dialog.GetValue()) + ".py", " ", "_") otherfilename = string.replace(fileutils.MakeFileName2(self.lstThemeList.GetStringSelection()) + ".py", " ", "_") otherfilename = string.replace(otherfilename, "(", "") otherfilename = string.replace(otherfilename, ")", "") foldername = utils.createSafeFilename(dialog.GetValue()) try: os.mkdir(os.path.join(themedir, foldername)) except: message = _("Cannot create theme. Check that a theme with this name does not already exist, and that you have write access to the '%(themedir)s' directory.") % {"themedir":os.path.join(settings.AppDir, "themes")} self.parent.log.error(message) wx.MessageBox(message) return copyfile = utils.openFile(os.path.join(themedir, otherfilename), "r") data = copyfile.read() copyfile.close() oldthemeline = 'themename = "%s"' % (string.replace(self.lstThemeList.GetStringSelection(), "\"", "\\\"")) newthemeline = 'themename = "%s"' % (string.replace(dialog.GetValue(), "\"", "\\\"")) data = string.replace(data, oldthemeline, newthemeline) myfile = utils.openFile(os.path.join(themedir, filename), "w") myfile.write(data) myfile.close() #copy support files from Default (no frames) fileutils.CopyFiles(os.path.join(themedir, self.lstThemeList.GetStringSelection()), os.path.join(themedir, foldername), 1) self.parent.themes.LoadThemes() self.ReloadThemes() dialog.Destroy()
def UpdateIndex(self, eclassdir): gsdl = settings.AppSettings["GSDL"] eclassdir = os.path.join(gsdl, "collect", self.parent.pub.pubid) self.processfinished = False gsdl_supportdir = os.path.join(settings.AppDir, "greenstone") if not os.path.exists(eclassdir): self.call = win32api.GetShortPathName(os.path.join(gsdlsupport_dir, "mkcol.bat")) + " " + \ self.parent.pub.pubid + " " + win32api.GetShortPathName(gsdl) self.running_thread = threading.Thread(None, self.cmdline) self.running_thread.start() if os.path.exists(eclassdir): collecttext = "" configfile = os.path.join(gsdl_supportdir, "collect.cfg") try: collectcfg = utils.openFile(configfile, "r") collecttext = collectcfg.read() collectcfg.close() except: message = _("There was an error reading the file '%(filename)s'. Please ensure that the file exists and that you have read permissions.") % {"filename": configfile} self.log.error(message) wx.MessageBox(message) return outfile = os.path.join(eclassdir, "etc", "collect.cfg") try: collecttext = string.replace(collecttext, "**[title]**", self.parent.pub.name) collectout = utils.openFile(outfile, "w") collectout.write(collecttext) collectout.close() except: message = _("There was an error writing the file '%(collectfile)s'. Please ensure that the file exists and that you have write permissions.") % {"collectfile": outfile} self.log.error(message) wx.MessageBox(message) return files.CopyFiles(os.path.join(settings.ProjectDir, "pub"), os.path.join(eclassdir, "import"), 1) #...and build the collection self.call = win32api.GetShortPathName(os.path.join(gsdl_supportdir, "buildcol.bat")) + " " + \ self.parent.pub.pubid + " " + win32api.GetShortPathName(gsdl) self.running_thread = threading.Thread(None, self.cmdline) self.running_thread.start() self.txtProgress.WriteText(_("Copying EClass publication files to Greenstone...")) exportdir = os.path.join(gsdl, "tmp", "exported_" + self.parent.pub.pubid) if not os.path.exists(os.path.join(exportdir, "gsdl", "eclass")): os.mkdir(os.path.join(exportdir, "gsdl", "eclass")) outputDir = os.path.join(gsdl, "tmp", "exported_" + self.parent.pub.pubid, "gsdl") files.CopyFiles(settings.ProjectDir, os.path.join(outputDir, "eclass"), 1) files.CopyFile("home.dm", gsdl_supportdir, os.path.join(outputDir, "macros")) files.CopyFile("style.dm", gsdl_supportdir, os.path.join(outputDir, "macros")) self.status.SetLabel(_("""Finished exporting. You can find the exported collection at:""") + exportdir)
def GetData(self): if isinstance(self.node, conman.conman.ConNode): filename = self.node.content.filename elif isinstance(self.node, ims.contentpackage.Item): resource = ims.utils.getIMSResourceForIMSItem(appdata.currentPackage, self.node) filename = eclassutils.getEClassPageForIMSResource(resource) if not filename: filename = resource.getFilename() filename = os.path.join(settings.ProjectDir, filename) if os.path.exists(filename): myfile = None myfile = utils.openFile(filename, 'r') #if myfile: myhtml = GetBodySoup(myfile) myfile.close() #else: # myhtml = "" else: myhtml = "" self.data['content'] = myhtml
def ShowModal(self): if isinstance(self.node, conman.conman.ConNode): filename = self.node.content.filename elif isinstance(self.node, ims.contentpackage.Item): resource = ims.utils.getIMSResourceForIMSItem(appdata.currentPackage, self.node) filename = eclassutils.getEClassPageForIMSResource(resource) if not filename: filename = resource.getFilename() self.filename = os.path.join(settings.ProjectDir, filename) if not os.path.exists(self.filename): global htmlpage file = utils.openFile(self.filename, "w") file.write(htmlpage) file.close() if False: size = wx.Display().ClientArea.Size size.x = size.x / 2 size.y = size.y / 2 print "size is %s" % size self.frame = EditorFrame(self.parent, self.filename, size=size) #self.frame.currentItem = self.currentItem self.frame.Show() self.frame.CentreOnScreen() else: guiutils.openInHTMLEditor(self.filename) return wx.ID_OK
def getTestFileToArray(): str = '' fp = utils.openFile('test.txt') for line in fp: str += line str = utils.toLower(str) return utils.extractWords(str)
def main(): while (True): contact_pages = set() emails = set() # Getting url from input try: url_input = input('\nWebsite url: ') except: print('An error has occurred') exit(1) # Checking input for other options if helper(url_input): path = input('Type file name: ') link_list = openFile(path) if link_list: for link in link_list: getEmails(link, contact_pages, emails) printResults(emails) else: continue else: # Call of our function to get emails getEmails(url_input, contact_pages, emails) # Print results printResults(emails) if emails: export = input('\nDo you want to export these emails? (y/n): ') if export.lower() == 'y' or export.lower() == 'yes': exportCSV(emails)
def getAlignmentPos(input_file, paired_end, output_file): """ Write, for each meaningful alignment, its first and last alignment position into file. Args: input_file: string, input alignment file in SAM or BAM format. paired_end: bool, True if input_file contains paired_end reads. output_file: a file name to which the data are written Notes: One line for each alignment, with two columns representing the first and last aligned position. For paired-end reads, the two segments are considered together and one-line will be output. """ almnt_file = utils.openFile(input_file) fout = open(output_file, "w") if not paired_end: for almnt in almnt_file: if not almnt.aligned: continue start = almnt.iv.start + 1 fout.write("%d\t%d\n" % (start, almnt.iv.end)) else: for first, second in HTSeq.pair_SAM_alignments(almnt_file, bundle=False): #if len(bundle) != 1: # skip multiple alignments # continue #first, second = bundle[0] if first.aligned and second.aligned and first.iv.chrom == second.iv.chrom: start = min(first.iv.start, second.iv.start) + 1 end = max(first.iv.end, second.iv.end) fout.write("%d\t%d\n" % (start, end)) fout.close()
def ApplyTemplate(self, template="default.meld", data={}): if not os.path.exists(template): template = os.path.join(settings.AppDir, "themes", themes.FindTheme(settings.ProjectSettings["Theme"]).themename, "default.meld") temp = utils.openFile(template, "r") html = temp.read() temp.close() charset = "utf-8" if 'charset' in self.data.keys(): charset = self.data['charset'] ext = os.path.splitext(template)[1] soup = BeautifulSoup(html) for key in data.keys(): value = data[key] key = key.lower() global metaTags if key in metaTags: tag = soup.find("meta", attrs={"http_equiv": key}) if not tag: tag = Tag(soup, "meta") tag['http-equiv'] = key soup.html.head.insert(0, tag) tag['content'] = value if key == 'name': soup.html.head.title.insert(0, value) elif key == 'content': soup.html.body.insert(0, value) html = soup.prettify(charset) return html
def UpdateContactInfo(self, name, role): """ Updates the contact's information, or adds them to the contact database if there's no info on the contact. """ if role == "": role = "Author" newcard = None if not name in appdata.vcards.keys(): newcard = vcard.VCard() newcard.fname.value = name newcard.filename = os.path.join(settings.PrefDir, "Contacts", fileutils.MakeFileName2(name) + ".vcf") myfile = utils.openFile(newcard.filename, "wb") myfile.write(newcard.asString()) myfile.close() appdata.vcards[newcard.fname.value] = newcard else: newcard = appdata.vcards[name] if isinstance(self.node, conman.conman.ConNode): hasPerson = False for person in self.content.metadata.lifecycle.contributors: if person.role == role: hasPerson = True person.entity = newcard if not hasPerson: contrib = conman.Contributor() contrib.role = role contrib.entity = newcard self.content.metadata.lifecycle.contributors.append(contrib)
def init(): '''Handles command line calls''' path = os.getcwd() cipherText = utils.openFile(path + '/' + sys.argv[1]) cipherText = utils.stripWhiteSpace(cipherText) key = getKey(cipherText) plainText = decryptText(cipherText, key) utils.writeFile(path + '/caesar_plain.txt', plainText)
def init(): '''Handles command line calls''' path = os.getcwd() cypherTxt = utils.openFile(path + '/' + sys.argv[1]) cypherTxt = utils.stripWhiteSpace(cypherTxt) # cypherText = ''.join(reversed(cypherTxt)) keyLength = ioc.estimateKeyLength(cypherTxt) key = findKey(cypherTxt, keyLength) print "Final Key: %s of length %d" % (key, keyLength) plainText = decryptText(cypherTxt, key) utils.writeFile(path + '/vigenere_plain.txt', plainText)
def uploadFile(self, sourcename, destname, callback=None): success = False myfile = utils.openFile(sourcename, "rb") bytes = os.path.getsize(sourcename) mtime = int(os.path.getmtime(sourcename)) rbytes, rmtime = self.getFileInfo(destname) if rmtime - self.clockskew >= mtime: # file is the same, consider it uploaded. return True else: print "Uploading %s" % sourcename self.connection.voidcmd('TYPE I') mysocket = self.connection.transfercmd('STOR ' + destname) self.filepercent = 0 if callback: callback.uploadFileStarted(destname) # TODO: What is this needed for? #callback.uploadFileProgressEvent(destname, self.filepercent) onepercent = bytes/100 if onepercent == 0: onepercent = 1 if mysocket: mysocket.setblocking(1) mysocket.settimeout(30) bytesuploaded = 0 while 1: if self.stopNow: break block = myfile.read(4096) if not block: break while block: try: resp = mysocket.sendall(block) bytesuploaded = bytesuploaded + len(block) block = None except socket.error, e: if e[0] != errno.EAGAIN: raise if callback: percent = int((float(bytesuploaded)/float(bytes))*100.0) callback.uploadFileProgressEvent(destname, percent) mysocket.close() mysocket = None
def init(): path = os.getcwd() text = utils.openFile(path + '/' + sys.argv[1]) if text is not None: text = utils.stripWhiteSpace(text) if len(sys.argv) > 2: subLength = int(sys.argv[2]) else: subLength = None result = getTwinIndex(text, subLength) print "Double Count: %s\nText Length: %s\nTwin Index: %s" % (result['dbl'], result['len'], result['ti'])
def PublishPages(self, node): page = "" if self.cancelled: return keepgoing = True #assuming no dialog to cancel, this must always be the case if self.progress: keepgoing = self.progress.Update(self.counter, _("Updating ") + node.title.text) if not keepgoing: result = wx.MessageDialog(self.parent, _("Are you sure you want to cancel publishing this EClass?"), _("Cancel Publishing?"), wx.YES_NO).ShowModal() if result == wx.ID_NO: self.cancelled = False self.progress.Resume() else: self.cancelled = True return self.counter = self.counter + 1 filename = eclassutils.getEditableFileForIMSItem(appdata.currentPackage, node) publisher = plugins.GetPublisherForFilename(filename) if publisher: resource = ims.utils.getIMSResourceForIMSItem(appdata.currentPackage, node) if resource: filename = resource.getFilename() try: publisher.node = node publisher.data['name'] = TextToHTMLChar(node.title.text) publisher.GetData() templatefile = os.path.join(settings.AppDir, "convert", "PDF.tpl") publisher.data['charset'] = publisher.GetConverterEncoding() myhtml = publisher.ApplyTemplate(templatefile, publisher.data) myhtml = publisher.EncodeHTMLToCharset(myhtml, publisher.data['charset']) #myhtml = publisher.Publish(self.parent, node, self.dir) #myhtml = GetBody(StringIO(myhtml)) #print "in PDF plugin, myhtml = " + myhtml[:100] if not myhtml == "": myfile = utils.openFile(os.path.join(self.tempdir, filename), "w") myfile.write(myhtml) myfile.close() self.files.append(os.path.join(self.tempdir, filename)) except: message = _("Could not publish page '%(page)s'") % {"page": os.path.join(self.tempdir, filename)} global log log.error(message) if "--debug" in sys.argv: raise if len(node.items) > 0: for child in node.items: self.PublishPages(child)
def test(): fp = utils.openFile('test.txt') str = utils.readFileToString(fp) n = utils.extractWords(str) array = ['Hello', 'hello', 'heLLO', 'Minh'] # print utils.arrayToLowerAndUnique(array) # print n # print helpers.getExamples()[1] # print helpers.getDocsByClass(2) # helpers.saveTerms("hello world", 1, 4.0) # print len(helpers.getVocabulary()) return
def copyDependentFilesAndUpdateLinks(oldfile, filename): myanalyzer = analyzer.ContentAnalyzer() myanalyzer.analyzeFile(filename) htmldir = os.path.dirname(oldfile) html = utils.openFile(filename, "r").read() encoding = GetEncoding(html) if encoding == None: encoding = utils.getCurrentEncoding() html = utils.makeUnicode(html, encoding) if not encoding: encoding = utils.guessEncodingForText(text) if encoding and encoding.lower() in ["windows-1252", "iso-8859-1", "iso-8859-2"]: html = convNotSoSmartQuotesToHtmlEntity(html) for link in myanalyzer.fileLinks: sourcefile = GetFullPathForURL(link, htmldir) if os.path.exists(sourcefile): sourcedir = os.path.dirname(sourcefile) htmlname = os.path.basename(filename) depName = os.path.basename(link) destLink = u"../File/" + htmlname + "_files/" + depName destdir = os.path.join(settings.ProjectDir, os.path.dirname(destLink[3:].replace("/", os.sep))) if not os.path.exists(destdir): os.makedirs(destdir) result = fileutils.CopyFile(depName, sourcedir, destdir) if result: html = html.replace(link, urllib.quote(destLink)) else: print "unable to copy file: " + sourcefile else: print "cannot find source file: " + sourcefile output = utils.openFile(filename, "w") output.write(html.encode(encoding)) output.close()
def SaveAsXML(self, filename, exporting=False): if exporting: self.exporting = True if filename == "": filename = self.filename else: self.filename = filename self.directory = os.path.dirname(filename) myxml = """<?xml version="1.0"?> <manifest identifier="%s" xmlns:imsmd="http://www.imsproject.org"> <metadata>%s</metadata> <organizations default="%s"> <organization identifier="%s" title="%s"> <title>%s</title> %s </organization> </organizations> <resources>%s </resources> </manifest> """ % (self.namespace + self.id, self._MetadataAsXML(), self.namespace + self.orgid, self.namespace + self.orgid, TextToXMLAttr(self.name), TextToXMLAttr(self.name), self._TOCAsXML(self.nodes[0]), self._ResourcesAsXML()) try: self.settings.SaveAsXML(os.path.join(self.directory, "settings.xml")) except: message = "There was an error saving the file " + os.path.join(self.directory, "settings.xml") + ". Please check to make sure you have write access to this file and try saving again." print message self.exporting = False raise IOError, message try: import types if type(myxml) != types.UnicodeType: import locale encoding = locale.getdefaultlocale()[1] myxml = unicode(myxml, encoding) myxml = myxml.encode("utf-8") myfile = utils.openFile(filename, "wb") myfile.write(myxml) myfile.close() except: message = "There was an error saving the file" + filename + ". Please check to make sure you have write access to this file and try saving again." print message self.exporting = False raise IOError, message self.exporting = False
def loadFromFile(path): data = None with utils.openFile(path) as f: data = json.load(f) if data["version"] != VERSION: raise RuntimeError("Incompatible file format version, should be %i" % VERSION) bones = {} for name, raw in data["bones"].items(): bone = SkinningBone(raw["name"]) bone.children = raw["children"] bone.parent = raw["parent"] image = raw.get("image") if image: bone.image = SkinnedImage(image["name"], image["x"], image["y"], image["width"], image["height"], image["originalWidth"], image["originalHeight"]) bone.pos = raw["pos"] bone.pivot = raw["pivot"] bone.translation = euclid.Vector3(*raw["translation"]) bone.rotation = euclid.Vector3(*raw["rotation"]) bone.scale = euclid.Vector3(*raw["scale"]) bone.zOrder = raw["zOrder"] bone.visible = raw["visible"] bone.blocker = raw["blocker"] bone.tessellate = raw["tessellate"] bone.transparency = raw["transparency"] bone.damping = raw["damping"] bone.points = [tuple(p) for p in raw["points"]] mesh = raw.get("mesh") if mesh: vertices = _getArray(gl.GLfloat, mesh, "vertices") indices = _getArray(gl.GLuint, mesh, "indices") boneWeights = _getArray(gl.GLfloat, mesh, "boneWeights") boneIndices = _getArray(gl.GLfloat, mesh, "boneIndices") bone.mesh = skinnedmesh.SkinnedMesh(vertices, indices, boneWeights, boneIndices) bones[bone.name] = bone return bones, data
def ImportTheme(self, event=None): import zipfile dialog = wx.FileDialog(self, _("Select Theme to Import"), "", "", _("Theme Files") + " (*.theme)|*.theme", wx.OPEN) if dialog.ShowModal() == wx.ID_OK: filename = dialog.GetPath() themezip = zipfile.ZipFile(filename, "r") files = themezip.infolist() for file in files: data = themezip.read(file.filename) if not os.path.exists(os.path.join(settings.AppDir, "themes", os.path.dirname(file.filename))): os.mkdir(os.path.join(settings.AppDir, "themes", os.path.dirname(file.filename))) file = utils.openFile(os.path.join(settings.AppDir, "themes", file.filename), "wb") file.write(data) file.close() self.ReloadThemes() wx.MessageBox(_("Theme imported successfully."))
def readGSMetadata(filename): doc = xml.dom.minidom.parse(utils.openFile(filename)) files = doc.getElementsByTagName("FileSet") metadata = {} def getNodeValue(node): if node and len(node.childNodes) > 0: return node.childNodes[0].nodeValue else: return "" for afile in files: filetags = afile.getElementsByTagName("FileName") if len(filetags) > 0: # greenstone <FileName> tags actually keep escape codes in them # for some reason filename = getNodeValue(filetags[0]).replace("\\\\", "/") filename = filename.replace("\\", "") # print "filename = %s" % filename file_metadata = FileMetadata() metadata_fields = afile.getElementsByTagName("Metadata") for field in metadata_fields: append = False name = "" for i in range(0, len(field.attributes)): attr = field.attributes.item(i) if attr.name == "mode": append = attr.value.lower() == "accumulate" if attr.name == "name": name = attr.value if name != "": value = getNodeValue(field) file_metadata.addMetadata(name, value, append) if filename != "": metadata[filename] = file_metadata return metadata
def SaveAsXML(self, filename="", encoding="ISO-8859-1"): """ Function: SaveAsXML(filename) Last Updated: 9/24/02 Description: Saves the Quiz to an XML file. Arguments: - filename: filename, including directory, of the XML file to save - if no value given, defaults to the filename used when loading the page Return values: Returns an error string if failed, or an empty string if successful. """ global log if filename == "": filename = self.filename else: self.filename = filename try: myxml = """<?xml version="1.0"?>%s""" % (self.WriteDoc()) except: message = _("There was an error updating the file %(filename)s. Please check to make sure you did not enter any invalid characters (i.e. Russian, Chinese/Japanese, Arabic) and try updating again.") % {"filename":filename} log.error(message) raise IOError, message try: import types if type(myxml) != types.UnicodeType: import locale encoding = locale.getdefaultlocale()[1] myxml = unicode(myxml, encoding) myxml = myxml.encode("utf-8") myfile = utils.openFile(filename, "w") myfile.write(myxml) myfile.close() except: message = utils.getStdErrorMessage("IOError", {"type":"write", "filename": filename}) log.error(message) raise IOError(message) return ""
def loadAnimationFromFile(path): data = None with utils.openFile(path) as f: data = json.load(f) if data["version"] != VERSION: raise RuntimeError( "Incompatible animation format version, should be %i" % VERSION) data = data["animation"] anim = SkinnedAnimation(data["name"]) anim.dirty = True anim.frames = [] for f in data["frames"]: frame = Frame() for name, key in f["keys"].items(): keyFrame = KeyFrame() keyFrame.translation = euclid.Vector3(*key["translation"]) keyFrame.rotation = euclid.Vector3(*key["rotation"]) keyFrame.scale = euclid.Vector3(*key["scale"]) keyFrame.visible = key["visible"] keyFrame.transparency = key["transparency"] checkJson(keyFrame, key) frame.keys[name] = keyFrame checkJson(frame, f) anim.frames.append(frame) anim.boneData = {} for name, entry in data["boneData"].items(): boneData = BoneData() boneData.repeat = entry["repeat"] boneData.reversed = entry["reversed"] boneData.easing = entry["easing"] checkJson(boneData, entry) anim.boneData[name] = boneData checkJson(anim, data) return anim
def loadObj(filename): verts = [] norms = [] uvs = [] vertsOut = [] normsOut = [] uvsOut = [] flipX = shader.config.flipMeshX for line in utils.openFile(filename): values = line.split() if values[0] == "v": v = map(float, values[1:4]) if flipX: v[0] = -v[0] verts.append(v) elif values[0] == "vt": t = map(float, values[1:3]) t[1] = 1.0 - t[1] #Flip texture y uvs.append(t) elif values[0] == "vn": n = map(float, values[1:4]) if flipX: n[0] = -n[0] norms.append(n) elif values[0] == "f": if len(values) != 4: raise RuntimeError("Mesh is not triangulated?") for face in values[1:]: pointers = face.split("/") vertsOut.append(list(verts[int(pointers[0]) - 1])) if pointers[1]: #Has texture coordinates uvsOut.append(list(uvs[int(pointers[1]) - 1])) else: uvsOut.append((0, 0)) normsOut.append(list(norms[int(pointers[2]) - 1])) return vertsOut, normsOut, uvsOut
def OnOK(self, event): if self.txtFullName.GetValue() == "": wx.MessageBox(_("You must enter a full name for this contact.")) return self.myvcard.fname.value = self.txtFullName.GetValue() self.myvcard.name.prefix = self.txtPrefix.GetValue() self.myvcard.name.suffix = self.txtSuffix.GetValue() self.myvcard.name.familyName = self.txtLastName.GetValue() self.myvcard.name.middleName = self.txtMiddleName.GetValue() self.myvcard.name.givenName = self.txtFirstName.GetValue() self.myvcard.title.value = self.txtTitle.GetValue() self.myvcard.organization.name = self.txtOrganization.GetValue() if len(self.myvcard.emails) == 0: self.myvcard.emails.append(vcard.Email()) self.myvcard.emails[0].value = self.txtEmail.GetValue() if self.myvcard.filename == "": prefdir = settings.PrefDir thisfilename = os.path.join(prefdir, "Contacts", fileutils.MakeFileName2(self.myvcard.fname.value) + ".vcf") if os.path.exists(thisfilename): result = wx.MessageDialog( self, _("A contact with this name already exists. Overwrite existing contact file?"), _("Overwrite contact?"), wx.YES_NO, ).ShowModal() if result == wx.ID_YES: self.myvcard.filename = thisfilename else: return else: self.myvcard.filename = thisfilename myfile = utils.openFile(self.myvcard.filename, "wb") myfile.write(self.myvcard.asString()) myfile.close() self.vcard = self.myvcard self.EndModal(wx.ID_OK)
def getContentPage(page, collection="", language="en"): contents_file = os.path.join("pages", language, "%s.html" % page) # local pages take precedence over system ones if collection != "": global manager indexdir = manager.getIndexProp(collection, "index_directory") fullpath = os.path.join(indexdir, contents_file) if os.path.exists(fullpath): contents_file = fullpath if not os.path.exists(contents_file): contents_file = os.path.join("library", contents_file) contents = "" try: contents = utils.openFile(contents_file, "r").read() except: sys.stderr.write("Could not load page %s" % contents_file) return contents
def getTemplateMeld(template, collection="", language="en"): template_file = os.path.join("templates", template, language, "template.html") # local templates take precedence over system ones if collection != "": global manager indexdir = manager.getIndexProp(collection, "index_directory") fullpath = os.path.join(indexdir, template_file) if os.path.exists(fullpath): template_file = fullpath if not os.path.exists(template_file): template_file = os.path.join("library", template_file) meld = None try: meld = PyMeld.Meld(utils.openFile(template_file, "r").read()) except: sys.stderr.write("Could not load template %s" % template_file) return meld
def LoadPage(self, filename): """ Function: LoadPage(filename) Last Updated: 12/26/02 Description: Loads an Quiz file in XML format Arguments: - filename: fully qualified filename, including directory, of the XML file to load Return values: Returns an empty string if successful, or an error string if failed. """ self.filename = filename self.directory = os.path.split(filename)[0] if USE_MINIDOM: doc = minidom.parse(utils.openFile(filename)) else: doc = FromXmlFile(filename) self.LoadDoc(doc) #except: # raise RuntimeError, `sys.exc_value.args` return ""
def OnNewTheme(self, event): dialog = wx.TextEntryDialog(self, _("Please type a name for your new theme"), _("New Theme"), _("New Theme")) if dialog.ShowModal() == wx.ID_OK: themedir = os.path.join(settings.AppDir, "themes") filename = string.replace(fileutils.MakeFileName2(dialog.GetValue()) + ".py", "-", "_") foldername = utils.createSafeFilename(dialog.GetValue()) try: os.mkdir(os.path.join(themedir, foldername)) except: message = _("Cannot create theme. Check that a theme with this name does not already exist, and that you have write access to the '%(themedir)s' directory.") % {"themedir":os.path.join(settings.AppDir, "themes")} self.parent.log.error(message) wx.MessageBox(message) return myfile = utils.openFile(os.path.join(themedir, filename), "w") data = """ from BaseTheme import * themename = "%s" import settings class HTMLPublisher(BaseHTMLPublisher): def __init__(self, parent): BaseHTMLPublisher.__init__(self, parent) self.themedir = os.path.join(settings.AppDir, "themes", themename) """ % (string.replace(dialog.GetValue(), "\"", "\\\"")) myfile.write(data) myfile.close() #copy support files from Default (no frames) fileutils.CopyFiles(os.path.join(themedir, "Default (no frames)"), os.path.join(themedir, foldername), 1) self.parent.themes.LoadThemes() self.ReloadThemes() dialog.Destroy()
def calculateStopCounts(input_file, references, paired_end): """ Calculate stop counts and coverage. Args: input_file: string, input alignment file in SAM or BAM format. references: dict, Dictionary for reference sequences, mapping from reference_name to the length of reference sequence. paired_end: bool, True if input_file contains paired_end reads. Returns: stop_counts: GenomicArray for stop counts. coverage: GenomicArray for coverage. """ almnt_file = utils.openFile(input_file) stop_counts = HTSeq.GenomicArray(references, stranded=False, typecode="i") coverage = HTSeq.GenomicArray(references, stranded=False, typecode="i") if not paired_end: for almnt in almnt_file: updateStopCountsForSingleEnd(almnt, stop_counts, coverage, False) else: for bundle in HTSeq.pair_SAM_alignments(almnt_file, bundle=True): if not bundle: # null case we pass only one or more continue #len(bundle) != 1: # continue #print (bundle[0]) first, second = bundle[0] ## allow multiple Jitu if first and second: ## must not have aligned Jitu if first.aligned and second.aligned and first.iv.chrom == second.iv.chrom: # both ends map to the same reference sequence updateStopCountsForPairedEnd(first, second, stop_counts, coverage) #else: # updateStopCountsForSingleEnd(first, stop_counts, coverage, True) # updateStopCountsForSingleEnd(second, stop_counts, coverage, True) return stop_counts, coverage
import utils as ut import disambiguation as dt import testsemcor as ts import nltk import re from prettytable import PrettyTable from nltk.corpus import semcor as sc sentences = ut.openFile('./txtFiles/sentences.txt') original_sentences = [] words_to_analyze = [] choosen_synsets = [] new_sentences = [] for s in sentences: keyword = s['word'] phrase = s['sentence'] best_sense = dt.lesk(phrase, keyword) #getting synonyms of the ambiguous word from its disambigued sense synonyms = str(best_sense.lemma_names()) #creating a new sentence replacing the ambiguous word with its synonyms new_sentence = phrase.replace(keyword, synonyms) #creating the lists of results words_to_analyze.append(keyword) original_sentences.append(phrase) choosen_synsets.append(best_sense.name()) new_sentences.append(new_sentence)
def read_construct_flow_field(direc, dict_files, dict_geometry): """ Construct a dictionary with the contents of the ASCII files split into relevant physical/spectral keys. INPUTS: direc: the directory where the solution files are dict_files: a dictionary containing the file names of the ascii files dict_geometry: a dictionary of the geometrical variables OUTPUTS: dict_flowField: a dictionary of the flow field stored in physical and spectral states. The physical flow field is stored as a 4D array: - (i, nx, ny, nz) The spectral flow field is stored as a 4D array: - (i, kx, ny, kz) """ dict_flowField = {} dict_flowField['is_physical'] = False dict_flowField['is_spectral'] = False if dict_files['physical'] == True: dict_flowField['is_physical'] = True # READ ut.printSectionHeader() ut.printSectionTitle('Reading physical ASCII file') f = ut.openFile(direc + '/' + dict_files['phy_asc']) # CONSTRUCT ut.message('Creating the flow field vector') # Create an empty 4D array to store the velocity data U = np.zeros((dict_geometry['physical']['Nd'], dict_geometry['physical']['Nx'], dict_geometry['physical']['Ny'], dict_geometry['physical']['Nz'])) for i, line in enumerate(f): values = line.split() nx = int(values[0]) ny = int(values[1]) nz = int(values[2]) nd = int(values[3]) vel = float(values[4]) U[nd, nx, ny, nz] = vel ut.message('Closing the physical ASCII file') f.close() dict_flowField['physical'] = U if dict_files['spectral'] == True: dict_flowField['is_spectral'] = True # READ ut.printSectionTitle('Reading spectral ASCII file') f = ut.openFile(direc + '/' + dict_files['spc_asc']) # CONSTRUCT ut.message('Creating the flow field vector') # Create an empty 4D array to store the velocity data U_hat = np.zeros((dict_geometry['spectral']['Nd'], dict_geometry['spectral']['kx'], dict_geometry['spectral']['Ny'], dict_geometry['spectral']['kz']), dtype=np.complex128) for i, line in enumerate(f): values = line.split() nd = int(values[0]) kx = int(values[1]) kz = int(values[2]) ny = int(values[3]) coeff = complex(values[4]) U_hat[nd, kx, ny, kz] = coeff ut.message('Closing the spectral ASCII file') f.close() dict_flowField['spectral'] = U_hat return dict_flowField
def read_construct_geometry(direc, dict_files): """ Construct a dictionary with all the geometry variables inside it. The top- level keys will be physical/spectral, then each value will contain a further dictionary which will store all the key value pairs found in the geo file. INPUTS: direc: the directory where the solution files are dict_files: a dictionary with all the solution files in it. OUTPUTS: var_geo: a dictionary of the geometrical variables that were contained in the geometry file. The key and value is exactly the same as in the file, i.e. the keys are the same labels as in the file. """ dict_geometry = {} if dict_files['physical'] == True: # READ ut.printSectionHeader() ut.printSectionTitle('Reading the physical geometry file') f = ut.openFile(direc + '/' + dict_files['phy_geo']) ut.message('Constructing geometry dictionary') var_geo = {} # CONSTRUCT for i, line in enumerate(f): values = line.split() if values[1] == '%Nx': var_geo['Nx'] = int(values[0]) elif values[1] == '%Ny': var_geo['Ny'] = int(values[0]) elif values[1] == '%Nz': var_geo['Nz'] = int(values[0]) elif values[1] == '%Nd': var_geo['Nd'] = int(values[0]) elif values[1] == '%Lx': var_geo['Lx'] = float(values[0]) elif values[1] == '%Lz': var_geo['Lz'] = float(values[0]) elif values[1] == '%lx=Lx/(2pi)': var_geo['lx'] = float(values[0]) elif values[1] == '%lz=Lz/(2pi)': var_geo['lz'] = float(values[0]) elif values[1] == '%alpha=2pi/Lx': var_geo['alpha'] = float(values[0]) elif values[1] == '%gamma=2pi/Lz': var_geo['gamma'] = float(values[0]) ut.message('Closing physical geometry file') f.close() var_geo['x'] = np.zeros((var_geo['Nx'])) var_geo['y'] = np.zeros((var_geo['Ny'])) var_geo['z'] = np.zeros((var_geo['Nz'])) for nx in range(0, var_geo['Nx']): var_geo['x'][nx] = nx * var_geo['Lx'] / var_geo['Nx'] for ny in range(0, var_geo['Ny']): var_geo['y'][ny] = math.cos(ny*math.pi/(var_geo['Ny']-1)) for nz in range(0, var_geo['Nz']): var_geo['z'][nz] = nz * var_geo['Lz'] / var_geo['Nz'] dict_geometry['is_physical'] = True dict_geometry['physical'] = var_geo else: dict_geometry['is_physical'] = False if dict_files['spectral'] == True: # READ ut.printSectionTitle('Reading the spectral geometry file') f = ut.openFile(direc + '/' + dict_files['spc_geo']) ut.message('Constructing geometry dictionary') spec_geo = {} # CONSTRUCT for i, line in enumerate(f): values = line.split() if values[1] == '%kx': spec_geo['kx'] = int(values[0]) if values[1] == '%kz': spec_geo['kz'] = int(values[0]) if values[1] == '%y': spec_geo['Ny'] = int(values[0]) if values[1] == '%Nd': spec_geo['Nd'] = int(values[0]) ut.message('Closing spectral geometry file') f.close() dict_geometry['is_spectral'] = True dict_geometry['spectral'] = spec_geo else: dict_geometry['is_spectral'] = False return dict_geometry
def init(): path = os.getcwd() cypherTxt = utils.openFile(path + '/' + sys.argv[1]) if cypherTxt is not None: print indexOfCoincidence(cypherTxt)
os.chdir(case_direc) # Loop through all files and find newton step flow fields whatWereLookingForIs = 'unewt' correctFileType = '.ff' files = [fi for fi in os.listdir(case_direc) if os.path.isfile(os.path.join(case_direc,fi))] files = sorted(files) init_cond = '/home/arslan/Documents/work/channelflow-related/set01/Re1200/KB/ampls-DNS-2015_10_25-further/wavepacket_007_4modes_(-1.51191822883+0j)/initialCondition/' initialFF = rc.main_read_construct(init_cond, [0,0,'all', 'all'], False) meanFile = ut.openFile(main_direc + '/data-skew/umean.asc') meanVec = np.zeros((initialFF['geometry']['physical']['Nd'], initialFF['geometry']['physical']['Nx'], initialFF['geometry']['physical']['Ny'], initialFF['geometry']['physical']['Nz'])) for u, line in enumerate(meanFile): values = line.split() nx = int(values[0]) ny = int(values[1]) nz = int(values[2]) nd = int(values[3]) vel = float(values[4]) if vel <= 1e-8: vel = 0.0 meanVec[nd, nx, ny, nz] = vel
def Publish(self): global log self.pdfdir = self.dir if os.path.exists(self.tempdir): try: shutil.rmtree(self.tempdir) except: log.error(_("Could not remove directory '%(dir)s'.") % {"dir": self.tempdir}) try: if isinstance(self.parent, wx.Frame): self.progress = wx.ProgressDialog("Publishing PDF", "Publishing PDF", self.parent.projectTree.GetCount() + 1, None, wx.PD_APP_MODAL | wx.PD_AUTO_HIDE | wx.PD_CAN_ABORT) self.counter = 1 self.PublishPages(appdata.currentPackage.organizations[0].items[0]) except: if self.progress: self.progress.Destroy() raise #self.myfile.close() lang = appdata.projectLanguage self.pdffile = os.path.join(self.pdfdir, MakeFileName2(appdata.currentPackage.metadata.lom.general.title[lang] + ".pdf")) bookfile = "#HTMLDOC\n" pdffile = self.pdffile if sys.platform == "win32": pdffile = string.replace(self.pdffile, "\\", "/") bookfile = bookfile + "-f \"" + pdffile + "\" -t pdf --no-toc --no-links --compression=9 --jpeg=90 --verbose\n" for afile in self.files: if afile != "" and os.path.exists(afile): if sys.platform == "win32": afile = afile.replace("\\", "/") bookfile = bookfile + afile + "\n" handle, bookpath = tempfile.mkstemp() #os.path.join(self.tempdir, "eclass.book") os.close(handle) try: book = utils.openFile(bookpath, "w") book.write(bookfile) book.close() os.rename(bookpath, bookpath + ".book") except: message = utils.getStdErrorMessage("IOError", {"type":"write", "filename":bookpath}) log.error(message) return False if sys.platform == "win32": htmldoc = os.path.join(settings.ThirdPartyDir, "htmldoc", "htmldoc.exe") else: htmldoc = os.path.join(settings.ThirdPartyDir, "htmldoc", "bin", "htmldoc") try: datadir = os.path.dirname(htmldoc) if sys.platform == "win32": # use quotes to avoid issues with spaces in filenames htmldoc = '"' + htmldoc + '"' datadir = '"' + datadir + '"' bookpath = '"' + bookpath + '"' else: bookpath = bookpath.replace(" ", "\\ ") datadir = datadir.replace(" ", "\\ ") #print 'Command is: ' + htmldoc + ' --datadir %s --batch %s' % (datadir, bookpath) command = htmldoc + " --datadir %s --batch %s" % (datadir, bookpath) result = wx.Execute(command, wx.EXEC_SYNC) if result == -1: message = _("Could not execute command '%(command)s'.") % {"command": command} log.error(message) wx.MessageBox(message) except: message = _("Could not publish PDF File.") log.error(message) if isinstance(self.parent, wx.Frame): wx.MessageBox(message + constants.errorInfoMsg) self.cancelled = True if self.progress: self.progress.Destroy() return not self.cancelled
import sys import math import pysam import HTSeq import utils import countReads2M # Python script to compute read stop counts # Modified from RNcode pipeline by Fei # https://gitlab.com/feideng/RNAcode input_file = sys.argv[1] output_count_file = sys.argv[2] almnt_file = utils.openFile(input_file) references = utils.getReference(input_file) paired_end = True stop_counts, coverage = countReads2M.calculateStopCounts( input_file, references, paired_end) countReads2M.writeStopCounts(stop_counts, coverage, references, output_count_file)
def LoadFromXML(self, filename): self.filename = filename self.directory = os.path.split(filename)[0] if os.path.exists(os.path.join(self.directory, "settings.xml")): self.settings.LoadFromXML(os.path.join(self.directory, "settings.xml")) self.content = ContentList() self.updatedids = {} self.nodes = [] try: myfile = utils.openFile(filename) data = myfile.read() myfile.close() if data.find("\x92") != -1 or data.find("\x93") != -1 or data.find("\x94") != -1: data = data.replace("encoding=\"iso8859-1\"", "encoding=\"cp1252\"") myfile = utils.openFile(filename, "w") myfile.write(data) myfile.close() print "Ugh, smart quotes..." if USE_MINIDOM: doc = minidom.parse(utils.openFile(filename)) else: doc = FromXmlFile(filename) except: return "The EClass project file cannot be loaded. The error message is: " + `sys.exc_value.args` manifest = doc.getElementsByTagName("manifest")[0] if manifest.attributes: for i in range(0, len(manifest.attributes)): attr = manifest.attributes.item(i) if attr.name == "identifier" and string.find(attr.value, self.namespace) != -1: self.id = string.replace(attr.value, self.namespace, "") self.id = string.replace(self.id, "-", "") if self.id == "": self.id = utils.getUUID() metadata = doc.getElementsByTagName("metadata")[0] if metadata.childNodes: if metadata.getElementsByTagName("PublicationID"): if metadata.getElementsByTagName("PublicationID")[0].childNodes: self.pubid = metadata.getElementsByTagName("PublicationID")[0].childNodes[0].nodeValue #print self.pubid else: self.pubid = "" imsmetadata = doc.getElementsByTagName("imsmd:General") self._GetMetadata(metadata) resources = doc.getElementsByTagName("resource") self._GetResources(resources) toc = doc.getElementsByTagName("tableofcontents") if not toc: toc = doc.getElementsByTagName("organization") if toc: toc = toc[0] else: toc = toc[0] if toc.attributes: for i in range(0, len(toc.attributes)): attr = toc.attributes.item(i) if attr.name == "identifier" and string.find(attr.value, self.namespace) != -1: self.orgid = string.replace(attr.value, self.namespace, "") self.orgid = string.replace(self.orgid, "-", "") if self.orgid == "": self.orgid = utils.getUUID() items = doc.getElementsByTagName("item")[0] self._GetNodes(items, None) self.CurrentNode = self.nodes[0] return ""
def IsEClass(self): myfile = utils.openFile(self.filename) data = myfile.read() myfile.close() return data.find("<PublicationID>") != -1
def __init__(self, *args, **kwargs): tk.Frame.__init__(self, *args, **kwargs) self.text = CustomText(self) self.vsb = tk.Scrollbar(orient="vertical", command=self.text.yview) self.text.configure(yscrollcommand=self.vsb.set, font=("Helvetica", "14", "bold")) self.linenumbers = TextLineNumbers(self, width=20) self.linenumbers.attach(self.text) self.vsb.pack(side="right", fill="y") self.text.pack(side="right", fill="both", expand=True) self.linenumbers.pack(side="right", fill="y") self.text.bind("<<Change>>", self._on_change) self.text.bind("<Configure>", self._on_change) self.console = CustomText(self) self.vsbConsole = tk.Scrollbar(command=self.console.yview) self.console.configure(yscrollcommand=self.vsbConsole.set, state=tk.DISABLED, background="black", foreground="green", font=("Helvetica", "12")) self.console.tag_config('error', foreground="red", font=("Helvetica", "12", "bold")) self.console.tag_config('normal', foreground="green", font=("Helvetica", "12")) self.linenumbersConsole = TextLineNumbers(self, width=20) self.linenumbersConsole.attach(self.console) self.vsbConsole.pack(side="right", fill="y") self.console.pack(side="right", fill="both", expand=True) self.linenumbersConsole.pack(side="right", fill="y") self.console.bind("<<Change>>", self._on_change_console) self.console.bind("<Configure>", self._on_change_console) menu = tk.Menu(root) root.config(menu=menu) fileMenu = tk.Menu(menu) menu.add_cascade(label="File", menu=fileMenu) fileMenu.add_command(label="New", command=lambda: utils.newFile(self.text)) fileMenu.add_command(label="Open", command=lambda: utils.openFile(root, self.text)) fileMenu.add_command(label="Save", command=lambda: utils.saveFile(self.text)) fileMenu.add_separator() fileMenu.add_command(label="Exit", command=utils.exitRoot) runMenu = tk.Menu(menu) menu.add_cascade(label="Run", menu=runMenu) runMenu.add_command(label="compile", command=lambda: compileOnly(self, self.assembler)) runMenu.add_command( label="compile and run", command=lambda: compileAndRun(self, self.assembler)) ramMenu = tk.Menu(menu) menu.add_cascade(label="Options", menu=ramMenu) ramMenu.add_command(label="Generate New RAM", command=utils.generateCleanMemory) ramMenu.add_command( label="Choose RAM File", command=lambda: utils.chooseRamFile(self.assembler)) ramMenu.add_command( label="Choose Debug directory", command=lambda: utils.chooseDebugDirectory(self.assembler)) helpMenu = tk.Menu(menu) menu.add_cascade(label="Help", menu=helpMenu) helpMenu.add_command(label="How To Use", command=utils.showHelp) helpMenu.add_command(label="About", command=utils.showAbout) sys.stdout = utils.PrintLogger(self.console)