-#!/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 <http://www.gnu.org/licenses/>.
+
+"""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()
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:
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
if __name__ == '__main__':
+ import io
import sys
import time
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')