#!/usr/bin/env python # coding: utf-8 # Copyright © 2007 Forest Bond. # This program may be distributed according to the terms of the GNU General # Public License version 2. See http://www.gnu.org/licenses/gpl-2.0.html for # the full text of the license. '''\ ${name} [options] {codepoint|charname} ... Options: -v, --version Print version info to standard output. -h, --help Print this text to standard output. Examples: ${name} bullet • ${name} 'greek capital letter delta' 'greek capital letter phi' \\ 'greek capital letter sigma' ΔΦΣ ''' __self__ = 'unichar' __author__ = 'Forest Bond ' __version__ = '0.2' __copyright__ = '© 2007' __license__ = 'GPL version 2' import sys, locale, unicodedata import sclapp def guessTerminalEncoding(): return locale.getpreferredencoding() def handle_argv(argv): if len(argv) < 2: raise sclapp.UsageError, 'Too few arguments' return [ a.strip() for a in argv[1:] ] def parseIdentifier(identifier): try: return 'codepoint', int(identifier) except (ValueError, TypeError): pass try: return 'codepoint', int(identifier, 16) except (ValueError, TypeError): pass if (len(identifier) > 2) and (identifier[:2].lower() == 'u+'): try: val = int(identifier[2:], 16) except (ValueError, TypeError): pass else: return 'codepoint', val return 'name', identifier.upper() def getUnicodeCharacter(identifier): identifier_type, identifier = parseIdentifier(identifier) if identifier_type == 'codepoint': return getUnicodeCharacterByCodepoint(identifier) elif identifier_type == 'name': return getUnicodeCharacterByName(identifier) raise ValueError, 'Identifier could not be parsed' def getUnicodeCharacterByCodepoint(codepoint): try: return unichr(codepoint) except ValueError: raise ValueError, ( 'No Unicode character with code point U+%X' % codepoint) def getUnicodeCharacterByName(name): try: return unicodedata.lookup(name) except KeyError: raise ValueError, 'No Unicode character named "%s"' % name def printUnicodeCharacter(identifier): # This depends upon sclapp's handling of unicode characters written to # stdout, which should result in the unicode string being encoded properly: sys.stdout.write(getUnicodeCharacter(identifier)) @sclapp.main_function( name = __self__, version = __version__, doc = __doc__, error_output_level = sclapp.WARNING ) def main(argv): identifiers = handle_argv(argv) success = True for identifier in identifiers: try: printUnicodeCharacter(identifier) except ValueError, e: print >>sys.stderr, str(e) success = False return int(not success) if __name__ == '__main__': sys.exit(main())