def __init__(self, xml_string):
self.xml_unescape_table = {}
self.xml_map = {}
try:
self.xml = minidom.parseString(xml_string)
except:
print xml_string
self.xml_unescape_tabl = get_xml_unescape_table()
self.xml_map = get_xml_unescape_map()
for k, v in self.xml_map.items():
xml_string = xml_string.replace(k, v)
self.xml = minidom.parseString(xml_string)
self.bucket = get_tag_text(self.xml, 'Bucket', convert_to_bool = False)
self.object = get_tag_text(self.xml, 'Key', convert_to_bool = False)
if self.xml_map:
for k, v in self.xml_map.items():
self.object = self.object.replace(v, k)
self.object = unescape(self.object, self.xml_unescape_table)
self.key = get_tag_text(self.xml, 'Key', convert_to_bool = False)
self.upload_id = get_tag_text(self.xml, 'UploadId')
self.marker = get_tag_text(self.xml, 'Marker', convert_to_bool = False)
python类unescape()的实例源码
def wrap_field(field):
"""
Given a field, detect if it has special characters <, > or & and if so
wrap it in a CDATA field.
"""
if not isinstance(field, str):
return field
if html_chars.search(field):
return '<![CDATA[' + field + ']]>'
else:
# If there's already escaped data like &, I want to unescape it
# first, so I can re-escape *everything*
field = saxutils.unescape(field)
return saxutils.escape(field)
def test_single_download(self):
self.set_http_response(status_code=200)
response = self.service_connection.get_log_file('db1', 'foo.log')
self.assertTrue(isinstance(response, LogFileObject))
self.assertEqual(response.marker, '0:4485')
self.assertEqual(response.dbinstance_id, 'db1')
self.assertEqual(response.log_filename, 'foo.log')
self.assertEqual(response.data, saxutils.unescape(self.logfile_sample))
self.assert_request_parameters({
'Action': 'DownloadDBLogFilePortion',
'DBInstanceIdentifier': 'db1',
'LogFileName': 'foo.log',
}, ignore_params_values=['Version'])
def append_skipped(self, report):
"""Append xml reports with skipped TC.
Args:
report(dict): Skipped report
"""
self.class_logger.info("Appending XML report with skip.")
# filename, lineno, skipreason = report['longrepr']
# self.class_logger.debug("Received longrepr: {0}".format(xml_unescape(report['longrepr'])))
longrepr = xml_unescape(report['longrepr'])
# TODO: fixed bug with longrepr crashing
skipreason = get_skipped_reason(report['longrepr'])
self.append(self.Junit.skipped("%s" % longrepr, # pylint: disable=no-member
type="pytest.skip", message=skipreason))
# self.append(self.Junit.skipped("%s:%s: %s" % longrepr, type="pytest.skip", message=skipreason))
self.skipped += 1
def GetAttributeValue(attr, extract=True):
"""
Function that extracts data from a tree node
@param attr: tree node containing data to extract
@param extract: attr is a tree node or not
@return: data extracted as string
"""
if not extract:
return attr
if len(attr.childNodes) == 1:
return unicode(unescape(attr.childNodes[0].data))
else:
# content is a CDATA
text = u''
for node in attr.childNodes:
if not (node.nodeName == "#text" and node.data.strip() == u''):
text += unicode(unescape(node.data))
return text
def SendReport(self, report):
"""Emails an exception report.
Args:
report: A string containing the report to send.
"""
subject = ('Daily exception report for app "%s", major version "%s"'
% (self.app_id, self.major_version))
report_text = saxutils.unescape(re.sub('<[^>]+>', '', report))
mail_args = {
'sender': self.sender,
'subject': subject,
'body': report_text,
'html': report,
}
if self.to:
mail_args['to'] = self.to
self.send_mail(**mail_args)
else:
self.send_mail_to_admins(**mail_args)
def quiz_func(nick,match,target):
if 'quiz' in mb.responses:
mb.tell("weak! the answer was "+mb.responses['quiz']['param']['answer'],target)
del mb.responses['quiz']
req=urllib2.Request('http://jservice.io/api/random')
response=urllib2.urlopen(req).read().decode('utf-8')
data=choice(json.loads(response))
answer=re.sub(html_tags,"",data['answer'])
answer=answer.replace(r"\'","'")
answer=unescape(answer)
mb.tell("Category: {}".format(data['category']['title']),target)
mb.tell(re.sub(html_tags,"",unescape(data['question']))+" Answer: "+re.sub("[a-zA-Z0-9]","*",answer),target)
pattern=re.compile("^(?:murderb[o0]t|mb)?[,\s:!]*"+re.escape(answer)+"\s*$",flags=re.IGNORECASE)
answer_response={'func':quiz_answer,'pattern':pattern,'nick':".*",'param':{'answer':answer},'target':target}
mb.responses['quiz']=answer_response
print(data['question'])
print(data['answer'])
return
def s2i(keys, inp, out):
for key in keys:
ind = inp.find(key)
if ind:
typ = TYPES[key]
if ind.contents:
val = ind.contents[0].strip()
try:
if typ == B:
res = (val == 'true')
elif typ == S:
res = str(unescape(val))
elif typ == I:
res = int(round(float(c2p(str(val)))))
else:
res = float(c2p(str(val)))
out.__setattr__(key, res)
except:
pass
def __init__(self, *args, **kwargs):
# The inicialization is taken from rst2pdf.flowables.Heading
hstyle = kwargs.pop('hstyle')
level = 0
text = kwargs.pop('text')
self.snum = kwargs.pop('snum')
self.parent_id= kwargs.pop('parent_id')
#self.stext =
Heading.__init__(self,text,hstyle,level=level,
parent_id=self.parent_id)
# Cleanup title text
#self.stext = re.sub(r'<[^>]*?>', '', unescape(self.stext))
#self.stext = self.stext.strip()
# Stuff needed for the outline entry
MyImage.__init__(self, *args, **kwargs)
def getWeatherReport(self,output):
self.loadinginprogress = False
trans = { 'ß' : 'ß' , 'ä' : 'ä' , 'ö' : 'ö' , 'ü' : 'ü' , 'Ä' : 'Ä', 'Ö' : 'Ö' , 'Ü' : 'Ü'}
output= util.unescape(output,trans)
if self.land == "de":
startpos = output.find('<!-- Anfang msg_Box Content -->')
endpos = output.find('<!-- Ende msg_Box Content -->')
bereich = output[startpos:endpos]
bereich = bereich.replace('<strong>', '\n')
else:
startpos = output.find('<div class="content"')
endpos = output.find('</div>', startpos)
bereich = output[startpos:endpos]
bereich = sub('<br\s*/?>',"\n",bereich)
bereich = sub('<[^>]*>',"",bereich)
bereich = sub('Fronten- und Isobarenkarte.*',"",bereich)
bereich = bereich.strip()
bereich = sub("\n[\s\n]+", "\n\n", bereich)
f = open(self.reportfile, "w")
f.write("%s" % bereich)
f.close()
self.session.open(Console,_("Warnlagebericht"),["cat %s" % self.reportfile])
def HTMLUnescape(text):
return unescape(text, Editor.getHtmlUnescapeTable())
def list(self):
cl = []
pl = []
for c in self.content_list:
key = c.key
if self.xml_map:
for k, v in self.xml_map.items():
key = key.replace(v, k)
key = unescape(key, self.xml_unescape_table)
cl.append((key, c.last_modified, c.etag, c.size, c.owner.id, c.owner.display_name, c.storage_class))
for p in self.prefix_list:
pl.append(p)
return (cl, pl)
def list(self):
cl = []
pl = []
for c in self.content_list:
key = c.key
if self.xml_map:
for k, v in self.xml_map.items():
key = key.replace(v, k)
key = unescape(key, self.xml_unescape_table)
cl.append((key, c.upload_id, c.init_time))
for p in self.prefix_list:
pl.append(p)
return (cl, pl)
def replace_entity(match):
"""Return the character corresponding to an HTML entity."""
number = match.group(2)
if number:
hex = match.group(1) is not None
result = chr(int(number, 16 if hex else 10))
else:
entity = match.group(3)
result = unescape(entity, html.entities.html5)
return result
def unescape(string):
"""
>>> unescape("one <![CDATA[two ]]>three")
'one two three'
"""
result = list()
for index, data in enumerate(cdata.split(string)):
if index % 3 != 2:
data = _unescape(data, {" ": " "})
result.append(data)
return "".join(result)
def unescape_html(s):
h = html_parser.HTMLParser()
s = h.unescape(s)
s = unquote_plus(s)
return unescape(s, HTML_UNESCAPE_TABLE)
def clean_filename(s, minimal_change=False):
"""
Sanitize a string to be used as a filename.
If minimal_change is set to true, then we only strip the bare minimum of
characters that are problematic for filesystems (namely, ':', '/' and
'\x00', '\n').
"""
# First, deal with URL encoded strings
h = html_parser.HTMLParser()
s = h.unescape(s)
s = unquote_plus(s)
# Strip forbidden characters
s = (
s.replace(':', '-')
.replace('/', '-')
.replace('\x00', '-')
.replace('\n', '')
)
if minimal_change:
return s
s = s.replace('(', '').replace(')', '')
s = s.rstrip('.') # Remove excess of trailing dots
s = s.strip().replace(' ', '_')
valid_chars = '-_.()%s%s' % (string.ascii_letters, string.digits)
return ''.join(c for c in s if c in valid_chars)
def unescape(self, text):
return unescape(text, html_unescape_table)
def get(self, unescape = False):
ele_tree = ElementTree.Element('items')
for item in self.items:
ele_tree.append(item.getXMLElement())
res = ElementTree.tostring(ele_tree) #! ????encoding='utf-8'????????????????alfred????
if unescape:
return saxutils.unescape(res)
return res
def get_unescaped_data(xmlNode):
return saxutils.unescape(xmlNode.childNodes[0].data)
def get_data(xmlnode):
data = []
for childnode in xmlnode.childNodes:
if childnode.nodeType == Node.ELEMENT_NODE:
data.append(get_data(childnode))
elif childnode.nodeType == Node.TEXT_NODE:
data.append(saxutils.unescape(childnode.data))
return u''.join(data)
def append_error(self, report, when=""):
"""Append xml report with error (in case TC failed on setup or teardown).
Args:
report(dict): Error report
when(str): Error occurance stage (setup|call|teardown)
"""
self.class_logger.info("Appending XML report with error.")
if 'longrepr' in list(report.keys()):
longrepr = xml_unescape(report['longrepr'])
if self.fail_traceback is not None:
failure_reason = None
if len(self.tests[-1]) > 0:
failure_reason = self.tests[-1].pop()
if self.update is not None and len(self.tests[-1]) > 0:
self.tests[-1].pop()
try:
self.tests[-1].pop()
except IndexError:
pass
self.append(
self.Junit.error(longrepr, # pylint: disable=no-member
message="Test error on %s and Test failure" % (when, )))
if hasattr(failure_reason.attr, "message") and failure_reason.attr.message == "Failure Reason":
self.tests[-1].append(failure_reason)
self.tests[-1][0].extend("\n" + "-" * 80 + "\nTest Case Failure\n" + "-" * 80 + "\n")
self.tests[-1][0].extend(self.fail_traceback)
else:
self.append(
self.Junit.error(longrepr, # pylint: disable=no-member
message="Test error on %s" % (when, )))
self.errors += 1
self.failed += 1
def append_xfail(self, report):
"""Append xml report with xfailed TC.
Args:
report(dict): XFail report
"""
self.class_logger.info("Appending XML report with xfail.")
self.append(
self.Junit.skipped(str(xml_unescape(report['keywords']['xfail'])), # pylint: disable=no-member
message="expected test failure"))
def append_failure(self, report):
"""Append xml report with failed TC.
Args:
report(dict): Failure report
"""
self.class_logger.info("Appending XML report with fail.")
report['sections'] = [xml_unescape(section) for section in report['sections']] # pylint: disable=no-member
sec = dict(report['sections'])
self.fail_traceback = self.Junit.failure(message="Test failure") # pylint: disable=no-member
# Removing BASH escape symbols (decolorizing)
# longrepr = xml_unescape(re_sub(r"\x1b.*?m", "", report['longrepr']))
longrepr = xml_unescape(report['longrepr'])
# TODO: Change str to encode for unicode text
try:
self.fail_traceback.append(str(longrepr))
except UnicodeEncodeError as err:
self.fail_traceback.append(ud.normalize('NFKD', longrepr))
self.class_logger.warning("Unicode data in traceback: %s" % (err, ))
self.append(self.fail_traceback)
for name in ("out", "err"):
content = sec.get("Captured std{0}".format(name))
if content:
tag = getattr(self.Junit, "system-{0}".format(name))
self.append(tag(content))
self.failed += 1
def _transform_page(self, content):
content = unescape(content)
for placeholder, buffer in self.placeholder_dict.items():
new_text = ", ".join([placeholder] + buffer)
content = content.replace(placeholder, new_text)
return content
def unquote(txt):
from xml.sax.saxutils import unescape
return unescape(txt, {"'": "'", """: '"'})
def replace_entity(match):
"""Return the character corresponding to an HTML entity."""
number = match.group(2)
if number:
hex = match.group(1) is not None
result = chr(int(number, 16 if hex else 10))
else:
entity = match.group(3)
result = unescape(entity, html.entities.html5)
return result
def render_resource(path, **context):
"""
Render static resource using provided context.
Returns: django.utils.safestring.SafeText
"""
html = Template(resource_string(path))
return html_parser.unescape(
html.render(Context(context))
)
def render_template(template_name, **context):
"""
Render static resource using provided context.
Returns: django.utils.safestring.SafeText
"""
template_dirs = [os.path.join(os.path.dirname(__file__), 'static/html')]
engine = Engine(dirs=template_dirs, debug=True)
html = engine.get_template(template_name)
return html_parser.unescape(
html.render(Context(context))
)
def remove_escaping(text):
"""
Clean text from special `escape` symbols.
Reference: https://wiki.python.org/moin/EscapingHtml.
"""
html_unescape_table = {
"&": "&",
""": '"',
"&#39;": "'",
"'": "'",
">": ">",
"<": "<"
}
return unescape(text, html_unescape_table)