def decode(self, input, final=False): error_function = codecs.lookup_error(self.errors) input_array = array('b', self.buffer + str(input)) input_buffer = ByteBuffer.wrap(input_array) builder = StringBuilder( int(self.decoder.averageCharsPerByte() * len(input))) self.output_buffer.rewind() while True: result = self.decoder.decode(input_buffer, self.output_buffer, final) pos = self.output_buffer.position() self.output_buffer.rewind() builder.append(self.output_buffer.subSequence(0, pos)) if result.isUnderflow(): if not final: # Keep around any remaining input for next call to decode self.buffer = input_array[input_buffer.position( ):input_buffer.limit()].tostring() else: _process_incomplete_decode(self.encoding, input, error_function, input_buffer, builder) break _process_decode_errors(self.encoding, input, result, error_function, input_buffer, builder) return builder.toString()
def getDoc(self, url): # print "call doc on %s" % url # doc=None; # try: # newurl = URL( url ); # dbf = DocumentBuilderFactory.newInstance(); # dbf.setNamespaceAware(True) # db = dbf.newDocumentBuilder(); # doc = db.parse( newurl.openStream() ); # print doc # except Exception, e: # print 'Failed to reach a server.' # print 'Details', str(e); # # return doc; # print "getDoc called with %s" % url # Java method above no longer works. doc=None; try: responseBuilder = StringBuilder(); newurl = URL( url ); conn = newurl.openConnection(); rd = BufferedReader( InputStreamReader(conn.getInputStream()) ); line = rd.readLine(); while line != None: responseBuilder.append(line + '\n'); line = rd.readLine(); rd.close(); except Exception, e: print 'Failed to reach a server.' print 'Details', str(e); return doc;
def getPayloadContent(self, payload): if payload is None: return "" try: sb = StringBuilder() reader = BufferedReader(InputStreamReader(payload.open(), "UTF-8")) line = reader.readLine() p = re.compile('\s*<\?xml.*\?>\s*') firstLine = True while line is not None: if firstLine: if p.match(line) is not None: self.log.debug("Ignoring xml declaration") line = reader.readLine() else: sb.append(line).append("\n") line = reader.readLine() firstLine = False else: sb.append(line).append("\n") line = reader.readLine() payload.close() if sb: return sb return "" except Exception, e: return ""
def schemaFor(self, alias): """ Returns string containing the schema of the specified alias """ self.register_script() sb = StringBuilder() Schema.stringifySchema(sb, self.pig.dumpSchema(alias), DataType.TUPLE) return sb.toString()
def read_jar_stream(self, template_path): reader = BufferedReader(InputStreamReader(Thread.currentThread().getContextClassLoader().getResourceAsStream(template_path))) line = reader.readLine() sb = StringBuilder() while line is not None: sb.append(line).append("\n") line = reader.readLine() return sb.toString()
def read_all(reader): arrsize = 8 * 1024 arr = zeros('c', arrsize) buffer = StringBuilder() numCharsRead = 0 while numCharsRead != -1: numCharsRead = reader.read(arr, 0, arrsize) if numCharsRead != -1: buffer.append(arr, 0, numCharsRead) return buffer.toString()
def stringify(stream): reader = BufferedReader(InputStreamReader(stream)) out = StringBuilder() while True: line = reader.readLine() if line is None: break out.append(line) out.append("\n") reader.close() return out.toString()
class FormattingVisitor(NodeVisitor): #--------------------------------------------------------------------------- # Name: __init__() # Role: constructor #--------------------------------------------------------------------------- def __init__(self, url): self.result = StringBuilder() self.header = re.compile('[hH][1-6]$') self.h3Text = '' #--------------------------------------------------------------------------- # Name: append() # Role: Add to the text #--------------------------------------------------------------------------- def append(self, text): newline = ['', '\n'][self.result.length() > 0] self.result.append(newline + text) #--------------------------------------------------------------------------- # Name: head() # Role: Invoked when open tag is first seen # Note: node.text() = "the combined text of this element and all its children" #--------------------------------------------------------------------------- def head(self, node, depth): name = node.nodeName() if re.match(self.header, name): if name[1] == '3': text = self.h3Text = node.text() if text.find('inherited') < 0: print '%s %s' % (name, text) self.append('%s: %s' % (name, text)) else: self.h3Text = '' #--------------------------------------------------------------------------- # Name: tail() # Role: Invoked when end tag is seen #--------------------------------------------------------------------------- def tail(self, node, depth): name = node.nodeName() if self.h3Text and name == 'table': print node print '-' * 50 #--------------------------------------------------------------------------- # Name: toString() # Role: Return the accumulated string #--------------------------------------------------------------------------- def toString(self): return str(self.result)
def __getJson(self): data = {} basePath = portalId + "/" + pageName uri = URLDecoder.decode(request.getAttribute("RequestURI")) oid = uri[len(basePath)+1:] payload = Services.storage.getPayload(oid, "imsmanifest.xml") if payload is not None: try: from xml.etree import ElementTree as ElementTree #xml = ElementTree() #IOUtils.copy(payload.inputStream, out) sb = StringBuilder() inputStream = payload.inputStream reader = BufferedReader(InputStreamReader(inputStream, "UTF-8")) while True: line=reader.readLine() if line is None: break sb.append(line).append("\n") inputStream.close() xmlStr = sb.toString() xml = ElementTree.XML(xmlStr) ns = xml.tag[:xml.tag.find("}")+1] resources = {} for res in xml.findall(ns+"resources/"+ns+"resource"): resources[res.attrib.get("identifier")] = res.attrib.get("href") #print resources organizations = xml.find(ns+"organizations") defaultName = organizations.attrib.get("default") organizations = organizations.findall(ns+"organization") organizations = [o for o in organizations if o.attrib.get("identifier")==defaultName] organization = organizations[0] title = organization.find(ns+"title").text data["title"] = title items = [] for item in organization.findall(ns+"item"): a = item.attrib isVisible = a.get("isvisible") idr = a.get("identifierref") id = resources.get(idr) iTitle = item.find(ns+"title").text if isVisible and id and id.endswith(".htm"): items.append({"attributes":{"id":id}, "data":iTitle}) data["nodes"] = items except Exception, e: data["error"] = "Error - %s" % str(e) print data["error"]
class Output (object): def __init__(self): self._builder = None def write(self, text): if not ( isinstance( text, str ) or isinstance( text, unicode ) ): raise TypeError, 'argument 1 must be string, not %s' % type( text ) if self._builder is None: self._builder = StringBuilder() self._builder.append( text ) def getText(self): if self._builder is not None: return self._builder.toString() else: return None
class FormattingVisitor(NodeVisitor): #--------------------------------------------------------------------------- # Name: __init__() # Role: constructor #--------------------------------------------------------------------------- def __init__(self, url): self.result = StringBuilder() #--------------------------------------------------------------------------- # Name: append() # Role: Add to the text #--------------------------------------------------------------------------- def append(self, text): newline = ['', '\n'][self.result.length() > 0] self.result.append(newline + text) #--------------------------------------------------------------------------- # Name: head() # Role: Invoked when tag is first seen #--------------------------------------------------------------------------- def head(self, node, depth): name = node.nodeName() if name == 'h3' and node.hasText(): text = node.text() if text.find('inherited') < 0: self.append('%s: %s' % (name, text)) #--------------------------------------------------------------------------- # Name: tail() # Role: Invoked when end tag is seen #--------------------------------------------------------------------------- def tail(self, node, depth): name = node.nodeName() if name == 'table': print node # What do we want / need to do here ? #--------------------------------------------------------------------------- # Name: toString() # Role: Return the accumulated string #--------------------------------------------------------------------------- def toString(self): return str(self.result)
def test_wrong_type(self): with self.assertRaises(ValueError): jarray.array([1, 2], 'x') with self.assertRaises(TypeError): jarray.array([1, 2]) with self.assertRaises(TypeError): jarray.array([1, 2], 6) with self.assertRaises(TypeError): jarray.array([1, 2], StringBuilder())
def _format_json_value(value): """ Format the value as a JSON snippet. :param value: the value :return: the JSON snippet """ import java.lang.StringBuilder as StringBuilder builder = StringBuilder() if type(value) == bool or (isinstance(value, types.StringTypes) and (value == 'true' or value == 'false')): builder.append(JBoolean.toString(value)) elif isinstance(value, types.StringTypes): builder.append('"').append(_escape_text(value.strip())).append('"') else: builder.append(value) return builder.toString()
def getDoc(self, url): doc=None; try: responseBuilder = StringBuilder(); newurl = URL( url ); conn = newurl.openConnection(); rd = BufferedReader( InputStreamReader(conn.getInputStream()) ); line = rd.readLine(); while line != None: responseBuilder.append(line + '\n'); line = rd.readLine(); rd.close(); except Exception, e: print 'Failed to reach a server.' print 'Details', str(e); return doc;
def decode(self, input, errors='strict', final=True): error_function = codecs.lookup_error(errors) input_buffer = ByteBuffer.wrap(array('b', input)) decoder = Charset.forName(self.encoding).newDecoder() output_buffer = CharBuffer.allocate(min(max(int(len(input) / 2), 256), 1024)) builder = StringBuilder(int(decoder.averageCharsPerByte() * len(input))) while True: result = decoder.decode(input_buffer, output_buffer, False) pos = output_buffer.position() output_buffer.rewind() builder.append(output_buffer.subSequence(0, pos)) if result.isUnderflow(): if final: _process_incomplete_decode(self.encoding, input, error_function, input_buffer, builder) break _process_decode_errors(self.encoding, input, result, error_function, input_buffer, builder) return builder.toString(), input_buffer.position()
def preprocess(text): """Tokenize and stop the input text.""" ts = StandardTokenizer(Lucene.get_version(), StringReader(text.lower())) ts = StopFilter(Lucene.get_version(), ts, StopAnalyzer.ENGLISH_STOP_WORDS_SET) string_builder = StringBuilder() ts.reset() char_term_attr = ts.addAttribute(CharTermAttribute.class_) while ts.incrementToken(): if string_builder.length() > 0: string_builder.append(" ") string_builder.append(char_term_attr.toString()) return string_builder.toString()
def _format_json_value(value): """ Format the value as a JSON snippet. :param value: the value :return: the JSON snippet """ import java.lang.StringBuilder as StringBuilder builder = StringBuilder() if type(value) == bool or (type(value) == str and (value == 'true' or value == 'false')): builder.append(JBoolean.toString(value)) elif type(value) == str: builder.append('"').append(_quote_embedded_quotes(value)).append('"') else: builder.append(value) return builder.toString()
def getPayloadContent(self, payload): if payload is None: return "" try: sb = StringBuilder() reader = BufferedReader(InputStreamReader(payload.open(), "UTF-8")) line = reader.readLine() while line is not None: sb.append(line).append("\n") line = reader.readLine() payload.close() if sb: return sb return "" except Exception, e: return ""
def clickBtnDel(event): global sb l = sb.length() if (l == 0): return else: st = sb.substring(0, l - 1) tf.setText(st) #show on text field sbNew = StringBuilder( st ) # now we have to convert the string into StringBuilder sowe pass the String into StringBuilder sb = sbNew # again update in sb
def readInputStream(inputStream): reader = BufferedReader(InputStreamReader(inputStream)) builder = StringBuilder() line = None while True: line = reader.readLine() if line is None: break builder.append(line) builder.append(System.getProperty("line.separator")) return builder.toString()
def decode(self, input, final=False): error_function = codecs.lookup_error(self.errors) input_array = array('b', self.buffer + str(input)) input_buffer = ByteBuffer.wrap(input_array) builder = StringBuilder(int(self.decoder.averageCharsPerByte() * len(input))) self.output_buffer.rewind() while True: result = self.decoder.decode(input_buffer, self.output_buffer, final) pos = self.output_buffer.position() self.output_buffer.rewind() builder.append(self.output_buffer.subSequence(0, pos)) if result.isUnderflow(): if not final: # Keep around any remaining input for next call to decode self.buffer = input_array[input_buffer.position():input_buffer.limit()].tostring() else: _process_incomplete_decode(self.encoding, input, error_function, input_buffer, builder) break _process_decode_errors(self.encoding, input, result, error_function, input_buffer, builder) return builder.toString()
def generateNameUid(self, user): if self.userEnforceAttributesUniqueness == None: print "Asimba. Build local external uid. User enforce attributes uniqueness not specified" return None sb = StringBuilder() first = True for userAttributeName in self.userEnforceAttributesUniqueness: if not first: sb.append("!") first = False attribute_values_list = user.getAttributeValues(userAttributeName) if (attribute_values_list != None) and (attribute_values_list.size() > 0): first_attribute_value = attribute_values_list.get(0) sb.append(first_attribute_value) return sb.toString()
def encode(self, input, final=False): error_function = codecs.lookup_error(self.errors) # workaround non-BMP issues - need to get the exact count of chars, not codepoints input_buffer = CharBuffer.allocate(StringBuilder(input).length()) input_buffer.put(input) input_buffer.rewind() self.output_buffer.rewind() builder = StringIO() while True: result = self.encoder.encode(input_buffer, self.output_buffer, final) pos = self.output_buffer.position() self.output_buffer.rewind() builder.write(self.output_buffer.array()[0:pos].tostring()) if result.isUnderflow(): break _process_encode_errors(self.encoding, input, result, error_function, input_buffer, builder) return builder.getvalue()
def encode(self, input, errors='strict'): error_function = codecs.lookup_error(errors) # workaround non-BMP issues - need to get the exact count of chars, not codepoints input_buffer = CharBuffer.allocate(StringBuilder(input).length()) input_buffer.put(input) input_buffer.rewind() encoder = Charset.forName(self.encoding).newEncoder() output_buffer = ByteBuffer.allocate(min(max(len(input) * 2, 256), 1024)) builder = StringIO() while True: result = encoder.encode(input_buffer, output_buffer, True) pos = output_buffer.position() output_buffer.rewind() builder.write(output_buffer.array()[0:pos].tostring()) if result.isUnderflow(): break _process_encode_errors(self.encoding, input, result, error_function, input_buffer, builder) return builder.getvalue(), len(input)
def generateNameUid(self, user): if (self.userEnforceAttributesUniqueness == None): print "Saml. Build local external uid. User enforce attributes uniqueness not specified" return None sb = StringBuilder() first = True for userAttributeName in self.userEnforceAttributesUniqueness: if not first: sb.append("!") first = False attribute_values_list = user.getAttributeValues(userAttributeName) if (attribute_values_list != None) and (attribute_values_list.size() > 0): first_attribute_value = attribute_values_list.get(0) sb.append(first_attribute_value) return sb.toString()
class XMLParser(object): def __init__(self, encoding, namespace_separator): self.encoding = encoding self.CurrentLineNumber = 1 self.CurrentColumnNumber = 0 self._NextLineNumber = 1 self._NextColumnNumber = 0 self.ErrorLineNumber = -1 self.ErrorColumnNumber = -1 self.ErrorCode = None if namespace_separator is None: self.namespace_separator = namespace_separator elif isinstance(namespace_separator, basestring): self.namespace_separator = str(namespace_separator) if len(self.namespace_separator) > 1: error = ("namespace_separator must be at most one character, " "omitted, or None") raise ValueError(error) else: error = ("ParserCreate() argument 2 must be string or None, " "not %s" % type(namespace_separator).__name__) raise TypeError(error) # See http://bugs.jython.org/issue1537 try: self._reader = XMLReaderFactory.createXMLReader( _mangled_xerces_parser_name) except: self._reader = XMLReaderFactory.createXMLReader( _xerces_parser_name) if self.namespace_separator is None: try: feature = "http://xml.org/sax/features/namespaces" self._reader.setFeature(feature, False) except SAXNotRecognizedException: error = ("namespace support cannot be disabled; " "set namespace_separator to a string of length 1.") raise ValueError(error) self._base = None self._buffer_text = True self._returns_unicode = True self._data = StringBuilder() self._handler = XMLEventHandler(self) self._reader.setContentHandler(self._handler) self._reader.setErrorHandler(self._handler) self._reader.setDTDHandler(self._handler) self._reader.setEntityResolver(self._handler) sax_properties = ("lexical-handler", "declaration-handler") for name in sax_properties: try: name = "http://xml.org/sax/properties/" + name self._reader.setProperty(name, self._handler) except SAXNotRecognizedException: error = "can't set property %r" % name raise NotImplementedError(error) apache_features = (("nonvalidating/load-external-dtd", False), ) for name, value in apache_features: try: name = "http://apache.org/xml/features/" + name self._reader.setFeature(name, value) except SAXNotRecognizedException: error = "can't set feature %r" % name raise NotImplementedError(error) # experimental #f = "http://xml.org/sax/features/external-general-entities" f = "http://xml.org/sax/features/external-parameter-entities" #self._reader.setFeature(f, False) # check f = "http://xml.org/sax/features/use-entity-resolver2" assert self._reader.getFeature(f) def GetBase(self): return self._base def SetBase(self, base): self._base = base def _error(self, value=None): raise AttributeError("'XMLParser' has no such attribute") def _get_buffer_text(self): return self._buffer_text def _set_buffer_text(self, value): self._buffer_text = bool(value) def _get_returns_unicode(self): return bool(self._returns_unicode) def _set_returns_unicode(self, value): self._returns_unicode = value # 'ordered' and 'specified' attributes are not supported ordered_attributes = property(_error, _error) specified_attributes = property(_error, _error) # any setting is allowed, but it won't make a difference buffer_text = property(_get_buffer_text, _set_buffer_text) # non-significant read-only values buffer_used = property(lambda self: None) buffer_size = property(lambda self: None) # 'returns_unicode' attribute is properly supported returns_unicode = property(_get_returns_unicode, _set_returns_unicode) def _expat_error(self, sax_error): sax_message = sax_error.getMessage() pattern = 'The entity ".*" was referenced, but not declared\.' if re.match(pattern, sax_message): expat_message = "undefined entity: line %s, column %s" % \ (self.ErrorLineNumber, self.ErrorColumnNumber) else: expat_message = sax_message error = ExpatError(expat_message) error.lineno = self.ErrorLineNumber error.offset = self.ErrorColumnNumber error.code = self.ErrorCode return error def Parse(self, data, isfinal=False): # The 'data' argument should be an encoded text: a str instance that # represents an array of bytes. If instead it is a unicode string, # only the us-ascii range is considered safe enough to be silently # converted. if isinstance(data, unicode): data = data.encode(sys.getdefaultencoding()) self._data.append(data) if isfinal: bytes = StringUtil.toBytes(self._data.toString()) byte_stream = ByteArrayInputStream(bytes) source = InputSource(byte_stream) if self.encoding is not None: source.setEncoding(self.encoding) try: self._reader.parse(source) except SAXParseException, sax_error: # Experiments tend to show that the '_Next*' parser locations # match more closely expat behavior than the 'Current*' or sax # error locations. self.ErrorLineNumber = self._NextLineNumber self.ErrorColumnNumber = self._NextColumnNumber self.ErrorCode = None raise self._expat_error(sax_error) return 1
class ObjectJArrayTest(AbstractJArrayTest, unittest.TestCase): type = StringBuilder default_value = None instance = StringBuilder("aaa")
def toString(self): i18nManager = ToolsLocator.getI18nManager() if self.exp != None: try: builder = StringBuilder() if self.exp != None: builder.append(self.exp) builder.append(", ") if self.name != None: builder.append("Field: ") builder.append(self.name) builder.append(", ") if self.comboFilterResults != None: builder.append("Filter: ") if self.comboFilterResults == 0: builder.append( i18nManager.getTranslation("_use_selection")) if self.comboFilterResults == 1: builder.append( i18nManager.getTranslation("_use_filter")) else: builder.append(i18nManager.getTranslation("_use_all")) builder.append(", ") if self.filterResults != None: builder.append(self.filterResults) if self.filterResults == None: builder.append("None") return builder.toString() except java.lang.Throwable, ex: logger("Error creando bookmarks.", LOGGER_WARN, ex) raise ex except:
def param_parl_info(cfg, params): """ Converts it to the transformers expected input form """ linfo = LegislatureInfo(cfg) li_parl_params = StringBuilder() li_parl_params.append( "<parliaments>" ) li_parl_params.append( ("<countryCode>%(country_code)s</countryCode>" "<parliament id=\"0\">" " <identifier>%(identifier)s</identifier>" " <startDate>%(start_date)s</startDate>" " <electionDate>%(election_date)s</electionDate>" " <type displayAs=\"National Assembly\">nat_assembly</type>" "</parliament>")% { "country_code" : linfo.get_country_code(), "identifier" : linfo.get_legislature_identifier(), "start_date" : linfo.get_legislature_start_date(), "election_date" : linfo.get_legislature_election_date() } ) for param in params: li_parl_params.append( ('<parliament id="%(parl_id)s">' " <startDate>%(start_date)s</startDate>" " <forParliament>%(for_parl)s</forParliament>" " <identifier>%(identifier)s</identifier>" '<type displayAs="%(type_display)s">%(type)s</type>' '<status>%(status)s</status>' "</parliament>") % { "parl_id" : param["parliament-id"], "start_date": param["chamber-start-date"], "identifier": param["identifier"], "status": param["status"], "for_parl": param["for-parliament"], "type": param["type"], "type_display": param["type_display"] } ) li_parl_params.append( "</parliaments>" ) print "parl_info = " + li_parl_params.toString() return li_parl_params.toString()
def asJSON(tree): """ Return a String with a JSON map containing the name, id, and list of connections. """ json = StringBuilder() json.append('{"id" : "').append(tree.id).append('",\n') json.append(' "name" : "').append(getTitle(tree)).append('",\n') tableOut, tableIn = findConnections(tree) json.append(' "outgoing" : [') for target, num in sortMapByValue(tableOut, True): json.append(' ["').append(getTitle(target)).append('", ').append(num).append('],\n') if len(tableOut) > 0: json.setLength(json.length()-2) json.append('],\n') json.append(' "incoming" : [') for origin, num in sortMapByValue(tableIn, True): json.append(' ["').append(getTitle(origin)).append('", ').append(num).append('],\n') if len(tableIn) > 0: json.setLength(json.length()-2) json.append(']}\n') return json.toString()
def pn_string(st): sb = StringBuilder() if st: sb.append(st) return sb
def cat(self, key, values): sb = StringBuilder() for value in values: if sb.length > 0: sb.append(self.sep) sb.append(str(value)) yield key, str(sb.toString())
# ... # </graph> # # The XML opens in a TextWindow # # While at it, will generate a .sif file for importing the networt into cytoscape # as well as a list of id vs name. from ini.trakem2.display import Display, Connector, Treeline from java.lang import StringBuilder from ij.text import TextWindow layerset = Display.getFront().getLayerSet() xml = StringBuilder("<graph>\n") sif = StringBuilder() names = StringBuilder() indent = ' ' type = Treeline alltargets = set() for con in layerset.getZDisplayables(Connector): print "Processing Connector", con origins = con.getOrigins(type) if origins.isEmpty(): print "Connector without origins:", con continue targetlists = con.getTargets(type) if targetlists.isEmpty():
def __init__(self, encoding, namespace_separator): self.encoding = encoding self.CurrentLineNumber = 1 self.CurrentColumnNumber = 0 self._NextLineNumber = 1 self._NextColumnNumber = 0 self.ErrorLineNumber = -1 self.ErrorColumnNumber = -1 self.ErrorCode = None if namespace_separator is None: self.namespace_separator = namespace_separator elif isinstance(namespace_separator, basestring): self.namespace_separator = str(namespace_separator) if len(self.namespace_separator) > 1: error = ("namespace_separator must be at most one character, " "omitted, or None") raise ValueError(error) else: error = ("ParserCreate() argument 2 must be string or None, " "not %s" % type(namespace_separator).__name__) raise TypeError(error) self._reader = XMLReaderFactory.createXMLReader(_xerces_parser) if self.namespace_separator is None: try: feature = "http://xml.org/sax/features/namespaces" self._reader.setFeature(feature, False) except SAXNotRecognizedException: error = ("namespace support cannot be disabled; " "set namespace_separator to a string of length 1.") raise ValueError(error) self._base = None self._buffer_text = True self._returns_unicode = True self._data = StringBuilder() self._handler = XMLEventHandler(self) self._reader.setContentHandler(self._handler) self._reader.setErrorHandler(self._handler) self._reader.setDTDHandler(self._handler) self._reader.setEntityResolver(self._handler) sax_properties = ("lexical-handler", "declaration-handler") for name in sax_properties: try: name = "http://xml.org/sax/properties/" + name self._reader.setProperty(name, self._handler) except SAXNotRecognizedException: error = "can't set property %r" % name raise NotImplementedError(error) apache_features = (("nonvalidating/load-external-dtd", False),) for name, value in apache_features: try: name = "http://apache.org/xml/features/" + name self._reader.setFeature(name, value) except SAXNotRecognizedException: error = "can't set feature %r" % name raise NotImplementedError(error) # experimental #f = "http://xml.org/sax/features/external-general-entities" f = "http://xml.org/sax/features/external-parameter-entities" #self._reader.setFeature(f, False) # check f = "http://xml.org/sax/features/use-entity-resolver2" assert self._reader.getFeature(f)
def doInBackground(self): """Download errors data """ #Download and parse errors self.tool = self.app.downloadingTool self.checks = self.app.downloadingChecks #Initialize progress property. progress = 0 self.super__setProgress(progress) progress = 1 self.super__setProgress(progress) #debug offline = False writeToFile = False if offline or self.tool.isLocal: if offline and not self.tool.isLocal: fileName = File.separator.join([self.app.SCRIPTDIR, "%s_errors.gfs" % self.tool.name]) else: fileName = self.tool.fileName print "\n read errors from file: %s" % fileName inFile = open(fileName, "r") self.app.errorsData = inFile.read() inFile.close() else: try: print "\n url: ", self.app.downloadingUrl url = URL(self.app.downloadingUrl) uc = url.openConnection() ins = uc.getInputStream() inb = BufferedReader(InputStreamReader(ins, Utils.UTF_8)) builder = StringBuilder() line = inb.readLine() while line is not None and not self.isCancelled(): builder.append(line) builder.append(System.getProperty("line.separator")) line = inb.readLine() if self.isCancelled(): if inb is not None: inb.close() if uc is not None: uc.disconnect() return inb.close() uc.disconnect() self.app.errorsData = builder.toString() except (UnknownHostException, FileNotFoundException, IOException, SocketException): msg = self.app.strings.getString("connection_not_working") self.app.downloadAndReadDlg.progressBar.setIndeterminate(False) self.app.downloadAndReadDlg.dispose() JOptionPane.showMessageDialog(Main.parent, msg) self.cancel(True) return if writeToFile: f = open(File.separator.join([self.app.SCRIPTDIR, "%s_errors.gfs" % self.tool.name]), "w") f.write(self.app.errorsData.encode("utf-8")) f.close() print "errors file saved", File.separator.join([self.app.SCRIPTDIR, "%s_errors.gfs" % self.tool.name])
class XMLParser(object): def __init__(self, encoding, namespace_separator): self.encoding = encoding self.CurrentLineNumber = 1 self.CurrentColumnNumber = 0 self._NextLineNumber = 1 self._NextColumnNumber = 0 self.ErrorLineNumber = -1 self.ErrorColumnNumber = -1 self.ErrorCode = None if namespace_separator is None: self.namespace_separator = namespace_separator elif isinstance(namespace_separator, basestring): self.namespace_separator = str(namespace_separator) if len(self.namespace_separator) > 1: error = ("namespace_separator must be at most one character, " "omitted, or None") raise ValueError(error) else: error = ("ParserCreate() argument 2 must be string or None, " "not %s" % type(namespace_separator).__name__) raise TypeError(error) self._reader = XMLReaderFactory.createXMLReader(_xerces_parser) if self.namespace_separator is None: try: feature = "http://xml.org/sax/features/namespaces" self._reader.setFeature(feature, False) except SAXNotRecognizedException: error = ("namespace support cannot be disabled; " "set namespace_separator to a string of length 1.") raise ValueError(error) self._base = None self._buffer_text = True self._returns_unicode = True self._data = StringBuilder() self._handler = XMLEventHandler(self) self._reader.setContentHandler(self._handler) self._reader.setErrorHandler(self._handler) self._reader.setDTDHandler(self._handler) self._reader.setEntityResolver(self._handler) sax_properties = ("lexical-handler", "declaration-handler") for name in sax_properties: try: name = "http://xml.org/sax/properties/" + name self._reader.setProperty(name, self._handler) except SAXNotRecognizedException: error = "can't set property %r" % name raise NotImplementedError(error) apache_features = (("nonvalidating/load-external-dtd", False),) for name, value in apache_features: try: name = "http://apache.org/xml/features/" + name self._reader.setFeature(name, value) except SAXNotRecognizedException: error = "can't set feature %r" % name raise NotImplementedError(error) # experimental #f = "http://xml.org/sax/features/external-general-entities" f = "http://xml.org/sax/features/external-parameter-entities" #self._reader.setFeature(f, False) # check f = "http://xml.org/sax/features/use-entity-resolver2" assert self._reader.getFeature(f) def GetBase(self): return self._base def SetBase(self, base): self._base = base def _error(self, value=None): raise AttributeError("'XMLParser' has no such attribute") def _get_buffer_text(self): return self._buffer_text def _set_buffer_text(self, value): self._buffer_text = bool(value) def _get_returns_unicode(self): return bool(self._returns_unicode) def _set_returns_unicode(self, value): self._returns_unicode = value # 'ordered' and 'specified' attributes are not supported ordered_attributes = property(_error, _error) specified_attributes = property(_error, _error) # any setting is allowed, but it won't make a difference buffer_text = property(_get_buffer_text, _set_buffer_text) # non-significant read-only values buffer_used = property(lambda self: None) buffer_size = property(lambda self: None) # 'returns_unicode' attribute is properly supported returns_unicode = property(_get_returns_unicode, _set_returns_unicode) def _expat_error(self, sax_error): sax_message = sax_error.getMessage() pattern = 'The entity ".*" was referenced, but not declared\.' if re.match(pattern, sax_message): expat_message = "undefined entity: line %s, column %s" % \ (self.ErrorLineNumber, self.ErrorColumnNumber) else: expat_message = sax_message error = ExpatError(expat_message) error.lineno = self.ErrorLineNumber error.offset = self.ErrorColumnNumber error.code = self.ErrorCode return error def Parse(self, data, isfinal=False): # The 'data' argument should be an encoded text: a str instance that # represents an array of bytes. If instead it is a unicode string, # only the us-ascii range is considered safe enough to be silently # converted. if isinstance(data, unicode): data = data.encode(sys.getdefaultencoding()) self._data.append(data) if isfinal: bytes = StringUtil.toBytes(self._data.toString()) byte_stream = ByteArrayInputStream(bytes) source = InputSource(byte_stream) if self.encoding is not None: source.setEncoding(self.encoding) try: self._reader.parse(source) except SAXParseException, sax_error: # Experiments tend to show that the '_Next*' parser locations # match more closely expat behavior than the 'Current*' or sax # error locations. self.ErrorLineNumber = self._NextLineNumber self.ErrorColumnNumber = self._NextColumnNumber self.ErrorCode = None raise self._expat_error(sax_error) return 1
from java.lang import StringBuilder # The following four variables are required for this # script to be integrated into the tool tree panel. name = "FeatureRequest" descriptiveName = "New Feature Request" description = "Send a request for a new feature (requires internet connection)" toolboxes = ["topmost"] # The following lines are necessary for the script # to be recognized as a menu extension. parentMenu = "Help" menuLabel = "New Feature Request" try: sb = StringBuilder() sb.append("<!DOCTYPE html>") sb.append("<html>") sb.append("<head><meta charset=\"UTF-8\">") sb.append("<title>New Feature Request</title>") sb.append("<style media=\"screen\" type=\"text/css\">") sb.append("table {margin-left: 15px;} ") sb.append("form {text-align: center;} ") sb.append("h1 {font-size: 14pt; margin-left: 15px; margin-right: 15px; text-align: center; font-family: Helvetica, Verdana, Geneva, Arial, sans-serif;} ") sb.append("p {font-size: 12pt; font-family: Helvetica, Verdana, Geneva, Arial, sans-serif; margin-left: 15px; margin-right: 15px;} ") sb.append("table {font-size: 12pt; font-family: Helvetica, Verdana, Geneva, Arial, sans-serif;}") sb.append("table th {border-width: 1px; padding: 8px; border-style: solid; border-color: #666666; background-color: #dedede; }") sb.append("table td {border-width: 1px; padding: 8px; border-style: solid; border-color: #666666; background-color: #ffffff; }") sb.append("caption {font-family: Helvetica, Verdana, Geneva, Arial, sans-serif; margin-left: 15px; margin-right: 15px;} ") sb.append(".numberCell { text-align: right; }") sb.append("</style> </head> <body>")
def format_json_value(self, value): """ Format the value as a JSON snippet. :param value: the value :return: the JSON snippet """ import java.lang.StringBuilder as StringBuilder builder = StringBuilder() debug("DEBUG: value %s TYPE %s", value, type(value)) if type(value) == bool or (type(value) == str and (value == 'true' or value == 'false')): if value: v = "true" else: v = "false" builder.append(v) elif type(value) == str: builder.append('"').append( self.quote_embedded_quotes(value)).append('"') elif type(value) == list: builder.append("[ ") ind = 0 for list_item in value: if ind > 0: builder.append(", ") builder.append('"').append(list_item).append('"') ind = ind + 1 builder.append(" ]") else: builder.append(value) return builder.toString()
def param_parl_info(cfg, params): """ Converts it to the transformers expected input form """ li_parl_params = StringBuilder() li_parl_params.append( "<parliaments>" ) li_parl_params.append( ("<countryCode>%(country_code)s</countryCode>" "<legislature>" " <identifier>%(identifier)s</identifier>" " <startDate>%(start_date)s</startDate>" " <electionDate>%(election_date)s</electionDate>" "</legislature>")% { "country_code" : cfg.get_country_code(), "identifier" : cfg.get_legislature_identifier(), "start_date" : cfg.get_legislature_start_date(), "election_date" : cfg.get_legislature_election_date() } ) for param in params: li_parl_params.append( ('<parliament id="%(parl_id)s">' " <electionDate>%(election_date)s</electionDate>" " <forParliament>%(for_parl)s</forParliament>" " <identifier>%(identifier)s</identifier>" " <type>%(type)s</type>" "</parliament>") % { "parl_id" : param["parliament-id"], "election_date": param["parliament-election-date"], "identifier": param["identifier"], "for_parl": param["for-parliament"], "type": param["type"] } ) li_parl_params.append( "</parliaments>" ) print "parl_info = " + li_parl_params.toString() return li_parl_params.toString()
def _format_json_value(value): """ Format the value as a JSON snippet. :param value: the value :return: the JSON snippet """ builder = StringBuilder() if isinstance(value, basestring): value = value.replace('\\', '\\\\') builder.append('"').append(_quote_embedded_quotes(value)).append('"') elif isinstance(value, (types.ListType, types.TupleType)): if not value: builder.append('[]') else: builder.append('[') nexttime = False for entry in value: if nexttime: builder.append(',') else: nexttime = True builder.append(_format_json_value(entry)) builder.append(']') else: builder.append(value) return builder.toString()
def export(profile): pl, p, pr = profile.getBezierArrays() n_points = len(p[0]) pl = profile.transformPoints(pl) p = profile.transformPoints(p) pr = profile.transformPoints(pr) cal = profile.getLayerSet().getCalibrationCopy() pw = float(cal.pixelWidth) ph = float(cal.pixelHeight) svg = StringBuilder() svg.append('<path id="').append(profile.id).append('" d="M') for i in range(0, n_points - 1): svg.append(' ').append(p[0][i] * pw).append(',').append(p[1][i] * ph) svg.append(' C ').append(pr[0][i] * pw).append(',').append(pr[1][i] * ph) svg.append(' ').append(pl[0][i+1] * pw).append(',').append(pl[1][i+1] * ph) svg.append(' ').append(p[0][n_points -1] * pw).append(',').append(p[1][n_points -1] * ph) if profile.isClosed(): svg.append(' C ').append(pr[0][n_points -1] * pw).append(',').append(pr[1][n_points -1] * ph) svg.append(' ').append(pl[0][0] * pw).append(',').append(pl[1][0] * ph) svg.append(' ').append(p[0][0] * pw).append(',').append(p[1][0] * ph) svg.append(' z') svg.append('" \>') return svg.toString()
titleList = [] descriptionList = [] creatorList = [] creationDate = [] contributorList = [] approverList = [] formatList = [] fulltext = [] relationDict = {} ### Check if dc.xml returned from ice is exist or not. if not... process the dc-rdf try: dcPayload = object.getPayload("dc.xml") # Stream the Payload into a string sb = StringBuilder() reader = BufferedReader(InputStreamReader(dcPayload.open(), "UTF-8")) line = reader.readLine() while line is not None: sb.append(line).append("\n") line = reader.readLine() dcPayload.close() # Convert the Java String to Python pyString = str(sb.toString()) # Find the escaped characters and replace with bytes pattern = "(?:&#(\d{3,3});)" replaceFunc = lambda(x): r"%s" % chr(int(x.group(1))) pyString = re.sub(pattern, replaceFunc, pyString) # Now decode to UTF-8 utf8String = pyString.decode("utf-8") # And parse the string through java
from Parameters import Parameters readme_fpath = os.path.join(script_path, "README.txt") params = Parameters() title = "Membrane Blebbing version " + Parameters._version_string try: f = open(readme_fpath, "rb") text = f.readlines() except: raise IOError("Error reading README.txt") finally: f.close() sb = StringBuilder() for line in text: sb.append(line) panel = Panel() txtArea = JTextArea(sb.toString()) txtArea.setEditable(False) txtArea.setLineWrap(True) txtArea.setWrapStyleWord(True) scrollpane = JScrollPane(txtArea) scrollpane.setPreferredSize(Dimension(500, 200)) panel.add(scrollpane) dialog = NonBlockingGenericDialog(title) #for line in text: # dialog.addMessage(line);
def asSWC(tree): """ Return a String with the treeline in SWC format. If root is None, returns None. """ root = tree.getRoot() if root is None: return None cal = tree.getLayerSet().getCalibrationCopy() pw = float(cal.pixelWidth) ph = float(cal.pixelHeight) aff = tree.getAffineTransform() swc = StringBuilder() table = {} nodeID = 1 for nd in tree.getRoot().getSubtreeNodes(): table[nd] = nodeID swc.append(nodeID).append(' ') swc.append('0').append(' ') fp = array([nd.x, nd.y], 'f') aff.transform(fp, 0, fp, 0, 1) swc.append(fp[0] * pw).append(' ') swc.append(fp[1] * ph).append(' ') swc.append(float(nd.layer.z) * pw).append(' ') swc.append(float(0)).append(' ') if nd.parent is None: swc.append(-1) # root node has id -1 else: swc.append(table[nd.parent]) # id of the parent node swc.append(' \n') nodeID += 1 return swc.toString()
def __init__(self, additionalVariables): " initialize oracle database" self.__hostname = "localhost"; try: self.__hostname = InetAddress.getLocalHost().getCanonicalHostName() except: type, value, traceback = sys.exc_info() logger.severe("Hostname error:" + `value`) additionalVariables.add(RuntimeContextVariable("ORACLE_HOSTNAME", self.__hostname, RuntimeContextVariable.ENVIRONMENT_TYPE)) dbPassword = getVariableValue("DB_PASSWORD_ALL") if dbPassword and dbPassword.strip(): self.__sysPassword = dbPassword additionalVariables.add(RuntimeContextVariable("SYS_PWD", dbPassword, RuntimeContextVariable.ENVIRONMENT_TYPE)) additionalVariables.add(RuntimeContextVariable("DBSNMP_PWD", dbPassword, RuntimeContextVariable.ENVIRONMENT_TYPE)); additionalVariables.add(RuntimeContextVariable("SYSMAN_PWD", dbPassword, RuntimeContextVariable.ENVIRONMENT_TYPE)) additionalVariables.add(RuntimeContextVariable("SYSTEM_PWD", dbPassword, RuntimeContextVariable.ENVIRONMENT_TYPE)); else: self.__sysPassword = getVariableValue("SYS_PWD") dbDataLocation = getVariableValue("DB_DATA_LOC") if dbDataLocation and os.path.isdir(dbDataLocation): dbName = getVariableValue("DB_NAME") dbDataDir = os.path.join(dbDataLocation, dbName) if os.path.isdir(dbDataDir): logger.info("DB Data directory already exists:" + dbDataDir + "; Setting DB_INSTALL_OPTION to INSTALL_DB_SWONLY") additionalVariables.add(RuntimeContextVariable( "DB_INSTALL_OPTION", "INSTALL_DB_SWONLY", RuntimeContextVariable.ENVIRONMENT_TYPE)) tcpPort = getVariableValue("TCP_PORT"); self.__serviceName = getVariableValue("DB_GLOBAL_NAME") sb = StringBuilder("jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)") sb.append("(HOST=").append(self.__hostname).append(")") sb.append("(PORT=").append(tcpPort).append("))") sb.append("(CONNECT_DATA=(SERVICE_NAME=").append(self.__serviceName).append(")))") self.__oracleServiceUrl = sb.toString() logger.info("Oracle listener service URL:" + self.__oracleServiceUrl) self.__jdbcUrl = "jdbc:oracle:thin:@" + self.__hostname +":"+ tcpPort + ":" + self.__serviceName runtimeContext.addVariable(RuntimeContextVariable("JDBC_URL", self.__jdbcUrl, RuntimeContextVariable.STRING_TYPE, "Oracle Thin Driver JDBC Url", True, RuntimeContextVariable.NO_INCREMENT)) oracleDriver = "oracle.jdbc.OracleDriver" runtimeContext.addVariable(RuntimeContextVariable("JDBC_DRIVER", oracleDriver, RuntimeContextVariable.STRING_TYPE, "Oracle Thin Driver class", True, RuntimeContextVariable.NO_INCREMENT)) self.__dbControl = Boolean.parseBoolean(getVariableValue("CONFIG_DBCONTROL", "false")) if self.__dbControl: self.__dbCtrlPort = getVariableValue("DBCONTROL_HTTP_PORT") additionalVariables.add(RuntimeContextVariable( "HTTPS_PORT", self.__dbCtrlPort, RuntimeContextVariable.STRING_TYPE)) oracleDir = getVariableValue("ORACLE_DIR") self.__markerFilePath = os.path.join(oracleDir, ".#dsoracle") self.__maintFilePath = getVariableValue("ORACLE_MAINT_FILE") dbInstallOption = getVariableValue("DB_INSTALL_OPTION") if dbInstallOption == "INSTALL_DB_AND_CONFIG": globalLockString = "OracleEnabler-" + self.__hostname logger.info("Requesting Global Lock with name: " + globalLockString) domain = proxy.getContainer().getCurrentDomain() options = domain.getOptions() maxActivationTimeOut = options.getProperty(Options.MAX_ACTIVATION_TIME_IN_SECONDS) lockTimeOut = Long.parseLong(maxActivationTimeOut) * 1000 acquired = ContainerUtils.acquireGlobalLock(globalLockString, lockTimeOut , lockTimeOut) if acquired: logger.info("Acquired Global lock with name: " + globalLockString) else: logger.severe("Could not acquire Global lock with name: " + globalLockString) raise Exception("Could not acquire Global lock with name: " + globalLockString)
def execSshRemoteUsrPwd(hostname, username, password, commandsSemiColonSeperated, sessionTimeoutSecs=0, waitForOutput=True): _hostname = hostname _username = username _password = password _command = commandsSemiColonSeperated jsch = JSch() session = jsch.getSession(_username, _hostname, 22) session.setPassword(_password) config = Properties() config.put("StrictHostKeyChecking", "no") config.put("GSSAPIAuthentication", "no") config.put("UnknownHostVerification", "no") #config.put("PreferredAuthentications", "publickey"); session.setConfig(config) if (sessionTimeoutSecs > 0): session.setTimeout(sessionTimeoutSecs * 10) print 'Logging into Remote SSH Shell u/p Auth...' try: if (sessionTimeoutSecs > 0): session.connect(sessionTimeoutSecs * 1000) else: session.connect() except: return 'None' channel = session.openChannel("exec") channel.setCommand('source ~/.bash_profile 2>/dev/null; ' + _command) outputBuffer = StringBuilder() stdin = channel.getInputStream() stdinExt = channel.getExtInputStream() channel.connect(sessionTimeoutSecs * 1000) if (waitForOutput == True): while (1): n = stdin.read() if n == -1: break if (chr(n) == '\n'): outputBuffer.append('|') elif (chr(n) == '\r'): outputBuffer.append('|') else: outputBuffer.append(chr(n)) while (1): n = stdinExt.read() if n == -1: break if (chr(n) == '\n'): outputBuffer.append('|') elif (chr(n) == '\r'): outputBuffer.append('|') else: outputBuffer.append(chr(n)) else: sleep(sessionTimeoutSecs) print "Command on: " + hostname + " : " + _command print "\toutput: " + outputBuffer.toString() channel.disconnect() session.disconnect() del channel del session return outputBuffer.toString()
def buildPostDataFortoken(self, encodedJWT, softwareStatementId) : postParameters = LinkedHashMap() postParameters.put("scope", self.clientScopes) postParameters.put("client_assertion_type", "urn:ietf:params:oauth:client-assertion-type:jwt-bearer") postParameters.put("grant_type", "client_credentials") postParameters.put("client_id", softwareStatementId) postParameters.put("client_assertion", encodedJWT) postData = StringBuilder() for param in postParameters.entrySet(): if postData.length() != 0: postData.append('&') postData.append(URLEncoder.encode(param.getKey(), "UTF-8")) postData.append('=') postData.append(URLEncoder.encode(String(param.getValue()), "UTF-8").replace("+", "%20")) print "Post data: "+postData.toString() return postData.toString()
def __init__(self, encoding, namespace_separator): self.encoding = encoding self.CurrentLineNumber = 1 self.CurrentColumnNumber = 0 self._NextLineNumber = 1 self._NextColumnNumber = 0 self.ErrorLineNumber = -1 self.ErrorColumnNumber = -1 self.ErrorCode = None if namespace_separator is None: self.namespace_separator = namespace_separator elif isinstance(namespace_separator, basestring): self.namespace_separator = str(namespace_separator) if len(self.namespace_separator) > 1: error = ("namespace_separator must be at most one character, " "omitted, or None") raise ValueError(error) else: error = ("ParserCreate() argument 2 must be string or None, " "not %s" % type(namespace_separator).__name__) raise TypeError(error) # See http://bugs.jython.org/issue1537 try: self._reader = XMLReaderFactory.createXMLReader( _mangled_xerces_parser_name) except: self._reader = XMLReaderFactory.createXMLReader( _xerces_parser_name) if self.namespace_separator is None: try: feature = "http://xml.org/sax/features/namespaces" self._reader.setFeature(feature, False) except SAXNotRecognizedException: error = ("namespace support cannot be disabled; " "set namespace_separator to a string of length 1.") raise ValueError(error) self._base = None self._buffer_text = True self._returns_unicode = True self._data = StringBuilder() self._handler = XMLEventHandler(self) self._reader.setContentHandler(self._handler) self._reader.setErrorHandler(self._handler) self._reader.setDTDHandler(self._handler) self._reader.setEntityResolver(self._handler) sax_properties = ("lexical-handler", "declaration-handler") for name in sax_properties: try: name = "http://xml.org/sax/properties/" + name self._reader.setProperty(name, self._handler) except SAXNotRecognizedException: error = "can't set property %r" % name raise NotImplementedError(error) apache_features = (("nonvalidating/load-external-dtd", False), ) for name, value in apache_features: try: name = "http://apache.org/xml/features/" + name self._reader.setFeature(name, value) except SAXNotRecognizedException: error = "can't set feature %r" % name raise NotImplementedError(error) # experimental #f = "http://xml.org/sax/features/external-general-entities" f = "http://xml.org/sax/features/external-parameter-entities" #self._reader.setFeature(f, False) # check f = "http://xml.org/sax/features/use-entity-resolver2" assert self._reader.getFeature(f)
def insert_sb(text, c1, c2): # Insert code points c1, c2 in the text, as a Java StringBuilder sb = StringBuilder() # c1 at the quarter point p1 = len(mat) // 4 for c in mat.text[:p1]: sb.appendCodePoint(ord(c)) sb.appendCodePoint(c1) # c2 at the three-quarter point p2 = 3 * p1 for c in mat.text[p1:p2]: sb.appendCodePoint(ord(c)) sb.appendCodePoint(c2) # Rest of text for c in mat.text[p2:]: sb.appendCodePoint(ord(c)) return sb
def sendJournalMessage(guid, sender, to, cc, bcc, domain, cloudDomain, mtaHost): randomId = String.valueOf(Random(System.currentTimeMillis()).nextInt(1000000)) print 'Sending message ' + randomId + ' to journaling address ' + guid + '@' + cloudDomain try: props = System.getProperties() props.put("mail.smtp.host", mtaHost) props.put("mail.smtp.starttls.enable", "false") session = Session.getDefaultInstance(props) session.setDebug(True) # create message envelope envelope = MimeMessage(session) envelope.setSentDate(Date(System.currentTimeMillis() - 2000)) envelope.setSubject("Test Journal Envelope " + randomId) envelope.setFrom(InternetAddress(sender + "@" + domain)) # alternating character sets if (int(randomId) % 2) == 0: envelope.setText(String('bullet \x95, euro sign \x80, latin F \x83, tilde \x98', 'windows-1252'), 'windows-1252') elif (int(randomId) % 3) == 0: envelope.setText('plain ascii text...', 'us-ascii') else: envelope.setText('bullet \xe2\x80\xa2, euro sign \xe2\x82\xac, latin F \xc6\x92, tilde \x7e', 'utf-8') if to is not None: for recipient in to: envelope.addRecipient(Message.RecipientType.TO, InternetAddress(recipient + "@" + domain)) if cc is not None: for recipient in cc: envelope.addRecipient(Message.RecipientType.CC, InternetAddress(recipient + "@" + domain)) if bcc is not None: for recipient in bcc: envelope.addRecipient(Message.RecipientType.BCC, InternetAddress(recipient + "@" + domain)) # generate message-id envelope.saveChanges() # create journal envelope journalEnvelope = MimeMessage(session) journalEnvelope.setHeader("X-MS-Journal-Report", "") journalEnvelope.setSentDate(Date()) journalEnvelope.setSubject(envelope.getSubject()) journalEnvelope.setFrom(InternetAddress("pseudoexchange@" + domain)) journalEnvelope.addRecipient(Message.RecipientType.TO, InternetAddress(guid + "@" + cloudDomain)) # Sender: [email protected] # Subject: RE: Test - load CAtSInk # Message-Id: <[email protected]= # 1dev.com> # To: [email protected] # Cc: [email protected] # Bcc: [email protected] journalReport = StringBuilder() journalReport.append("Sender: ").append(sender + "@" + domain).append("\r\n") journalReport.append("Subject: ").append(envelope.getSubject()).append("\r\n") journalReport.append("Message-Id: ").append(envelope.getMessageID()).append("\r\n") if to is not None: for recipient in to: journalReport.append("To: ").append(recipient + "@" + domain).append("\r\n") if cc is not None: for recipient in cc: journalReport.append("Cc: ").append(recipient + "@" + domain).append("\r\n") if bcc is not None: for recipient in bcc: journalReport.append("Bcc: ").append(recipient + "@" + domain).append("\r\n") multipart = MimeMultipart() reportPart = MimeBodyPart() reportPart.setText(journalReport.toString(), "us-ascii") multipart.addBodyPart(reportPart) messagePart = MimeBodyPart() messagePart.setContent(envelope, "message/rfc822") multipart.addBodyPart(messagePart) journalEnvelope.setContent(multipart) try: Transport.send(journalEnvelope) except: print "Send failed, will try to propagate MTA configuration again..." propagateMtaConfig() Transport.send(journalEnvelope) return True except: print 'Failed to send journaled message', randomId raise
def languages_info_xml(cfg): lang_map = cfg.languages_info() from babel import Locale allowed_languages = lang_map["allowed_languages"].split(" ") li_langs = None if len(allowed_languages) > 0: li_langs = StringBuilder() li_langs.append("<languages>") for lang in allowed_languages: default_lang = False right_to_left = False if lang == lang_map["default_language"]: default_lang = True if lang in lang_map["right_to_left_languages"].split(" "): right_to_left = True li_langs.append( '<language id="%s" ' % lang ) if default_lang: li_langs.append( ' default="true" ' ) if right_to_left: li_langs.append( ' rtl="true" ' ) locale = Locale(lang) if locale is not None: li_langs.append( ' english-name="%s"' % locale.english_name ) li_langs.append( ' display-name="%s"' % locale.display_name ) li_langs.append(" />") li_langs.append("</languages>") if li_langs is None: return li_langs else: return li_langs.toString()
def execute(self, report, feature): if feature.getType().get("LID_ACCIDENTE") == None: # Si no es la tabla de accidentes no hacenos nada return titularidad_accidente = feature.get("TITULARIDAD_VIA") storeVehiculos = feature.getStore().getStoresRepository().getStore( "ARENA2_VEHICULOS") accidente = feature.get("ID_ACCIDENTE") if accidente != None: ## Rellenar por campos de la feature # Mantiene el none # no es lo mismo que llegue un None # a que se confirme porque no tiene valores en la tabla # de vehiculos con que es 0 conteoPorFeature = { 'NUM_TURISMOS': None, 'NUM_FURGONETAS': None, 'NUM_CAMIONES': None, 'NUM_AUTOBUSES': None, 'NUM_CICLOMOTORES': None, 'NUM_MOTOCICLETAS': None, 'NUM_BICICLETAS': None, 'NUM_OTROS_VEHI': None } for key in conteoPorFeature.keys(): value = feature.get(key) conteoPorFeature[key] = value ## Conteo por la tabla asociada de vehiculos builder = ExpressionUtils.createExpressionBuilder() expression = builder.eq(builder.variable("ID_ACCIDENTE"), builder.constant(accidente)).toString() #fset = storeVehiculos.getFeatureSet(expression) conteoPorTablas = { 'NUM_TURISMOS': 0, 'NUM_FURGONETAS': 0, 'NUM_CAMIONES': 0, 'NUM_AUTOBUSES': 0, 'NUM_CICLOMOTORES': 0, 'NUM_MOTOCICLETAS': 0, 'NUM_BICICLETAS': 0, 'NUM_OTROS_VEHI': 0 } fset = storeVehiculos.getFeatureSet(expression).iterable() for f in fset: tipoVehiculo = f.get("TIPO_VEHICULO") keyValue = self.getKeyFromTypeVehicle(tipoVehiculo) if keyValue != None: conteoPorTablas[keyValue] += 1 DisposeUtils.dispose(fset) DisposeUtils.dispose(storeVehiculos) toReport = False builder = StringBuilder() for key in conteoPorTablas.keys(): if conteoPorTablas[key] != conteoPorFeature[key]: if toReport: builder.append(", ") toReport = True builder.append(key + " valor:" + str(conteoPorFeature[key]) + " correccion:" + str(conteoPorTablas[key])) if toReport: report.add( feature.get("ID_ACCIDENTE"), CODERR_VEHICULOS_NO_COINCIDEN, "Vehiculos no coinciden: %s." % (builder.toString(), ), fixerId="UpdateCountVehicles", selected=True, NUM_TURISMOS=conteoPorTablas['NUM_TURISMOS'], NUM_FURGONETAS=conteoPorTablas['NUM_FURGONETAS'], NUM_CAMIONES=conteoPorTablas['NUM_CAMIONES'], NUM_AUTOBUSES=conteoPorTablas['NUM_AUTOBUSES'], NUM_CICLOMOTORES=conteoPorTablas['NUM_CICLOMOTORES'], NUM_MOTOCICLETAS=conteoPorTablas['NUM_MOTOCICLETAS'], NUM_BICICLETAS=conteoPorTablas['NUM_BICICLETAS'], NUM_OTROS_VEHI=conteoPorTablas['NUM_OTROS_VEHI'])