Debian Codegraph @

tools/utility/hex_range.py

raw
1 #!/usr/bin/env python
2 from __future__ import print_function
3 __license__ = """
4 This file is part of GNU FreeFont.
5
6 GNU FreeFont is free software: you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free Software
8 Foundation, either version 3 of the License, or (at your option) any later
9 version.
10
11 GNU FreeFont is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License along with
16 GNU FreeFont. If not, see <http://www.gnu.org/licenses/>.
17 """
18 __author__ = "Stevan White"
19 __email__ = "[email protected]"
20 __copyright__ = "Copyright 2009, 2010, 2011 Stevan White"
21 __date__ = "$Date: 2018-06-03 22:17:08 +0200 (So, 03. Jun 2018) $"
22 __version__ = "$Revision: 3918 $"
23
24 __doc__ = """Sends to standard output a range of hex values formatted
25 for e.g. HTML.
26 Takes one or two numerical arguments. With one argument the output
27 is just the formatted value of the argument.
28 """
29
30 import sys
31
32 # Python Unicode prefix
33 prefix = '\\u'
34 postfix = ''
35 # General Unicode prefix
36 prefix = 'U+'
37 postfix = ''
38 # HTML Entity
39 prefix = '&#x'
40 postfix = ';'
41
42 def explain_error_and_quit( e=None ):
43 if e:
44 print( 'Error:', e )
45 print( "Usage:" )
46 print( " hex_range num1 [num2]" )
47 sys.exit( 1 )
48
49 def print_formatted_hex_value( n ):
50 hexstr = '{0}{1:04x}{2}'.format( prefix, n, postfix )
51 print( hexstr )
52
53 if len( sys.argv ) == 3:
54 try:
55 a = int( sys.argv[1], 0 )
56 b = int( sys.argv[2], 0 )
57 for i in range( a, b + 1 ):
58 print_formatted_hex_value( i )
59 except ValueError as e:
60 explain_error_and_quit( e )
61 elif len( sys.argv ) == 2:
62 try:
63 a = int( sys.argv[1], 0 )
64 print_formatted_hex_value( a )
65 except ValueError as e:
66 explain_error_and_quit( e )
67 else:
68 explain_error_and_quit()
69