def writexml(self, stream, indent='', addindent='', newl='', strip=0,
nsprefixes={}, namespace=''):
if self.raw:
val = self.nodeValue
if not isinstance(val, StringTypes):
val = str(self.nodeValue)
else:
v = self.nodeValue
if not isinstance(v, StringTypes):
v = str(v)
if strip:
v = ' '.join(v.split())
val = escape(v)
if isinstance(val, UnicodeType):
val = val.encode('utf8')
stream.write(val)
python类UnicodeType()的实例源码
def __init__(self):
"""SecurityOptions()
Initialize.
"""
# I don't believe any of these types can ever pose a security hazard,
# except perhaps "reference"...
self.allowedTypes = {"None": 1,
"bool": 1,
"boolean": 1,
"string": 1,
"str": 1,
"int": 1,
"float": 1,
"datetime": 1,
"time": 1,
"date": 1,
"timedelta": 1,
"NoneType": 1}
if hasattr(types, 'UnicodeType'):
self.allowedTypes['unicode'] = 1
self.allowedModules = {}
self.allowedClasses = {}
def WriteXML(self, file):
""" Dump non-empty contents to the output file, in XML format. """
if not self.loc:
return
out = SITEURL_XML_PREFIX
for attribute in self.__slots__:
value = getattr(self, attribute)
if value:
if type(value) == types.UnicodeType:
value = encoder.NarrowText(value, None)
elif type(value) != types.StringType:
value = str(value)
value = xml.sax.saxutils.escape(value)
out = out + (' <%s>%s</%s>\n' % (attribute, value, attribute))
out = out + SITEURL_XML_SUFFIX
file.write(out)
#end def WriteXML
#end class URL
def fmtstr(self, *fmt):
str = ''
encoding = 'utf8'#??utf8??
for i in fmt:
if not type(i) in [types.UnicodeType, types.StringTypes, types.StringType]:
s= repr(i)
else:
s = i
if type(s) == type(u''):
str += s.encode(encoding)
else:
str += s
str += '.'
#str += '/n'
#print 'fmtstr:'+str
return str
def test_unicode(self):
if not test_support.have_unicode: return
# The StringIO module also supports concatenating Unicode
# snippets to larger Unicode strings. This is tested by this
# method. Note that cStringIO does not support this extension.
f = self.MODULE.StringIO()
f.write(self._line[:6])
f.seek(3)
f.write(unicode(self._line[20:26]))
f.write(unicode(self._line[52]))
s = f.getvalue()
self.assertEqual(s, unicode('abcuvwxyz!'))
self.assertEqual(type(s), types.UnicodeType)
def test_unicode(self):
if not test_support.have_unicode: return
# The StringIO module also supports concatenating Unicode
# snippets to larger Unicode strings. This is tested by this
# method. Note that cStringIO does not support this extension.
f = self.MODULE.StringIO()
f.write(self._line[:6])
f.seek(3)
f.write(unicode(self._line[20:26]))
f.write(unicode(self._line[52]))
s = f.getvalue()
self.assertEqual(s, unicode('abcuvwxyz!'))
self.assertEqual(type(s), types.UnicodeType)
def writexml(self, stream, indent='', addindent='', newl='', strip=0,
nsprefixes={}, namespace=''):
if self.raw:
val = self.nodeValue
if not isinstance(val, StringTypes):
val = str(self.nodeValue)
else:
v = self.nodeValue
if not isinstance(v, StringTypes):
v = str(v)
if strip:
v = ' '.join(v.split())
val = escape(v)
if isinstance(val, UnicodeType):
val = val.encode('utf8')
stream.write(val)
def __init__(self):
"""SecurityOptions()
Initialize.
"""
# I don't believe any of these types can ever pose a security hazard,
# except perhaps "reference"...
self.allowedTypes = {"None": 1,
"bool": 1,
"boolean": 1,
"string": 1,
"str": 1,
"int": 1,
"float": 1,
"datetime": 1,
"time": 1,
"date": 1,
"timedelta": 1,
"NoneType": 1}
if hasattr(types, 'UnicodeType'):
self.allowedTypes['unicode'] = 1
self.allowedModules = {}
self.allowedClasses = {}
def XMLTreeRecursion(node,handlers,res):
'''This is the dispatcher for a recursive walk through the XMLTree.
It is first called by "ProcessXMLTree" with the root of the tree as "tree"
and can in turn be called by handler routines to work on subtrees.'''
# node can be string node, a unicode node or a true node
if type(node) == types.StringType:
handlers.handleString(node,res) # default behaviour is a res.write
elif type(node) == types.UnicodeType:
handlers.handleString(node.encode('ISO-8859-1','replace'),res)
# note that we ignore encoding problems here!
# Now "node" must be a tuple:
elif node[0] == '<![CDATA[': # a CDATA section node
handlers.handleCDATA(node,res)
elif node[0] == '<!--': # a comment node
handlers.handleComment(node,res)
elif node[0] == '<?': # a processing instruction
handlers.handlePI(node,res)
elif hasattr(handlers,'handle_'+node[0]):
method = getattr(handlers,'handle_'+node[0])
method(node,res)
else:
handlers.handleDefault(node,res)
# For the processing of XML trees in memory we define a class:
def renderContents(self, showStructureIndent=None, needUnicode=None):
"""Renders the contents of this tag as a (possibly Unicode)
string."""
s=[]
for c in self:
text = None
if isinstance(c, NavigableUnicodeString) or type(c) == types.UnicodeType:
text = unicode(c)
elif isinstance(c, Tag):
s.append(c.__str__(needUnicode, showStructureIndent))
elif needUnicode:
text = unicode(c)
else:
text = str(c)
if text:
if showStructureIndent != None:
if text[-1] == '\n':
text = text[:-1]
s.append(text)
return ''.join(s)
#Soup methods
def endData(self):
currentData = ''.join(self.currentData)
if currentData:
if not currentData.strip():
if '\n' in currentData:
currentData = '\n'
else:
currentData = ' '
c = NavigableString
if type(currentData) == types.UnicodeType:
c = NavigableUnicodeString
o = c(currentData)
o.setup(self.currentTag, self.previous)
if self.previous:
self.previous.next = o
self.previous = o
self.currentTag.contents.append(o)
self.currentData = []
def main(args):
rf = RecentFeed(args)
if args:
articles = []
for arg in args.urls[0]:
if args.verbose:
print arg
rf.get(arg)
rf.parse()
articles.append(rf.recently())
for article in articles[0]:
if args.output == 'html':
if type(article['title']) is types.UnicodeType:
article['title'] = article['title'].encode('utf-8', 'replace')
print '<li><a href="{0}">{1}</a></li>'.format(article['id'], article['title'])
elif args.output == 'json':
json.dumps({'title': article['title'], 'url': article['id']})
_beautifulsoup.py 文件源码
项目:plugin.video.streamondemand-pureita
作者: orione7
项目源码
文件源码
阅读 24
收藏 0
点赞 0
评论 0
def renderContents(self, showStructureIndent=None, needUnicode=None):
"""Renders the contents of this tag as a (possibly Unicode)
string."""
s=[]
for c in self:
text = None
if isinstance(c, NavigableUnicodeString) or type(c) == types.UnicodeType:
text = unicode(c)
elif isinstance(c, Tag):
s.append(c.__str__(needUnicode, showStructureIndent))
elif needUnicode:
text = unicode(c)
else:
text = str(c)
if text:
if showStructureIndent != None:
if text[-1] == '\n':
text = text[:-1]
s.append(text)
return ''.join(s)
#Soup methods
_beautifulsoup.py 文件源码
项目:plugin.video.streamondemand-pureita
作者: orione7
项目源码
文件源码
阅读 22
收藏 0
点赞 0
评论 0
def endData(self):
currentData = ''.join(self.currentData)
if currentData:
if not currentData.strip():
if '\n' in currentData:
currentData = '\n'
else:
currentData = ' '
c = NavigableString
if type(currentData) == types.UnicodeType:
c = NavigableUnicodeString
o = c(currentData)
o.setup(self.currentTag, self.previous)
if self.previous:
self.previous.next = o
self.previous = o
self.currentTag.contents.append(o)
self.currentData = []
def _escape_json(json):
"""Escapes all string fields of JSON data.
Operates recursively."""
t = type(json)
if t == types.StringType or t == types.UnicodeType:
return cgi.escape(json)
elif t == types.IntType:
return json
elif t == types.FloatType:
return json
elif t == types.DictType:
result = {}
for f in json.keys():
result[f] = _escape_json(json[f])
return result
elif t == types.ListType:
result = []
for f in json:
result.append(_escape_json(f))
return result
else:
raise RuntimeError, "Unsupported type: %s" % str(t)
def GetValue(self, row, col):
if row < self.GetNumberRows():
if col == 0:
return self.data[row].Number
colname = self.GetColLabelValue(col, False)
if colname == "Initial Value":
colname = "InitialValue"
value = getattr(self.data[row], colname, "")
if colname == "Type" and isinstance(value, TupleType):
if value[0] == "array":
return "ARRAY [%s] OF %s" % (",".join(map(lambda x: "..".join(x), value[2])), value[1])
if not isinstance(value, (StringType, UnicodeType)):
value = str(value)
if colname in ["Class", "Option"]:
return _(value)
return value
def test_unicode(self):
if not test_support.have_unicode: return
# The StringIO module also supports concatenating Unicode
# snippets to larger Unicode strings. This is tested by this
# method. Note that cStringIO does not support this extension.
f = self.MODULE.StringIO()
f.write(self._line[:6])
f.seek(3)
f.write(unicode(self._line[20:26]))
f.write(unicode(self._line[52]))
s = f.getvalue()
self.assertEqual(s, unicode('abcuvwxyz!'))
self.assertEqual(type(s), types.UnicodeType)
def test_unicode(self):
if not test_support.have_unicode: return
# The StringIO module also supports concatenating Unicode
# snippets to larger Unicode strings. This is tested by this
# method. Note that cStringIO does not support this extension.
f = self.MODULE.StringIO()
f.write(self._line[:6])
f.seek(3)
f.write(unicode(self._line[20:26]))
f.write(unicode(self._line[52]))
s = f.getvalue()
self.assertEqual(s, unicode('abcuvwxyz!'))
self.assertEqual(type(s), types.UnicodeType)
def runsource(self, source):
"Extend base class method: Stuff the source in the line cache first"
filename = self.stuffsource(source)
self.more = 0
self.save_warnings_filters = warnings.filters[:]
warnings.filterwarnings(action="error", category=SyntaxWarning)
if isinstance(source, types.UnicodeType):
from idlelib import IOBinding
try:
source = source.encode(IOBinding.encoding)
except UnicodeError:
self.tkconsole.resetoutput()
self.write("Unsupported characters in input\n")
return
try:
# InteractiveInterpreter.runsource() calls its runcode() method,
# which is overridden (see below)
return InteractiveInterpreter.runsource(self, source, filename)
finally:
if self.save_warnings_filters is not None:
warnings.filters[:] = self.save_warnings_filters
self.save_warnings_filters = None
def __call__(self, message, verbosity):
"""Log message that has verbosity importance
message can be a string, which is logged as-is, or a function,
which is then called and should return the string to be
logged. We do it this way in case producing the string would
take a significant amount of CPU.
"""
if verbosity > self.verbosity and verbosity > self.term_verbosity:
return
if not (type(message) is types.StringType
or type(message) is types.UnicodeType):
assert type(message) is types.FunctionType
message = message()
if verbosity <= self.verbosity: self.log_to_file(message)
if verbosity <= self.term_verbosity:
self.log_to_term(message, verbosity)
def renderContents(self, showStructureIndent=None, needUnicode=None):
"""Renders the contents of this tag as a (possibly Unicode)
string."""
s=[]
for c in self:
text = None
if isinstance(c, NavigableUnicodeString) or type(c) == types.UnicodeType:
text = unicode(c)
elif isinstance(c, Tag):
s.append(c.__str__(needUnicode, showStructureIndent))
elif needUnicode:
text = unicode(c)
else:
text = str(c)
if text:
if showStructureIndent != None:
if text[-1] == '\n':
text = text[:-1]
s.append(text)
return ''.join(s)
#Soup methods
def endData(self):
currentData = ''.join(self.currentData)
if currentData:
if not currentData.strip():
if '\n' in currentData:
currentData = '\n'
else:
currentData = ' '
c = NavigableString
if type(currentData) == types.UnicodeType:
c = NavigableUnicodeString
o = c(currentData)
o.setup(self.currentTag, self.previous)
if self.previous:
self.previous.next = o
self.previous = o
self.currentTag.contents.append(o)
self.currentData = []
def alert_count(conf):
import types
# convert string handle to Conf object automatically
if isinstance(conf, types.UnicodeType):
conf = Conf.objects.get(handle=conf)
qs = conf.alerts.filter(seen=False)
unread = len(qs)
if unread:
severity = max([x.severity for x in qs])
css = {
0: 'badge-info',
1: 'badge-warning',
2: 'badge-important'
}
css_class = css.get(severity)
else:
css_class = 'badge-default'
return u'<span class="badge %s">%d</span>' % (css_class, unread)
def alert_count(conf):
import types
# convert string handle to Conf object automatically
if isinstance(conf, types.UnicodeType):
conf = Conf.objects.get(handle=conf)
qs = conf.alerts.filter(seen=False)
unread = len(qs)
if unread:
severity = max([x.severity for x in qs])
css = {
0: 'badge-info',
1: 'badge-warning',
2: 'badge-important'
}
css_class = css.get(severity)
else:
css_class = 'badge-default'
return u'<span class="badge %s">%d</span>' % (css_class, unread)
def alert_count(conf):
import types
# convert string handle to Conf object automatically
if isinstance(conf, types.UnicodeType):
conf = Conf.objects.get(handle=conf)
qs = conf.alerts.filter(seen=False)
unread = len(qs)
if unread:
severity = max([x.severity for x in qs])
css = {
0: 'badge-info',
1: 'badge-warning',
2: 'badge-important'
}
css_class = css.get(severity)
else:
css_class = 'badge-default'
return u'<span class="badge %s">%d</span>' % (css_class, unread)
def alert_count(conf):
import types
# convert string handle to Conf object automatically
if isinstance(conf, types.UnicodeType):
conf = Conf.objects.get(handle=conf)
qs = conf.alerts.filter(seen=False)
unread = len(qs)
if unread:
severity = max([x.severity for x in qs])
css = {
0: 'badge-info',
1: 'badge-warning',
2: 'badge-important'
}
css_class = css.get(severity)
else:
css_class = 'badge-default'
return u'<span class="badge %s">%d</span>' % (css_class, unread)
def alert_count(conf):
import types
# convert string handle to Conf object automatically
if isinstance(conf, types.UnicodeType):
conf = Conf.objects.get(handle=conf)
qs = conf.alerts.filter(seen=False)
unread = len(qs)
if unread:
severity = max([x.severity for x in qs])
css = {
0: 'badge-info',
1: 'badge-warning',
2: 'badge-important'
}
css_class = css.get(severity)
else:
css_class = 'badge-default'
return u'<span class="badge %s">%d</span>' % (css_class, unread)
def alert_count(conf):
import types
# convert string handle to Conf object automatically
if isinstance(conf, types.UnicodeType):
conf = Conf.objects.get(handle=conf)
qs = conf.alerts.filter(seen=False)
unread = len(qs)
if unread:
severity = max([x.severity for x in qs])
css = {
0: 'badge-info',
1: 'badge-warning',
2: 'badge-important'
}
css_class = css.get(severity)
else:
css_class = 'badge-default'
return u'<span class="badge %s">%d</span>' % (css_class, unread)
def writexml(self, stream, indent='', addindent='', newl='', strip=0,
nsprefixes={}, namespace=''):
if self.raw:
val = self.nodeValue
if not isinstance(val, StringTypes):
val = str(self.nodeValue)
else:
v = self.nodeValue
if not isinstance(v, StringTypes):
v = str(v)
if strip:
v = ' '.join(v.split())
val = escape(v)
if isinstance(val, UnicodeType):
val = val.encode('utf8')
stream.write(val)
def apply_zen21467_patch(self):
"""Patch cause of ZEN-21467 issue.
The problem is that zenpacklib sets string property values to unicode
strings instead of regular strings. There's a platform bug that
prevents unicode values from being serialized to be used by zenjmx.
This means that JMX datasources won't work without this patch.
"""
try:
from Products.ZenHub.XmlRpcService import XmlRpcService
if types.UnicodeType not in XmlRpcService.PRIMITIVES:
XmlRpcService.PRIMITIVES.append(types.UnicodeType)
except Exception:
# The above may become wrong in future platform versions.
pass