- id: debug-statements
- id: end-of-file-fixer
- id: flake8
- exclude: (docs/conf.py|babel/messages/__init__.py|babel/__init__.py|babel/_compat.py|tests/messages/data|scripts/import_cldr.py)
+ exclude: (docs/conf.py|babel/messages/__init__.py|babel/__init__.py|tests/messages/data|scripts/import_cldr.py)
- id: name-tests-test
args: ['--django']
exclude: (tests/messages/data/)
+++ /dev/null
-import sys
-import array
-
-PY2 = sys.version_info[0] == 2
-
-_identity = lambda x: x
-
-
-if not PY2:
- text_type = str
- string_types = (str,)
- integer_types = (int, )
-
- text_to_native = lambda s, enc: s
- unichr = chr
-
- iterkeys = lambda d: iter(d.keys())
- itervalues = lambda d: iter(d.values())
- iteritems = lambda d: iter(d.items())
-
- from io import StringIO, BytesIO
- import pickle
-
- izip = zip
- imap = map
- range_type = range
-
- cmp = lambda a, b: (a > b) - (a < b)
-
- array_tobytes = array.array.tobytes
- from collections import abc
-
-else:
- text_type = unicode
- string_types = (str, unicode)
- integer_types = (int, long)
-
- text_to_native = lambda s, enc: s.encode(enc)
- unichr = unichr
-
- iterkeys = lambda d: d.iterkeys()
- itervalues = lambda d: d.itervalues()
- iteritems = lambda d: d.iteritems()
-
- from cStringIO import StringIO as BytesIO
- from StringIO import StringIO
- import cPickle as pickle
-
- from itertools import imap
- from itertools import izip
- range_type = xrange
-
- cmp = cmp
-
- array_tobytes = array.array.tostring
- import collections as abc
-
-number_types = integer_types + (float,)
-
-
-def force_text(s, encoding='utf-8', errors='strict'):
- if isinstance(s, text_type):
- return s
- if isinstance(s, bytes):
- return s.decode(encoding, errors)
- return text_type(s)
-
-
-#
-# Since Python 3.3, a fast decimal implementation is already included in the
-# standard library. Otherwise use cdecimal when available
-#
-if sys.version_info[:2] >= (3, 3):
- import decimal
-else:
- try:
- import cdecimal as decimal
- except ImportError:
- import decimal
:license: BSD, see LICENSE for more details.
"""
+import pickle
import os
from babel import localedata
-from babel._compat import pickle, string_types
from babel.plural import PluralRule
__all__ = ['UnknownLocaleError', 'Locale', 'default_locale', 'negotiate_locale',
return None
elif isinstance(identifier, Locale):
return identifier
- elif not isinstance(identifier, string_types):
+ elif not isinstance(identifier, str):
raise TypeError('Unexpected value for identifier: %r' % (identifier,))
parts = parse_locale(identifier, sep=sep)
from babel.core import default_locale, get_global, Locale
from babel.util import UTC, LOCALTZ
-from babel._compat import string_types, integer_types, number_types, PY2
# "If a given short metazone form is known NOT to be understood in a given
# locale and the parent locale has this value such that it would normally
if dt_or_tzinfo is None:
dt = datetime.now()
tzinfo = LOCALTZ
- elif isinstance(dt_or_tzinfo, string_types):
+ elif isinstance(dt_or_tzinfo, str):
dt = None
tzinfo = get_timezone(dt_or_tzinfo)
- elif isinstance(dt_or_tzinfo, integer_types):
+ elif isinstance(dt_or_tzinfo, int):
dt = None
tzinfo = UTC
elif isinstance(dt_or_tzinfo, (datetime, time)):
"""
if instant is None:
return datetime_.utcnow()
- elif isinstance(instant, integer_types) or isinstance(instant, float):
+ elif isinstance(instant, int) or isinstance(instant, float):
return datetime_.utcfromtimestamp(instant)
elif isinstance(instant, time):
return datetime_.combine(date.today(), instant)
"""
if time is None:
time = datetime.utcnow()
- elif isinstance(time, number_types):
+ elif isinstance(time, (int, float)):
time = datetime.utcfromtimestamp(time)
if time.tzinfo is None:
time = time.replace(tzinfo=UTC)
"""
if zone is None:
return LOCALTZ
- if not isinstance(zone, string_types):
+ if not isinstance(zone, str):
return zone
try:
return _pytz.timezone(zone)
def __str__(self):
pat = self.pattern
- if PY2:
- pat = pat.encode('utf-8')
return pat
def __mod__(self, other):
:license: BSD, see LICENSE for more details.
"""
+import pickle
import os
import re
import sys
import threading
+from collections import abc
from itertools import chain
-from babel._compat import pickle, string_types, abc
-
_cache = {}
_cache_lock = threading.RLock()
Returns the normalized locale ID string or `None` if the ID is not
recognized.
"""
- if not name or not isinstance(name, string_types):
+ if not name or not isinstance(name, str):
return None
name = name.strip().lower()
for locale_id in chain.from_iterable([_cache, locale_identifiers()]):
:param name: the locale identifier string
"""
- if not name or not isinstance(name, string_types):
+ if not name or not isinstance(name, str):
return False
if name in _cache:
return True
from babel.core import Locale, UnknownLocaleError
from babel.dates import format_datetime
from babel.messages.plurals import get_plural
-from babel.util import distinct, LOCALTZ, FixedOffsetTimezone
-from babel._compat import string_types, number_types, PY2, cmp, text_type, force_text
+from babel.util import distinct, LOCALTZ, FixedOffsetTimezone, _cmp
__all__ = ['Message', 'Catalog', 'TranslationError']
self.flags.discard('python-format')
self.auto_comments = list(distinct(auto_comments))
self.user_comments = list(distinct(user_comments))
- if isinstance(previous_id, string_types):
+ if isinstance(previous_id, str):
self.previous_id = [previous_id]
else:
self.previous_id = list(previous_id)
if isinstance(obj, Message) and obj.pluralizable:
return obj.id[0], obj.context or ''
return obj.id, obj.context or ''
- return cmp(values_to_compare(self), values_to_compare(other))
+ return _cmp(values_to_compare(self), values_to_compare(other))
def __gt__(self, other):
return self.__cmp__(other) > 0
#"""
-if PY2:
- def _parse_header(header_string):
- # message_from_string only works for str, not for unicode
- headers = message_from_string(header_string.encode('utf8'))
- decoded_headers = {}
- for name, value in headers.items():
- name = name.decode('utf8')
- value = value.decode('utf8')
- decoded_headers[name] = value
- return decoded_headers
-
-else:
- _parse_header = message_from_string
-
-
class Catalog(object):
"""Representation of a message catalog."""
return
if isinstance(locale, Locale):
- self._locale_identifier = text_type(locale)
+ self._locale_identifier = str(locale)
self._locale = locale
return
- if isinstance(locale, string_types):
- self._locale_identifier = text_type(locale)
+ if isinstance(locale, str):
+ self._locale_identifier = str(locale)
try:
self._locale = Locale.parse(locale)
except UnknownLocaleError:
headers.append(('POT-Creation-Date',
format_datetime(self.creation_date, 'yyyy-MM-dd HH:mmZ',
locale='en')))
- if isinstance(self.revision_date, (datetime, time_) + number_types):
+ if isinstance(self.revision_date, (datetime, time_, int, float)):
headers.append(('PO-Revision-Date',
format_datetime(self.revision_date,
'yyyy-MM-dd HH:mmZ', locale='en')))
headers.append(('Generated-By', 'Babel %s\n' % VERSION))
return headers
+ def _force_text(self, s, encoding='utf-8', errors='strict'):
+ if isinstance(s, str):
+ return s
+ if isinstance(s, bytes):
+ return s.decode(encoding, errors)
+ return str(s)
+
def _set_mime_headers(self, headers):
for name, value in headers:
- name = force_text(name.lower(), encoding=self.charset)
- value = force_text(value, encoding=self.charset)
+ name = self._force_text(name.lower(), encoding=self.charset)
+ value = self._force_text(value, encoding=self.charset)
if name == 'project-id-version':
parts = value.split(' ')
self.project = u' '.join(parts[:-1])
>>> Catalog(locale='ding').plural_expr # unknown locale
'(n != 1)'
- :type: `string_types`"""
+ :type: `str`"""
if self._plural_expr is None:
expr = '(n != 1)'
if self.locale:
message = current
elif id == '':
# special treatment for the header message
- self.mime_headers = _parse_header(message.string).items()
+ self.mime_headers = message_from_string(message.string).items()
self.header_comment = '\n'.join([('# %s' % c).rstrip() for c
in message.user_comments])
self.fuzzy = message.fuzzy
fuzzy = True
fuzzy_matches.add(oldkey)
oldmsg = messages.get(oldkey)
- if isinstance(oldmsg.id, string_types):
+ if isinstance(oldmsg.id, str):
message.previous_id = [oldmsg.id]
else:
message.previous_id = list(oldmsg.id)
"""
from babel.messages.catalog import TranslationError, PYTHON_FORMAT
-from babel._compat import string_types, izip
#: list of format chars that are compatible to each other
def num_plurals(catalog, message):
"""Verify the number of plurals in the translation."""
if not message.pluralizable:
- if not isinstance(message.string, string_types):
+ if not isinstance(message.string, str):
raise TranslationError("Found plural forms for non-pluralizable "
"message")
return
if not isinstance(msgstrs, (list, tuple)):
msgstrs = (msgstrs,)
- for msgid, msgstr in izip(msgids, msgstrs):
+ for msgid, msgstr in zip(msgids, msgstrs):
if msgstr:
_validate_format(msgid, msgstr)
if len(a) != len(b):
raise TranslationError('positional format placeholders are '
'unbalanced')
- for idx, ((_, first), (_, second)) in enumerate(izip(a, b)):
+ for idx, ((_, first), (_, second)) in enumerate(zip(a, b)):
if not _compatible(first, second):
raise TranslationError('incompatible format for placeholder '
'%d: %r and %r are not compatible' %
from tokenize import generate_tokens, COMMENT, NAME, OP, STRING
from babel.util import parse_encoding, parse_future_flags, pathmatch
-from babel._compat import PY2, text_type
from textwrap import dedent
... print(_('Hello, world!'))
... '''
- >>> from babel._compat import BytesIO
+ >>> from io import BytesIO
>>> for message in extract('python', BytesIO(source)):
... print(message)
(3, u'Hello, world!', [], None)
encoding = parse_encoding(fileobj) or options.get('encoding', 'UTF-8')
future_flags = parse_future_flags(fileobj, encoding)
-
- if PY2:
- next_line = fileobj.readline
- else:
- next_line = lambda: fileobj.readline().decode(encoding)
+ next_line = lambda: fileobj.readline().decode(encoding)
tokens = generate_tokens(next_line)
for tok, value, (lineno, _), _, _ in tokens:
continue
elif call_stack == -1 and tok == COMMENT:
# Strip the comment token from the line
- if PY2:
- value = value.decode(encoding)
value = value[1:].strip()
if in_translator_comments and \
translator_comments[-1][0] == lineno - 1:
code = compile('# coding=%s\n%s' % (str(encoding), value),
'<string>', 'eval', future_flags)
value = eval(code, {'__builtins__': {}}, {})
- if PY2 and not isinstance(value, text_type):
- value = value.decode(encoding)
buf.append(value)
elif tok == OP and value == ',':
if buf:
import sys
import tempfile
from collections import OrderedDict
+from configparser import RawConfigParser
from datetime import datetime
+from io import StringIO
from locale import getpreferredencoding
from babel import __version__ as VERSION
from babel import Locale, localedata
-from babel._compat import StringIO, string_types, text_type, PY2
from babel.core import UnknownLocaleError
from babel.messages.catalog import Catalog
from babel.messages.extract import DEFAULT_KEYWORDS, DEFAULT_MAPPING, check_and_call_extract_file, extract_from_dir
from distutils.cmd import Command as _Command
from distutils.errors import DistutilsOptionError, DistutilsSetupError
-try:
- from ConfigParser import RawConfigParser
-except ImportError:
- from configparser import RawConfigParser
-
-
-po_file_read_mode = ('rU' if PY2 else 'r')
-
def listify_value(arg, split=None):
"""
if isinstance(val, (list, tuple)):
out.extend(listify_value(val, split=split))
continue
- out.extend(s.strip() for s in text_type(val).split(split))
- assert all(isinstance(val, string_types) for val in out)
+ out.extend(s.strip() for s in str(val).split(split))
+ assert all(isinstance(val, str) for val in out)
return out
"are mutually exclusive")
if self.input_paths:
- if isinstance(self.input_paths, string_types):
+ if isinstance(self.input_paths, str):
self.input_paths = re.split(r',\s*', self.input_paths)
elif self.distribution is not None:
self.input_paths = dict.fromkeys([
mappings = []
if self.mapping_file:
- with open(self.mapping_file, po_file_read_mode) as fileobj:
+ with open(self.mapping_file) as fileobj:
method_map, options_map = parse_mapping(fileobj)
for path in self.input_paths:
mappings.append((path, method_map, options_map))
elif getattr(self.distribution, 'message_extractors', None):
message_extractors = self.distribution.message_extractors
for path, mapping in message_extractors.items():
- if isinstance(mapping, string_types):
+ if isinstance(mapping, str):
method_map, options_map = parse_mapping(StringIO(mapping))
else:
method_map, options_map = [], {}
parser = RawConfigParser()
parser._sections = OrderedDict(parser._sections) # We need ordered sections
-
- if PY2:
- parser.readfp(fileobj, filename)
- else:
- parser.read_file(fileobj, filename)
+ parser.read_file(fileobj, filename)
for section in parser.sections():
if section == 'extractors':
"""
from collections import namedtuple
import re
-from babel._compat import unichr
operators = sorted([
'+', '-', '*', '%', '!=', '==', '<', '>', '<=', '>=', '=',
escaped_value = escaped.group()
if len(escaped_value) == 4:
try:
- add(unichr(int(escaped_value, 16)))
+ add(chr(int(escaped_value, 16)))
except ValueError:
pass
else:
import struct
from babel.messages.catalog import Catalog, Message
-from babel._compat import range_type, array_tobytes
LE_MAGIC = 0x950412de
# Now put all messages from the .mo file buffer into the catalog
# dictionary
- for i in range_type(0, msgcount):
+ for i in range(0, msgcount):
mlen, moff = unpack(ii, buf[origidx:origidx + 8])
mend = moff + mlen
tlen, toff = unpack(ii, buf[transidx:transidx + 8])
>>> import sys
>>> from babel.messages import Catalog
>>> from gettext import GNUTranslations
- >>> from babel._compat import BytesIO
+ >>> from io import BytesIO
>>> catalog = Catalog(locale='en_US')
>>> catalog.add('foo', 'Voh')
7 * 4, # start of key index
7 * 4 + len(messages) * 8, # start of value index
0, 0 # size and offset of hash table
- ) + array_tobytes(array.array("i", offsets)) + ids + strs)
+ ) + array.array.tobytes(array.array("i", offsets)) + ids + strs)
import re
from babel.messages.catalog import Catalog, Message
-from babel.util import wraptext
-from babel._compat import text_type, cmp
+from babel.util import wraptext, _cmp
def unescape(string):
if not other:
return 1
- return cmp(text_type(self), text_type(other))
+ return _cmp(str(self), str(other))
def __gt__(self, other):
return self.__cmp__(other) > 0
for lineno, line in enumerate(fileobj):
line = line.strip()
- if not isinstance(line, text_type):
+ if not isinstance(line, str):
line = line.decode(self.catalog.charset)
if not line:
continue
self._add_message()
def _invalid_pofile(self, line, lineno, msg):
- assert isinstance(line, text_type)
+ assert isinstance(line, str)
if self.abort_invalid:
raise PoFileError(msg, self.catalog, line, lineno)
print("WARNING:", msg)
file-like object and return a `Catalog`.
>>> from datetime import datetime
- >>> from babel._compat import StringIO
+ >>> from io import StringIO
>>> buf = StringIO('''
... #: main.py:1
... #, fuzzy, python-format
<Message...>
>>> catalog.add((u'bar', u'baz'), locations=[('main.py', 3)])
<Message...>
- >>> from babel._compat import BytesIO
+ >>> from io import BytesIO
>>> buf = BytesIO()
>>> write_po(buf, catalog, omit_header=True)
>>> print(buf.getvalue().decode("utf8"))
return normalize(key, prefix=prefix, width=width)
def _write(text):
- if isinstance(text, text_type):
+ if isinstance(text, str):
text = text.encode(catalog.charset, 'backslashreplace')
fileobj.write(text)
# TODO:
# Padding and rounding increments in pattern:
# - https://www.unicode.org/reports/tr35/ (Appendix G.6)
+import decimal
import re
from datetime import date as date_, datetime as datetime_
import warnings
from babel.core import default_locale, Locale, get_global
-from babel._compat import decimal, string_types
try:
# Python 2
This method always return a Boolean and never raise.
"""
- if not currency or not isinstance(currency, string_types):
+ if not currency or not isinstance(currency, str):
return False
try:
validate_currency(currency, locale)
Returns None if the currency is unknown to Babel.
"""
- if isinstance(currency, string_types):
+ if isinstance(currency, str):
currency = currency.upper()
if not is_currency(currency, locale):
return
# Step 2.
# Correct number to numeric type, important for looking up plural rules:
- if isinstance(number, string_types):
+ if isinstance(number, str):
number_n = float(number)
else:
number_n = number
:copyright: (c) 2013-2021 by the Babel Team.
:license: BSD, see LICENSE for more details.
"""
+import decimal
import re
-from babel._compat import decimal
-
_plural_tags = ('zero', 'one', 'two', 'few', 'many', 'other')
_fallback_tag = 'other'
format_timedelta
from babel.numbers import format_number, format_decimal, format_currency, \
format_percent, format_scientific
-from babel._compat import PY2, text_type, text_to_native
class Format(object):
if self._fallback:
return self._fallback.pgettext(context, message)
return message
- # Encode the Unicode tmsg back to an 8-bit string, if possible
- if self._output_charset:
- return text_to_native(tmsg, self._output_charset)
- elif self._charset:
- return text_to_native(tmsg, self._charset)
return tmsg
def lpgettext(self, context, message):
ctxt_msg_id = self.CONTEXT_ENCODING % (context, singular)
try:
tmsg = self._catalog[(ctxt_msg_id, self.plural(num))]
- if self._output_charset:
- return text_to_native(tmsg, self._output_charset)
- elif self._charset:
- return text_to_native(tmsg, self._charset)
return tmsg
except KeyError:
if self._fallback:
if tmsg is missing:
if self._fallback:
return self._fallback.upgettext(context, message)
- return text_type(message)
+ return str(message)
return tmsg
def unpgettext(self, context, singular, plural, num):
if self._fallback:
return self._fallback.unpgettext(context, singular, plural, num)
if num == 1:
- tmsg = text_type(singular)
+ tmsg = str(singular)
else:
- tmsg = text_type(plural)
+ tmsg = str(plural)
return tmsg
def dpgettext(self, domain, context, message):
return self._domains.get(domain, self).lnpgettext(context, singular,
plural, num)
- if not PY2:
- ugettext = gettext.NullTranslations.gettext
- ungettext = gettext.NullTranslations.ngettext
+ ugettext = gettext.NullTranslations.gettext
+ ungettext = gettext.NullTranslations.ngettext
class Translations(NullTranslations, gettext.GNUTranslations):
super(Translations, self).__init__(fp=fp)
self.domain = domain or self.DEFAULT_DOMAIN
- if not PY2:
- ugettext = gettext.GNUTranslations.gettext
- ungettext = gettext.GNUTranslations.ngettext
+ ugettext = gettext.GNUTranslations.gettext
+ ungettext = gettext.GNUTranslations.ngettext
@classmethod
def load(cls, dirname=None, locales=None, domain=None):
# -- encoding: UTF-8 --
-from babel._compat import string_types
from babel.core import Locale
from babel.numbers import format_decimal, LC_NUMERIC
Number formats may be overridden with the ``format`` parameter.
- >>> from babel._compat import decimal
+ >>> import decimal
>>> format_unit(decimal.Decimal("-42.774"), 'temperature-celsius', 'short', format='#.0', locale='fr')
u'-42,8\\u202f\\xb0C'
raise UnknownUnitError(unit=measurement_unit, locale=locale)
unit_patterns = locale._data["unit_patterns"][q_unit].get(length, {})
- if isinstance(value, string_types): # Assume the value is a preformatted singular.
+ if isinstance(value, str): # Assume the value is a preformatted singular.
formatted_value = value
plural_form = "one"
else:
# ... failing that, construct one "by hand".
- if isinstance(numerator_value, string_types): # Numerator is preformatted
+ if isinstance(numerator_value, str): # Numerator is preformatted
formatted_numerator = numerator_value
elif numerator_unit: # Numerator has unit
formatted_numerator = format_unit(
else: # Unitless numerator
formatted_numerator = format_decimal(numerator_value, format=format, locale=locale)
- if isinstance(denominator_value, string_types): # Denominator is preformatted
+ if isinstance(denominator_value, str): # Denominator is preformatted
formatted_denominator = denominator_value
elif denominator_unit: # Denominator has unit
if denominator_value == 1: # support perUnitPatterns when the denominator is 1
import os
import re
import textwrap
-from babel._compat import izip, imap
import pytz as _pytz
from babel import localtime
DSTOFFSET = localtime.DSTOFFSET
DSTDIFF = localtime.DSTDIFF
ZERO = localtime.ZERO
+
+
+def _cmp(a, b):
+ return (a > b) - (a < b)
import collections
from optparse import OptionParser
import os
+import pickle
import re
import sys
import logging
sys.path.insert(0, CHECKOUT_ROOT)
from babel import dates, numbers
-from babel._compat import pickle, text_type
from babel.dates import split_interval_pattern
from babel.localedata import Alias
from babel.plural import PluralRule
for elem in prsup.findall('.//plurals/pluralRules'):
rules = []
for rule in elem.findall('pluralRule'):
- rules.append((rule.attrib['count'], text_type(rule.text)))
+ rules.append((rule.attrib['count'], str(rule.text)))
pr = PluralRule(rules)
for locale in elem.attrib['locales'].split():
rule_dict[locale] = pr
for map_zone in sup_windows_zones.findall('.//windowsZones/mapTimezones/mapZone'):
if map_zone.attrib.get('territory') == '001':
win_mapping[map_zone.attrib['other']] = map_zone.attrib['type'].split()[0]
- for tzid in text_type(map_zone.attrib['type']).split():
- _zone_territory_map[tzid] = text_type(map_zone.attrib['territory'])
+ for tzid in str(map_zone.attrib['type']).split():
+ _zone_territory_map[tzid] = str(map_zone.attrib['territory'])
for key_elem in bcp47_timezone.findall('.//keyword/key'):
if key_elem.attrib['name'] == 'tz':
for elem in key_elem.findall('type'):
if 'deprecated' not in elem.attrib:
- aliases = text_type(elem.attrib['alias']).split()
+ aliases = str(elem.attrib['alias']).split()
tzid = aliases.pop(0)
territory = _zone_territory_map.get(tzid, '001')
territory_zones.setdefault(territory, []).append(tzid)
zone_formats = data.setdefault('zone_formats', {})
for elem in tree.findall('.//timeZoneNames/gmtFormat'):
if not _should_skip_elem(elem):
- zone_formats['gmt'] = text_type(elem.text).replace('{0}', '%s')
+ zone_formats['gmt'] = str(elem.text).replace('{0}', '%s')
break
for elem in tree.findall('.//timeZoneNames/regionFormat'):
if not _should_skip_elem(elem):
- zone_formats['region'] = text_type(elem.text).replace('{0}', '%s')
+ zone_formats['region'] = str(elem.text).replace('{0}', '%s')
break
for elem in tree.findall('.//timeZoneNames/fallbackFormat'):
if not _should_skip_elem(elem):
zone_formats['fallback'] = (
- text_type(elem.text).replace('{0}', '%(0)s').replace('{1}', '%(1)s')
+ str(elem.text).replace('{0}', '%(0)s').replace('{1}', '%(1)s')
)
break
for elem in tree.findall('.//timeZoneNames/fallbackRegionFormat'):
if not _should_skip_elem(elem):
zone_formats['fallback_region'] = (
- text_type(elem.text).replace('{0}', '%(0)s').replace('{1}', '%(1)s')
+ str(elem.text).replace('{0}', '%(0)s').replace('{1}', '%(1)s')
)
break
time_zones = data.setdefault('time_zones', {})
info = {}
city = elem.findtext('exemplarCity')
if city:
- info['city'] = text_type(city)
+ info['city'] = str(city)
for child in elem.findall('long/*'):
- info.setdefault('long', {})[child.tag] = text_type(child.text)
+ info.setdefault('long', {})[child.tag] = str(child.text)
for child in elem.findall('short/*'):
- info.setdefault('short', {})[child.tag] = text_type(child.text)
+ info.setdefault('short', {})[child.tag] = str(child.text)
time_zones[elem.attrib['type']] = info
meta_zones = data.setdefault('meta_zones', {})
for elem in tree.findall('.//timeZoneNames/metazone'):
info = {}
city = elem.findtext('exemplarCity')
if city:
- info['city'] = text_type(city)
+ info['city'] = str(city)
for child in elem.findall('long/*'):
- info.setdefault('long', {})[child.tag] = text_type(child.text)
+ info.setdefault('long', {})[child.tag] = str(child.text)
for child in elem.findall('short/*'):
- info.setdefault('short', {})[child.tag] = text_type(child.text)
+ info.setdefault('short', {})[child.tag] = str(child.text)
meta_zones[elem.attrib['type']] = info
for day_period in day_period_width.findall('dayPeriod'):
period_type = day_period.attrib['type']
if 'alt' not in day_period.attrib:
- dest_dict[period_type] = text_type(day_period.text)
+ dest_dict[period_type] = str(day_period.text)
def parse_calendar_date_formats(data, calendar):
continue
try:
date_formats[type] = dates.parse_pattern(
- text_type(elem.findtext('dateFormat/pattern'))
+ str(elem.findtext('dateFormat/pattern'))
)
except ValueError as e:
log.error(e)
continue
try:
time_formats[type] = dates.parse_pattern(
- text_type(elem.findtext('timeFormat/pattern'))
+ str(elem.findtext('timeFormat/pattern'))
)
except ValueError as e:
log.error(e)
if _should_skip_elem(elem, type, datetime_formats):
continue
try:
- datetime_formats[type] = text_type(elem.findtext('dateTimeFormat/pattern'))
+ datetime_formats[type] = str(elem.findtext('dateTimeFormat/pattern'))
except ValueError as e:
log.error(e)
elif elem.tag == 'alias':
elif elem.tag == 'availableFormats':
for datetime_skeleton in elem.findall('dateFormatItem'):
datetime_skeletons[datetime_skeleton.attrib['id']] = (
- dates.parse_pattern(text_type(datetime_skeleton.text))
+ dates.parse_pattern(str(datetime_skeleton.text))
)
for elem in symbol_elem.findall('./*'):
if _should_skip_elem(elem):
continue
- number_symbols[elem.tag] = text_type(elem.text)
+ number_symbols[elem.tag] = str(elem.text)
def parse_decimal_formats(data, tree):
continue
for pattern_el in elem.findall('./decimalFormat/pattern'):
pattern_type = pattern_el.attrib.get('type')
- pattern = numbers.parse_pattern(text_type(pattern_el.text))
+ pattern = numbers.parse_pattern(str(pattern_el.text))
if pattern_type:
# This is a compact decimal format, see:
# https://www.unicode.org/reports/tr35/tr35-45/tr35-numbers.html#Compact_Number_Formats
type = elem.attrib.get('type')
if _should_skip_elem(elem, type, scientific_formats):
continue
- pattern = text_type(elem.findtext('scientificFormat/pattern'))
+ pattern = str(elem.findtext('scientificFormat/pattern'))
scientific_formats[type] = numbers.parse_pattern(pattern)
type = elem.attrib.get('type')
if _should_skip_elem(elem, type, percent_formats):
continue
- pattern = text_type(elem.findtext('percentFormat/pattern'))
+ pattern = str(elem.findtext('percentFormat/pattern'))
percent_formats[type] = numbers.parse_pattern(pattern)
continue
if 'count' in name.attrib:
currency_names_plural.setdefault(code, {})[
- name.attrib['count']] = text_type(name.text)
+ name.attrib['count']] = str(name.text)
else:
- currency_names[code] = text_type(name.text)
+ currency_names[code] = str(name.text)
for symbol in elem.findall('symbol'):
if 'draft' in symbol.attrib or 'choice' in symbol.attrib: # Skip drafts and choice-patterns
continue
if symbol.attrib.get('alt'): # Skip alternate forms
continue
- currency_symbols[code] = text_type(symbol.text)
+ currency_symbols[code] = str(symbol.text)
def parse_unit_patterns(data, tree):
rel_time_type = rel_time.attrib['type']
for pattern in rel_time.findall('relativeTimePattern'):
type_dict = date_fields[field_type].setdefault(rel_time_type, {})
- type_dict[pattern.attrib['count']] = text_type(pattern.text)
+ type_dict[pattern.attrib['count']] = str(pattern.text)
def parse_interval_formats(data, tree):
child.attrib['path'])
)
elif child.tag == 'pattern':
- pattern = text_type(child.text)
+ pattern = str(child.text)
currency_formats[type] = numbers.parse_pattern(pattern)
continue
for unit_pattern_elem in currency_formats_elem.findall('./unitPattern'):
count = unit_pattern_elem.attrib['count']
- pattern = text_type(unit_pattern_elem.text)
+ pattern = str(unit_pattern_elem.text)
currency_unit_patterns[count] = pattern
import copy
import datetime
import unittest
+from io import StringIO
-from babel._compat import StringIO
from babel.dates import format_datetime, UTC
from babel.messages import catalog, pofile
from babel.util import FixedOffsetTimezone
from datetime import datetime
import time
import unittest
+from io import BytesIO
from babel import __version__ as VERSION
from babel.core import Locale, UnknownLocaleError
from babel.messages.plurals import PLURALS
from babel.messages.pofile import read_po
from babel.util import LOCALTZ
-from babel._compat import BytesIO
class CheckersTestCase(unittest.TestCase):
import codecs
import sys
import unittest
+from io import BytesIO, StringIO
from babel.messages import extract
-from babel._compat import BytesIO, StringIO
class ExtractPythonTestCase(unittest.TestCase):
from distutils.dist import Distribution
from distutils.errors import DistutilsOptionError
from distutils.log import _global_log
+from io import StringIO
import logging
import os
import shutil
from babel import __version__ as VERSION
from babel.dates import format_datetime
from babel.messages import frontend, Catalog
-from babel.messages.frontend import CommandLineInterface, extract_messages, update_catalog, po_file_read_mode
+from babel.messages.frontend import CommandLineInterface, extract_messages, update_catalog
from babel.util import LOCALTZ
from babel.messages.pofile import read_po, write_po
-from babel._compat import StringIO
this_dir = os.path.abspath(os.path.dirname(__file__))
data_dir = os.path.join(this_dir, 'data')
self.cmd.finalize_options()
self.cmd.run()
- with open(pot_file, po_file_read_mode) as f:
+ with open(pot_file) as f:
catalog = read_po(f)
msg = catalog.get('bar')
self.assertEqual(1, len(msg.locations))
'year': time.strftime('%Y'),
'date': format_datetime(datetime(1994, 11, 11, 00, 00), 'yyyy-MM-dd HH:mmZ',
tzinfo=LOCALTZ, locale='en')}
- with open(pot_file, po_file_read_mode) as f:
+ with open(pot_file) as f:
actual_content = f.read()
self.assertEqual(expected_content, actual_content)
'year': time.strftime('%Y'),
'date': format_datetime(datetime(1994, 11, 11, 00, 00), 'yyyy-MM-dd HH:mmZ',
tzinfo=LOCALTZ, locale='en')}
- with open(pot_file, po_file_read_mode) as f:
+ with open(pot_file) as f:
actual_content = f.read()
self.assertEqual(expected_content, actual_content)
'year': time.strftime('%Y'),
'date': format_datetime(datetime(1994, 11, 11, 00, 00), 'yyyy-MM-dd HH:mmZ',
tzinfo=LOCALTZ, locale='en')}
- with open(pot_file, po_file_read_mode) as f:
+ with open(pot_file) as f:
actual_content = f.read()
self.assertEqual(expected_content, actual_content)
msgstr[1] ""
"""
- with open(pot_file, po_file_read_mode) as f:
+ with open(pot_file) as f:
actual_content = f.read()
self.assertEqual(expected_content, actual_content)
""" % {'version': VERSION,
'date': format_datetime(datetime(1994, 11, 11, 00, 00), 'yyyy-MM-dd HH:mmZ',
tzinfo=LOCALTZ, locale='en')}
- with open(po_file, po_file_read_mode) as f:
+ with open(po_file) as f:
actual_content = f.read()
self.assertEqual(expected_content, actual_content)
""" % {'version': VERSION,
'date': format_datetime(datetime(1994, 11, 11, 00, 00), 'yyyy-MM-dd HH:mmZ',
tzinfo=LOCALTZ, locale='en')}
- with open(po_file, po_file_read_mode) as f:
+ with open(po_file) as f:
actual_content = f.read()
self.assertEqual(expected_content, actual_content)
""" % {'version': VERSION,
'date': format_datetime(datetime(1994, 11, 11, 00, 00), 'yyyy-MM-dd HH:mmZ',
tzinfo=LOCALTZ, locale='en')}
- with open(po_file, po_file_read_mode) as f:
+ with open(po_file) as f:
actual_content = f.read()
self.assertEqual(expected_content, actual_content)
""" % {'version': VERSION,
'date': format_datetime(datetime(1994, 11, 11, 00, 00), 'yyyy-MM-dd HH:mmZ',
tzinfo=LOCALTZ, locale='ja_JP')}
- with open(po_file, po_file_read_mode) as f:
+ with open(po_file) as f:
actual_content = f.read()
self.assertEqual(expected_content, actual_content)
'date': format_datetime(datetime(1994, 11, 11, 00, 00), 'yyyy-MM-dd HH:mmZ',
tzinfo=LOCALTZ, locale='en_US'),
'long_message': long_message}
- with open(po_file, po_file_read_mode) as f:
+ with open(po_file) as f:
actual_content = f.read()
self.assertEqual(expected_content, actual_content)
'date': format_datetime(datetime(1994, 11, 11, 00, 00), 'yyyy-MM-dd HH:mmZ',
tzinfo=LOCALTZ, locale='en_US'),
'long_message': long_message}
- with open(po_file, po_file_read_mode) as f:
+ with open(po_file) as f:
actual_content = f.read()
self.assertEqual(expected_content, actual_content)
'year': time.strftime('%Y'),
'date': format_datetime(datetime(1994, 11, 11, 00, 00), 'yyyy-MM-dd HH:mmZ',
tzinfo=LOCALTZ, locale='en')}
- with open(pot_file, po_file_read_mode) as f:
+ with open(pot_file) as f:
actual_content = f.read()
self.assertEqual(expected_content, actual_content)
'year': time.strftime('%Y'),
'date': format_datetime(datetime(1994, 11, 11, 00, 00), 'yyyy-MM-dd HH:mmZ',
tzinfo=LOCALTZ, locale='en')}
- with open(pot_file, po_file_read_mode) as f:
+ with open(pot_file) as f:
actual_content = f.read()
self.assertEqual(expected_content, actual_content)
'year': time.strftime('%Y'),
'date': format_datetime(datetime(1994, 11, 11, 00, 00), 'yyyy-MM-dd HH:mmZ',
tzinfo=LOCALTZ, locale='en')}
- with open(pot_file, po_file_read_mode) as f:
+ with open(pot_file) as f:
actual_content = f.read()
self.assertEqual(expected_content, actual_content)
""" % {'version': VERSION,
'date': format_datetime(datetime(1994, 11, 11, 00, 00), 'yyyy-MM-dd HH:mmZ',
tzinfo=LOCALTZ, locale='en')}
- with open(po_file, po_file_read_mode) as f:
+ with open(po_file) as f:
actual_content = f.read()
self.assertEqual(expected_content, actual_content)
""" % {'version': VERSION,
'date': format_datetime(datetime(1994, 11, 11, 00, 00), 'yyyy-MM-dd HH:mmZ',
tzinfo=LOCALTZ, locale='en')}
- with open(po_file, po_file_read_mode) as f:
+ with open(po_file) as f:
actual_content = f.read()
self.assertEqual(expected_content, actual_content)
""" % {'version': VERSION,
'date': format_datetime(datetime(1994, 11, 11, 00, 00), 'yyyy-MM-dd HH:mmZ',
tzinfo=LOCALTZ, locale='en')}
- with open(po_file, po_file_read_mode) as f:
+ with open(po_file) as f:
actual_content = f.read()
self.assertEqual(expected_content, actual_content)
# -- encoding: UTF-8 --
+from io import BytesIO
import pytest
-from babel._compat import BytesIO
from babel.messages import extract
import os
import unittest
+from io import BytesIO
from babel.messages import mofile, Catalog
-from babel._compat import BytesIO, text_type
from babel.support import Translations
buf.seek(0)
translations = Translations(fp=buf)
self.assertEqual(u'Voh', translations.ugettext('foo'))
- assert isinstance(translations.ugettext('foo'), text_type)
+ assert isinstance(translations.ugettext('foo'), str)
self.assertEqual(u'Es gibt', translations.ungettext('There is', 'There are', 1))
- assert isinstance(translations.ungettext('There is', 'There are', 1), text_type)
+ assert isinstance(translations.ungettext('There is', 'There are', 1), str)
self.assertEqual(u'Fizz', translations.ugettext('Fizz'))
- assert isinstance(translations.ugettext('Fizz'), text_type)
+ assert isinstance(translations.ugettext('Fizz'), str)
self.assertEqual(u'Fuzz', translations.ugettext('Fuzz'))
- assert isinstance(translations.ugettext('Fuzz'), text_type)
+ assert isinstance(translations.ugettext('Fuzz'), str)
self.assertEqual(u'Fuzzes', translations.ugettext('Fuzzes'))
- assert isinstance(translations.ugettext('Fuzzes'), text_type)
+ assert isinstance(translations.ugettext('Fuzzes'), str)
def test_more_plural_forms(self):
catalog2 = Catalog(locale='ru_RU')
from datetime import datetime
import unittest
+from io import BytesIO, StringIO
from babel.core import Locale
from babel.messages.catalog import Catalog, Message
from babel.messages import pofile
from babel.util import FixedOffsetTimezone
-from babel._compat import StringIO, BytesIO
class ReadPoTestCase(unittest.TestCase):
'babel/locale-data/es_419.dat',
])
def test_compatible_classes_in_global_and_localedata(filename):
- # Use pickle module rather than cPickle since cPickle.Unpickler is a method
- # on Python 2
import pickle
class Unpickler(pickle.Unpickler):
# individuals. For the exact contribution history, see the revision
# history and logs, available at http://babel.edgewall.org/log/.
+import decimal
import unittest
import pytest
from babel.numbers import (
list_currencies, validate_currency, UnknownCurrencyError, is_currency, normalize_currency,
get_currency_precision, get_decimal_precision, get_currency_unit_pattern)
-from babel._compat import decimal
class FormatDecimalTestCase(unittest.TestCase):
# This software consists of voluntary contributions made by many
# individuals. For the exact contribution history, see the revision
# history and logs, available at http://babel.edgewall.org/log/.
+import decimal
import unittest
import pytest
from babel import plural, localedata
-from babel._compat import decimal
EPSILON = decimal.Decimal("0.0001")
operations don't fail due to odd corner cases on any locale that
we ship.
"""
+import decimal
from datetime import datetime
import pytest
from babel import Locale
from babel import dates
from babel import numbers
-from babel._compat import decimal
@pytest.mark.all_locales
import pytest
import sys
from datetime import date, datetime, timedelta
+from io import BytesIO
from babel import support
from babel.messages import Catalog
from babel.messages.mofile import write_mo
-from babel._compat import BytesIO, PY2
-get_arg_spec = (inspect.getargspec if PY2 else inspect.getfullargspec)
SKIP_LGETTEXT = sys.version_info >= (3, 8)
translations_method = getattr(self.translations, name)
null_method = getattr(self.null_translations, name)
self.assertEqual(
- get_arg_spec(translations_method),
- get_arg_spec(null_method),
+ inspect.getfullargspec(translations_method),
+ inspect.getfullargspec(null_method),
)
def test_same_return_values(self):
for name in self.method_names():
method = getattr(self.translations, name)
null_method = getattr(self.null_translations, name)
- signature = get_arg_spec(method)
+ signature = inspect.getfullargspec(method)
parameter_names = [name for name in signature.args if name != 'self']
values = [data[name] for name in parameter_names]
self.assertEqual(method(*values), null_method(*values))
t1 = support.Translations()
assert t1.files == []
t1._catalog["foo"] = "bar"
- if PY2:
- # Explicitly use the pure-Python `StringIO` class, as we need to
- # augment it with the `name` attribute, which we can't do for
- # `babel._compat.BytesIO`, which is `cStringIO.StringIO` under
- # `PY2`...
- from StringIO import StringIO
- fp = StringIO()
- else:
- fp = BytesIO()
+ fp = BytesIO()
write_mo(fp, Catalog())
fp.seek(0)
fp.name = "pro.mo"
import __future__
import unittest
+from io import BytesIO
import pytest
from babel import util
-from babel._compat import BytesIO
from babel.util import parse_future_flags