def process(self, input_stream, output_stream): try: writer = OutputStreamWriter(output_stream, "UTF-8") reader = BufferedReader(InputStreamReader(input_stream, "UTF-8")) line = reader.readLine() found_quote_flag = False while line != None: # Write the line always writer.write(line) # If there is an odd number of double quotes (free text field) # then there is a newline somewhere in one of the column fields. # Raise the flag to not add any newlines until we've found the matching # line that contains an odd number of quotes if line.count('"') % 2 != 0: found_quote_flag = not found_quote_flag if not found_quote_flag: writer.write('\n') line = reader.readLine() writer.flush() writer.close() reader.close() except: traceback.print_exc(file=sys.stdout) raise
def __init__(self, dump_path, dsl_pipe): self.dump_path = dump_path if not os.path.exists(dsl_pipe): raise Exception(""" The path to pipe for DSL reporter %s does not exists. Make sure SPADE is running and DSL reporter has been setup. For more infromation, take a look at http://code.google.com/p/data-provenance/wiki/Pipe""" ) self.pipe = BufferedWriter( OutputStreamWriter(FileOutputStream(dsl_pipe))) self.logger = logging.getLogger(self.__class__.__name__) self.user_data = self._read_json_data("me_data") self.user_newsfeed = self._read_json_data("me_newsfeed") self.friends = dict() self.created_user_nodes = set() for f in self._read_json_data("me_friends"): fuid = f['id'] try: self.friends[fuid] = self._read_json_data("%s_info" % fuid) except IOError, e: logger.info( "Skipping data for friend %s; unable to read data" % f['name']) self.friends[fuid] = f
def GenSubjectNav(self): subjectService = __spring__.getBean("subjectService") mGradeId = subjectService.getGradeIdList() MetaGrade = ArrayList() metaSubject = ArrayList() for grade in mGradeId: mGrade = subjectService.getGrade(int(grade)) MetaGrade.add(mGrade) subj = subjectService.getSubjectByGradeId(int(grade)) m = ArrayList() if subj != None: for sj in range(0, subj.size()): m.add(subj[sj].metaSubject) metaSubject.add({"gradeName" : mGrade.gradeName, "gradeId" : grade, "metaSubject" : m }) map = HashMap() map.put("metaGrade", MetaGrade) map.put("meta_Grade", MetaGrade) map.put("SubjectNav", metaSubject) map.put("SiteUrl", CommonUtil.getContextUrl(request)) templateProcessor = __spring__.getBean("templateProcessor") str = templateProcessor.processTemplate(map, "/WEB-INF/ftl/site_subject_nav.ftl", "utf-8") rootPath = JitarRequestContext.getRequestContext().getServletContext().getRealPath("/") fileIndex = rootPath + "html" + File.separator + "subject_nav.html" try: file = File(fileIndex) fw = OutputStreamWriter(FileOutputStream(file), "utf-8") # fw = FileWriter(file, False) fw.flush() fw.write(str) fw.close() finally: file = None fw = None
def process(self, inputStream, outputStream): try: self.total = 0 reader = InputStreamReader(inputStream, "UTF-8") bufferedReader = BufferedReader(reader) writer = OutputStreamWriter(outputStream, "UTF-8") line = bufferedReader.readLine() while line != None: ChangedRec = line.upper() writer.write(ChangedRec) writer.write('\n') a = line.split(",") for valu in a: b = valu.strip() self.total += int(b) line = bufferedReader.readLine() print("Summation of Records are %s ", self.total) writer.flush() writer.close() reader.close() bufferedReader.close() except: print "Exception in Reader:" print '-' * 60 traceback.print_exc(file=sys.stdout) print '-' * 60 raise session.transfer(flowFile, ExecuteScript.REL_FAILURE) finally: if bufferedReader is not None: bufferedReader.close() if reader is not None: reader.close()
def test_calc_blob(self): cursor = tBlobCursor(self.context) cursor.deleteAll() cursor.insert() cursor.get(1) self.assertEquals(1, cursor.id) self.assertEquals(None, cursor.dat) cursor.calcdat() self.assertTrue(cursor.dat and cursor.dat.isNull()) os = cursor.dat.getOutStream() osw = OutputStreamWriter(os, 'utf-8') try: osw.append('blob field') finally: osw.close() cursor.update() cursor.clear() cursor.get(1) cursor.calcdat() bf = BufferedReader( InputStreamReader(cursor.dat.getInStream(), 'utf-8')) self.assertEquals('blob field', bf.readLine()) bf.close() cursor.clear() cursor.calcdat() os = cursor.dat.getOutStream() osw = OutputStreamWriter(os, 'utf-8') try: osw.append('blob field 2!') finally: osw.close() cursor.insert() cursor.clear() cursor.get(2) cursor.calcdat() bf = BufferedReader( InputStreamReader(cursor.dat.getInStream(), 'utf-8')) self.assertEquals('blob field 2!', bf.readLine()) bf.close()
def createInputFile(self, fs, fileName, input_data): if (fs.exists(Path(fileName))): raise IOException("File " + fileName + " already exists on the minicluster") stream = fs.create(Path(fileName)) pw = PrintWriter(OutputStreamWriter(stream, "UTF-8")) for i in xrange(len(input_data)): pw.println(input_data[i]) pw.close()
def execute(self): if self.loginUser == None: return ActionResult.LOGIN accessControlService = __spring__.getBean("accessControlService") if accessControlService.isSystemAdmin(self.loginUser) == False: self.addActionError(u"没有管理站点配置的权限,只有超级管理员才能进行管理。") return ActionResult.ERROR strFile = request.getServletContext().getRealPath("/") strFile = strFile + "html" + File.separator strFile = URLDecoder.decode(strFile, "utf-8") file = File(strFile) if file.exists() == False: file.mkdirs() #创建学科导航 strFile = strFile + "updateinfo.htm" file = File(strFile) if request.getMethod() == "POST": params = ParamUtil(request) html = params.safeGetStringParam("updateInfo") fw = OutputStreamWriter(FileOutputStream(file), "utf-8") fw.flush() fw.write(html) fw.close() siteIndexPartService = __spring__.getBean("siteIndexPartService") if html == "": siteIndexPart = siteIndexPartService.getSiteIndexPartByModuleName( u"系统维护通知") if siteIndexPart != None: siteIndexPartService.deleteSiteIndexPart(siteIndexPart) else: siteIndexPart = siteIndexPartService.getSiteIndexPartByModuleName( u"系统维护通知") if siteIndexPart == None: siteIndexPart = SiteIndexPart() siteIndexPart.setModuleName(u"系统维护通知") siteIndexPart.setModuleZone(1) siteIndexPart.setModuleOrder(0) siteIndexPart.setModuleDisplay(1) siteIndexPart.setModuleHeight(0) siteIndexPart.setContent(html) siteIndexPart.setPartType(100) siteIndexPart.setShowType(0) siteIndexPart.setShowBorder(0) siteIndexPartService.saveOrUpdateSiteIndexPart(siteIndexPart) request.setAttribute("deleteCache", "1") else: html = CommonUtil.readFile(file.getCanonicalPath(), "UTF-8") request.setAttribute("deleteCache", "0") request.setAttribute("updateInfo", html) return "/WEB-INF/ftl/admin/admin_site_update.ftl"
def saveItem(self, projectdir, itemfile): content = self.model.getItemContent(projectdir, itemfile) storagepath = itemfile.getPath() print storagepath try: fos = FileOutputStream(storagepath) osw = OutputStreamWriter(fos) osw.write(content, 0, len(content)) osw.close() self.view.status(storagepath + " Saved") except FileNotFoundException, e: self.view.status(storagepath + " could not be opened for writing")
def http_post(self, json_string, url): '''Return java.lang.String JSON Input: java.lang.String JSON, java.lang.String URL ''' start_timer = System.currentTimeMillis() try: # Get a connection and set the request properties url = URL(url) urlconnect = url.openConnection() urlconnect.setDoOutput(True) urlconnect.setRequestMethod("POST") urlconnect.setRequestProperty("Content-Type", "application/json; UTF-8") urlconnect.setRequestProperty("Accept", "application/json") # Write to the body bw = BufferedWriter( OutputStreamWriter( urlconnect.getOutputStream() ) ) bw.write(json_string) bw.flush() bw.close() # Read the result from the POST br = BufferedReader( InputStreamReader( urlconnect.getInputStream(), "UTF-8" ) ) s = br.readLine() br.close() except MalformedURLException() as ex: cumulus_logger.error(str(ex)) MessageBox.showError(str(ex), "Exception") raise Exception(ex) except IOException as ex: cumulus_logger.error(str(ex)) MessageBox.showError(str(ex), "Exception") raise Exception(ex) end_timer = System.currentTimeMillis() cumulus_logger.debug( "HTTP GET (milliseconds): {}".format( (end_timer - start_timer) ) ) return s
def _setSettings(self, path, value): newFilePath = self._getUserSettingsFilePath(uuid.uuid1()) stringWriter = OutputStreamWriter(FileOutputStream(newFilePath), "UTF8") xmlWriter = XMLOutputFactory.newInstance().createXMLStreamWriter(stringWriter) result = self._createReader(path, value, xmlWriter) xmlWriter.close() stringWriter.close() os.rename(self._getUserSettingsFilePath(), self._getUserSettingsFilePath('_old')) os.rename(newFilePath, self._getUserSettingsFilePath()) os.remove(self._getUserSettingsFilePath('_old')) return result
def GenSubjectNav(self): strFile = request.getServletContext().getRealPath("/") strFile = URLDecoder.decode(strFile, "utf-8") strSubjectFile = strFile + "html" + File.separator file = File(strSubjectFile) if file.exists() == False: file.mkdirs() #创建学科导航 strSubjectFile = strSubjectFile + "subject_nav.html" file = File(strSubjectFile) #先得到年级 subjectService = __spring__.getBean("subjectService") qry = Command( " SELECT DISTINCT metaGrade.gradeId FROM Subject Order By metaGrade.gradeId ASC" ) mGradeId = qry.open() MetaGrade = ArrayList() metaSubject = ArrayList() for grade in mGradeId: mGrade = subjectService.getGrade(int(grade)) MetaGrade.add(mGrade) subj = subjectService.getSubjectByGradeId(int(grade)) m = ArrayList() if subj != None: for sj in range(0, subj.size()): m.add(subj[sj].metaSubject) metaSubject.add({ "gradeName": mGrade.gradeName, "gradeId": grade, "metaSubject": m }) map = HashMap() map.put("metaGrade", MetaGrade) map.put("meta_Grade", MetaGrade) map.put("SubjectNav", metaSubject) templateProcessor = __spring__.getBean("templateProcessor") str = templateProcessor.processTemplate( map, "/WEB-INF/ftl/site_subject_nav.ftl", "utf-8") try: fw = OutputStreamWriter(FileOutputStream(file), "utf-8") fw.flush() fw.write(str) fw.close() finally: file = None fw = None
def save(self, parent, format): chooser = JFileChooser() # filter = FileNameExtensionFilter( # format + " files", # array([format, format.lower()], String)) # chooser.setFileFilter(filter); returnVal = chooser.showSaveDialog(parent) if returnVal == JFileChooser.APPROVE_OPTION: fileName = chooser.getSelectedFile().getPath() if not fileName.upper().endswith('.' + format): fileName += '.' + format.lower() file = File(fileName) else: return if format == 'PNG': ChartUtils.saveChartAsPNG(file, self.chart, self.exportX, self.exportY) elif format == 'SVG': domImpl = GenericDOMImplementation.getDOMImplementation() doc = domImpl.createDocument(None, "svg", None) svgGen = SVGGraphics2D(doc) svgGen.getGeneratorContext().setPrecision(6) self.chart.draw( svgGen, Rectangle2D.Double(0, 0, self.exportX, self.exportY), None) out = OutputStreamWriter(FileOutputStream(file), "UTF-8") svgGen.stream(out, True) #True is for useCSS out.close() elif format == 'PDF': mapper = DefaultFontMapper() pageSize = Rectangle(self.exportX, self.exportY) doc = TextDocument(pageSize, 50, 50, 50, 50) out = BufferedOutputStream(FileOutputStream(file)) writer = PdfWriter.getInstance(doc, out) doc.open() cb = writer.getDirectContent() tp = cb.createTemplate(self.exportX, self.exportY) g2 = tp.createGraphics(self.exportX, self.exportY, mapper) r2D = Rectangle2D.Double(0, 0, self.exportX, self.exportY) self.chart.draw(g2, r2D) g2.dispose() cb.addTemplate(tp, 0, 0) doc.close()
def DoXPath (Filename,XPathString): IS = InputSource (FileInputStream (Filename)) df = DocumentBuilderFactory.newInstance() df.setNamespaceAware(1) doc = df.newDocumentBuilder().parse(IS) serializer = TransformerFactory.newInstance().newTransformer() serializer.setOutputProperty (OutputKeys.OMIT_XML_DECLARATION, "yes") nl = XPathAPI.selectNodeIterator (doc,XPathString) n = nl.nextNode() while n: if IsTextNode (n): # Coalesce contiguous text nodes res = [n.getNodeValue()] nn = n.getNextSibling() while (nn): res.append (nn.getNodeValue()) nn = n.getNextSibling() java.lang.System.out (string.join(res,"")) else: serializer.transform (DOMSource(n), StreamResult (OutputStreamWriter (java.lang.System.out))) java.lang.System.out.println() n = nl.nextNode()
def create_subject_nav(self): subjectService = __spring__.getBean("subjectService") qry = Command(" SELECT DISTINCT metaGrade.gradeId FROM Subject Order By metaGrade.gradeId ASC") mGradeId = qry.open() MetaGrade = ArrayList() metaSubject = ArrayList() for grade in mGradeId: mGrade = subjectService.getGrade(int(grade)) MetaGrade.add(mGrade) subj = subjectService.getSubjectByGradeId(int(grade)) m = ArrayList() if subj != None: for sj in range(0, subj.size()): m.add(subj[sj].metaSubject) metaSubject.add({"gradeName" : mGrade.gradeName, "gradeId" : grade, "metaSubject" : m }) map = HashMap() map.put("metaGrade", MetaGrade) map.put("meta_Grade", MetaGrade) map.put("SubjectNav", metaSubject) map.put("SiteUrl", request.getContextPath() + "/") templateProcessor = __spring__.getBean("templateProcessor") str = templateProcessor.processTemplate(map, "/WEB-INF/ftl/site_subject_nav.ftl", "utf-8") rootPath = JitarRequestContext.getRequestContext().getServletContext().getRealPath("/") fileIndex = rootPath + "html" + File.separator + "subject_nav.html" try: file = File(fileIndex) fw = OutputStreamWriter(FileOutputStream(file), "utf-8") #fw = FileWriter(file, False) fw.flush() fw.write(str) fw.close() finally: file = None fw = None request.setAttribute("errorMessage", u"生成学科导航缓存文件完毕!")
def GenSubjectNav(self, strPath): # 先得到年级 subjectService = __spring__.getBean("subjectService") mGradeId = subjectService.getGradeIdList() MetaGrade = ArrayList() metaSubject = ArrayList() for grade in mGradeId: mGrade = subjectService.getGrade(int(grade)) MetaGrade.add(mGrade) subj = subjectService.getSubjectByGradeId(int(grade)) m = ArrayList() if subj != None: for sj in range(0, subj.size()): m.add(subj[sj].metaSubject) metaSubject.add({ "gradeName": mGrade.gradeName, "gradeId": grade, "metaSubject": m }) map = HashMap() map.put("metaGrade", MetaGrade) map.put("meta_Grade", MetaGrade) map.put("SubjectNav", metaSubject) map.put("SiteUrl", self.get_context_url()) templateProcessor = __spring__.getBean("templateProcessor") str = templateProcessor.processTemplate( map, "/WEB-INF/ftl/site_subject_nav.ftl", "utf-8") try: file = File(strPath) fw = OutputStreamWriter(FileOutputStream(file), "utf-8") fw.flush() fw.write(str) fw.close() finally: file = None fw = None
from net.ontopia.persistence.rdbms import DatabaseProjectReader, GenericSQLProducer from java.lang import System from java.io import OutputStreamWriter dbreader = DatabaseProjectReader() project = dbreader.loadProject( "file:/ontopia/src/java/net/ontopia/topicmaps/impl/proxy/schema/TopicMaps.xml" ) producer = GenericSQLProducer(project) writer = OutputStreamWriter(System.out) producer.produceCreate(writer) producer.produceDrop(writer) writer.close()
def __createNew(self): self.vc("log").debug("Creating a new package...") packageType, jsonConfigFile = self.__getPackageTypeAndJsonConfigFile() self.vc("log").debug("packageType = '{}'", packageType) self.vc("log").debug("jsonConfigFile = '{}'", jsonConfigFile) manifestHash = "%s.tfpackage" % uuid.uuid4() # store the manifest file for harvesting packageDir = FascinatorHome.getPathFile("packages") packageDir.mkdirs() manifestFile = File(packageDir, manifestHash) outStream = FileOutputStream(manifestFile) outWriter = OutputStreamWriter(outStream, "UTF-8") self.vc("sessionState").set("package/active", None) manifest = self.__getActiveManifest() manifest.setType(packageType) metaList = list(self.vc("formData").getValues("metaList")) jsonObj = manifest.getJsonObject() for metaName in metaList: value = self.vc("formData").get(metaName) jsonObj.put(metaName, value) if self.vc("formData").getValues("sequencesMetaList") != None: sequenceService = ApplicationContextProvider.getApplicationContext( ).getBean("sequenceService") sequencesMetaList = list( self.vc("formData").getValues("sequencesMetaList")) for sequenceInfo in sequencesMetaList: sequenceInfoJson = JsonSimple(sequenceInfo) sequenceIndex = sequenceService.getSequence( sequenceInfoJson.getString(None, "sequenceName")) jsonObj.put( sequenceInfoJson.getString(None, "metadataName"), String.format( sequenceInfoJson.getString(None, "stringFormat"), sequenceIndex)) self.vc("log").debug("json object created is: %r" % jsonObj) outWriter.write(manifest.toString(True)) outWriter.close() # adding ability to set access plugin accessPlugin = self.vc("formData").get("access_plugin", "derby") if accessPlugin is not None: self.vc("page").authentication.set_access_plugin(accessPlugin) try: # harvest the package as an object username = self.vc("sessionState").get("username") if username is None: username = "******" # necessary? harvester = None # set up config files, creating if necessary workflowsDir = FascinatorHome.getPathFile("harvest/workflows") configFile = self.__getFile(workflowsDir, jsonConfigFile) self.__getFile(workflowsDir, "packaging-rules.py") # run the harvest client with our packaging workflow config harvester = HarvestClient(configFile, manifestFile, username) harvester.start() manifestId = harvester.getUploadOid() harvester.shutdown() except Exception, ex: error = "Packager workflow failed: %s" % str(ex) self.vc("log").error(error, ex) if harvester is not None: harvester.shutdown() return '{ "status": "failed" }'
def __createFromSelected(self): self.vc("log").debug("Creating package from selected...") packageType, jsonConfigFile = self.__getPackageTypeAndJsonConfigFile() #self.vc("log").debug("packageType = '{}'", packageType) #self.vc("log").debug("jsonConfigFile = '{}'", jsonConfigFile) # if modifying existing manifest, we already have an identifier, # otherwise create a new one manifestId = self.__getActiveManifestId() if manifestId is None: manifestHash = "%s.tfpackage" % uuid.uuid4() else: manifestHash = self.__getActiveManifestPid() # store the manifest file for harvesting packageDir = FascinatorHome.getPathFile("packages") packageDir.mkdirs() manifestFile = File(packageDir, manifestHash) outStream = FileOutputStream(manifestFile) outWriter = OutputStreamWriter(outStream, "UTF-8") manifest = self.__getActiveManifest() oldType = manifest.getType() if oldType is None: manifest.setType(packageType) else: manifest.setType(oldType) self.vc("log").debug("Manifest: %s" % manifest) outWriter.write(manifest.toString(True)) outWriter.close() try: if manifestId is None: # harvest the package as an object username = self.vc("sessionState").get("username") if username is None: username = "******" # necessary? harvester = None # set up config files, and make sure they are both deployed workflowsDir = FascinatorHome.getPathFile("harvest/workflows") configFile = self.__getFile(workflowsDir, jsonConfigFile) rulesFile = self.__getFile(workflowsDir, "packaging-rules.py") # run the harvest client with our packaging workflow config harvester = HarvestClient(configFile, manifestFile, username) harvester.start() manifestId = harvester.getUploadOid() harvester.shutdown() else: # update existing object object = StorageUtils.getDigitalObject(Services.getStorage(), manifestId) manifestStream = FileUtils.openInputStream(manifestFile) StorageUtils.createOrUpdatePayload(object, manifestHash, manifestStream) manifestStream.close() object.close() except Exception, ex: error = "Packager workflow failed: %s" % str(ex) self.vc("log").error(error, ex) if harvester is not None: harvester.shutdown() return '{ "status": "failed" }'