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
python类UnicodeType()的实例源码
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 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 setUp(self):
class OtherInfo:
__metaclass__ = autoxml.autoxml
t_BirthDate = [types.StringType, autoxml.mandatory]
t_Interest = [types.StringType, autoxml.optional]
t_CodesWith = [ [types.UnicodeType], autoxml.optional, 'CodesWith/Person']
class A(xmlfile.XmlFile):
__metaclass__ = autoxml.autoxml
t_Name = [types.UnicodeType, autoxml.mandatory]
t_Description = [autoxml.LocalText, autoxml.mandatory]
t_Number = [types.IntType, autoxml.optional]
t_Email = [types.StringType, autoxml.optional]
a_href = [types.StringType, autoxml.mandatory]
t_Projects = [ [types.StringType], autoxml.mandatory, 'Project']
t_OtherInfo = [ OtherInfo, autoxml.optional ]
s_Comment = [ autoxml.Text, autoxml.mandatory]
self.A = A
def isbasestring(x):
return isinstance(x, (types.StringType, types.UnicodeType))
def isbasestring(x):
return isinstance(x, (types.StringType, types.UnicodeType))
def writexml(self, stream, indent='', addindent='', newl='', strip=0,
nsprefixes={}, namespace=''):
val=self.data
if isinstance(val, UnicodeType):
val=val.encode('utf8')
stream.write("<!--%s-->" % val)
def parseString(st, *args, **kw):
if isinstance(st, UnicodeType):
# this isn't particularly ideal, but it does work.
return parse(StringIO(st.encode('UTF-16')), *args, **kw)
return parse(StringIO(st), *args, **kw)
def _unjelly_unicode(self, exp):
if UnicodeType:
return unicode(exp[0], "UTF-8")
else:
return Unpersistable(exp[0])
def recode(self, utext):
if type(utext) is types.UnicodeType:
return utext.encode(self.options.charset, 'replace')
return utext
def _MungeLocationListIntoFiles(self, urllist):
"""Given a list of URLs, munge them into our self._pathlist property.
We do this by assuming all the files live in the same directory as
the first file in the existing pathlist. That is, we assume a
Sitemap index points to Sitemaps only in the same directory. This
is not true in general, but will be true for any output produced
by this script.
"""
assert self._pathlist
path = self._pathlist[0]
path = os.path.normpath(path)
dir = os.path.dirname(path)
wide = False
if type(path) == types.UnicodeType:
wide = True
for url in urllist:
url = URL.Canonicalize(url)
output.Log('Index points to Sitemap file at: %s' % url, 2)
(scheme, netloc, path, query, frag) = urlparse.urlsplit(url)
file = os.path.basename(path)
file = urllib.unquote(file)
if wide:
file = encoder.WidenText(file)
if dir:
file = dir + os.sep + file
if file:
self._pathlist.append(file)
output.Log('Will attempt to read Sitemap file: %s' % file, 1)
#end def _MungeLocationListIntoFiles
def str2long(s):
"""Convert a string to a long integer."""
if type(s) not in (types.StringType, types.UnicodeType):
raise ValueError, 'the input must be a string'
l = 0L
for i in s:
l <<= 8
l |= ord(i)
return l
def recode(self, utext):
if type(utext) is types.UnicodeType:
return utext.encode(self.options.charset, 'replace')
return utext
def __init__(self, URI):
_checkInit() # init required
if type(URI) in (StringType,UnicodeType):
URI=processStringURI(URI)
self.URI = URI
self.objectID = URI.objectID
# Delay adapter binding to enable transporting of proxies.
# We just create an adapter, and don't connect it...
self.adapter = Pyro.protocol.getProtocolAdapter(self.URI.protocol)
# ---- don't forget to register local vars with DynamicProxyWithAttrs, see below
def _process_result(self, method, url, response, request_class, response_class, query_params, path_params):
cls = response_class and response_class or request_class
if cls in [types.StringType, types.UnicodeType]: # if we expect string type result, no need for further processing
return response.text
else:
# prepare context to hold url and data along with method to navigate pages
context = { 'method': method, 'url': url, 'query_params': query_params, 'response_class': response_class, 'func': self._get_all, 'request_class': request_class, 'path_params': path_params, 'response_data': response.text }
return self._json_object_to_object(json.loads(response.text), cls, context=context) # first load JSON to a Python list or dict
def isbasestring(x):
return isinstance(x, (types.StringType, types.UnicodeType))
def unicode_line(self, line):
try:
if type(line) is types.UnicodeType:
return line
else:
return unicode(line, errors='ignore')
except Exception as e:
print("unicode err:%r" % e)
print("line can't decode:%s" % line)
print("Except stack:%s" % traceback.format_exc())
return ""
def isbasestring(x):
return isinstance(x, (types.StringType, types.UnicodeType))
def unicode_line(self, line):
try:
if type(line) is types.UnicodeType:
return line
else:
return unicode(line, errors='ignore')
except Exception as e:
print("unicode err:%r" % e)
print("line can't decode:%s" % line)
print("Except stack:%s" % traceback.format_exc())
return ""