def end_test(self, result): # type: (TestResult) -> None """Called after each test.""" title = escape_xml(result.case.uid) if result.case.description: title = '<span title="%s">%s</span>' % \ (title, escape_xml(result.case.description)) if result.case.bugs or result.case.otrs: links = [] links += [ '<a href="%s">Bug #%d</a>' % (escape_xml(URI_BUG % bug), bug) for bug in result.case.bugs ] links += [ '<a href="%s">OTRS #%d</a>' % (escape_xml(URI_OTRS % tick), tick) for tick in result.case.otrs ] title = '%s (%s)' % (title, ', '.join(links)) msg = TestCodes.MESSAGE.get(result.reason, TestCodes.REASON_INTERNAL) colorname = TestCodes.COLOR.get(result.reason, 'BLACK') msg = '<span style="color:%s;">%s</span>' % \ (colorname.lower(), escape_xml(msg)) print('<tr><td>%s</td><td>%s</td></tr>' % (title, msg), file=self.stream) super(HTML, self).end_test(result)
def add_contacts(self, *nodes, **kwargs): context = kwargs.pop("context", None) if kwargs: raise TypeError("%r is ann invalid keyword argument for this function" % next(iter(kwargs.keys()))) contacts = [] for node in filter(bool, nodes): for typ in (Has_postal_address, Has_mobile, Has_fixed, Has_fax, Has_email): for link in node.links(outgoing & is_link(typ)): self._is_not_empty() label = "" if isinstance(link, Has_postal_address): self.add_address(link, link.postal) elif isinstance(link, Has_email): bullet = EMAIL_BULLET cnode = link.email contact = escape_xml(str(cnode).replace(" ", NBSP)) if isinstance(link, At_work): if context != At_work: label = str(qual_work).capitalize() elif isinstance(link, At_home): if context != At_home: label = str(qual_home).capitalize() if label: label = "<i>" + label + "</i>" else: bullet = TELEPHONE_BULLET cnode = link.tel contact = "<b>" + escape_xml(str(link.tel.relative(self.local))) + "</b>" if isinstance(link, Has_fixed): if isinstance(link, At_work): label = str(qual_work).capitalize() elif isinstance(link, At_home): label = str(qual_home).capitalize() else: label = str(multilang(en="Tel", es="Tlf")) else: if isinstance(link, Has_mobile): label = str(multilang(en="Mob", es="Móv")) else: assert isinstance(link, Has_fax) label = "Fax" if isinstance(link, At_work): label += " " + str(qual_work) elif isinstance(link, At_home): label += " " + str(qual_home) comments = [] for n in cnode, link: comment = getattr(n, "comment", None) if comment: comments.append(comment) if comments: comments = NBSP + "<i>" + "; ".join(escape_xml(str(c)) for c in comments) + "</i>" else: comments = "" if label: label += ": " label = bullet + " " + label contacts.append((label + contact).replace(" ", NBSP) + comments) self._para(" ".join(contacts), contacts_style)
def load_candidate_config(self, filename=None, config=None): """ Load candidate confguration. Populate the attribute candidate_config with the desired configuration and loads it into the router. You can populate it from a file or from a string. If you send both a filename and a string containing the configuration, the file takes precedence. :param filename: Path to the file containing the desired configuration. By default is None. :param config: String containing the desired configuration. """ configuration = '' if filename is None: configuration = config else: with open(filename) as f: configuration = f.read() rpc_command = '<CLI><Configuration>{configuration}</Configuration></CLI>'.format( configuration=escape_xml(configuration) # need to escape, otherwise will try to load invalid XML ) try: self._execute_rpc(rpc_command) except InvalidInputError as e: self.discard_config() raise InvalidInputError(e.message, self)
def add_entry(self, item): letter = text_sort_key(item.key)[:1].upper() or "Z" if self.sections: section = self.section while letter not in self.sections[section]: section += 1 self.flowables.append(ActionFlowable(("newSection", self.sections[section]))) self.flowables.append(FrameBreak()) self.section = section self.entry = [] self.indent = 0 self._empty_flag = True self.start_keep_together() self.entry.append(ActionFlowable(("entryTitle", Paragraph(escape_xml(item.key + "..."), continue_style)))) if item is not item.single: self.add_names([item.key], refname=item.single.key, bold=False, refstyle=toplevel_ref_style) else: self.add_names([item.key] + list(item.node.names())) if isinstance(item.node, Person): self.add_person(item.node) elif isinstance(item.node, Family): self.add_family(item.node) elif isinstance(item.node, Company): self.add_organisation(item.node) last_flowable = self.entry.pop() self.entry.append( KeepTogether([last_flowable, Rule(lineWidth=0.5, spaceBefore=2, spaceAfter=1)], maxHeight=4 * cm) ) self.entry.append(ActionFlowable(("entryTitle", None))) self.end_keep_together() self.flowables.extend(self.entry)
def load_candidate_config(self, filename=None, config=None): """ Load candidate confguration. Populate the attribute candidate_config with the desired configuration and loads it into the router. You can populate it from a file or from a string. If you send both a filename and a string containing the configuration, the file takes precedence. :param filename: Path to the file containing the desired configuration. By default is None. :param config: String containing the desired configuration. """ configuration = "" if filename is None: configuration = config else: with open(filename) as f: configuration = f.read() rpc_command = ( "<CLI><Configuration>{configuration}</Configuration></CLI>".format( configuration=escape_xml( configuration ) # need to escape, otherwise will try to load invalid XML )) try: self._execute_rpc(rpc_command) except InvalidInputError as e: self.discard_config() raise InvalidInputError(e.args[0], self)
def append_subtitle(self, from_ms, to_ms, content, new_paragraph=False, region=None, escape=True): """Append a subtitle to the end of the list. NO UNICODE ALLOWED! USE XML ENTITIES TO REPRESENT UNICODE CHARACTERS! """ if escape: content = escape_xml(content) content = self._fix_xml_content(content) p = self._create_subtitle_p(from_ms, to_ms, content) if region: p.set('region', region) if new_paragraph and len(self.last_div()) > 0: div = etree.SubElement(self._body, TTML + 'div') else: div = self.last_div() div.append(p) self._adjust_whitespace_after_append(div, p, new_paragraph)
def add_address(self, link, addr, prefix=""): self._is_not_empty() self._para(prefix + escape_xml(addr.as_string(with_country=False)), address_style) self.indent += 1 self.add_comments(link, addr) self.add_contacts(link, addr) self.indent -= 1
def submit(self): results = list() for fused_filename in self.fused_files: matlab_code = self.get_matlab_code(fused_filename) fused_report = fused_filename.replace( '.mat', '.results_%s' % get_timestamp_str()) submission_result = self.submit_matlab_job( matlab_code, report_file=fused_report, ) results.append(submission_result) if not results: raise BrainyProcessError(warning='Failed to find complete ' 'measurements.. check or restart previous' ' step') print( ''' <status action="%(step_name)s">submitting (%(results_count)d) fusion jobs.. <output>%(submission_result)s</output> </status> ''' % { 'step_name': self.step_name, 'results_count': len(results), 'submission_result': escape_xml(str(results)), }) self.set_flag('submitted')
def submit(self): results = list() for batch_filename in self.batch_files: matlab_code = self.get_matlab_code(batch_filename) batch_report = batch_filename.replace( '.mat', '.results_%s' % get_timestamp_str()) submission_result = self.submit_matlab_job( matlab_code, report_file=batch_report, ) results.append(submission_result) if not results: # We want to explicitly check for errors and report them, if any # found. self.check_logs_for_errors() # If no errors were found, we still have to report a warning! raise BrainyProcessError(warning='Failed to find any batches.. ' 'check or restart previous step') print( ''' <status action="%(step_name)s">submitting (%(batch_count)d) batches.. <output>%(submission_result)s</output> </status> ''' % { 'step_name': self.step_name, 'batch_count': len(results), 'submission_result': escape_xml(str(results)), }) self.set_flag('submitted')
def append_subtitle(self, from_ms, to_ms, content, new_paragraph=False, escape=True): """Append a subtitle to the end of the list. NO UNICODE ALLOWED! USE XML ENTITIES TO REPRESENT UNICODE CHARACTERS! """ begin_value = milliseconds_to_time_clock_exp(from_ms) begin = 'begin="%s"' % begin_value if begin_value is not None else '' end_value = milliseconds_to_time_clock_exp(to_ms) end = 'end="%s"' % end_value if end_value is not None else '' if escape: content = escape_xml(content) content = self._fix_xml_content(content) p = etree.fromstring(SubtitleSet.SUBTITLE_XML % (begin, end, content)) # fromstring has no sane way to set an attribute namespace (yay) spans = [el for el in p.getchildren() if el.tag.endswith('span')] for span in spans: # so we delete the old attrib, and add the new one with the # prefixed namespace for attr_name, value in span.attrib.items(): if attr_name in ('fontStyle', 'textDecoration', 'fontWeight'): span.set('{%s}%s' % (TTS_NAMESPACE_URI , attr_name), value) del span.attrib[attr_name] div = find_els(self._ttml, '/tt/body/div')[-1] if new_paragraph: body = find_els(self._ttml, '/tt/body')[-1] div = etree.fromstring(SubtitleSet.SUBTITLE_DIV_XML) body.append(div) div.append(p)
def append_subtitle(self, from_ms, to_ms, content, new_paragraph=False, escape=True): """Append a subtitle to the end of the list. NO UNICODE ALLOWED! USE XML ENTITIES TO REPRESENT UNICODE CHARACTERS! """ begin_value = milliseconds_to_time_clock_exp(from_ms) begin = 'begin="%s"' % begin_value if begin_value is not None else '' end_value = milliseconds_to_time_clock_exp(to_ms) end = 'end="%s"' % end_value if end_value is not None else '' if escape: content = escape_xml(content) p = etree.fromstring(SubtitleSet.SUBTITLE_XML % (begin, end, content)) # fromstring has no sane way to set an attribute namespace (yay) spans = [el for el in p.getchildren() if el.tag.endswith('span')] for span in spans: # so we delete the old attrib, and add the new one with the # prefixed namespace for attr_name, value in span.attrib.items(): if attr_name in ('fontStyle', 'textDecoration', 'fontWeight'): span.set('{%s}%s' % (TTS_NAMESPACE_URI , attr_name), value) del span.attrib[attr_name] div = find_els(self._ttml, '/tt/body/div')[-1] if new_paragraph: body = find_els(self._ttml, '/tt/body')[-1] div = etree.fromstring(SubtitleSet.SUBTITLE_DIV_XML) body.append(div) div.append(p)
def export_opml(self): OPML_START = """<?xml version="1.0" encoding="UTF-8"?> <!-- OPML generated by FeedCrunch.io --> <opml version="1.1"> <head> <title>User = """ + self.username + """ - FeedCrunch Feeds Export</title> </head> <body> """ BODY = "" for subscription in self.rel_sub_feed.all(): sub_link = subscription.link() sub_title = escape_xml(subscription.title) BODY += '<outline type="rss" xmlUrl="' + sub_link + '" htmlUrl="' + sub_link + '" title="' + sub_title + '"/>' BODY += '\n' OPML_END = """</body> </opml> """ #opml = re.sub('\s+',' ', OPML_START + OPML_END) opml = OPML_START + BODY + OPML_END return opml.replace("\t", "")
def resubmit(self): results = list() for fused_filename in self.fused_files: matlab_code = self.get_matlab_code(fused_filename) fused_report = 'DataFusion_' + fused_filename.replace( '.mat', '.results_%s' % get_timestamp_str()) resubmission_result = self.submit_matlab_job( matlab_code, report_file=fused_report, is_resubmitting=True, ) results.append(resubmission_result) if not results: raise BrainyProcessError(warning='Failed to resubmit data fusion') print( ''' <status action="%(step_name)s">resubmitting (%(results_count)d) fusion jobs.. <output>%(resubmission_result)s</output> </status> ''' % { 'step_name': self.step_name, 'results_count': len(results), 'resubmission_result': escape_xml(str(results)), }) self.set_flag('resubmitted') super(CPDataFusion, self).resubmit()
def getText(self): l = self.source.list lut = self.source.lut conv_args = self.converter_arguments # now build a ["string", 1, "string", 2]-styled list, with indices into the # list to avoid lookup of item name for each entry lutlist = [ ] for element in conv_args: if isinstance(element, basestring): lutlist.append((element, None)) elif isinstance(element, ListItem): lutlist.append((lut[element.name], element.filternum)) elif isinstance(element, ListMacroItem): lutlist.append((element.macrodict[element.macroname], None)) else: raise Exception("neither string, ListItem nor ListMacroItem") # now, for the huge list, do: strlist = [ ] append = strlist.append for item in l: for (element, filternum) in lutlist: #None becomes "" curitem = "" if filternum: #filter out "non-displayable" Characters - at the very end, do it the hard way... curitem = str(item[element]).replace('\xc2\x86', '').replace('\xc2\x87', '').replace("\x19", "").replace("\x1c", "").replace("\x1e", "").decode("utf-8", "ignore").encode("utf-8") if curitem is None: curitem = "" else: if element is None: element = "" if not filternum: append(element) elif filternum == 2: append(curitem.replace("\\", "\\\\").replace("\n", "\\n").replace('"', '\\"')) elif filternum == 3: append( escape_xml( curitem )) elif filternum == 4: append(curitem.replace("%", "%25").replace("+", "%2B").replace('&', '%26').replace('?', '%3f').replace(' ', '+')) elif filternum == 5: append(quote(curitem)) elif filternum == 6: from time import localtime time = int(float(curitem)) or 0 t = localtime(time) append("%02d:%02d" % (t.tm_hour, t.tm_min)) elif filternum == 7: from time import localtime time = int(float(curitem)) or 0 t = localtime(time) append("%d min" % (time / 60)) else: append(curitem) # (this will be done in c++ later!) return ''.join(strlist)
def add_works_at(self, org): for link in sorted(org.links(incoming & is_link(Works_at))): self.start_keep_together() # If the person has a top level reference to the company entry in # which this organisation appears, or has no top level entry but is # a principal of the company, then list the person here. # Otherwise, list a reference to the entry where the person is # listed. com = org if isinstance(org, Company) else org.company() top = self.refs.get(link.person) position = "" if link.position: self._is_not_empty() position = ", " + escape_xml(str(link.position)) if top is com or (top is None and (link.is_head or self.predicate(link))): self._is_not_empty() # Special case: if the person has no top level entry but # belongs to a family that does, then list a reference to that # family. for linkf in link.person.links(outgoing & is_link(Belongs_to)): if linkf.family in self.refs: self.add_names( [next(link.person.sort_keys())], suffix=position, refname=next(linkf.family.sort_keys()), bullet=RIGHT_ARROW, comments=self.all_comments(link), ) self.indent += 2 self.add_contacts(link, context=At_work) self.indent -= 2 break else: self.add_names( link.person.names(), suffix=position, bullet=EM_DASH, comments=self.all_comments(link) ) self.indent += 1 self.add_person(link.person, link, show_work=False) self.indent -= 1 elif top is not None: name = next(link.person.sort_keys()) if top is link.person: self.add_names([name], bullet=RIGHT_ARROW, suffix=position, comments=self.all_comments(link)) self.indent += 2 self.add_contacts(link, context=At_work) self.indent -= 2 else: self.add_names( [name], bullet=EM_DASH, refname=next(top.sort_keys()), bold=True, suffix=position, comments=self.all_comments(link), ) self.indent += 2 self.add_contacts(link, context=At_work) self.indent -= 2 self.end_keep_together()
def getText(self): l = self.source.simplelist conv_args = self.converter_arguments list = [] append = list.append for element in conv_args: if isinstance(element, basestring): append((element, None)) elif isinstance(element, ListItem): append((element, element.filternum)) elif isinstance(element, ListMacroItem): append(element.macrodict[element.macroname], None) else: raise Exception("neither string, ListItem nor ListMacroItem") strlist = [] append = strlist.append for item in l: if item is None: item = "" else: # filter out "non-displayable" Characters - at the very end, do it the hard way... item = str( item ) # .replace('\xc2\x86', '').replace('\xc2\x87', '').replace("\x19", "").replace("\x1c", "").replace("\x1e", "").decode("utf-8", "ignore").encode("utf-8") for (element, filternum) in list: if not filternum: append(element) elif filternum == 2: append(item.replace("\\", "\\\\").replace("\n", "\\n").replace('"', '\\"')) elif filternum == 3: append(escape_xml(item)) elif filternum == 4: append( item.replace("%", "%25") .replace("+", "%2B") .replace("&", "%26") .replace("?", "%3f") .replace(" ", "+") ) elif filternum == 5: append(quote(item)) elif filternum == 6: from time import localtime time = int(item) or 0 t = localtime(time) append("%02d:%02d" % (t.tm_hour, t.tm_min)) elif filternum == 7: time = int(item) or 0 append("%d min" % (time / 60)) else: append(item) # (this will be done in c++ later!) return "".join(strlist)
def _execute_config_show(self, show_command, delay_factor=0.1): """ Executes a configuration show-type command. """ rpc_command = "<CLI><Configuration>{show_command}</Configuration></CLI>".format( show_command=escape_xml(show_command)) response = self._execute_rpc(rpc_command, delay_factor=delay_factor) raw_response = response.xpath(".//CLI/Configuration")[0].text return raw_response.strip() if raw_response else ""
def _execute_show(self, show_command): """ Executes an operational show-type command. """ rpc_command = "<CLI><Exec>{show_command}</Exec></CLI>".format( show_command=escape_xml(show_command)) response = self._execute_rpc(rpc_command) raw_response = response.xpath(".//CLI/Exec")[0].text return raw_response.strip() if raw_response else ""
def getText(self): l = self.source.simplelist conv_args = self.converter_arguments list = [] for element in conv_args: if isinstance(element, basestring): list.append((element, None)) elif isinstance(element, ListItem): list.append((element, element.filternum)) elif isinstance(element, ListMacroItem): list.append(element.macrodict[element.macroname], None) else: raise Exception("neither string, ListItem nor ListMacroItem") strlist = [] append = strlist.append for item in l: if item is None: item = "" for (element, filternum) in list: #filter out "non-displayable" Characters - at the very end, do it the hard way... item = str(item).replace('\xc2\x86', '').replace( '\xc2\x87', '').replace("\x19", "").replace("\x1c", "").replace( "\x1e", "").decode("utf-8", "ignore").encode("utf-8") if not filternum: append(element) elif filternum == 2: append( item.replace("\\", "\\\\").replace("\n", "\\n").replace( '"', '\\"')) elif filternum == 3: append(escape_xml(item)) elif filternum == 4: append( item.replace("%", "%25").replace("+", "%2B").replace( '&', '%26').replace('?', '%3f').replace(' ', '+')) elif filternum == 5: append(quote(item)) elif filternum == 6: time = parseint(item) or 0 t = localtime(time) append("%02d:%02d" % (t.tm_hour, t.tm_min)) elif filternum == 7: time = parseint(item) or 0 t = localtime(time) append("%d min" % (time / 60)) else: append(item) # (this will be done in c++ later!) return ''.join(strlist)
def _execute_config_show(self, show_command, delay_factor=.1): """ Executes a configuration show-type command. """ rpc_command = '<CLI><Configuration>{show_command}</Configuration></CLI>'.format( show_command=escape_xml(show_command) ) response = self._execute_rpc(rpc_command, delay_factor=delay_factor) raw_response = response.xpath('.//CLI/Configuration')[0].text return raw_response.strip() if raw_response else ''
def end_test(self, result): """Called after each test.""" print >> self.stream, '<run>' try: mime, content = result.artifacts['stdout'] except KeyError: pass else: print >> self.stream, '<log encoding="hexBinary">%s</log>' % \ (encode(content, 'hex'),) print >> self.stream, '<result>%d</result>' % (result.result,) print >> self.stream, '<duration>%d</duration>' % \ (result.duration or -1,) print >> self.stream, '<displayName>%s</displayName>' % \ (escape_xml(result.case.uid),) print >> self.stream, '<description>%s</description>' % \ (escape_xml(result.case.description or ''),) print >> self.stream, '</run>' super(Jenkins, self).end_test(result)
def _execute_show(self, show_command): """ Executes an operational show-type command. """ rpc_command = '<CLI><Exec>{show_command}</Exec></CLI>'.format( show_command=escape_xml(show_command) ) response = self._execute_rpc(rpc_command) raw_response = response.xpath('.//CLI/Exec')[0].text return raw_response.strip() if raw_response else ''
def _create_pango_markup(message_string: str) -> str: # Escape all existing potential markup before # processing workd_on_str = escape_xml(message_string) workd_on_str = _process_links(workd_on_str) # For now actual markdown isn't actually implemented pass return workd_on_str
def getHTML(self, id): return ( escape_xml(self.source.text) .replace("\xc2\x86", "") .replace("\xc2\x87", "") .replace("\x19", "") .replace("\x1c", "") .replace("\x1e", "") .decode("utf-8", "ignore") .encode("utf-8") )
def formatMeasurementText(txt, escape=False, preformat=True): ''' Helper to correctly convert CTest measurements to valid HTML (with or without special character escaping and in an optional <pre> tag). ''' from xml.sax.saxutils import escape as escape_xml from codecs import encode if escape: txt = escape_xml(txt) if preformat: txt = '<pre>{0}</pre>'.format(txt) return txt
def end_test(self, result): """Called after each test.""" title = escape_xml(result.case.uid) if result.case.description: title = '<span title="%s">%s</span>' % \ (title, escape_xml(result.case.description)) if result.case.bugs or result.case.otrs: links = [] links += ['<a href="%s">Bug #%d</a>' % \ (escape_xml(URI_BUG % bug), bug) \ for bug in result.case.bugs] links += ['<a href="%s">OTRS #%d</a>' % \ (escape_xml(URI_OTRS % tick), tick) \ for tick in result.case.otrs] title = '%s (%s)' % (title, ', '.join(links)) msg = TestCodes.MESSAGE.get(result.reason, TestCodes.REASON_INTERNAL) colorname = TestCodes.COLOR.get(result.reason, 'BLACK') msg = '<span style="color:%s;">%s</span>' % \ (colorname.lower(), escape_xml(msg)) print >> self.stream, '<tr><td>%s</td><td>%s</td></tr>' % (title, msg) super(HTML, self).end_test(result)
def export_to_opml(): storage = leselys.core.storage header = """<?xml version="1.0"?> <opml version="1.0"> <head> <title>Leselys feeds export</title> </head> <body> """ body = "" footer = """\n</body>\n</opml>""" for feed in storage.get_feeds(): body += """ <outline text="%s" title="%s" type="rss" xmlUrl="%s" htmlUrl="%s"/>""" % (escape_xml( feed['title']), escape_xml(feed['title']), escape_xml( feed['url']), escape_xml(feed['url'])) return header + body + footer
def getText(self): l = self.source.simplelist conv_args = self.converter_arguments list = [] for element in conv_args: if isinstance(element, basestring): list.append((element, None)) elif isinstance(element, ListItem): list.append((element, element.filternum)) elif isinstance(element, ListMacroItem): list.append(element.macrodict[element.macroname], None) else: raise Exception("neither string, ListItem nor ListMacroItem") strlist = [] append = strlist.append for item in l: if item is None: item = "" for (element, filternum) in list: if not filternum: append(element) elif filternum == 2: append(str(item).replace("\\", "\\\\").replace("\n", "\\n").replace('"', '\\"')) elif filternum == 3: append(escape_xml(str(item))) elif filternum == 4: append( str(item) .replace("%", "%25") .replace("+", "%2B") .replace("&", "%26") .replace("?", "%3f") .replace(" ", "+") ) elif filternum == 5: append(quote(str(item))) elif filternum == 6: time = parseint(item) or 0 t = localtime(time) append("%02d:%02d" % (t.tm_hour, t.tm_min)) elif filternum == 7: time = parseint(item) or 0 t = localtime(time) append("%d min" % (time / 60)) else: append(str(item)) # (this will be done in c++ later!) return "".join(strlist)
def getText(self): l = self.source.simplelist conv_args = self.converter_arguments list = [] for element in conv_args: if isinstance(element, basestring): list.append((element, None)) elif isinstance(element, ListItem): list.append((element, element.filternum)) elif isinstance(element, ListMacroItem): list.append(element.macrodict[element.macroname], None) else: raise Exception("neither string, ListItem nor ListMacroItem") strlist = [] append = strlist.append for item in l: if item is None: item = "" for (element, filternum) in list: if not filternum: append(element) elif filternum == 2: append( str(item).replace("\\", "\\\\").replace("\n", "\\n").replace( '"', '\\"')) elif filternum == 3: append(escape_xml(str(item))) elif filternum == 4: append( str(item).replace("%", "%25").replace( "+", "%2B").replace('&', '%26').replace( '?', '%3f').replace(' ', '+')) elif filternum == 5: append(quote(str(item))) elif filternum == 6: time = parseint(item) or 0 t = localtime(time) append("%02d:%02d" % (t.tm_hour, t.tm_min)) elif filternum == 7: time = parseint(item) or 0 t = localtime(time) append("%d min" % (time / 60)) else: append(str(item)) # (this will be done in c++ later!) return ''.join(strlist)
def export_to_opml(): storage = feedcollector.core.storage header = """<?xml version="1.0"?> <opml version="1.0"> <head> <title>Feedcollector feeds export</title> </head> <body> """ body = "" footer = """\n</body>\n</opml>""" for feed in storage.get_feeds(): body += """ <outline text="%s" title="%s" type="rss" xmlUrl="%s" htmlUrl="%s"/>""" % ( escape_xml(feed['title']), escape_xml(feed['title']), escape_xml(feed['url']), escape_xml(feed['url'])) return header + body + footer
def appendListItem(item, filternum, append): if filternum == 2: append( item.replace("\\", "\\\\").replace("\n", "\\n").replace('"', '\\"')) elif filternum == 3: append(escape_xml(item)) elif filternum == 4: append( item.replace("%", "%25").replace("+", "%2B").replace( '&', '%26').replace('?', '%3f').replace(' ', '+')) elif filternum == 5: append(quote(item)) elif filternum == 6: from time import localtime time = 0 try: time = int(item) t = localtime(time) append("%04d-%02d-%02d" % (t.tm_year, t.tm_mon, t.tm_mday)) except: append("---") elif filternum == 7: from time import localtime time = 0 try: time = int(item) t = localtime(time) append("%02d:%02d" % (t.tm_hour, t.tm_min)) except: append("--:--") elif filternum == 8: time = 0 try: time = int(item) append("%d min" % (time / 60)) except: append("-- min") elif filternum == 9: append(quote(item).replace("\"", "%22")) elif filternum == 10: if item == "None": append("n/a") else: append(item.replace("\n", "<br />")) elif filternum == 11: append(item.replace("\"", """)) else: append(item)
def appendListItem(item, filternum, append): if filternum == webifHandler.FILTER_JAVASCRIPT: append( item.replace("\\", "\\\\").replace("\n", "\\n").replace('"', '\\"')) elif filternum == webifHandler.FILTER_XML: append(escape_xml(item)) elif filternum == webifHandler.FILTER_URI: append( item.replace("%", "%25").replace("+", "%2B").replace( '&', '%26').replace('?', '%3f').replace(' ', '+')) elif filternum == webifHandler.FILTER_URLENCODE: append(quote(item)) elif filternum == webifHandler.FILTER_DATE: from time import localtime time = 0 try: time = int(item) t = localtime(time) append("%04d-%02d-%02d" % (t.tm_year, t.tm_mon, t.tm_mday)) except: append("---") elif filternum == webifHandler.FILTER_TIME: from time import localtime time = 0 try: time = int(item) t = localtime(time) append("%02d:%02d" % (t.tm_hour, t.tm_min)) except: append("--:--") elif filternum == webifHandler.FILTER_MINUTES: time = 0 try: time = int(item) append("%d min" % (time / 60)) except: append("-- min") elif filternum == webifHandler.FILTER_URI_ATTRIB: append(quote(item).replace("\"", "%22")) elif filternum == webifHandler.FILTER_HTML: if item == "None": append("n/a") else: append(item.replace("\n", "<br />")) elif filternum == webifHandler.FILTER_ATTRIBUTE: append(item.replace("\"", """)) else: append(item)
def submit(self): matlab_code = self.get_matlab_code() submission_result = self.submit_matlab_job(matlab_code) print( ''' <status action="%(step_name)s">submitting <output>%(submission_result)s</output> </status> ''' % { 'step_name': self.step_name, 'submission_result': escape_xml(submission_result), }) self.set_flag('submitted')
def resubmit(self): resubmission_results = list() output_batches = self.get_out_files() result_batches = self.get_result_files() for batch_filename in self.batch_files: # Resubmit only those files that have no data, i.e. failed with no # output. batch_prefix = self.parse_batch(batch_filename) if batch_prefix in output_batches \ and batch_prefix in result_batches: # This batch is complete. Do not resubmit it. print '<!-- CP batch %s is complete. Skipping.. -->' % \ batch_filename continue # Batch has not produced any output yet. We can resubmit it. matlab_code = self.get_matlab_code(batch_filename) batch_report = batch_filename.replace( '.mat', '.results_%s' % get_timestamp_str()) resubmission_result = self.submit_matlab_job( matlab_code, report_file=batch_report, is_resubmitting=True, ) resubmission_results.append(resubmission_result) if not resubmission_results: # We want to explicitly check for errors and report them, if any # found. self.check_logs_for_errors() # If no errors were found, we still have to report a warning! raise BrainyProcessError(warning='Failed to find any batches to ' 'resubmit. Check or restart previous ' 'step.') print( ''' <status action="%(step_name)s">resubmitting (%(batch_count)d) batches.. <output>%(resubmission_result)s</output> </status> ''' % { 'step_name': self.step_name, 'batch_count': len(resubmission_results), 'resubmission_result': escape_xml(str(resubmission_results)), }) self.set_flag('resubmitted') super(CPCluster, self).resubmit()
def resubmit(self): matlab_code = self.get_matlab_code() resubmission_result = self.submit_matlab_job(matlab_code, is_resubmitting=True) print( ''' <status action="%(step_name)s">resubmitting <output>%(resubmission_result)s</output> </status> ''' % { 'step_name': self.step_name, 'resubmission_result': escape_xml(resubmission_result), }) self.set_flag('resubmitted') super(PreCluster, self).resubmit()
def add_names( self, names, refname=None, bullet="", bold=True, prefix="", suffix="", comments=(), style=name_style, akastyle=aka_style, refstyle=ref_style, comstyle=name_comment_style, ): names = uniq(names) first = next(names) if bold: startbold, endbold = "<b>", "</b>" else: startbold, endbold = "", "" if bullet: bullet = bullet + " " bulletWidth = stringWidth(bullet, style.fontName, style.fontSize) if hasattr(first, "sortsplit"): pre, sort, post = first.sortsplit() name = "".join([escape_xml(pre), startbold, escape_xml(sort), endbold, escape_xml(post)]) else: name = startbold + escape_xml(str(first)) + endbold first = str(first) para = [prefix, name, suffix] self._para("".join(para), style, bullet=bullet) # Join AKA names onto the end of the name. para = [] for aka in map(str, names): if aka != first: para += [" =", NBSP, escape_xml(aka)] if para: self._para("".join(para), akastyle, returnIndent=bulletWidth, join_to_prev=True) # Join reference arrow and text onto end of name. if refname: para = [" ", RIGHT_ARROW, NBSP, escape_xml(refname)] self._para("".join(para), refstyle, returnIndent=bulletWidth, join_to_prev=True) # Join comments onto end of name. para = [] for c in comments: para += [" ", EN_DASH, NBSP, escape_xml(str(c))] if para: self._para("".join(para), comstyle, returnIndent=bulletWidth, join_to_prev=True)
def appendListItem(item, filternum, append): if filternum == 2: append(item.replace("\\", "\\\\").replace("\n", "\\n").replace('"', '\\"')) elif filternum == 3: append( escape_xml( item )) elif filternum == 4: append(item.replace("%", "%25").replace("+", "%2B").replace('&', '%26').replace('?', '%3f').replace(' ', '+')) elif filternum == 5: append(quote(item)) elif filternum == 6: from time import localtime time = 0 try: time = int(item) t = localtime(time) append("%04d-%02d-%02d" % (t.tm_year, t.tm_mon, t.tm_mday)) except: append("---") elif filternum == 7: from time import localtime time = 0 try: time = int(item) t = localtime(time) append("%02d:%02d" % (t.tm_hour, t.tm_min)) except: append("--:--") elif filternum == 8: time = 0 try: time = int(item) append("%d min" % (time / 60)) except: append("-- min") elif filternum == 9: append(quote(item).replace("\"", "%22")) elif filternum == 10: if item == "None": append("n/a") else: append(item.replace("\n", "<br />")) elif filternum == 11: append( item.replace("\"", """)) else: append(item)
def appendListItem(item, filternum, append): if filternum == webifHandler.FILTER_JAVASCRIPT: append(item.replace("\\", "\\\\").replace("\n", "\\n").replace('"', '\\"')) elif filternum == webifHandler.FILTER_XML: append( escape_xml( item )) elif filternum == webifHandler.FILTER_URI: append(item.replace("%", "%25").replace("+", "%2B").replace('&', '%26').replace('?', '%3f').replace(' ', '+')) elif filternum == webifHandler.FILTER_URLENCODE: append(quote(item)) elif filternum == webifHandler.FILTER_DATE: from time import localtime time = 0 try: time = int(item) t = localtime(time) append("%04d-%02d-%02d" % (t.tm_year, t.tm_mon, t.tm_mday)) except: append("---") elif filternum == webifHandler.FILTER_TIME: from time import localtime time = 0 try: time = int(item) t = localtime(time) append("%02d:%02d" % (t.tm_hour, t.tm_min)) except: append("--:--") elif filternum == webifHandler.FILTER_MINUTES: time = 0 try: time = int(item) append("%d min" % (time / 60)) except: append("-- min") elif filternum == webifHandler.FILTER_URI_ATTRIB: append(quote(item).replace("\"", "%22")) elif filternum == webifHandler.FILTER_HTML: if item == "None": append("n/a") else: append(item.replace("\n", "<br />")) elif filternum == webifHandler.FILTER_ATTRIBUTE: append( item.replace("\"", """)) else: append(item)
def append_subtitle(self, from_ms, to_ms, content, new_paragraph=False, escape=True): """Append a subtitle to the end of the list. NO UNICODE ALLOWED! USE XML ENTITIES TO REPRESENT UNICODE CHARACTERS! """ if escape: content = escape_xml(content) content = self._fix_xml_content(content) p = self._create_subtitle_p(from_ms, to_ms, content) if new_paragraph: div = etree.SubElement(self._body, TTML + 'div') else: div = self.last_div() div.append(p) self._adjust_whitespace_after_append(div, p, new_paragraph)
def generate_configuration_xml(self, configuration, config_file, exe_path, duration, name, **kwargs): # See: https://www.flocklab.ethz.ch/wiki/wiki/Public/Man/XmlConfig # for the details of the xml config short_name = self.short_name(name) if len(short_name) > 45: print( f"Name {short_name} too long at {len(short_name)} characters needs to be 45, truncating it" ) short_name = short_name[:45] duration_secs = int(duration.total_seconds()) name = escape_xml(name) short_name = escape_xml(short_name) image_id = self.fix_image_id(short_name) print('<?xml version="1.0" encoding="UTF-8"?>', file=config_file) print('<!-- $Id: flocklab.xml {} {} $ -->'.format( datetime.now().strftime("%Y-%m-%d %H:%M:%S Z%Z"), getpass.getuser()), file=config_file) print('', file=config_file) print( '<testConf xmlns="http://www.flocklab.ethz.ch" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.flocklab.ethz.ch xml/flocklab.xsd">', file=config_file) print(' <generalConf>', file=config_file) print(' <name>{}</name>'.format(short_name), file=config_file) print(' <description>{}</description>'.format(name), file=config_file) print(' <scheduleAsap>', file=config_file) print(' <durationSecs>{}</durationSecs>'.format( duration_secs), file=config_file) print(' </scheduleAsap>', file=config_file) print(' <emailResults>yes</emailResults>', file=config_file) print(' </generalConf>', file=config_file) nodes = configuration.topology.node_ids() print(' <targetConf>', file=config_file) print(' <obsIds>{}</obsIds>'.format(nodes), file=config_file) print(' <targetIds>{}</targetIds>'.format(nodes), file=config_file) print(' <voltage>3.3</voltage>', file=config_file) print(' <embeddedImageId>{}</embeddedImageId>'.format(image_id), file=config_file) print(' </targetConf>', file=config_file) # serialConf tests show: # - Do not use the serial port, serial out is over usb print(' <serialConf>', file=config_file) print(' <obsIds>{}</obsIds>'.format(nodes), file=config_file) print(' <port>usb</port>', file=config_file) print(' <baudrate>115200</baudrate>', file=config_file) print(' <mode>ascii</mode>', file=config_file) # Note that we don't actually use the remoteIp. # If no IP address is specified then our IP address will be used # IPv6 is not supported, so if we send an IPv6 address then an error # will be returned. To fix this we just supply a valid IPv4 address. # See: https://gitlab.ethz.ch/tec/public/flocklab/wikis/Man/XmlConfig print(' <remoteIp>127.0.0.1</remoteIp>', file=config_file) print(' </serialConf>', file=config_file) with open(exe_path, "rb") as exe_file: encoded_file = base64.b64encode(exe_file.read()).decode("utf-8") platform = self._get_platform(configuration.topology.platform) print(' <imageConf>', file=config_file) print(' <embeddedImageId>{}</embeddedImageId>'.format(image_id), file=config_file) print(' <name>{}</name>'.format(short_name), file=config_file) print(' <description>{}</description>'.format(name), file=config_file) print(' <platform>{}</platform>'.format(platform), file=config_file) print(' <os>tinyos</os>', file=config_file) print(' <data>{}</data>'.format(encoded_file), file=config_file) print(' </imageConf>', file=config_file) delayed_boot_secs = flocklab.build_arguments().get( "DELAYED_BOOT_TIME_MINUTES", 0) * 60 print(' <powerProfilingConf>', file=config_file) print(' <obsIds>{}</obsIds>'.format(nodes), file=config_file) print(' <profConf>', file=config_file) print(' <durationMillisecs>{}</durationMillisecs>'.format( (duration_secs - delayed_boot_secs) * 1000), file=config_file) print(' <relativeTime>', file=config_file) print(' <offsetSecs>{}</offsetSecs>'.format( delayed_boot_secs), file=config_file) print(' <offsetMicrosecs>0</offsetMicrosecs>', file=config_file) print(' </relativeTime>', file=config_file) print(' <samplingDivider>{}</samplingDivider>'.format(128), file=config_file) print(' </profConf>', file=config_file) print(' </powerProfilingConf>', file=config_file) # We want to trace the actions of the Led pins for two reasons: # 1. To enable GUI support for displaying the Leds # 2. Led 2 is used to indicate if the radio is on or off print(' <gpioTracingConf>', file=config_file) print(' <obsIds>{}</obsIds>'.format(nodes), file=config_file) print(' <pinConf>', file=config_file) print(' <pin>LED1</pin>', file=config_file) print(' <edge>both</edge>', file=config_file) print(' <mode>continuous</mode>', file=config_file) print(' </pinConf>', file=config_file) print(' <pinConf>', file=config_file) print(' <pin>LED2</pin>', file=config_file) print(' <edge>both</edge>', file=config_file) print(' <mode>continuous</mode>', file=config_file) print(' </pinConf>', file=config_file) print(' <pinConf>', file=config_file) print(' <pin>LED3</pin>', file=config_file) print(' <edge>both</edge>', file=config_file) print(' <mode>continuous</mode>', file=config_file) print(' </pinConf>', file=config_file) print(' </gpioTracingConf>', file=config_file) print('</testConf>', file=config_file)
def begin_section(self, section): """Called before each section.""" super(HTML, self).begin_section(section) print >> self.stream, '<h2>Section %s</h2>' % (escape_xml(section), ) print >> self.stream, '<table>'
def markup_for_match(text, start, end, matchtarget=None): source = matchtarget or text return '%s<b>%s</b>%s%s' % (escape_xml(source[0:start]), escape_xml(source[start:end]), escape_xml(source[end:]), matchtarget and (' - <i>' + text + '</i>') or '')
def add_person(self, per, link=None, show_family=True, show_work=True): self.add_comments(per) if per.birthday(): self._is_not_empty() self._para(str(multilang(en="Birthday", es="Fecha nac.")) + ": " + str(per.birthday()), birthday_style) self.indent += 1 self.add_addresses(per) self.add_contacts(per, link) self.indent -= 1 if show_family: for link in per.links(outgoing & is_link(Belongs_to)): # If this person's family has no top level entry, or has a top # level reference to this person, then list the family here. # Otherwise, just list a reference to the family. top = self.refs.get(link.family, per) if top is per: self.add_names(link.family.names(), bullet=EM_DASH) self.indent += 1 self.add_comments(link) self.add_family(link.family, link, show_members=False) self.indent -= 1 else: self.add_names( [next(link.family.sort_keys())], bullet=RIGHT_ARROW, bold=False, comments=self.all_comments(link), ) self.indent += 2 self.add_contacts(link, context=At_home) self.indent -= 2 if show_work: for link in per.links(outgoing & is_link(Works_at)): position = "" if link.position: self._is_not_empty() position = escape_xml(str(link.position)) + ", " # If the organisation/residence where this person works has no # top level entry or has a top level reference to this person, # then list the organisation/residence here. Otherwise, just # list a reference to its top level entry. top = self.refs.get(link.org, per) if top is per: if isinstance(link.org, Residence): self.indent += 1 self.add_address(link, link.org, prefix="<i>" + str(qual_work).capitalize() + ":</i> ") self.indent -= 1 else: self.add_names(link.org.names(), bullet=EM_DASH, prefix=position) self.indent += 1 self.add_comments(link) self.add_organisation(link.org, link, show_parents=True, show_workers=False) self.indent -= 1 else: self.add_names( [next(link.org.sort_keys())], bullet=RIGHT_ARROW, bold=False, prefix=position, comments=self.all_comments(link), ) self.indent += 2 self.add_contacts(link, context=At_work) self.indent -= 2
def add_comments(self, *nodes): for com in self.all_comments(*nodes): self._is_not_empty() self._para(escape_xml(str(com)), comment_style)
def getHTML(self, id): return escape_xml(self.source.text).replace("\x19", "").replace( "\x1c", "").replace("\x1e", "")
def begin_section(self, section): """Called before each secion.""" super(HTML, self).begin_section(section) print >> self.stream, '<h2>Section %s</h2>' % (escape_xml(section),) print >> self.stream, '<table>'
def getText(self): l = self.source.list lut = self.source.lut conv_args = self.converter_arguments # now build a ["string", 1, "string", 2]-styled list, with indices into the # list to avoid lookup of item name for each entry lutlist = [] for element in conv_args: if isinstance(element, basestring): lutlist.append((element, None)) elif isinstance(element, ListItem): lutlist.append((lut[element.name], element.filternum)) elif isinstance(element, ListMacroItem): lutlist.append((element.macrodict[element.macroname], None)) else: raise Exception("neither string, ListItem nor ListMacroItem") # now, for the huge list, do: strlist = [] append = strlist.append for item in l: for (element, filternum) in lutlist: # None becomes "" curitem = "" if filternum: curitem = item[element] if curitem is None: curitem = "" else: if element is None: element = "" if not filternum: append(element) elif filternum == 2: append(str(curitem).replace("\\", "\\\\").replace("\n", "\\n").replace('"', '\\"')) elif filternum == 3: append(escape_xml(str(curitem))) elif filternum == 4: append( str(curitem) .replace("%", "%25") .replace("+", "%2B") .replace("&", "%26") .replace("?", "%3f") .replace(" ", "+") ) elif filternum == 5: append(quote(str(curitem))) elif filternum == 6: from time import localtime time = int(float(curitem)) or 0 t = localtime(time) append("%02d:%02d" % (t.tm_hour, t.tm_min)) elif filternum == 7: from time import localtime time = int(float(curitem)) or 0 t = localtime(time) append("%d min" % (time / 60)) else: append(str(curitem)) # (this will be done in c++ later!) return "".join(strlist)
def getHTML(self, id): return escape_xml(self.source.text).replace("\x19", "").replace("\x1c", "").replace("\x1e", "")
def getHTML(self, id): return escape_xml(self.source.text).replace('\xc2\x86', '').replace( '\xc2\x87', '').replace("\x19", "").replace("\x1c", "").replace( "\x1e", "").decode("utf-8", "ignore").encode("utf-8")
def markup_for_match(text, start, end, matchtarget=None): source = matchtarget or text return '%s<b>%s</b>%s%s' % (escape_xml( source[0:start]), escape_xml( source[start:end]), escape_xml(source[end:]), matchtarget and (' - <i>' + text + '</i>') or '')
def getText(self): l = self.source.list lut = self.source.lut conv_args = self.converter_arguments # now build a ["string", 1, "string", 2]-styled list, with indices into the # list to avoid lookup of item name for each entry lutlist = [] for element in conv_args: if isinstance(element, basestring): lutlist.append((element, None)) elif isinstance(element, ListItem): lutlist.append((lut[element.name], element.filternum)) elif isinstance(element, ListMacroItem): lutlist.append((element.macrodict[element.macroname], None)) else: raise Exception("neither string, ListItem nor ListMacroItem") # now, for the huge list, do: strlist = [] append = strlist.append for item in l: for (element, filternum) in lutlist: #None becomes "" curitem = "" if filternum: #filter out "non-displayable" Characters - at the very end, do it the hard way... curitem = str(item[element]).replace( '\xc2\x86', '').replace('\xc2\x87', '').replace( "\x19", "").replace("\x1c", "").replace( "\x1e", "").decode("utf-8", "ignore").encode("utf-8") if curitem is None: curitem = "" else: if element is None: element = "" if not filternum: append(element) elif filternum == 2: append( curitem.replace("\\", "\\\\").replace("\n", "\\n").replace( '"', '\\"')) elif filternum == 3: append(escape_xml(curitem)) elif filternum == 4: append( curitem.replace("%", "%25").replace( "+", "%2B").replace('&', '%26').replace( '?', '%3f').replace(' ', '+')) elif filternum == 5: append(quote(curitem)) elif filternum == 6: from time import localtime time = int(float(curitem)) or 0 t = localtime(time) append("%02d:%02d" % (t.tm_hour, t.tm_min)) elif filternum == 7: from time import localtime time = int(float(curitem)) or 0 t = localtime(time) append("%d min" % (time / 60)) else: append(curitem) # (this will be done in c++ later!) return ''.join(strlist)
def begin_section(self, section): # type: (str) -> None """Called before each section.""" super(HTML, self).begin_section(section) print('<h2>Section %s</h2>' % (escape_xml(section), ), file=self.stream) print('<table>', file=self.stream)