def __init__(self): dataDir = Settings.dataDir + "WorkingWithText/ExtractTextFromAllPages/" # Open the target document pdf = Document(dataDir + "input1.pdf") # create TextAbsorber object to extract text text_absorber = TextAbsorber() # accept the absorber for all the pages pdf.getPages().accept(text_absorber) # In order to extract text from specific page of document, we need to specify the particular page using its index against accept(..) method. # accept the absorber for particular PDF page # pdfDocument.getPages().get_Item(1).accept(textAbsorber) # get the extracted text extracted_text = text_absorber.getText() # create a writer and open the file writer = FileWriter(File(dataDir + "extracted_text.out.txt")) writer.write(extracted_text) # write a line of text to the file # tw.WriteLine(extractedText) # close the stream writer.close() print "Text extracted successfully. Check output file."
def writePeakList(peakList, listName=None, folder="genpeaks"): if listName == None: listName = peakList.getName() fileName = os.path.join(folder, listName + '.xpk2') writer = FileWriter(fileName) peakWriter = PeakWriter() peakWriter.writePeaksXPK2(writer, peakList) writer.close()
def runTestsWithXMLResultAsFile(suite, file="jython_test.xml"): # Create writer fileWriter = FileWriter(file) pyFileWriter = PyFileWriter(fileWriter) # Run test runTestsWithXMLResult(suite, pyFileWriter) # Close writer pyFileWriter.close() fileWriter.close()
def export_calibration(imp, path): cal = imp.getCalibration() meta = dict(w=cal.pixelWidth, w_unit=cal.getXUnit(), h=cal.pixelHeight, h_unit=cal.getYUnit(), t=cal.frameInterval, t_unit=cal.getTimeUnit()) writer = FileWriter(path) Gson().toJson(meta, writer) writer.close() # important
def runTestsWithXMLResultAsFile(suite, file='jython_test.xml'): # Create writer fileWriter = FileWriter(file) pyFileWriter = PyFileWriter(fileWriter) # Run test runTestsWithXMLResult(suite, pyFileWriter) # Close writer pyFileWriter.close() fileWriter.close()
def run(file='jython_test.xml'): # Create writer fileWriter = FileWriter(file) pyFileWriter = PyFileWriter(fileWriter) # Run test suite = unittest.TestSuite([unittest.TestLoader().loadTestsFromTestCase(Test1)]) runner = xmlrunner.XMLTestRunner(pyFileWriter) s = runner.run(suite) # Close writer pyFileWriter.close() fileWriter.close()
def ig_dump_dot(dmuid, filename): igm = project.getIGM() uid = DMUID.parse(dmuid) module = igm.findModule(Toplevel(uid, None)) if module == None: printf('DM %s not found', dmuid) else: printf('Dumping %s...', module) dot = IG2DOT(module) dot.blacklistField("fImportedLibs") dot.blacklistField("fImportedPackages") dot.blacklistField("fZPrjID") dot.blacklistField("fSFDBID") dot.blacklistField("fLine") dot.blacklistField("fCol") dot.blacklistField("fScore") dot.blacklistField("fFailed") dot.blacklistField("fReject") dot.blacklistField("fInertial") dot.blacklistField("fDelay") out = PrintWriter(BufferedWriter(FileWriter(filename))) dot.convert(out) out.close() printf("python: wrote dot file to %s", filename)
def rtl_dump_svg(dmuid, filename): rtlmanager = project.getRTLM() uid = DMUID.parse(dmuid) rtlm = rtlmanager.findModule(Toplevel(uid, None)) if rtlm == None: printf('RTLM %s not found', dmuid) else: printf('Dumping %s...', rtlm) out = PrintWriter(BufferedWriter(FileWriter(filename))) gc = VGGCSVG(out) contentProvider = RTLVisualGraphContentProvider(rtlm) labelProvider = RTLVisualGraphLabelProvider(rtlm) selectionProvider = RTLVisualGraphSelectionProvider() layout = VGLayout(contentProvider, labelProvider, gc) layout.paint(selectionProvider) out.close() printf("python: wrote svg file to %s", filename)
def export_rois(rm, is_hyperstack, path): frame = (lambda roi: roi.getTPosition()) if is_hyperstack else ( lambda roi: roi.getPosition()) rois = rm.getRoisAsArray() polys = {} for roi in rois: fp = roi.getFloatPolygon() polys[roi.getName()] = { 't': frame(roi), 'x': fp.xpoints, 'y': fp.ypoints } writer = FileWriter(path) Gson().toJson(polys, writer) writer.close() # important
def testCSVPipe(self): """testing the CSV pipe""" from java.io import PrintWriter, FileWriter from com.ziclix.python.sql.pipe import Pipe from com.ziclix.python.sql.pipe.db import DBSource from com.ziclix.python.sql.pipe.csv import CSVSink try: src = self.connect() fn = tempfile.mktemp(suffix="csv") writer = PrintWriter(FileWriter(fn)) csvSink = CSVSink(writer) c = self.cursor() try: c.execute("insert into zxtesting (id, name, state) values (?, ?, ?)", [(1000, 'this,has,a,comma', 'and a " quote')]) c.execute("insert into zxtesting (id, name, state) values (?, ?, ?)", [(1001, 'this,has,a,comma and a "', 'and a " quote')]) # ORACLE has a problem calling stmt.setObject(index, null) c.execute("insert into zxtesting (id, name, state) values (?, ?, ?)", [(1010, '"this,has,a,comma"', None)], {2:zxJDBC.VARCHAR}) self.db.commit() finally: self.db.rollback() c.close() dbSource = DBSource(src, c.datahandler.__class__, "zxtesting", None, None, None) cnt = Pipe().pipe(dbSource, csvSink) - 1 # ignore the header row finally: writer.close() src.close() os.remove(fn)
def dm_dump_dot(dmuid, filename): dmm = project.getDUM() uid = DMUID.parse(dmuid) dm = dmm.getDM(uid) if dm == None: printf('DM %s not found', dmuid) else: printf('Dumping %s...', dm) dot = AST2DOT(dm, project.getZDB()) dot.blacklistField("fParent") # dot.blacklistField("fSource") # dot.blacklistField("fStartCol") # dot.blacklistField("fStartLine") dot.blacklistField("fEndCol") dot.blacklistField("fEndLine") dot.blacklistField("fDeclarationMap") out = PrintWriter(BufferedWriter(FileWriter(filename))) dot.convert(out) out.close() printf("python: wrote dot file to %s", filename)
def rawPython(a, b, c): """Execute the text as a new CPython process""" tmpFile = File.createTempFile("fieldRawPython", ".py", None) writer = BufferedWriter(FileWriter(tmpFile)) writer.write(b, 0, len(b)) writer.close() com = ExecuteCommand(".", ( "/System/Library/Frameworks/Python.framework/Versions/Current/bin/python", tmpFile.getAbsolutePath()), 1) com.waitFor(1) print com.getOutput()
def saveToFile(self, event): fileChooser = JFileChooser() if not (self.targetURL is None): fileChooser.setSelectedFile(File("Burp_SSL_Scanner_Result_%s.html" \ % (self.targetURL.getHost()))) else: fileChooser.setSelectedFile(File("Burp_SSL_Scanner_Result.html")) if (fileChooser.showSaveDialog(self.getUiComponent()) == JFileChooser.APPROVE_OPTION): fw = FileWriter(fileChooser.getSelectedFile()) fw.write(self.textPane.getText()) fw.flush() fw.close() print "Saved results to disk"
def run(self): import tempfile import os try: ps = PySelection(editor) doc = ps.getDoc() startLine = ps.getStartLineIndex() p1 = tempfile.mktemp() p2 = tempfile.mktemp() f1 = FileWriter(p1) formatAll = False if ps.getTextSelection().getLength() == 0: # format all. c = doc.get() f1.write(c) formatAll = True else: # format selection. #c = ps.getSelectedText() #f1.write(ps.getSelectedText()) print("Format selected text is not supported yet.") f1.write("") # A kind of solution is to insert a special comment in # front and end of selection text, pythontidy it, and # extract text according that comment. f1.close() os.system('PythonTidy.py "%s" "%s"' % (p1, p2)) f2 = open(p2, "r") result = f2.read() f2.close() os.remove(p1) os.remove(p2) if startLine >= doc.getNumberOfLines(): startLine = doc.getNumberOfLines() - 1 if formatAll: doc.set(result) else: #doc.replace(doc.getLineOffset(startLine), 0, result) pass sel = TextSelection(doc, doc.getLineOffset(startLine), 0) self.getTextEditor().getSelectionProvider().setSelection(sel) except java.lang.Exception as e: self.beep(e)
def save_xml(self, path , pretty = True): """ Saves the xml document to file """ self.backup_xml(path) format = None xmlwriter = None if pretty == True: format = OutputFormat.createPrettyPrint() format.setIndent(" ") format.setLineSeparator("\r\n") if format is None: xmlwriter = XMLWriter(FileWriter(path)) else: xmlwriter = XMLWriter(FileWriter(path), format) xmlwriter.write(self.xmldoc) xmlwriter.close()
def __init__(self): dataDir = Settings.dataDir + 'WorkingWithText/ExtractTextFromAllPages/' # Open the target document pdf = Document(dataDir + 'input1.pdf') # create TextAbsorber object to extract text text_absorber = TextAbsorber() # accept the absorber for all the pages pdf.getPages().accept(text_absorber) # In order to extract text from specific page of document, we need to specify the particular page using its index against accept(..) method. # accept the absorber for particular PDF page # pdfDocument.getPages().get_Item(1).accept(textAbsorber) #get the extracted text extracted_text = text_absorber.getText() # create a writer and open the file writer = FileWriter(File(dataDir + "extracted_text.out.txt")) writer.write(extracted_text) # write a line of text to the file # tw.WriteLine(extractedText) # close the stream writer.close() print "Text extracted successfully. Check output file."
def GenernateIndex(self): fileIndex = JitarRequestContext.getRequestContext().getServletContext().getRealPath("/") fileIndex = fileIndex + "index.html" try: file = File(fileIndex) fw = FileWriter(file, False) fw.write("<!doctype html>") fw.write("<html><head><meta http-equiv=\"content-type\" content=\"text/html; charset=UTF-8\"><title></title>") fw.close() finally: fw = None print fileIndex
def run(self): import tempfile import os try: ps = PySelection(editor) doc = ps.getDoc() startLine = ps.getStartLineIndex() p1 = tempfile.mktemp() p2 = tempfile.mktemp() f1 = FileWriter(p1) formatAll = False if ps.getTextSelection().getLength() == 0: # format all. c = doc.get() f1.write(c) formatAll = True else: # format selection. #c = ps.getSelectedText() #f1.write(ps.getSelectedText()) print "Format selected text is not supported yet." f1.write("") # A kind of solution is to insert a special comment in # front and end of selection text, pythontidy it, and # extract text according that comment. f1.close() os.system('PythonTidy.py "%s" "%s"' % (p1, p2)) f2 = open(p2, "r") result = f2.read() f2.close() os.remove(p1) os.remove(p2) if startLine >= doc.getNumberOfLines(): startLine = doc.getNumberOfLines() - 1 if formatAll: doc.set(result) else: #doc.replace(doc.getLineOffset(startLine), 0, result) pass sel = TextSelection(doc, doc.getLineOffset(startLine), 0) self.getTextEditor().getSelectionProvider().setSelection(sel) except java.lang.Exception, e: self.beep(e)
def writeLocations(self): f = Files.createExternalFile(Environment.DIRECTORY_DOWNLOADS, "WeatherForecast", "locations.txt", None, None) try: stream = FileWriter(f) for key in self.order: stream.write(self.locations[key] + "\n") stream.flush() stream.close() except FileNotFoundException: pass
def run(): print base = raw_input('Base file, eg. config/templates/wl_as_template.properties? ') env = raw_input('Environment, eg. local? ') print new_cfg = 'config/' + env + '/new_config.properties' input = BufferedReader(FileReader(base)) output = BufferedWriter(FileWriter(new_cfg)) output.write('base=' + base + '\n\n') line = input.readLine() while line is not None: if re.match('.*\?', line): output.write(line) output.newLine() line = input.readLine() input.close() output.close() log.info('new configuration file written to: ' + new_cfg)
def saveModels(mol, files, type): active = mol.getActiveStructures() if type == 'cif': mol.resetActiveStructures() if 'final' not in files[ -1]: #don't write out reference structure if in file list sNums = [i for i in range(len(files) - 1)] treeSet = TreeSet(sNums) mol.setActiveStructures(treeSet) molName = mol.getName() cifFile = os.path.join(os.getcwd(), molName + "_all.cif") out = FileWriter(cifFile) MMcifWriter.writeAll(out, molName) elif type == 'pdb': for (i, file) in zip(active, files): (dir, fileName) = os.path.split(file) newFileName = 'sup_' + fileName newFile = os.path.join(dir, newFileName) molio.savePDB(mol, newFile, i)
def actionPerformed(self,actionEvent): self.scl_long_tuneup_controller.getMessageTextField().setText("") fc = JFileChooser(constants_lib.const_path_dict["LINAC_WIZARD_FILES_DIR_PATH"]) fc.setDialogTitle("Save SCL Table data into ASCII file") fc.setApproveButtonText("Save") fl_filter = FileNameExtensionFilter("ASCII *.dat File",["dat",]) fc.setFileFilter(fl_filter) returnVal = fc.showOpenDialog(self.scl_long_tuneup_controller.linac_wizard_document.linac_wizard_window.frame) if(returnVal == JFileChooser.APPROVE_OPTION): fl_out = fc.getSelectedFile() fl_path = fl_out.getPath() if(fl_path.rfind(".dat") != (len(fl_path) - 4)): fl_out = File(fl_out.getPath()+".dat") buffer_out = BufferedWriter(FileWriter(fl_out)) txt = "# cav pos cav_amp_epics cav_amp_model cav_phase rf_gap_avg_phase" txt += " phase_offset real_offset eKin_in eKin_out " txt += " delta_eKin_in_out_keV bpm_eKin_out model_eKin_out delta_eKin_fit_keV E0TL_MeV" buffer_out.write(txt) buffer_out.newLine() buffer_out.flush() cav_wrappers = self.scl_long_tuneup_controller.cav_wrappers for cav_ind in range(len(cav_wrappers)): cav_wrapper = cav_wrappers[cav_ind] txt = str(cav_ind+1)+" " txt += cav_wrapper.cav.getId()+" %8.3f "%cav_wrapper.pos+" %12.5g "%cav_wrapper.initLiveAmp txt += " %12.5g "%cav_wrapper.designAmp + " %8.3f "%cav_wrapper.designPhase txt += " %8.3f "%cav_wrapper.avg_gap_phase + " %8.3f "%cav_wrapper.scanPhaseShift txt += " %8.3f "%cav_wrapper.real_scanPhaseShift txt += " %12.5g "%cav_wrapper.eKin_in + " %12.5g "%cav_wrapper.eKin_out dE = 0. if(cav_ind != 0): dE = (cav_wrapper.eKin_in - cav_wrappers[cav_ind-1].eKin_out)*1000. txt += " %12.5g "%dE + " %12.5g "%cav_wrapper.bpm_eKin_out+ " %12.5g "%cav_wrapper.model_eKin_out txt += "% 6.1f"%(1000.*cav_wrapper.eKin_err) E0TL = 0. if(len(cav_wrapper.energy_guess_harm_funcion.getParamArr()) > 1): E0TL = cav_wrapper.energy_guess_harm_funcion.getParamArr()[1] txt += " %12.5g "%E0TL buffer_out.write(txt) buffer_out.newLine() #---- end of writing buffer_out.flush() buffer_out.close()
def _testXMLPipe(self): """testing the XML pipe""" from java.io import PrintWriter, FileWriter from com.ziclix.python.sql.pipe import Pipe from com.ziclix.python.sql.pipe.db import DBSource from com.ziclix.python.sql.pipe.xml import XMLSink try: src = self.connect() fn = tempfile.mktemp(suffix="csv") writer = PrintWriter(FileWriter(fn)) xmlSink = XMLSink(writer) dbSource = DBSource(src, self.datahandler, "zxtesting", None, None, None) cnt = Pipe().pipe(dbSource, xmlSink) - 1 # ignore the header row finally: writer.close() src.close() os.remove(fn)
filename = sys.argv[1] print "Try to parse %s" % filename # Create a Parser of specified format format = sys.argv[2] builder = ChannelBuilder() if format == "0.91": parser = RSS_0_91_Parser(builder) else: parser = RSS_1_0_Parser(builder) # Read in Channel from File channel = parser.parse(File(filename).toURL()) # Create a File to marshal to writer = FileWriter("test-persist-channel.xml") # Load the mapping information from the file mapping = Mapping() mapping.loadMapping("../../src/de/nava/informa/impl/jdo/mapping.xml") # Marshal the channel object marshaller = Marshaller(writer) marshaller.setMapping(mapping) marshaller.marshal(channel) # ---- Unmarshalling # Create a Reader to the file to unmarshal from # reader = FileReader("test-persist-channel.xml")
import de.embl.cba.metadata.MetaData as MetaData import de.embl.cba.metadata.MetadataCreator as MetadataCreator import ij.IJ as IJ import org.yaml.snakeyaml.DumperOptions as DumperOptions import org.yaml.snakeyaml.Yaml as Yaml import java.io.FileWriter as FileWriter file = "/Volumes/cba/exchange/OeyvindOedegaard/yaml_project/01_TestFiles/20180627_LSM780M2_208_ibidi1_fcs_B_Posx96.lsm" metadataCreator = MetadataCreator(file) metadata = metadataCreator.getMetadata() dumperOptions = DumperOptions() dumperOptions.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK) outputPath = "/Volumes/cba/exchange/OeyvindOedegaard/yaml_project/test.yaml" yaml = Yaml(dumperOptions) writer = FileWriter(outputPath) yaml.dump(metadata, writer) writer.flush() writer.close() IJ.open(outputPath)
def process(self): print " * settings.py: formData=%s" % self.vc("formData") result = "{}" portalManager = Services.getPortalManager() portal = portalManager.get(self.vc("portalId")) func = self.vc("formData").get("func") if func == "view-update": portal.setDescription(self.vc("formData").get("view-description")) portal.setQuery(self.vc("formData").get("view-query")) portal.setSearchQuery(self.vc("formData").get("view-search-query")) portal.setRecordsPerPage(int(self.vc("formData").get("view-records-per-page"))) portal.setFacetCount(int(self.vc("formData").get("view-facet-count"))) portal.setFacetDisplay(int(self.vc("formData").get("view-facet-display"))) portal.setFacetSort(self.vc("formData").get("view-facet-sort") is not None) portalManager.save(portal) elif func == "general-update": config = JsonSimpleConfig() email = StringUtils.trimToEmpty(self.vc("formData").get("general-email")) systemEmail = StringUtils.trimToEmpty(config.getString(None, ["email"])) if systemEmail != email: obj = config.writableSystemConfig() obj.put("email", self.vc("formData").get("general-email")) obj.put("configured", "true") config.storeSystemConfig() # mark restart Services.getHouseKeepingManager().requestUrgentRestart() else: print " * settings.py: email not updated: did not change" self.throw_error("Email address is the same! No change saved.") elif func == "facets-update": portal.getObject(["portal"]).remove("facet-fields") fields = self.vc("formData").getValues("field") labels = self.vc("formData").getValues("label") displays = self.vc("formData").getValues("display") deletes = self.vc("formData").getValues("delete") for i in range(0, len(fields)): field = fields[i] if deletes[i] == "false": node = portal.writeObject(["portal", "facet-fields", field]) node.put("label", labels[i]) node.put("display", displays[i]) portalManager.save(portal) elif func == "sort-update": portal.getObject(["portal"]).remove("sort-fields") default = self.vc("formData").get("default") if default: portal.setSortFieldDefault(default) order = self.vc("formData").get("order") if order: portal.setSortFieldDefaultOrder(order) fields = self.vc("formData").getValues("field") labels = self.vc("formData").getValues("label") deletes = self.vc("formData").getValues("delete") for i in range(0, len(fields)): field = fields[i] if deletes[i] == "false": node = portal.writeObject(["portal", "sort-fields"]) node.put(field, labels[i]) portalManager.save(portal) elif func == "watcher-update": configFile = self.getWatcherFile() if configFile is not None: json = JsonSimpleConfig(configFile) pathIds = self.vc("formData").get("pathIds").split(",") actives = self.vc("formData").getValues("watcher-active") if actives is None: actives = [] deletes = self.vc("formData").getValues("watcher-delete") if deletes is None: deletes = [] for pathId in pathIds: if pathId not in deletes: path = self.vc("formData").get("%s-path" % pathId) stopped = str(pathId not in actives).lower() watchDir = json.writeObject(["watcher", "watchDirs", path]) watchDir.put("ignoreFileFilter", self.vc("formData").get("%s-file" % pathId)) watchDir.put("ignoreDirectories", self.vc("formData").get("%s-dir" % pathId)) watchDir.put("stopped", stopped) json.writeArray(["watcher", "watchDirs", path, "cxtTags"]) writer = FileWriter(configFile) writer.write(json.toString(True)) writer.close() else: result = "The Watcher is not installed properly." elif func == "restore-default-config": # backup the file JsonSimpleConfig.backupSystemFile() # delete the file JsonSimpleConfig.getSystemFile().delete() # restore default JsonSimpleConfig.getSystemFile() # mark restart Services.getHouseKeepingManager().requestUrgentRestart() elif func == "housekeeping-update": config = JsonSimpleConfig() freq = StringUtils.trimToEmpty(self.vc("formData").get("housekeeping-timeout")) systemFreq = StringUtils.trimToEmpty(config.getString(None, ["portal", "houseKeeping", "config", "frequency"])) result = "House Keeper refreshed" if systemFreq != freq: # Get writeable access to underlying system sysConfig = JsonSimple(config.writableSystemConfig()) # Modify the house keeping node hkConfig = sysConfig.writeObject(["portal", "houseKeeping", "config"]) hkConfig.put("frequency", freq) # Write the underlying config back to disk config.storeSystemConfig() result = "Frequency updated, refreshing House Keeper" # Refresh the HouseKeeper message = JsonObject() message.put("type", "refresh") Services.getHouseKeepingManager().sendMessage(message.toString()) self.writer.println(result) self.writer.close()
def __processRequest(self): depositUrl = "%s/sword/deposit.post" % portalPath sword = SwordSimpleServer(depositUrl) try: p = self.vc("request").path.split( self.vc("portalId") + "/" + self.vc("pageName") + "/")[1] # portalPath except: p = "" if p == "post": print "\n--- post ---" c = sword.getClient() c.clearProxy() c.clearCredentials() postMsg = sword.getPostMessage() postMsg.filetype = "application/zip" postMsg.filepath = "/home/ward/Desktop/Test.zip" depositResponse = c.postFile(postMsg) return str(depositResponse) elif p == "servicedocument": #print "\n--- servicedocument ---" sdr = sword.getServiceDocumentRequest() sdr.username = self.vc("formData").get("username", "test") sdr.password = self.vc("formData").get("password", "test") if self.vc("formData").get("test"): depositUrl += "?test=1" sd = sword.doServiceDocument(sdr) # get a serviceDocument out = self.vc("response").getPrintWriter("text/xml; charset=UTF-8") out.println(str(sd)) out.close() self.velocityContext["pageName"] = "-noTemplate-" return sd elif p == "deposit.post": #print "\n--- deposit --- formData='%s'" % str(formData) inputStream = self.vc("formData").getInputStream() headers = {} for x in self.vc("formData").getHeaders().entrySet(): headers[x.getKey()] = x.getValue() deposit = sword.getDeposit() noOp = headers.get("X-No-Op") or "false" deposit.noOp = (noOp.lower()=="true") or \ (formData.get("test") is not None) contentDisposition = headers.get("Content-Disposition", "") filename = "" if contentDisposition != "": try: filename = contentDisposition.split("filename=")[1] deposit.filename = filename except: pass slug = headers.get("Slug") if slug is not None and slug != "": deposit.slug = slug #elif filename!="": # deposit.slug = filename deposit.username = "******" deposit.password = deposit.username try: file = File.createTempFile("tmptf", ".zip") file.deleteOnExit() fos = FileOutputStream(file.getAbsolutePath()) IOUtils.copy(inputStream, fos) fos.close() print "copied posted data to '%s'" % file.getAbsolutePath() except Exception, e: print "--- Exception - '%s'" % str(e) deposit.contentDisposition = file.getAbsolutePath() #???? deposit.file = inputStream depositResponse = sword.doDeposit(deposit) id = str(depositResponse.getEntry().id) try: print #imsPlugin = PluginManager.getTransformer("ims") jsonConfig = JsonConfig() #imsPlugin.init(jsonConfig.getSystemFile()) #harvestClient = HarvestClient(jsonConfig.getSystemFile()); storagePlugin = PluginManager.getStorage( jsonConfig.get("storage/type")) #storagePlugin.init(jsonConfig.getSystemFile()) setConfigUri = self.__getPortal().getClass().getResource( "/swordRule.json").toURI() configFile = File(setConfigUri) harvestConfig = JsonConfigHelper(configFile) tFile = File.createTempFile("harvest", ".json") tFile.deleteOnExit() harvestConfig.set("configDir", configFile.getParent()) harvestConfig.set("sourceFile", file.getAbsolutePath()) harvestConfig.store(FileWriter(tFile)) zipObject = GenericDigitalObject(id) zipObject.addPayload(FilePayload(file, id)) #digitalObject = imsPlugin.transform(zipObject, file) qStorage = QueueStorage(storagePlugin, tFile) qStorage.init(jsonConfig.getSystemFile()) qStorage.addObject(zipObject) if deposit.noOp: print "-- Testing noOp='true' --" else: # deposit the content pass except Exception, e: print "---" print " -- Exception - '%s'" % str(e) print "---"
if i.strip() != '' ] de_vocab = [ i.strip() for i in codecs.open(options.de_vocab, 'r', 'utf8').readlines() if i.strip() != '' ] for env in en_vocab: add_to_tags(env) uc_training = UnCachedFgList(training_instanes=training_ti, en_vocab=en_vocab) for idx, ti in enumerate(training_ti): print idx, uc_training.get(idx) trainer = CrfTrainer(get_trainer_prm()) exit(1) feature_ids, feature_labels = zip( *sorted([(v, k) for k, v in feature_label2id.iteritems()])) # initialize weight for each feature factor_graph_model = FgModel(len(feature_label2id), list(feature_labels)) for fid in list(feature_ids): factor_graph_model.add(fid, 0.0) trainer.train(factor_graph_model, uc_training) sw = FileWriter('feature.weights') factor_graph_model.printModel(sw) sw = codecs.open('feature.names', 'w', 'utf8') for k, i in feature_label2id.iteritems(): sw.write(str(i) + '\t' + str(k) + '\n') sw.flush() sw.close()
def process(self): self.log.debug(" * settings.py: formData={}", self.vc("formData")) valid = self.vc("page").csrfSecurePage() if not valid: self.throw_error("Invalid request") return result = "{}" portalManager = self.vc("Services").getPortalManager() portal = portalManager.get(self.vc("portalId")) func = self.vc("formData").get("func") if func == "view-update": portal.setDescription(self.vc("formData").get("view-description")) portal.setQuery(self.vc("formData").get("view-query")) portal.setSearchQuery(self.vc("formData").get("view-search-query")) portal.setRecordsPerPage( int(self.vc("formData").get("view-records-per-page"))) portal.setFacetCount( int(self.vc("formData").get("view-facet-count"))) portal.setFacetDisplay( int(self.vc("formData").get("view-facet-display"))) portal.setFacetSort( self.vc("formData").get("view-facet-sort") is not None) portalManager.save(portal) elif func == "general-update": config = JsonSimpleConfig() email = StringUtils.trimToEmpty( self.vc("formData").get("general-email")) systemEmail = StringUtils.trimToEmpty( config.getString(None, ["email"])) if systemEmail != email: obj = config.writableSystemConfig() obj.put("email", self.vc("formData").get("general-email")) obj.put("configured", "true") config.storeSystemConfig() # mark restart self.vc("Services").getHouseKeepingManager( ).requestUrgentRestart() else: self.log.debug( " * settings.py: email not updated: did not change") self.throw_error("Email address is the same! No change saved.") elif func == "facets-update": portal.getObject(["portal"]).remove("facet-fields") fields = self.vc("formData").getValues("field") labels = self.vc("formData").getValues("label") displays = self.vc("formData").getValues("display") deletes = self.vc("formData").getValues("delete") for i in range(0, len(fields)): field = fields[i] if deletes[i] == "false": node = portal.writeObject( ["portal", "facet-fields", field]) node.put("label", labels[i]) node.put("display", displays[i]) portalManager.save(portal) elif func == "sort-update": portal.getObject(["portal"]).remove("sort-fields") default = self.vc("formData").get("default") if default: portal.setSortFieldDefault(default) order = self.vc("formData").get("order") if order: portal.setSortFieldDefaultOrder(order) fields = self.vc("formData").getValues("field") labels = self.vc("formData").getValues("label") deletes = self.vc("formData").getValues("delete") for i in range(0, len(fields)): field = fields[i] if deletes[i] == "false": node = portal.writeObject(["portal", "sort-fields"]) node.put(field, labels[i]) portalManager.save(portal) elif func == "watcher-update": configFile = self.getWatcherFile() if configFile is not None: json = JsonSimpleConfig(configFile) pathIds = self.vc("formData").get("pathIds").split(",") actives = self.vc("formData").getValues("watcher-active") if actives is None: actives = [] deletes = self.vc("formData").getValues("watcher-delete") if deletes is None: deletes = [] for pathId in pathIds: if pathId not in deletes: path = self.vc("formData").get("%s-path" % pathId) stopped = str(pathId not in actives).lower() watchDir = json.writeObject( ["watcher", "watchDirs", path]) watchDir.put( "ignoreFileFilter", self.vc("formData").get("%s-file" % pathId)) watchDir.put( "ignoreDirectories", self.vc("formData").get("%s-dir" % pathId)) watchDir.put("stopped", stopped) json.writeArray( ["watcher", "watchDirs", path, "cxtTags"]) writer = FileWriter(configFile) writer.write(json.toString(True)) writer.close() else: result = "The Watcher is not installed properly." elif func == "restore-default-config": # backup the file JsonSimpleConfig.backupSystemFile() # delete the file JsonSimpleConfig.getSystemFile().delete() # restore default JsonSimpleConfig.getSystemFile() # mark restart self.vc("Services").getHouseKeepingManager().requestUrgentRestart() elif func == "housekeeping-update": config = JsonSimpleConfig() freq = StringUtils.trimToEmpty( self.vc("formData").get("housekeeping-timeout")) systemFreq = StringUtils.trimToEmpty( config.getString( None, ["portal", "houseKeeping", "config", "frequency"])) result = "House Keeper refreshed" if systemFreq != freq: # Get writeable access to underlying system sysConfig = JsonSimple(config.writableSystemConfig()) # Modify the house keeping node hkConfig = sysConfig.writeObject( ["portal", "houseKeeping", "config"]) hkConfig.put("frequency", freq) # Write the underlying config back to disk config.storeSystemConfig() result = "Frequency updated, refreshing House Keeper" # Refresh the HouseKeeper message = JsonObject() message.put("type", "refresh") self.vc("Services").getHouseKeepingManager().sendMessage( message.toString()) self.writer.println(result) self.writer.close()
ti, obs, guess = get_instance(line) training_ti.append(ti) for line in open(options.test_file).readlines(): ti, obs, guess = get_instance(line) testing_ti.append(ti) en_vocab = [i.strip() for i in codecs.open(options.en_vocab, 'r', 'utf8').readlines() if i.strip() != ''] de_vocab = [i.strip() for i in codecs.open(options.de_vocab, 'r', 'utf8').readlines() if i.strip() != ''] for env in en_vocab: add_to_tags(env) uc_training = UnCachedFgList(training_instanes=training_ti, en_vocab=en_vocab) for idx, ti in enumerate(training_ti): print idx, uc_training.get(idx) trainer = CrfTrainer(get_trainer_prm()) exit(1) feature_ids, feature_labels = zip(*sorted([(v, k) for k, v in feature_label2id.iteritems()])) # initialize weight for each feature factor_graph_model = FgModel(len(feature_label2id), list(feature_labels)) for fid in list(feature_ids): factor_graph_model.add(fid, 0.0) trainer.train(factor_graph_model, uc_training) sw = FileWriter('feature.weights') factor_graph_model.printModel(sw) sw = codecs.open('feature.names', 'w', 'utf8') for k, i in feature_label2id.iteritems(): sw.write(str(i) + '\t' + str(k) + '\n') sw.flush() sw.close()
from java.io import FileWriter from java.io import File from java.io import FileReader f1 = File("file2.txt") fr = FileReader(f1) f2 = File("file3.txt") fw = FileWriter(f2) while True: ch = fr.read() if ch != -1: fw.write(ch) else: break fr.close() fw.close()
def run(self): import tempfile import os import traceback logger = FormatterLogger( self._editor.getEditorFile().getName()) try: # PySelection(editor) ps = self._editor.createPySelection() doc = ps.getDoc() startLine = ps.getStartLineIndex() tmp_src = tempfile.mktemp() tmp_src_fileWriter = FileWriter(tmp_src) formatAll = False if ps.getTextSelection().getLength() == 0: # format all. c = doc.get() tmp_src_fileWriter.write(c) formatAll = True else: # format selection. # c = ps.getSelectedText() # tmp_src_fileWriter.write(ps.getSelectedText()) logger.warn( 'Format selected text is not supported yet.') tmp_src_fileWriter.write('') # A kind of solution is to insert a special comment in # front and end of selection text, pythontidy it, and # extract text according that comment. tmp_src_fileWriter.close() PythonTidyAction.apply_formatting( tmp_src, self._editor.getEditorFile().getName(), logger) resulting_file = open(tmp_src, 'r') result = resulting_file.read() resulting_file.close() os.remove(tmp_src) if startLine >= doc.getNumberOfLines(): startLine = doc.getNumberOfLines() - 1 if formatAll: doc.set(result) else: # doc.replace(doc.getLineOffset(startLine), 0, result) pass if startLine >= doc.getNumberOfLines(): startLine = doc.getNumberOfLines() - 1 self._editor.selectAndReveal( doc.getLineOffset(startLine), 0) except java.lang.Exception as e: self.beep(e) logger.error(traceback.format_exc()) except: logger.error(traceback.format_exc())
val minDate = LocalDate.of(2003, 1, 1) val maxDate = LocalDate.of(2015, 5, 14) val dateFormatter = DateTimeFormatter.ISO_LOCAL_DATE val urlTemplate = "https://www.wunderground.com/history/airport/KSFO/%d/%d/%d/DailyHistory.html?format=1" val urlsQeueue = mutable.Queue( Util.daysBetween(minDate, maxDate) .map { d => val formattedDate = d.format(dateFormatter) (formattedDate, urlTemplate.format(d.getYear, d.getMonthValue, d.getDayOfMonth)) }: _* ) require(urlsQeueue.nonEmpty, "there should be URLs to query") val fileWriter = new FileWriter("sfCrime/src/main/resources/weather.json") fileWriter.write("[\n") val timer = new JavaTimer(isDaemon = false) lazy val task: TimerTask = timer.schedule(Duration.fromSeconds(5)) { val (date, url) = urlsQeueue.dequeue() Try(Source.fromURL(url).getLines().drop(2)) match { case Success(lines) => val wds = lines.map(WeatherData.parseWeatherData).toSeq val wdd = WeatherDataDate.buildWeatherDataDate(date, wds) val json = wdd.asJson.spaces2 if (urlsQeueue.isEmpty) { task.cancel() timer.stop() fileWriter.write(s"$json\n") fileWriter.write("]") fileWriter.close()
# A gl user testing framework # import java from java.io import PrintWriter, FileReader, FileWriter, File from java.lang import System from org.fenfire.swamp import Graphs from com.hp.hpl.mesa.rdf.jena.model import Model from com.hp.hpl.mesa.rdf.jena.mem import ModelMem model = ModelMem() file = "exper.rdf" if File(file).exists(): print "Reading", file model.read(FileReader(file), "") else: print file, "not found" # TODO: read a graph from a file graph = Graphs.toGraph(model) model = Graphs.toModel(graph) print "Writing", file model.write(FileWriter(file))
result = pluginsTreeToString() if uploadToWiki or compareToWiki: from fiji import MediaWikiClient client = MediaWikiClient(URL) wiki = client.sendRequest(['title', PAGE, 'action', 'edit'], None) begin = wiki.find('<textarea') begin = wiki.find('>', begin) + 1 end = wiki.find('</textarea>', begin) wiki = wiki[begin:end].replace('<', '<') if wiki != result: if compareToWiki: from fiji import SimpleExecuter from java.io import File, FileWriter file1 = File.createTempFile('PluginList', '.wiki') writer1 = FileWriter(file1) writer1.write(wiki) writer1.close() file2 = File.createTempFile('PluginList', '.wiki') writer2 = FileWriter(file2) writer2.write(result) writer2.close() diff = SimpleExecuter(['git', 'diff', '--patience', '--no-index', '--src-prefix=wiki/', '--dst-prefix=local/', file1.getAbsolutePath(), file2.getAbsolutePath()]) file1.delete() file2.delete() print diff.getOutput() else: # get username and password user = None password = None from os import getenv, path
import java.io.FileWriter import scala.io.Source val in = "/Users/daria/Downloads/C-small-attempt3.in" //val in = "/Users/daria/Downloads/in.txt" val out = new FileWriter("/Users/daria/Downloads/out.txt") val lines = Source.fromFile(in).getLines().toList var T = lines(0).toInt for (i <- 1 to T) { val offset = 1 + (i - 1) * 2 val t = lines(offset).split(" ") val L = t(0).toInt val X = t(1).toInt val ch = lines(offset+1).toArray.map(c => c.toString) val r = "\nCase #%d: %s\n".format(i, if (process(L, X, ch)) "YES" else "NO") out.write(r) println(r) } out.close() val step = Map( "1" -> Map("1" -> "1", "i" -> "i", "j" -> "j", "k" -> "k"), "i" -> Map("1" -> "i", "i" -> "-1", "j" -> "k", "k" -> "-j"), "j" -> Map("1" -> "j", "i" -> "-k", "j" -> "-1", "k" -> "i"), "k" -> Map("1" -> "k", "i" -> "j", "j" -> "-i", "k" -> "-1"), "-1" -> Map("1" -> "-1", "i" -> "-i", "j" -> "-j", "k" -> "-k"), "-i" -> Map("1" -> "-i", "i" -> "1", "j" -> "-k", "k" -> "j"), "-j" -> Map("1" -> "-j", "i" -> "k", "j" -> "1", "k" -> "-i"), "-k" -> Map("1" -> "-k", "i" -> "-j", "j" -> "i", "k" -> "1") ) def process(L:Int, X: Int, ch: Array[String]): Boolean = {