|
32 | 32 | import sys |
33 | 33 | import os |
34 | 34 |
|
| 35 | +# Python 2/3 compatibility. |
| 36 | +from six import PY2, MAXSIZE |
| 37 | + |
35 | 38 | # Change path so we find Xlib |
36 | 39 | sys.path.append(os.path.join(os.path.dirname(__file__), '..')) |
37 | 40 |
|
38 | 41 | import re |
39 | 42 | from Xlib import display, error |
40 | 43 | from optparse import OptionParser |
41 | 44 |
|
| 45 | + |
| 46 | +if PY2: |
| 47 | + integer_type = long |
| 48 | +else: |
| 49 | + integer_type = int |
| 50 | + |
42 | 51 | parser = OptionParser() |
43 | | -parser.add_option("-d","--display",dest="display",help="This option specifies the X server to which to connect",metavar="dpy",default=":0.0") |
| 52 | +parser.add_option("-d","--display",dest="display",help="This option specifies the X server to which to connect",metavar="dpy",default=None) |
44 | 53 | parser.add_option("-n","--name",dest="name",help="This option specifies the name of an atom to list. If the atom does not exist, a message will be printed on the standard error.",metavar="string",default=None) |
45 | 54 | parser.add_option("-m","--match",dest="match_re",help="This option specifies a regular expression to match against name of an atom to list. If the atom does not exist, a message will be printed on the standard error.",metavar="reg-exp",default=None) |
46 | 55 | parser.add_option("-f","--format",dest="format",help="This option specifies a printf-style string used to list each atom <value,name> pair, printed in that order (value is an unsigned long and name is a char *). Xlsatoms will supply a newline at the end of each line. The default is %ld\\t%s.",metavar="string",default="%ld\t%s") |
|
54 | 63 | d = display.Display(options.display) |
55 | 64 |
|
56 | 65 | def print_atom(print_format,atom,value): |
57 | | - print(print_format%(atom,value)) |
| 66 | + print(print_format%(atom,value)) |
58 | 67 |
|
59 | 68 | def list_atoms(d,re_obj,low,high): |
60 | | - while(low <= high): |
61 | | - try: |
62 | | - val = d.get_atom_name(low) |
63 | | - if (re_obj == None) : |
64 | | - print_atom(options.format,low,val) |
65 | | - elif re_obj.match(val) != None: |
66 | | - print_atom(options.format,low,val) |
67 | | - low += 1 |
68 | | - except: |
69 | | - sys.exit(0) |
| 69 | + while(low <= high): |
| 70 | + try: |
| 71 | + val = d.get_atom_name(low) |
| 72 | + if (re_obj == None) : |
| 73 | + print_atom(options.format,low,val) |
| 74 | + elif re_obj.match(val) != None: |
| 75 | + print_atom(options.format,low,val) |
| 76 | + low += 1 |
| 77 | + except: |
| 78 | + sys.exit(0) |
70 | 79 |
|
71 | 80 | if options.name != None: |
72 | | - try: |
73 | | - atom = d.intern_atom(options.name) |
74 | | - val = d.get_atom_name(atom) |
75 | | - print_atom(options.format,atom,val) |
76 | | - except: |
77 | | - sys.stderr.write('xlsatoms: no atom named "%s" on server "%s"'%(options.name,options.display)) |
78 | | - sys.stderr.write("\n") |
79 | | - sys.exit(1) |
80 | | - sys.exit(0) |
| 81 | + try: |
| 82 | + atom = d.intern_atom(options.name) |
| 83 | + val = d.get_atom_name(atom) |
| 84 | + print_atom(options.format,atom,val) |
| 85 | + except: |
| 86 | + sys.stderr.write('xlsatoms: no atom named "%s" on server "%s"'%(options.name,options.display)) |
| 87 | + sys.stderr.write("\n") |
| 88 | + sys.exit(1) |
| 89 | + sys.exit(0) |
81 | 90 |
|
82 | 91 | rangeVals = options.range.split("-") |
83 | 92 | if rangeVals[0] != "": |
84 | | - low = long(rangeVals[0]) |
| 93 | + low = integer_type(rangeVals[0]) |
85 | 94 |
|
86 | 95 | if rangeVals[1] != "": |
87 | | - high = long(rangeVals[1]) |
| 96 | + high = integer_type(rangeVals[1]) |
88 | 97 | else: |
89 | | - high = sys.maxint |
| 98 | + high = MAXSIZE |
90 | 99 |
|
91 | 100 | if options.match_re != None: |
92 | | - re_obj = re.compile(options.match_re) |
| 101 | + re_obj = re.compile(options.match_re) |
93 | 102 | else: |
94 | | - re_obj = None |
| 103 | + re_obj = None |
95 | 104 |
|
96 | 105 | list_atoms(d,re_obj,low,high) |
0 commit comments