def showPreview(self): if self.togglePreviewCheckbox.isChecked(): try: # Get nodes key_nodes = self.input_extract.text() subkey = key_nodes.split('|').pop(0).rsplit('.', 1)[0] key_id = self.input_id.text() #selected = self.mainWindow.tree.selectionModel().selectedRows() objecttypes = self.objecttypeEdit.text().replace(' ', '').split(',') level = self.levelEdit.value() - 1 conditions = { 'filter': { 'level': level, '!objecttype': objecttypes } } selected = self.mainWindow.tree.selectedIndexesAndChildren( conditions) nodes = [] for item in selected: if not item.isValid(): continue treenode = item.internalPointer() dbnode = treenode.dbnode() if dbnode is not None: name, nodes = extractValue(dbnode.response, key_nodes, dump=False) break # Dump nodes value = [] nodes = [nodes] if not (type(nodes) is list) else nodes for n in nodes: nodedata = json.dumps(n) if isinstance(n, Mapping) else n n = n if isinstance(n, Mapping) else {subkey: n} objectid = extractValue( n, key_id, default=None)[1] if key_id != '' else '' value.append((str(objectid), str(nodedata))) except Exception as e: value = [('', str(e))] value = [ '<b>{}</b><p>{}</p><hr>'.format(html.escape(x), html.escape(y)) for x, y in value ] value = "\n\n".join(value) self.dataEdit.setHtml(value) self.dataEdit.setVisible(self.togglePreviewCheckbox.isChecked()) if not self.togglePreviewCheckbox.isChecked(): self.adjustSize() self.show()
def gen_html_report_DefaultPort_section(self): accordion_id = "accordion7" accordion_title = "修改默认监听端口" collapse_id = "collapse7" current_item = self.node_xpath(self.xml_obj,"/root/checklist/section[@id='checkDefaultPort']/item")[0] check_object = self.text_xpath(current_item, "check_object") check_command = self.text_xpath(current_item, "check_command") check_comment = self.text_xpath(current_item, "check_comment") check_result_item = current_item.xpath("check_result")[0] check_result_not_escape = lxml.html.tostring(check_result_item).decode('utf-8') start_pos = re.search("<check_result>", check_result_not_escape).end() end_pos = re.search("</check_result>", check_result_not_escape).start() check_result = html.escape(check_result_not_escape[start_pos:end_pos].strip("\n")) check_result = check_result.replace("\n", "<br />") if check_result.strip() != "": self.config_warn += 1 card_class = "bg-warning text-white" else: card_class = "bg-success text-white" self.config_right += 1 self.create_accordion_card(accordion_id, accordion_title, collapse_id, card_class=card_class) self.html_report_obj.writelines("""<table id="DefaultPort_list" class="table">\n""") self.html_report_obj.writelines(f"""<tr><th>检测项</th><td>{check_object}</td></tr>\n""") self.html_report_obj.writelines(f"""<tr><th>检测命令</th><td>{check_command}</td></tr>\n""") self.html_report_obj.writelines(f"""<tr><th>检测说明</th><td>{check_comment}</td></tr>\n""") self.html_report_obj.writelines(f"""<tr><th>检测结果</th><td>{check_result}</td></tr>\n""") self.html_report_obj.writelines("""</table>\n""") self.close_div_label(5)
def wraptip(value): try: value = '<qt>{}</qt>'.format(html.escape( str(value))) if value is not None else value except: pass return value
def inner_html(element): inner = element.text if inner is None: inner = '' inner = html.escape(str(inner)) for child in element: inner += lxml.html.tostring(child, method='html', pretty_print=True).decode('utf-8') return inner
def inner_html(element): inner = element.text if inner is None: inner = '' inner = html.escape(str(inner)) for child in element: inner += lxml.html.tostring(child, method='html').decode('utf-8') return inner
def gen_html_report_ServerVersion_section(self): accordion_id = "accordion6" accordion_title = "隐藏版本号" collapse_id = "collapse6" current_item = self.xml_obj.xpath( "/root/checklist/section[@id='checkServerVersion']/item")[0] check_object = current_item.xpath("check_object")[0].text check_command = current_item.xpath("check_command")[0].text check_comment = current_item.xpath("check_comment")[0].text check_result_item = current_item.xpath("check_result")[0] check_result_not_escape = lxml.html.tostring(check_result_item).decode( 'utf-8') start_pos = re.search("<check_result>", check_result_not_escape).end() end_pos = re.search("</check_result>", check_result_not_escape).start() check_result = html.escape( check_result_not_escape[start_pos:end_pos].strip("\n")) check_result = check_result.replace("\n", "<br />") version_name_pattern = "Server\s*number:\s*" version_value_pattern = "Server\s*number:\s*[\d|\.]+" version_str = '' # 找表版本号,表示未隐藏版本号 try: version_value = re.search(version_value_pattern, check_result).group() self.config_error += 1 card_class = "bg-danger text-white" except: # 如果找不到版本号,但找得到Server number:则表示已隐藏版本号 try: version_name = re.search(version_name_pattern, check_result).group() self.config_right += 1 card_class = "bg-success text-white" # 两个都找不到说明version.sh执行出错 except: self.config_warn += 1 card_class = "bg-warning text-white" self.create_accordion_card(accordion_id, accordion_title, collapse_id, card_class=card_class) self.html_report_obj.writelines( """<table id="ServerVersion_list" class="table">\n""") self.html_report_obj.writelines( f"""<tr><th>检测项</th><td>{check_object}</td></tr>\n""") self.html_report_obj.writelines( f"""<tr><th>检测命令</th><td>{check_command}</td></tr>\n""") self.html_report_obj.writelines( f"""<tr><th>检测说明</th><td>{check_comment}</td></tr>\n""") self.html_report_obj.writelines( f"""<tr><th>检测结果</th><td>{check_result}</td></tr>\n""") self.html_report_obj.writelines("""</table>\n""") self.close_div_label(5)
def replace_mustache_tag(self, html_source, tag, replacement_text, encode=False): """ Replaces the tag in the text with (optionally) html escaped replacement """ if encode: return html_source.replace( tag, html.escape(replacement_text, quote=True)) else: return html_source.replace(tag, replacement_text)
def gen_html_report_ListDir_section(self): accordion_id = "accordion3" accordion_title = "禁止列目录" collapse_id = "collapse3" current_item = self.xml_obj.xpath( "/root/checklist/section[@id='checkListDir']/item")[0] check_object = current_item.xpath("check_object")[0].text check_command = current_item.xpath("check_command")[0].text check_comment = current_item.xpath("check_comment")[0].text check_result_item = current_item.xpath("check_result")[0] check_result = "" try: list_dir_item = check_result_item.xpath( "init-param[param-name = 'listings']")[0] list_dir_item_value = list_dir_item.xpath("param-value/text()")[0] check_result_not_escape = lxml.html.tostring(list_dir_item).decode( 'utf-8') start_pos = re.search("<init-param>", check_result_not_escape).end() end_pos = re.search("</init-param>", check_result_not_escape).start() check_result = html.escape( check_result_not_escape[start_pos:end_pos].strip("\n")) check_result = check_result.replace("\n", "<br />") if list_dir_item_value == "false": card_class = "bg-success text-white" self.config_right += 1 else: self.config_error += 1 card_class = "bg-danger text-white" except: card_class = "bg-success text-white" self.config_right += 1 self.create_accordion_card(accordion_id, accordion_title, collapse_id, card_class=card_class) self.html_report_obj.writelines( """<table id="ListDir_list" class="table">\n""") self.html_report_obj.writelines( f"""<tr><th>检测项</th><td>{check_object}</td></tr>\n""") self.html_report_obj.writelines( f"""<tr><th>检测命令</th><td>{check_command}</td></tr>\n""") self.html_report_obj.writelines( f"""<tr><th>检测说明</th><td>{check_comment}</td></tr>\n""") self.html_report_obj.writelines( f"""<tr><th>检测结果</th><td>{check_result}</td></tr>\n""") self.html_report_obj.writelines("""</table>\n""") self.close_div_label(5)
def gen_html_report_DefaultAccount_section(self): accordion_id = "accordion2" accordion_title = "禁用tomcat默认帐号" collapse_id = "collapse2" current_item = self.xml_obj.xpath( "/root/checklist/section[@id='checkDefaultAccount']/item")[0] check_object = current_item.xpath("check_object")[0].text check_command = current_item.xpath("check_command")[0].text check_comment = current_item.xpath("check_comment")[0].text check_result_item = current_item.xpath("check_result")[0] check_result_not_escape = lxml.html.tostring(check_result_item).decode( 'utf-8') start_pos = re.search("<check_result>", check_result_not_escape).end() end_pos = re.search("</check_result>", check_result_not_escape).start() check_result_not_escape = check_result_not_escape.replace( "></user>", "/>") check_result = html.escape( check_result_not_escape[start_pos:end_pos].strip("\n")) check_result = check_result.replace("\n", "<br />") try: user_items = check_result_item.xpath("user") if len(user_items) == 0: card_class = "bg-success text-white" self.config_right += 1 else: self.config_error += 1 card_class = "bg-danger text-white" #check_result = etree.tostring(user_items[0]) except: card_class = "bg-success text-white" self.config_right += 1 check_result = "error occur while parse, please check it manually" self.create_accordion_card(accordion_id, accordion_title, collapse_id, card_class=card_class) self.html_report_obj.writelines( """<table id="DefaultAccount_list" class="table">\n""") self.html_report_obj.writelines( f"""<tr><th>检测项</th><td>{check_object}</td></tr>\n""") self.html_report_obj.writelines( f"""<tr><th>检测命令</th><td>{check_command}</td></tr>\n""") self.html_report_obj.writelines( f"""<tr><th>检测说明</th><td>{check_comment}</td></tr>\n""") self.html_report_obj.writelines( f"""<tr><th>检测结果</th><td>{check_result}</td></tr>\n""") self.html_report_obj.writelines("""</table>\n""") self.close_div_label(5)
def gen_html_report_ErrorPage_section(self): accordion_id = "accordion4" accordion_title = "自定义错误页面" collapse_id = "collapse4" current_item = self.xml_obj.xpath( "/root/checklist/section[@id='checkErrorPage']/item")[0] check_object = current_item.xpath("check_object")[0].text check_command = current_item.xpath("check_command")[0].text check_comment = current_item.xpath("check_comment")[0].text check_result = '' check_result_item = current_item.xpath("check_result")[0] check_result_not_escape = lxml.html.tostring(check_result_item).decode( 'utf-8') # start_pos = re.search("<init-param>", check_result_not_escape).end() # end_pos = re.search("</init-param>", check_result_not_escape).start() check_result = html.escape(check_result_not_escape.strip("\n")) check_result = check_result.replace("\n", "<br />") try: error_code_list = check_result_item.xpath( "error-page/error-code/text()") error_code_str = "、".join(error_code_list) if ("401" in error_code_str) and ("404" in error_code_str) and ( "500" in error_code_str): card_class = "bg-success text-white" self.config_right += 1 else: self.config_error += 1 card_class = "bg-danger text-white" except: self.config_error += 1 card_class = "bg-danger text-white" self.create_accordion_card(accordion_id, accordion_title, collapse_id, card_class=card_class) self.html_report_obj.writelines( """<table id="ErrorPage_list" class="table">\n""") self.html_report_obj.writelines( f"""<tr><th>检测项</th><td>{check_object}</td></tr>\n""") self.html_report_obj.writelines( f"""<tr><th>检测命令</th><td>{check_command}</td></tr>\n""") self.html_report_obj.writelines( f"""<tr><th>检测说明</th><td>{check_comment}</td></tr>\n""") self.html_report_obj.writelines( f"""<tr><th>检测结果</th><td>{check_result}</td></tr>\n""") self.html_report_obj.writelines("""</table>\n""") self.close_div_label(5)
def replace_link_tag(match): attrs = lxml.html.fromstring(match.group(0)).attrib asset_path = href_to_path(attrs.get('href')) if asset_path: title = attrs.get('alt', attrs.get('title', asset_path.name)) with asset_path.open('rb') as f: document = Document.objects.create( title=title, file=File(f, asset_path.name), ) return '<a %s>' % ' '.join([ '%s="%s"' % (k, html.escape(v, quote=True)) for k, v in ( ('id', str(document.pk)), ('linktype', 'document'), ) ]) else: return match.group(0)
def gen_html_report_EnableAccessLog_section(self): accordion_id = "accordion5" accordion_title = "开启访问日志" collapse_id = "collapse5" current_item = self.xml_obj.xpath( "/root/checklist/section[@id='checkEnableAccessLog']/item")[0] check_object = current_item.xpath("check_object")[0].text check_command = current_item.xpath("check_command")[0].text check_comment = current_item.xpath("check_comment")[0].text check_result_item = current_item.xpath("check_result")[0] check_result_not_escape = lxml.html.tostring(check_result_item).decode( 'utf-8') start_pos = re.search("<check_result>", check_result_not_escape).end() end_pos = re.search("</check_result>", check_result_not_escape).start() check_result = html.escape( check_result_not_escape[start_pos:end_pos].strip("\n")) check_result = check_result.replace("\n", "<br />") if check_result.strip() == "": self.config_error += 1 card_class = "bg-danger text-white" else: card_class = "bg-success text-white" self.config_right += 1 self.create_accordion_card(accordion_id, accordion_title, collapse_id, card_class=card_class) self.html_report_obj.writelines( """<table id="EnableAccessLog_list" class="table">\n""") self.html_report_obj.writelines( f"""<tr><th>检测项</th><td>{check_object}</td></tr>\n""") self.html_report_obj.writelines( f"""<tr><th>检测命令</th><td>{check_command}</td></tr>\n""") self.html_report_obj.writelines( f"""<tr><th>检测说明</th><td>{check_comment}</td></tr>\n""") self.html_report_obj.writelines( f"""<tr><th>检测结果</th><td>{check_result}</td></tr>\n""") self.html_report_obj.writelines("""</table>\n""") self.close_div_label(5)
def replace_image_tag(match): attrs = lxml.html.fromstring(match.group(0)).attrib asset_path = self.convert_url_to_path(path.parent, attrs.get('src')) if asset_path: with asset_path.open('rb') as f: image = Image.objects.create( title=attrs.get('alt', attrs.get('title', '')), file=File(f, asset_path.name), width=attrs.get('width', 0), height=attrs.get('height', 0), ) return '<embed %s/>' % ' '.join([ '%s="%s"' % (k, html.escape(v, quote=True)) for k, v in ( ('alt', image.title), ('embedtype', 'image'), ('format', get_image_format(attrs)), ('id', str(image.pk)), ) ]) else: return match.group(0)
def process_source_file(self, sourceFileDef, destDir, site_config, additional_mustache_tags={}, force_write=False): """ process a source file and output the files required """ header = sourceFileDef.metadata title = header["title"] author = header["author"] template_type = sourceFileDef.template_type() full_url = site_config.root_url + sourceFileDef.dest_file_name() dest_file_path = os.path.join(destDir, sourceFileDef.dest_file_name()) dest_file_dir = os.path.split(dest_file_path)[0] os.makedirs(dest_file_dir, exist_ok=True) number_of_subdirs = 0 t = sourceFileDef.dest_file_name().split(os.path.sep) number_of_subdirs = len(t) - 1 relative_path_to_top = "/".join([".."] * number_of_subdirs) if (len(relative_path_to_top) > 0): relative_path_to_top += '/' outputFileDef = FileDef(dest_file_path) if (sourceFileDef.older(outputFileDef) and not force_write): return if (template_type not in self.templates): raise errors.CompileError( "Unknown template type: " + template_type, sourceFileDef.file_name) """ Calculate the list of tags for this article""" article_tags = [] for tag_name in sourceFileDef.tags(): if not site_config.is_tag_allowed(tag_name): raise errors.CompileError( "Unknown tag: " + tag_name + ". Add to site config file to use.", sourceFileDef.file_name) article_tags.append(site_config.allowed_tags[tag_name]) article_tags.sort(key=lambda s: s.title) all_tag_ids = [] all_tag_titles = [] for tag in article_tags: all_tag_titles.append(html.escape(tag.title, quote=True)) all_tag_ids.append(tag.tag) tag_link_text = "in <a href=\"/tagcloud.html#" + "+".join( all_tag_ids) + "\">" + ", ".join(all_tag_titles) + "</a>" article_text = sourceFileDef.processed_text summary = sourceFileDef.summary image_url = "" image_url = "" if (len(sourceFileDef.images) > 0): image_url = sourceFileDef.images[0] html_source = self.templates[template_type].contents for t, v in additional_mustache_tags.items(): html_source = self.replace_mustache_tag(html_source, "{{" + t + "}}", v) html_source = self.replace_mustache_tag(html_source, "{{title}}", title, encode=True) html_source = self.replace_mustache_tag(html_source, "{{author}}", author, encode=True) html_source = self.replace_mustache_tag( html_source, "{{pretty_date}}", pretty_date(sourceFileDef.original_date)) html_source = self.replace_mustache_tag(html_source, "{{full_url}}", full_url) html_source = self.replace_mustache_tag(html_source, "{{tag_links}}", tag_link_text) html_source = self.replace_mustache_tag(html_source, "{{twitter_handle}}", site_config.twitter_handle, encode=True) html_source = self.replace_mustache_tag(html_source, "{{first_words}}", summary, encode=True) html_source = self.replace_mustache_tag(html_source, "{{first_image}}", image_url) html_source = html_source.replace("{{css_relative_path}}", relative_path_to_top) html_source = html_source.replace("{{article_content}}", article_text) with open(outputFileDef.file_name, "w", encoding="utf-8") as f: f.write(html_source)
<description> </description> <items> <rdf:Seq> </rdf:Seq> </items> </channel> </rdf:RDF>''' rss_item = '''<item xmlns="{x}" xmlns:rdf="{rdf}" rdf:about="{url}"> <title>{title}</title> <link>{link}</link> <description rdf:datatype="rdf:HTML"></description> </item> ''' html_escape_dict = lambda o: dict([(k, html.escape(v) if isinstance(v, str) else v) for k, v in o.items()]) def remove_script(node): for c in node.getchildren(): if c.tag == "script": node.remove(c) else: remove_script(c) return node class HintRequired(Exception): def fill(self, ev):
<description> </description> <items> <rdf:Seq> </rdf:Seq> </items> </channel> </rdf:RDF>''' rss_item = '''<item xmlns="{x}" xmlns:rdf="{rdf}" rdf:about="{url}"> <title>{title}</title> <link>{link}</link> <description rdf:datatype="rdf:HTML"></description> </item> ''' html_escape_dict = lambda o: dict([(k,html.escape(v) if isinstance(v, str) else v) for k,v in o.items()]) def remove_script(node): for c in node.getchildren(): if c.tag == "script": node.remove(c) else: remove_script(c) return node class HintRequired(Exception): def fill(self, ev): for line in self.args: p = re.match("(?P<name>[^:;]+)(?P<params>;[^:;]+)?:(?P<value>.*)$", line) if not p: raise Exception("Hint arg error {0}".format(line))
program_html = lxml.html.fromstring(result.text, base_url=entry.link) if not keyword in entry.title: if not keyword in get_section(program_html, "番組概要"): if not keyword in get_section(program_html, "人名リンク"): if not keyword in get_section(program_html, "番組詳細"): sys.stderr.write( "[info] skipping %s (no matching keyword)\n" % entry.link) continue checked_thistime.append(url_num) mes = u"<a href=\"%s\">%s</a> (%s)" % ( entry.link, html.escape(entry.title), keyword) messages.append(mes) sess.close() if len(messages) > 0: message_str = "<br />\n".join(messages) sys.stderr.write(u"[info] mailing via sendgrid\n") sg_client = sendgrid.SendGridAPIClient(sg_apikey) sg_from = sendgrid.Email(name="Check TV Programs", email=sg_username) message = sendgrid.Mail(from_email=sg_from, to_emails=[sg_recipient], subject=u"Update of TV Programs", html_content=message_str) message.reply_to = sg_recipient sg_client.send(message)
def HtmlEncode(strToEncode): return html.escape(strToEncode)
def wraptip(value): value = '<qt>{}</qt>'.format(html.escape(value)) if value is not None else value return value