Commit 4df2d42b authored by Mike Frysinger's avatar Mike Frysinger Committed by Commit Bot

grit: replace types module with six

Python 3 has dropped the types module, so switch to six for the types
that still make sense.  For basic types (like tuple), we can just use
the builtin instead.

Bug: 983071
Test: `./grit/test_suite_all.py` passes

Change-Id: I9d1fffdcf38c5550dffb9e946bf5890930399280
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1700507Reviewed-by: default avatarAndrew Grieve <agrieve@chromium.org>
Commit-Queue: Mike Frysinger <vapier@chromium.org>
Cr-Commit-Position: refs/heads/master@{#686513}
parent e40146ed
......@@ -9,7 +9,8 @@ collections of cliques (uber-cliques).
from __future__ import print_function
import re
import types
import six
from grit import constants
from grit import exception
......@@ -271,7 +272,7 @@ class CustomType(object):
'''
contents = translation.GetContent()
for ix in range(len(contents)):
if (isinstance(contents[ix], types.StringTypes)):
if (isinstance(contents[ix], six.string_types)):
contents[ix] = self.ModifyTextPart(lang, contents[ix])
......
......@@ -9,7 +9,8 @@ from __future__ import print_function
import os
import re
import types
import six
from grit import util
......@@ -35,7 +36,7 @@ def _FormatHeader(root, output_dir):
def Format(root, lang='en', output_dir='.'):
"""Outputs a C switch statement representing the string table."""
from grit.node import message
assert isinstance(lang, types.StringTypes)
assert isinstance(lang, six.string_types)
yield _FormatHeader(root, output_dir)
......
......@@ -8,10 +8,11 @@
from __future__ import print_function
import os
import types
import re
from functools import partial
import six
from grit import util
from grit.node import misc
......@@ -315,7 +316,7 @@ def RcSubstitutions(substituter, lang):
def _FormatHeader(root, lang, output_dir):
'''Returns the required preamble for RC files.'''
assert isinstance(lang, types.StringTypes)
assert isinstance(lang, six.string_types)
assert isinstance(root, misc.GritNode)
# Find the location of the resource header file, so that we can include
# it.
......@@ -373,7 +374,7 @@ def FormatMessage(item, lang):
def _FormatSection(item, lang, output_dir):
'''Writes out an .rc file section.'''
assert isinstance(lang, types.StringTypes)
assert isinstance(lang, six.string_types)
from grit.node import structure
assert isinstance(item, structure.StructureNode)
......@@ -402,7 +403,7 @@ def FormatInclude(item, lang, output_dir, type=None, process_html=False):
StructureNode)
process_html: False/True (ignored unless item is a StructureNode)
'''
assert isinstance(lang, types.StringTypes)
assert isinstance(lang, six.string_types)
from grit.node import structure
from grit.node import include
assert isinstance(item, (structure.StructureNode, include.IncludeNode))
......
......@@ -8,7 +8,8 @@
from __future__ import print_function
import os.path
import types
import six
from grit import clique
from grit import util
......@@ -161,7 +162,7 @@ class GathererBase(object):
'''A convenience function for subclasses that loads the contents of the
input file.
'''
if isinstance(self.rc_file, types.StringTypes):
if isinstance(self.rc_file, six.string_types):
path = self.GetInputPath()
# Hack: some unit tests supply an absolute path and no root node.
if not os.path.isabs(path):
......
......@@ -8,7 +8,7 @@ list.
from __future__ import print_function
import types
import six
from grit.gather import interface
from grit import clique
......@@ -78,17 +78,17 @@ class SkeletonGatherer(interface.GathererBase):
out = []
for ix in range(len(self.skeleton_)):
if isinstance(self.skeleton_[ix], types.StringTypes):
if isinstance(self.skeleton_[ix], six.string_types):
if skeleton_gatherer:
# Make sure the skeleton is like the original
assert(isinstance(skeleton_gatherer.skeleton_[ix], types.StringTypes))
assert(isinstance(skeleton_gatherer.skeleton_[ix], six.string_types))
out.append(skeleton_gatherer.skeleton_[ix])
else:
out.append(self.skeleton_[ix])
else:
if skeleton_gatherer: # Make sure the skeleton is like the original
assert(not isinstance(skeleton_gatherer.skeleton_[ix],
types.StringTypes))
six.string_types))
msg = self.skeleton_[ix].MessageForLanguage(lang,
pseudo_if_not_available,
fallback_to_english)
......
......@@ -52,7 +52,8 @@ extern.tclib.api.handlers.html.TCHTMLParser.
from __future__ import print_function
import re
import types
import six
from grit import clique
from grit import exception
......@@ -573,7 +574,7 @@ def HtmlToMessage(html, include_block_tags=False, description=''):
current += m.end()
continue
if len(parts) and isinstance(parts[-1], types.StringTypes):
if len(parts) and isinstance(parts[-1], six.string_types):
parts[-1] += html[current]
else:
parts.append(html[current])
......@@ -582,7 +583,7 @@ def HtmlToMessage(html, include_block_tags=False, description=''):
msg_text = ''
placeholders = []
for part in parts:
if isinstance(part, types.TupleType):
if isinstance(part, tuple):
final_name = part[0]()
original = part[1]
msg_text += final_name
......@@ -594,7 +595,7 @@ def HtmlToMessage(html, include_block_tags=False, description=''):
description=description)
content = msg.GetContent()
for ix in range(len(content)):
if isinstance(content[ix], types.StringTypes):
if isinstance(content[ix], six.string_types):
content[ix] = util.UnescapeHtml(content[ix], replace_nbsp=False)
return msg
......@@ -658,7 +659,7 @@ class TrHtml(interface.GathererBase):
out = []
for item in self.skeleton_:
if isinstance(item, types.StringTypes):
if isinstance(item, six.string_types):
out.append(item)
else:
msg = item.MessageForLanguage(lang,
......@@ -718,8 +719,8 @@ class TrHtml(interface.GathererBase):
if isinstance(self.skeleton_[ix], clique.MessageClique):
msg = self.skeleton_[ix].GetMessage()
for item in msg.GetContent():
if (isinstance(item, types.StringTypes) and _NON_WHITESPACE.search(item)
and item != '&nbsp;'):
if (isinstance(item, six.string_types)
and _NON_WHITESPACE.search(item) and item != '&nbsp;'):
got_text = True
break
if not got_text:
......
......@@ -12,9 +12,9 @@ import sys
if __name__ == '__main__':
sys.path.append(os.path.join(os.path.dirname(__file__), '../..'))
import types
import unittest
import six
from six import StringIO
from grit.gather import tr_html
......@@ -369,7 +369,7 @@ bla
# For manual results inspection only...
list = []
for item in html.skeleton_:
if isinstance(item, types.StringTypes):
if isinstance(item, six.string_types):
list.append(item)
else:
list.append(item.GetMessage().GetPresentableContent())
......
......@@ -10,10 +10,11 @@ from __future__ import print_function
import os.path
import sys
import types
import xml.sax
import xml.sax.handler
import six
from grit import exception
from grit import util
from grit.node import mapping
......@@ -187,7 +188,7 @@ def Parse(filename_or_stream, dir=None, stop_after=None, first_ids_file=None,
grit.exception.Parsing
'''
if isinstance(filename_or_stream, types.StringType):
if isinstance(filename_or_stream, six.string_types):
source = filename_or_stream
if dir is None:
dir = util.dirname(filename_or_stream)
......
......@@ -11,9 +11,10 @@ import ast
import os
import struct
import sys
import types
from xml.sax import saxutils
import six
from grit import constants
from grit import clique
from grit import exception
......@@ -112,7 +113,7 @@ class Node(object):
name: u'elementname'
parent: grit.node.base.Node or subclass or None
'''
assert isinstance(name, types.StringTypes)
assert isinstance(name, six.string_types)
assert not parent or isinstance(parent, Node)
self.name = name
self.parent = parent
......@@ -155,7 +156,7 @@ class Node(object):
Return:
None
'''
assert isinstance(content, types.StringTypes)
assert isinstance(content, six.string_types)
if self._ContentType() != self._CONTENT_TYPE_NONE:
self.mixed_content.append(content)
elif content.strip() != '':
......@@ -172,8 +173,8 @@ class Node(object):
Return:
None
'''
assert isinstance(attrib, types.StringTypes)
assert isinstance(value, types.StringTypes)
assert isinstance(attrib, six.string_types)
assert isinstance(value, six.string_types)
if self._IsValidAttribute(attrib, value):
self.attrs[attrib] = value
else:
......@@ -184,34 +185,34 @@ class Node(object):
# TODO(joi) Rewrite this, it's extremely ugly!
if len(self.mixed_content):
if isinstance(self.mixed_content[0], types.StringTypes):
if isinstance(self.mixed_content[0], six.string_types):
# Remove leading and trailing chunks of pure whitespace.
while (len(self.mixed_content) and
isinstance(self.mixed_content[0], types.StringTypes) and
isinstance(self.mixed_content[0], six.string_types) and
self.mixed_content[0].strip() == ''):
self.mixed_content = self.mixed_content[1:]
# Strip leading and trailing whitespace from mixed content chunks
# at front and back.
if (len(self.mixed_content) and
isinstance(self.mixed_content[0], types.StringTypes)):
isinstance(self.mixed_content[0], six.string_types)):
self.mixed_content[0] = self.mixed_content[0].lstrip()
# Remove leading and trailing ''' (used to demarcate whitespace)
if (len(self.mixed_content) and
isinstance(self.mixed_content[0], types.StringTypes)):
isinstance(self.mixed_content[0], six.string_types)):
if self.mixed_content[0].startswith("'''"):
self.mixed_content[0] = self.mixed_content[0][3:]
if len(self.mixed_content):
if isinstance(self.mixed_content[-1], types.StringTypes):
if isinstance(self.mixed_content[-1], six.string_types):
# Same stuff all over again for the tail end.
while (len(self.mixed_content) and
isinstance(self.mixed_content[-1], types.StringTypes) and
isinstance(self.mixed_content[-1], six.string_types) and
self.mixed_content[-1].strip() == ''):
self.mixed_content = self.mixed_content[:-1]
if (len(self.mixed_content) and
isinstance(self.mixed_content[-1], types.StringTypes)):
isinstance(self.mixed_content[-1], six.string_types)):
self.mixed_content[-1] = self.mixed_content[-1].rstrip()
if (len(self.mixed_content) and
isinstance(self.mixed_content[-1], types.StringTypes)):
isinstance(self.mixed_content[-1], six.string_types)):
if self.mixed_content[-1].endswith("'''"):
self.mixed_content[-1] = self.mixed_content[-1][:-3]
......@@ -244,7 +245,7 @@ class Node(object):
'''Returns all CDATA of this element, concatenated into a single
string. Note that this ignores any elements embedded in CDATA.'''
return ''.join([c for c in self.mixed_content
if isinstance(c, types.StringTypes)])
if isinstance(c, six.string_types)])
def __unicode__(self):
'''Returns this node and all nodes below it as an XML document in a Unicode
......@@ -259,7 +260,7 @@ class Node(object):
children and CDATA are layed out in a way that preserves internal
whitespace.
'''
assert isinstance(indent, types.StringTypes)
assert isinstance(indent, six.string_types)
content_one_line = (one_line or
self._ContentType() == self._CONTENT_TYPE_MIXED)
......@@ -293,7 +294,7 @@ class Node(object):
def ContentsAsXml(self, indent, one_line):
'''Returns the contents of this node (CDATA and child elements) in XML
format. If 'one_line' is true, the content will be laid out on one line.'''
assert isinstance(indent, types.StringTypes)
assert isinstance(indent, six.string_types)
# Build the contents of the element.
inside_parts = []
......@@ -319,7 +320,7 @@ class Node(object):
# If the last item is a string (not a node) and ends with whitespace,
# we need to add the ''' delimiter.
if (isinstance(last_item, types.StringTypes) and
if (isinstance(last_item, six.string_types) and
last_item.rstrip() != last_item):
inside_parts[-1] = inside_parts[-1] + u"'''"
......
......@@ -8,7 +8,8 @@
from __future__ import print_function
import re
import types
import six
from grit.node import base
......@@ -166,7 +167,7 @@ class MessageNode(base.ContentNode):
placeholders = []
for item in self.mixed_content:
if isinstance(item, types.StringTypes):
if isinstance(item, six.string_types):
# Not a <ph> element: fail if any <ph> formatters are detected.
if _FORMATTERS.search(item):
print(_BAD_PLACEHOLDER_MSG % (item, self.source))
......@@ -303,7 +304,7 @@ class MessageNode(base.ContentNode):
items = message.GetContent()
for ix, item in enumerate(items):
if isinstance(item, types.StringTypes):
if isinstance(item, six.string_types):
# Ensure whitespace at front and back of message is correctly handled.
if ix == 0:
item = "'''" + item
......
......@@ -8,7 +8,8 @@
from __future__ import print_function
import re
import types
import six
from grit import exception
from grit import lazy_re
......@@ -83,7 +84,7 @@ class BaseMessage(object):
'''
bits = []
for item in self.parts:
if isinstance(item, types.StringTypes):
if isinstance(item, six.string_types):
bits.append(escaping_function(item))
else:
bits.append(item.GetOriginal())
......@@ -112,7 +113,7 @@ class BaseMessage(object):
self.dirty = True
def AppendText(self, text):
assert isinstance(text, types.StringTypes)
assert isinstance(text, six.string_types)
assert text != ''
self.parts.append(text)
......
......@@ -12,9 +12,10 @@ import os.path
if __name__ == '__main__':
sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
import types
import unittest
import six
from grit import tclib
from grit import exception
......@@ -26,14 +27,14 @@ class TclibUnittest(unittest.TestCase):
msg = tclib.Message(text=u'Hello Earthlings',
description='Greetings\n\t message')
self.failUnlessEqual(msg.GetPresentableContent(), 'Hello Earthlings')
self.failUnless(isinstance(msg.GetPresentableContent(), types.StringTypes))
self.failUnless(isinstance(msg.GetPresentableContent(), six.string_types))
self.failUnlessEqual(msg.GetDescription(), 'Greetings message')
def testGetAttr(self):
msg = tclib.Message()
msg.AppendText(u'Hello') # Tests __getattr__
self.failUnless(msg.GetPresentableContent() == 'Hello')
self.failUnless(isinstance(msg.GetPresentableContent(), types.StringTypes))
self.failUnless(isinstance(msg.GetPresentableContent(), six.string_types))
def testAll(self):
text = u'Howdie USERNAME'
......@@ -43,7 +44,7 @@ class TclibUnittest(unittest.TestCase):
trans = tclib.Translation(text=text, placeholders=phs)
self.failUnless(trans.GetPresentableContent() == 'Howdie USERNAME')
self.failUnless(isinstance(trans.GetPresentableContent(), types.StringTypes))
self.failUnless(isinstance(trans.GetPresentableContent(), six.string_types))
def testUnicodeReturn(self):
text = u'\u00fe'
......@@ -65,7 +66,7 @@ class TclibUnittest(unittest.TestCase):
transl = tclib.Translation(text=msg.GetPresentableContent(),
placeholders=msg.GetPlaceholders())
content = transl.GetContent()
self.failUnless(isinstance(content[3], types.UnicodeType))
self.failUnless(isinstance(content[3], six.string_types))
def testFingerprint(self):
# This has Windows line endings. That is on purpose.
......
......@@ -6,7 +6,7 @@
from __future__ import print_function
import types
import six
from grit import grd_reader
from grit import util
......@@ -61,7 +61,7 @@ to being one message for the whole menu.'''
contents = message.GetContent()
for part in contents:
if isinstance(part, types.StringTypes):
if isinstance(part, six.string_types):
id = grit.extern.tclib.GenerateMessageId(part)
if id not in xtb:
print("WARNING didn't find all translations for menu %s" %
......
......@@ -10,8 +10,8 @@ import os.path
import getopt
import re
import sys
import types
import six
from six import StringIO
import grit.node.empty
......@@ -341,7 +341,7 @@ C preprocessor on the .rc file or manually edit it before using this tool.
# Messages that contain only placeholders do not need translation.
is_translateable = False
for item in msg_obj.GetContent():
if isinstance(item, types.StringTypes):
if isinstance(item, six.string_types):
if not _WHITESPACE_ONLY.match(item):
is_translateable = True
......@@ -389,7 +389,7 @@ C preprocessor on the .rc file or manually edit it before using this tool.
# TODO(joi) Allow use of non-TotalRecall flavors of HTML placeholderizing
msg = tr_html.HtmlToMessage(text, True)
for item in msg.GetContent():
if not isinstance(item, types.StringTypes):
if not isinstance(item, six.string_types):
return msg # Contained at least one placeholder, so we're done
# HTML placeholderization didn't do anything, so try to find printf or
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment