From: W. Trevor King Date: Fri, 28 Sep 2012 04:33:36 +0000 (-0400) Subject: posts:xmodmap:gen_sampler.py: update to Python 3. X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=69d747850b744fdd147f484c20f4a55a6bfc9edd;p=blog.git posts:xmodmap:gen_sampler.py: update to Python 3. --- diff --git a/posts/Xmodmap/gen_sampler.py b/posts/Xmodmap/gen_sampler.py index f65b7d3..07341e3 100755 --- a/posts/Xmodmap/gen_sampler.py +++ b/posts/Xmodmap/gen_sampler.py @@ -1,12 +1,29 @@ -#!/usr/bin/env python -# Generate a unicode sampler from an Xmodmap file +#!/usr/bin/env python3 +# +# Copyright (C) 2010-2012 W. Trevor King +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +"""Generate a unicode sampler from an Xmodmap file +""" ENCODING = 'utf-8' XKEYSIMDEF_H = "/usr/include/X11/keysymdef.h" def sample(lines, keysyms): - """Conver lines from an Xmodmap file to sampler lines.""" + """Convert lines from an Xmodmap file to sampler lines.""" for line in lines: # parse line line = line.strip() @@ -31,8 +48,8 @@ def sample(lines, keysyms): keys[i] = keysyms[key] continue assert key.startswith('U'), key - key = r'\u'+key[len('U'):] - keys[i] = unicode(key, 'unicode escape') + codepoint = int(key[len('U'):], 16) + keys[i] = chr(codepoint) line = (' '.join([key or ' ' for key in keys])).rstrip() if len(line) > 0: @@ -59,8 +76,8 @@ def read_keysyms(file=None): if not codepoint.startswith('U+'): codepoint = None else: - codepoint = r'\u'+codepoint[len('U+'):] - codepoint = unicode(codepoint, 'unicode escape') + codepoint = int(codepoint[len('U+'):], 16) + codepoint = chr(codepoint) else: codepoint = None # keysym to unicode mapping not well defined keysyms[keysym] = codepoint @@ -68,6 +85,7 @@ def read_keysyms(file=None): if __name__ == '__main__': + import io import sys import time @@ -77,8 +95,10 @@ if __name__ == '__main__': lines = open(xmodmap_filename, 'r').readlines() #lines = sample(lines, keysyms) lines = sorted(sample(lines, keysyms)) - print 'These characters are bound in my current .Xmodmap' - print '(%s)' % time.asctime() - print '' + out = io.TextIOWrapper(sys.stdout.buffer, ENCODING) + out.write('These characters are bound in my current .Xmodmap\n') + out.write('({})\n'.format(time.asctime())) + out.write('\n') for line in lines: - print line.encode(ENCODING) + out.write(line) + out.write('\n')