From: Jeremy Kerr Date: Mon, 15 Mar 2021 09:19:29 +0000 (+0800) Subject: bitfield: python3 support X-Git-Url: https://git.ozlabs.org/?p=bitfield;a=commitdiff_plain;ds=sidebyside bitfield: python3 support print(), except and 'in' changes to work with python3. Signed-off-by: Jeremy Kerr --- diff --git a/bitfield b/bitfield old mode 100644 new mode 100755 index a35fd20..345ee9f --- a/bitfield +++ b/bitfield @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 # # Utility to decode register values # Copyright (c) 2006 Jeremy Kerr @@ -9,6 +9,7 @@ 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 @@ -119,7 +120,7 @@ class register: 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) @@ -143,8 +144,8 @@ class register: 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 \ @@ -169,7 +170,7 @@ def parse_config(bnf, regs, file): 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) @@ -283,27 +284,28 @@ def parse_all_configs(configs): 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(): @@ -316,13 +318,13 @@ 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 = {} @@ -349,12 +351,12 @@ def main(): 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__() @@ -364,7 +366,7 @@ def main(): try: for value in value_iter: decode_value(reg, value.strip(), options) - except KeyboardInterrupt, e: + except KeyboardInterrupt: pass return 0