summaryrefslogtreecommitdiff
path: root/parse_datasheet/parse_320066.py
blob: 44fe164b00965a6556f9be11636eb926f21b768c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#!/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] <tables_group> 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())