本文整理汇总了Python中utils.escape函数的典型用法代码示例。如果您正苦于以下问题:Python escape函数的具体用法?Python escape怎么用?Python escape使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了escape函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: po2csv
def po2csv(po):
"""Converts a polib.POFile object to a csv string (contained in an instance
of StringIO)
"""
csv_file = StringIO()
csv_writer = csv.writer(csv_file)
for header_row in csv_header_rows:
csv_writer.writerow(header_row)
for entry in po:
msgstrs = force_plurals(entry)
fuzzy = bool2str('fuzzy' in entry.flags, empty_for_false=True)
obsolete = bool2str(entry.obsolete, empty_for_false=True)
row = [
escape(entry.msgid),
escape(entry.msgid_plural),
entry.msgctxt,
msgstrs,
fuzzy,
entry.comment,
obsolete
]
csv_writer.writerow(row)
return csv_file
示例2: force_plurals
def force_plurals(entry):
"""A po file's plural handling is slightly weird. If there are no plurals
for a translation:
msgid "hello"
msgstr "hola"
If there are plurals however:
msgid "%(count)s house"
msgid_plural "%(count)s houses"
msgstr[0] "%(count)s casa"
msgstr[1] "%(count)s casas"
So to keep things consistent, we force non-plural translations into the
plural form for consistency, and then reading them back, we still have
enough information to convert them back into singular translations if
that's what they were meant to be.
"""
plurals = entry.msgstr_plural
if not plurals:
plurals = {u'0': escape(entry.msgstr)}
sorted_plurals = sorted(plurals.items(), key=lambda t: t[0])
return PLURAL_SEPARATOR.join(escape(v) for k, v in sorted_plurals)
示例3: writeFavourites
def writeFavourites(file, faves):
f = sfile.file(file, 'w')
f.write('<favourites>')
for fave in faves:
try:
name = utils.escape(fave[0])
thumb = utils.escape(fave[1])
cmd = utils.escape(fave[2])
thumb = convertToHome(thumb)
name = 'name="%s" ' % name
thumb = 'thumb="%s">' % thumb
f.write('\n\t<favourite ')
f.write(name)
f.write(thumb)
f.write(cmd)
f.write('</favourite>')
except:
pass
f.write('\n</favourites>')
f.close()
import xbmcgui
try: count = int(xbmcgui.Window(10000).getProperty('Super_Favourites_Count'))
except: count = 0
xbmcgui.Window(10000).setProperty('Super_Favourites_Count', str(count+1))
示例4: harvest
def harvest(text, harvesters=DEFAULT_HARVESTERS):
instances = [load_class(namespace) for namespace in harvesters]
display_text = ''
display_html = ''
entities = []
current_text = text
for instance in instances:
e = instance.harvest(current_text)
current_position = 0
for entity in e:
entities.append(entity)
current_position = entity.start_index
l = len(entity.original_text)
replacement = repeat_to_length(' ', l)
current_text = current_text[:current_position] + \
replacement + current_text[current_position + l:]
current_index = 0
for entity in entities:
display_html = display_html + escape(text[current_index:entity.start_index]) + entity.display_html
display_text = display_text + escape(text[current_index:entity.start_index]) + entity.display_text
current_index = entity.end_index
display_text = display_text + escape(text[current_index:])
display_html = display_html + escape(text[current_index:])
return {
'display_text': display_text,
'display_html': display_html,
}
示例5: set_label_text
def set_label_text(self, info, label_text, label_extra=None):
label_text = label_text.replace('\n', '').replace('\r', '')
if label_extra:
self.info_label.set_markup(info % (escape(label_text),
escape(label_extra)))
else:
self.info_label.set_markup(info % escape(label_text))
示例6: build_html_params
def build_html_params(**params):
params_list = list()
for (name, value) in params.items():
if name[0] == u'_':
name = name[1:]
params_list.append(u' {0}="{1}"'.format(
escape(name),
escape(value),
))
return u''.join(params_list)
示例7: build_signature_base_string
def build_signature_base_string(self, oauth_request, consumer, token):
sig = (
escape(oauth_request.get_normalized_http_method()),
escape(oauth_request.get_normalized_http_url()),
escape(oauth_request.get_normalized_parameters()),
)
key = '%s&' % escape(consumer.secret)
if token:
key += escape(token.secret)
raw = '&'.join(sig)
return key, raw
示例8: get_normalized_parameters
def get_normalized_parameters(self):
"""Return a string that contains the parameters that must be signed."""
params = self.parameters
try:
# Exclude the signature if it exists.
del params["oauth_signature"]
except:
pass
# Escape key values before sorting.
key_values = [(escape(_utf8_str(k)), escape(_utf8_str(v))) for k, v in params.items()]
# Sort lexicographically, first after key, then after value.
key_values.sort()
# Combine key value pairs into a string.
return "&".join(["%s=%s" % (k, v) for k, v in key_values])
示例9: json_data
def json_data(self):
return dict(
id = self.id,
content = escape(self.content),
date_created = self.date_created,
user = self.user.json_data()
)
示例10: get_content
def get_content(self):
"""Returns the content of the message"""
text = self.parsed_obj.get_payload()
output = ''
if isinstance(text, types.ListType) and self.parsed_obj.is_multipart():
for i in text:
if i.get_filename():
logging.error('Attachment found!!!')
output += "\nSorry but the attachment has been stripped from this message!"
continue
if (i.get_content_type() == "text/plain" or i.get_content_type() == "text/html"):
output+=(i.get_payload()) + '\n\n'
#logging.debug("Appending %s to output" % i.get_payload())
else:
output = text
#logging.error("get_content(): Output before wrap is %s" % output)
output = escape(output)
output = nl2br(output)
output1 = textwrap.wrap(output, 65)
output = ''
for i in output1:
output += i + '<br />'
#logging.error("get_content returning %s" % output)
return output
示例11: _parse_urls
def _parse_urls(self, match):
'''Parse URLs.'''
mat = match.group(0)
# Fix a bug in the regex concerning www...com and www.-foo.com domains
# TODO fix this in the regex instead of working around it here
domain = match.group(5)
if domain[0] in '.-':
return mat
# Only allow IANA one letter domains that are actually registered
if len(domain) == 5 \
and domain[-4:].lower() in ('.com', '.org', '.net') \
and not domain.lower() in IANA_ONE_LETTER_DOMAINS:
return mat
# Check for urls without http(s)
pos = mat.find('http')
if pos != -1:
pre, url = mat[:pos], mat[pos:]
full_url = url
# Find the www and force http://
else:
pos = mat.lower().find('www')
pre, url = mat[:pos], mat[pos:]
full_url = 'http://%s' % url
self._urls.append(url)
if self._html:
return '%s%s' % (pre, self.format_url(full_url,
self._shorten_url(escape(url))))
示例12: search_foods
def search_foods(query):
"""
Palauttaa hakuehtoja vastaavat elintarvikkeet + id:t.
Paluuarvona lista, jossa arvot tyyppiä [id, nimi].
"""
query = re.sub("\s+", " ", query).strip()
stored = db.get_search(query)
if stored:
return stored
url = URL_ROOT + "/foodsearch.php?name=" + escape(query)
print "url=", url
root = html.parse(url)
foods = []
rows = root.xpath("/html/body/div/table[2]/tr[1]/td[2]/ul/li/a")
for row in rows:
try:
fid = row.attrib["href"].split("=")[1].split("&")[0]
except IndexError:
logging.error("Scraping food ID failed: " + query)
continue
foods.append((fid, row.text.strip()))
db.add_search({"query": query, "results": foods})
return foods
示例13: to_header
def to_header(self, realm=""):
"""Serialize as a header for an HTTPAuth request."""
auth_header = 'OAuth realm="%s"' % realm
# Add the oauth parameters.
if self.parameters:
for k, v in self.parameters.iteritems():
if k[:6] == "oauth_":
auth_header += ', %s="%s"' % (k, escape(str(v)))
return {"Authorization": auth_header}
示例14: to_tsv
def to_tsv(self):
'''
Returns the instance's column values as a tab-separated line. A newline is not included.
'''
parts = []
for name, field in self._fields:
value = field.get_db_prep_value(field.to_python(getattr(self, name)))
parts.append(escape(value, quote=False))
return '\t'.join(parts)
示例15: out
def out(self):
res = self.tree.to_string()
res = ut.escape(res, ut.TEX_ESCAPE)
res = " ".join(res.split())
res = res.replace(" .", ".")
res = res.replace(" ,", ",")
res = res.replace(" ;", ";")
res = res.replace(" :", ":")
res = unicode("{{{0}}}", "utf8").format(res)
return res