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 __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 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, e: self.beep(e)
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 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 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
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): 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()
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 home = getenv('HOME')
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 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()
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())
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()
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())