#!/usr/bin/env python import cPickle as pickle import sys from pprint import pprint import json try: import yaml except ImportError: yaml = None from datasheet_tables import load_datasheet_pages, \ abs_tables_from_pages, \ profile_factory # register profiles: import smrbase import imch_conf import gbe def print_stuff(a_t, options): for t in a_t.tables: if options.warning_only and t.check_ranges(): continue print '============================================================' print '%xh' % t.offset, t.reg_name for h, fmt in t.HeaderAttr: print " %-15s %s" % (h, fmt(t, getattr(t, h))) print for l in t.lines: print " ----------------" l.simple_print() print print def make_t(t): d = { "offset": t.offset, "fields": [dict(c.simple_datastructure() for c in l.cells) for l in t.lines] } d.update((h, getattr(t, h)) for h, _ in t.HeaderAttr) return d def make_stuff(a_t): return dict((t.reg_name, make_t(t)) for t in a_t.tables) def main(): from optparse import OptionParser parser = OptionParser(usage="usage: %prog [options] datasheet.xml") parser.add_option("-H", "--human", action="store_true", dest="human", help="print a human-readable description") parser.add_option("-j", "--json", action="store_true", dest="json", help="save parsed data to JSON") parser.add_option("-P", "--python", action="store_true", dest="python", help="dump datastructure in Python") if yaml: parser.add_option("-y", "--yaml", action="store_true", dest="yaml", help="save oarsed data to YAML") parser.add_option("-p", "--pickle", dest="pickle_filename", help="save parsed data to PICKLE", metavar="PICKLE") parser.add_option("-f", "--filename", dest="filename", help="Write data to file instead of stdout") parser.add_option("-W", "--Warning-only", action="store_true", dest="warning_only", default=False, help="only display tables for which a problem " "has been detected") (options, args) = parser.parse_args() if len(args) < 1: parser.print_help() return 1 try: datasheet = args[1] except IndexError: datasheet = "datasheet_320066.xml" tables_group = args[0] profile = profile_factory(tables_group) pages = load_datasheet_pages(datasheet, profile.min_page, profile.max_page) a_t = abs_tables_from_pages(pages, profile) out = open(options.filename, 'w') if options.filename else sys.stdout if options.human: print_stuff(a_t, options) elif options.python: pprint(make_stuff(a_t), out) elif options.json: json.dump(make_stuff(a_t), out, indent=4) elif yaml and options.yaml: yaml.safe_dump(make_stuff(a_t), stream=out) if out is not sys.stdout: out.close() if options.pickle_filename: f = open(options.pickle_filename, "wb") pickle.dump(a_t, f, -1) f.close() sys.exit(main())