-#!/usr/bin/env python
+#!/usr/bin/env python3
#
# Utility to decode register values
# Copyright (c) 2006 Jeremy Kerr <jk@ozlabs.org>
import os
import sys
import pprint
+import math
from pyparsing import Literal, Word, ZeroOrMore, Group, Dict, Optional, \
printables, ParseException, restOfLine
from getopt import getopt, GetoptError
self.fields.append(field)
def decode(self, value, ignore_zero):
- field_width = (self.width + 3) / 4
+ field_width = math.floor((self.width + 3) / 4)
name_width = max(map(lambda f: len(f.name), self.fields))
str = "0x%0*lx [%d]\n" % (field_width, value, value)
return number
def list_regs(regs):
- for (id, r) in regs.iteritems():
- print "%18s : %s" % (id, r.name)
+ for (id, r) in regs.items():
+ print("%18s : %s" % (id, r.name))
def search_regs(regs, str):
return dict((k, regs[k]) for k in regs \
ts = tok.asList()
id = ts.pop(0)
- if regs.has_key(id):
+ if id in regs:
raise ConfigurationError(file,
"Register %s is already defined" % id)
if not os.path.exists(conf):
continue
if os.path.isdir(conf):
- os.path.walk(conf, parse_config_dir, conf_data)
+ for c in os.walk(conf):
+ parse_config_dir(conf_data, c[0], c[2])
else:
parse_config(bnf, regs, conf)
return regs
def usage(prog):
- print "Usage: %s <-l> | <-s pattern> | [-n] register [value...]" % prog
+ print("Usage: %s <-l> | <-s pattern> | [-n] register [value...]" % prog)
def decode_value(reg, value, options):
try:
- i = long(value, 0)
- except ValueError, e:
- print "error: invalid value '%s'" % value
+ i = int(value, 0)
+ except ValueError:
+ print("error: invalid value '%s'" % value)
return
if i > ((1 << reg.width) - 1):
- print ("error: value '%s' is too large " + \
+ print("error: value '%s' is too large " + \
"for %d-bit register '%s'") % (value, reg.width, reg.id)
return
- print reg.decode(i, options['non-zero'])
+ print(reg.decode(i, options['non-zero']))
def main():
try:
regs = parse_all_configs(configs)
- except ConfigurationError, e:
- print "Error parsing configuration file %s:\n\t%s" % \
- (e.file, e.message)
+ except ConfigurationError as e:
+ print("Error parsing configuration file %s:\n\t%s" % \
+ (e.file, e.message))
return 1
if regs == {}:
- print "No configuration available"
+ print("No configuration available")
return 1
options = {}
return 1
reg_id = args.pop(0)
- if not regs.has_key(reg_id):
- print "No such register '%s'" % reg_id
+ if reg_id not in regs:
+ print("No such register '%s'" % reg_id)
return 1
reg = regs[reg_id]
- print "decoding as %s" % reg.name
+ print("decoding as %s" % reg.name)
if args:
value_iter = args.__iter__()
try:
for value in value_iter:
decode_value(reg, value.strip(), options)
- except KeyboardInterrupt, e:
+ except KeyboardInterrupt:
pass
return 0