Skip to content

Commit

Permalink
Modernize Python 2 code to get ready for Python 3
Browse files Browse the repository at this point in the history
  • Loading branch information
cclauss committed Sep 14, 2017
1 parent 7159f45 commit 4607d55
Show file tree
Hide file tree
Showing 12 changed files with 137 additions and 125 deletions.
25 changes: 13 additions & 12 deletions add_aliases.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from __future__ import print_function
import argparse
import glob
import os
Expand All @@ -31,7 +32,7 @@
def str_to_seq(seq_str):
res = [int(s, 16) for s in seq_str.split('_')]
if 0xfe0f in res:
print '0xfe0f in file name: %s' % seq_str
print('0xfe0f in file name: %s' % seq_str)
res = [x for x in res if x != 0xfe0f]
return tuple(res)

Expand Down Expand Up @@ -66,7 +67,7 @@ def read_emoji_aliases(filename):
als_seq = tuple([int(x, 16) for x in als.split('_')])
trg_seq = tuple([int(x, 16) for x in trg.split('_')])
except:
print 'cannot process alias %s -> %s' % (als, trg)
print('cannot process alias %s -> %s' % (als, trg))
continue
result[als_seq] = trg_seq
return result
Expand All @@ -82,7 +83,7 @@ def add_aliases(
be done. Dstdir will be created if necessary, even if dry_run is true."""

if not path.isdir(srcdir):
print >> sys.stderr, '%s is not a directory' % srcdir
print('%s is not a directory' % srcdir, file=sys.stderr)
return

if not dstdir:
Expand All @@ -104,16 +105,16 @@ def add_aliases(
alias_exists = False
for als, trg in sorted(aliases.items()):
if trg not in seq_to_file:
print >> sys.stderr, 'target %s for %s does not exist' % (
seq_to_str(trg), seq_to_str(als))
print('target %s for %s does not exist' % (
seq_to_str(trg), seq_to_str(als)), file=sys.stderr)
continue
alias_name = '%s%s.%s' % (prefix, seq_to_str(als), ext)
alias_path = path.join(dstdir, alias_name)
if path.exists(alias_path):
if replace:
aliases_to_replace.append(alias_name)
else:
print >> sys.stderr, 'alias %s exists' % seq_to_str(als)
print('alias %s exists' % seq_to_str(als), file=sys.stderr)
alias_exists = True
continue
target_file = seq_to_file[trg]
Expand All @@ -123,15 +124,15 @@ def add_aliases(
if not dry_run:
for k in sorted(aliases_to_replace):
os.remove(path.join(dstdir, k))
print 'replacing %d files' % len(aliases_to_replace)
print('replacing %d files' % len(aliases_to_replace))
elif alias_exists:
print >> sys.stderr, 'aborting, aliases exist.'
print('aborting, aliases exist.', file=sys.stderr)
return

for k, v in sorted(aliases_to_create.items()):
if dry_run:
msg = 'replace ' if k in aliases_to_replace else ''
print '%s%s -> %s' % (msg, k, v)
print('%s%s -> %s' % (msg, k, v))
else:
try:
if copy:
Expand All @@ -143,10 +144,10 @@ def add_aliases(
else:
raise Exception('can\'t create cross-directory symlinks yet')
except Exception as e:
print >> sys.stderr, 'failed to create %s -> %s' % (k, v)
print('failed to create %s -> %s' % (k, v), file=sys.stderr)
raise Exception('oops, ' + str(e))
print 'created %d %s' % (
len(aliases_to_create), 'copies' if copy else 'symlinks')
print('created %d %s' % (
len(aliases_to_create), 'copies' if copy else 'symlinks'))


def main():
Expand Down
1 change: 1 addition & 0 deletions add_svg_glyphs.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
# Google Author(s): Doug Felt

"""Tool to update GSUB, hmtx, cmap, glyf tables with svg image glyphs."""
from __future__ import print_function

import argparse
import glob
Expand Down
75 changes: 38 additions & 37 deletions check_emoji_sequences.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
# limitations under the License.

"""Compare emoji image file namings against unicode property data."""
from __future__ import print_function

import argparse
import collections
Expand Down Expand Up @@ -95,9 +96,9 @@ def _check_valid_emoji(sorted_seq_to_filepath):
not_emoji[cp].append(fp)

if len(not_emoji):
print >> sys.stderr, '%d non-emoji found:' % len(not_emoji)
print('%d non-emoji found:' % len(not_emoji), file=sys.stderr)
for cp in sorted(not_emoji):
print >> sys.stderr, '%04x (in %s)' % (cp, ', '.join(not_emoji[cp]))
print('%04x (in %s)' % (cp, ', '.join(not_emoji[cp])), file=sys.stderr)


def _check_zwj(sorted_seq_to_filepath):
Expand All @@ -109,21 +110,21 @@ def _check_zwj(sorted_seq_to_filepath):
if ZWJ not in seq:
continue
if seq[0] == 0x200d:
print >> sys.stderr, 'zwj at head of sequence in %s' % fp
print('zwj at head of sequence in %s' % fp, file=sys.stderr)
if len(seq) == 1:
continue
if seq[-1] == 0x200d:
print >> sys.stderr, 'zwj at end of sequence in %s' % fp
print('zwj at end of sequence in %s' % fp, file=sys.stderr)
for i, cp in enumerate(seq):
if cp == ZWJ:
if i > 0:
pcp = seq[i-1]
if pcp != EMOJI_PRESENTATION_VS and not unicode_data.is_emoji(pcp):
print >> sys.stderr, 'non-emoji %04x preceeds ZWJ in %s' % (pcp, fp)
print('non-emoji %04x preceeds ZWJ in %s' % (pcp, fp), file=sys.stderr)
if i < len(seq) - 1:
fcp = seq[i+1]
if not unicode_data.is_emoji(fcp):
print >> sys.stderr, 'non-emoji %04x follows ZWJ in %s' % (fcp, fp)
print('non-emoji %04x follows ZWJ in %s' % (fcp, fp), file=sys.stderr)


def _check_flags(sorted_seq_to_filepath):
Expand All @@ -136,11 +137,11 @@ def _check_flags(sorted_seq_to_filepath):
if have_reg == None:
have_reg = is_reg
elif have_reg != is_reg:
print >> sys.stderr, 'mix of regional and non-regional in %s' % fp
print('mix of regional and non-regional in %s' % fp, file=sys.stderr)
if have_reg and len(seq) > 2:
# We provide dummy glyphs for regional indicators, so there are sequences
# with single regional indicator symbols.
print >> sys.stderr, 'regional indicator sequence length != 2 in %s' % fp
print('regional indicator sequence length != 2 in %s' % fp, file=sys.stderr)


def _check_skintone(sorted_seq_to_filepath):
Expand All @@ -153,23 +154,23 @@ def _check_skintone(sorted_seq_to_filepath):
if _is_skintone_modifier(cp):
if i == 0:
if len(seq) > 1:
print >> sys.stderr, 'skin color selector first in sequence %s' % fp
print('skin color selector first in sequence %s' % fp, file=sys.stderr)
# standalone are ok
continue
pcp = seq[i-1]
if not unicode_data.is_emoji_modifier_base(pcp):
print >> sys.stderr, (
'emoji skintone modifier applied to non-base at %d: %s' % (i, fp))
print((
'emoji skintone modifier applied to non-base at %d: %s' % (i, fp)), file=sys.stderr)
elif unicode_data.is_emoji_modifier_base(cp):
if i < len(seq) - 1 and _is_skintone_modifier(seq[i+1]):
base_to_modifiers[cp].add(seq[i+1])
elif cp not in base_to_modifiers:
base_to_modifiers[cp] = set()
for cp, modifiers in sorted(base_to_modifiers.iteritems()):
if len(modifiers) != 5:
print >> sys.stderr, 'emoji base %04x has %d modifiers defined (%s) in %s' % (
print('emoji base %04x has %d modifiers defined (%s) in %s' % (
cp, len(modifiers),
', '.join('%04x' % cp for cp in sorted(modifiers)), fp)
', '.join('%04x' % cp for cp in sorted(modifiers)), fp), file=sys.stderr)


def _check_zwj_sequences(seq_to_filepath):
Expand All @@ -189,7 +190,7 @@ def _check_zwj_sequences(seq_to_filepath):
for seq, fp in zwj_seq_to_filepath.iteritems():
if seq not in zwj_sequence_to_name:
if seq not in zwj_sequence_without_vs_to_name_canonical:
print >> sys.stderr, 'zwj sequence not defined: %s' % fp
print('zwj sequence not defined: %s' % fp, file=sys.stderr)
else:
_, can = zwj_sequence_without_vs_to_name_canonical[seq]
# print >> sys.stderr, 'canonical sequence %s contains vs: %s' % (
Expand All @@ -211,7 +212,7 @@ def read_emoji_aliases():
try:
trg_seq = tuple([int(x, 16) for x in trg.split('_')])
except:
print 'cannot process alias %s -> %s' % (als, trg)
print('cannot process alias %s -> %s' % (als, trg))
continue
result[als_seq] = trg_seq
return result
Expand All @@ -229,11 +230,11 @@ def _check_coverage(seq_to_filepath):
aliases = read_emoji_aliases()
for k, v in sorted(aliases.items()):
if v not in seq_to_filepath and v not in non_vs_to_canonical:
print 'alias %s missing target %s' % (_seq_string(k), _seq_string(v))
print('alias %s missing target %s' % (_seq_string(k), _seq_string(v)))
continue
if k in seq_to_filepath or k in non_vs_to_canonical:
print 'alias %s already exists as %s (%s)' % (
_seq_string(k), _seq_string(v), seq_name(v))
print('alias %s already exists as %s (%s)' % (
_seq_string(k), _seq_string(v), seq_name(v)))
continue
filename = seq_to_filepath.get(v) or seq_to_filepath[non_vs_to_canonical[v]]
seq_to_filepath[k] = 'alias:' + filename
Expand All @@ -242,13 +243,13 @@ def _check_coverage(seq_to_filepath):
emoji = sorted(unicode_data.get_emoji(age=age))
for cp in emoji:
if tuple([cp]) not in seq_to_filepath:
print 'missing single %04x (%s)' % (cp, unicode_data.name(cp, '<no name>'))
print('missing single %04x (%s)' % (cp, unicode_data.name(cp, '<no name>')))

# special characters
# all but combining enclosing keycap are currently marked as emoji
for cp in [ord('*'), ord('#'), ord(u'\u20e3')] + range(0x30, 0x3a):
if cp not in emoji and tuple([cp]) not in seq_to_filepath:
print 'missing special %04x (%s)' % (cp, unicode_data.name(cp))
print('missing special %04x (%s)' % (cp, unicode_data.name(cp)))

# combining sequences
comb_seq_to_name = sorted(
Expand All @@ -258,22 +259,22 @@ def _check_coverage(seq_to_filepath):
# strip vs and try again
non_vs_seq = strip_vs(seq)
if non_vs_seq not in seq_to_filepath:
print 'missing combining sequence %s (%s)' % (_seq_string(seq), name)
print('missing combining sequence %s (%s)' % (_seq_string(seq), name))

# flag sequences
flag_seq_to_name = sorted(
unicode_data.get_emoji_flag_sequences(age=age).iteritems())
for seq, name in flag_seq_to_name:
if seq not in seq_to_filepath:
print 'missing flag sequence %s (%s)' % (_seq_string(seq), name)
print('missing flag sequence %s (%s)' % (_seq_string(seq), name))

# skin tone modifier sequences
mod_seq_to_name = sorted(
unicode_data.get_emoji_modifier_sequences(age=age).iteritems())
for seq, name in mod_seq_to_name:
if seq not in seq_to_filepath:
print 'missing modifier sequence %s (%s)' % (
_seq_string(seq), name)
print('missing modifier sequence %s (%s)' % (
_seq_string(seq), name))

# zwj sequences
# some of ours include the emoji presentation variation selector and some
Expand All @@ -294,14 +295,14 @@ def _check_coverage(seq_to_filepath):
else:
test_seq = seq
if test_seq not in zwj_seq_without_vs:
print 'missing (canonical) zwj sequence %s (%s)' % (
_seq_string(seq), name)
print('missing (canonical) zwj sequence %s (%s)' % (
_seq_string(seq), name))

# check for 'unknown flag'
# this is either emoji_ufe82b or 'unknown_flag', we filter out things that
# don't start with our prefix so 'unknown_flag' would be excluded by default.
if tuple([0xfe82b]) not in seq_to_filepath:
print 'missing unknown flag PUA fe82b'
print('missing unknown flag PUA fe82b')


def check_sequence_to_filepath(seq_to_filepath):
Expand All @@ -322,20 +323,20 @@ def create_sequence_to_filepath(name_to_dirpath, prefix, suffix):
result = {}
for name, dirname in name_to_dirpath.iteritems():
if not name.startswith(prefix):
print 'expected prefix "%s" for "%s"' % (prefix, name)
print('expected prefix "%s" for "%s"' % (prefix, name))
continue

segments = name[len(prefix): -len(suffix)].split('_')
segfail = False
seq = []
for s in segments:
if not segment_re.match(s):
print 'bad codepoint name "%s" in %s/%s' % (s, dirname, name)
print('bad codepoint name "%s" in %s/%s' % (s, dirname, name))
segfail = True
continue
n = int(s, 16)
if n > 0x10ffff:
print 'codepoint "%s" out of range in %s/%s' % (s, dirname, name)
print('codepoint "%s" out of range in %s/%s' % (s, dirname, name))
segfail = True
continue
seq.append(n)
Expand All @@ -356,8 +357,8 @@ def collect_name_to_dirpath(directory, prefix, suffix):
if not f.endswith(suffix):
continue
if f in result:
print >> sys.stderr, 'duplicate file "%s" in %s and %s ' % (
f, dirname, result[f])
print('duplicate file "%s" in %s and %s ' % (
f, dirname, result[f]), file=sys.stderr)
continue
result[f] = dirname
return result
Expand All @@ -375,15 +376,15 @@ def collect_name_to_dirpath_with_override(dirs, prefix, suffix):


def run_check(dirs, prefix, suffix):
print 'Checking files with prefix "%s" and suffix "%s" in:\n %s' % (
prefix, suffix, '\n '.join(dirs))
print('Checking files with prefix "%s" and suffix "%s" in:\n %s' % (
prefix, suffix, '\n '.join(dirs)))
name_to_dirpath = collect_name_to_dirpath_with_override(
dirs, prefix=prefix, suffix=suffix)
print 'checking %d names' % len(name_to_dirpath)
print('checking %d names' % len(name_to_dirpath))
seq_to_filepath = create_sequence_to_filepath(name_to_dirpath, prefix, suffix)
print 'checking %d sequences' % len(seq_to_filepath)
print('checking %d sequences' % len(seq_to_filepath))
check_sequence_to_filepath(seq_to_filepath)
print 'done.'
print('done.')


def main():
Expand Down
5 changes: 3 additions & 2 deletions flag_glyph_name.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
# limitations under the License.

"""Generate a glyph name for flag emojis."""
from __future__ import print_function

__author__ = '[email protected] (Roozbeh Pournader)'

Expand Down Expand Up @@ -48,8 +49,8 @@ def flag_code_to_glyph_name(flag_code):


def main():
print ' '.join([
flag_code_to_glyph_name(flag_code) for flag_code in sys.argv[1:]])
print(' '.join([
flag_code_to_glyph_name(flag_code) for flag_code in sys.argv[1:]]))

if __name__ == '__main__':
main()
13 changes: 7 additions & 6 deletions flag_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"""Quick tool to display count/ids of flag images in a directory named
either using ASCII upper case pairs or the emoji_u+codepoint_sequence
names."""
from __future__ import print_function

import argparse
import re
Expand Down Expand Up @@ -44,22 +45,22 @@ def _flag_names_from_file_names(src):
for f in glob.glob(path.join(src, '*.png')):
m = flag_re.match(path.basename(f))
if not m:
print 'no match'
print('no match')
continue
flags.add(m.group(1))
return flags


def _dump_flag_info(names):
prev = None
print '%d flags' % len(names)
print('%d flags' % len(names))
for n in sorted(names):
if n[0] != prev:
if prev:
print
print()
prev = n[0]
print n,
print
print(n, end=' ')
print()


def main():
Expand All @@ -76,7 +77,7 @@ def main():
names = _flag_names_from_file_names(args.srcdir)
else:
names = _flag_names_from_emoji_file_names(args.srcdir)
print args.srcdir
print(args.srcdir)
_dump_flag_info(names)


Expand Down
Loading

0 comments on commit 4607d55

Please sign in to comment.