annotate excel.py @ 71:54bb53434887

begin work on decoder that allows identifiers as keys and values
author Henry S. Thompson <ht@markup.co.uk>
date Wed, 28 Jun 2017 19:08:30 +0100
parents 0003fe7b6b67
children 4bd5de7ac247
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
70
0003fe7b6b67 beginning work on class structure for excel annotation
Henry S. Thompson <ht@markup.co.uk>
parents:
diff changeset
1 #!/usr/bin/python3
0003fe7b6b67 beginning work on class structure for excel annotation
Henry S. Thompson <ht@markup.co.uk>
parents:
diff changeset
2 '''Class model for analysis of Excel spreadsheets'''
0003fe7b6b67 beginning work on class structure for excel annotation
Henry S. Thompson <ht@markup.co.uk>
parents:
diff changeset
3 from jsonweb.encode import to_object, dumper
71
54bb53434887 begin work on decoder that allows identifiers as keys and values
Henry S. Thompson <ht@markup.co.uk>
parents: 70
diff changeset
4 from jsonweb.decode import from_object, loader
54bb53434887 begin work on decoder that allows identifiers as keys and values
Henry S. Thompson <ht@markup.co.uk>
parents: 70
diff changeset
5 import json
54bb53434887 begin work on decoder that allows identifiers as keys and values
Henry S. Thompson <ht@markup.co.uk>
parents: 70
diff changeset
6 import eDecoder
54bb53434887 begin work on decoder that allows identifiers as keys and values
Henry S. Thompson <ht@markup.co.uk>
parents: 70
diff changeset
7
70
0003fe7b6b67 beginning work on class structure for excel annotation
Henry S. Thompson <ht@markup.co.uk>
parents:
diff changeset
8 try:
0003fe7b6b67 beginning work on class structure for excel annotation
Henry S. Thompson <ht@markup.co.uk>
parents:
diff changeset
9 string_types=basestring
0003fe7b6b67 beginning work on class structure for excel annotation
Henry S. Thompson <ht@markup.co.uk>
parents:
diff changeset
10 except NameError:
0003fe7b6b67 beginning work on class structure for excel annotation
Henry S. Thompson <ht@markup.co.uk>
parents:
diff changeset
11 string_types=str
0003fe7b6b67 beginning work on class structure for excel annotation
Henry S. Thompson <ht@markup.co.uk>
parents:
diff changeset
12
71
54bb53434887 begin work on decoder that allows identifiers as keys and values
Henry S. Thompson <ht@markup.co.uk>
parents: 70
diff changeset
13 @from_object()
70
0003fe7b6b67 beginning work on class structure for excel annotation
Henry S. Thompson <ht@markup.co.uk>
parents:
diff changeset
14 @to_object(exclude_nulls=True)
0003fe7b6b67 beginning work on class structure for excel annotation
Henry S. Thompson <ht@markup.co.uk>
parents:
diff changeset
15 class Book(object):
0003fe7b6b67 beginning work on class structure for excel annotation
Henry S. Thompson <ht@markup.co.uk>
parents:
diff changeset
16 def __init__(self,source,sheets=[],formats=[]):
0003fe7b6b67 beginning work on class structure for excel annotation
Henry S. Thompson <ht@markup.co.uk>
parents:
diff changeset
17 assert(isinstance(source,string_types))
0003fe7b6b67 beginning work on class structure for excel annotation
Henry S. Thompson <ht@markup.co.uk>
parents:
diff changeset
18 self.source=source
0003fe7b6b67 beginning work on class structure for excel annotation
Henry S. Thompson <ht@markup.co.uk>
parents:
diff changeset
19 sheets=list(sheets)
0003fe7b6b67 beginning work on class structure for excel annotation
Henry S. Thompson <ht@markup.co.uk>
parents:
diff changeset
20 assert(all(isinstance(s,Sheet) for s in sheets))
0003fe7b6b67 beginning work on class structure for excel annotation
Henry S. Thompson <ht@markup.co.uk>
parents:
diff changeset
21 self.sheets=sheets
0003fe7b6b67 beginning work on class structure for excel annotation
Henry S. Thompson <ht@markup.co.uk>
parents:
diff changeset
22 formats=list(formats)
0003fe7b6b67 beginning work on class structure for excel annotation
Henry S. Thompson <ht@markup.co.uk>
parents:
diff changeset
23 self.formats=formats
0003fe7b6b67 beginning work on class structure for excel annotation
Henry S. Thompson <ht@markup.co.uk>
parents:
diff changeset
24
0003fe7b6b67 beginning work on class structure for excel annotation
Henry S. Thompson <ht@markup.co.uk>
parents:
diff changeset
25 def addSheet(self,sheet):
0003fe7b6b67 beginning work on class structure for excel annotation
Henry S. Thompson <ht@markup.co.uk>
parents:
diff changeset
26 assert(isinstance(sheet,Sheet))
0003fe7b6b67 beginning work on class structure for excel annotation
Henry S. Thompson <ht@markup.co.uk>
parents:
diff changeset
27 self.sheets.append(sheet)
0003fe7b6b67 beginning work on class structure for excel annotation
Henry S. Thompson <ht@markup.co.uk>
parents:
diff changeset
28
71
54bb53434887 begin work on decoder that allows identifiers as keys and values
Henry S. Thompson <ht@markup.co.uk>
parents: 70
diff changeset
29 @from_object()
70
0003fe7b6b67 beginning work on class structure for excel annotation
Henry S. Thompson <ht@markup.co.uk>
parents:
diff changeset
30 @to_object(exclude_nulls=True,suppress=["book"])
0003fe7b6b67 beginning work on class structure for excel annotation
Henry S. Thompson <ht@markup.co.uk>
parents:
diff changeset
31 class Sheet(object):
71
54bb53434887 begin work on decoder that allows identifiers as keys and values
Henry S. Thompson <ht@markup.co.uk>
parents: 70
diff changeset
32 def __init__(self,name,book=None,tables=[],docs=[],misc=[]):
70
0003fe7b6b67 beginning work on class structure for excel annotation
Henry S. Thompson <ht@markup.co.uk>
parents:
diff changeset
33 assert(isinstance(name,string_types))
0003fe7b6b67 beginning work on class structure for excel annotation
Henry S. Thompson <ht@markup.co.uk>
parents:
diff changeset
34 self.name=name
0003fe7b6b67 beginning work on class structure for excel annotation
Henry S. Thompson <ht@markup.co.uk>
parents:
diff changeset
35 tables=list(tables)
71
54bb53434887 begin work on decoder that allows identifiers as keys and values
Henry S. Thompson <ht@markup.co.uk>
parents: 70
diff changeset
36 assert(all(isinstance(s,Table) for s in tables))
70
0003fe7b6b67 beginning work on class structure for excel annotation
Henry S. Thompson <ht@markup.co.uk>
parents:
diff changeset
37 self.tables=tables
0003fe7b6b67 beginning work on class structure for excel annotation
Henry S. Thompson <ht@markup.co.uk>
parents:
diff changeset
38 docs=list(docs)
0003fe7b6b67 beginning work on class structure for excel annotation
Henry S. Thompson <ht@markup.co.uk>
parents:
diff changeset
39 assert(all(isinstance(s,Region) for s in docs))
0003fe7b6b67 beginning work on class structure for excel annotation
Henry S. Thompson <ht@markup.co.uk>
parents:
diff changeset
40 self.docs=docs
0003fe7b6b67 beginning work on class structure for excel annotation
Henry S. Thompson <ht@markup.co.uk>
parents:
diff changeset
41 misc=list(misc)
0003fe7b6b67 beginning work on class structure for excel annotation
Henry S. Thompson <ht@markup.co.uk>
parents:
diff changeset
42 assert(all(isinstance(s,Region) for s in misc))
0003fe7b6b67 beginning work on class structure for excel annotation
Henry S. Thompson <ht@markup.co.uk>
parents:
diff changeset
43 self.misc=misc
71
54bb53434887 begin work on decoder that allows identifiers as keys and values
Henry S. Thompson <ht@markup.co.uk>
parents: 70
diff changeset
44 if book is not None:
54bb53434887 begin work on decoder that allows identifiers as keys and values
Henry S. Thompson <ht@markup.co.uk>
parents: 70
diff changeset
45 assert(isinstance(book,Book))
54bb53434887 begin work on decoder that allows identifiers as keys and values
Henry S. Thompson <ht@markup.co.uk>
parents: 70
diff changeset
46 book.addSheet(self)
54bb53434887 begin work on decoder that allows identifiers as keys and values
Henry S. Thompson <ht@markup.co.uk>
parents: 70
diff changeset
47 self.book=book
70
0003fe7b6b67 beginning work on class structure for excel annotation
Henry S. Thompson <ht@markup.co.uk>
parents:
diff changeset
48
0003fe7b6b67 beginning work on class structure for excel annotation
Henry S. Thompson <ht@markup.co.uk>
parents:
diff changeset
49 def addTable(self,table):
0003fe7b6b67 beginning work on class structure for excel annotation
Henry S. Thompson <ht@markup.co.uk>
parents:
diff changeset
50 assert(isinstance(table,Region))
0003fe7b6b67 beginning work on class structure for excel annotation
Henry S. Thompson <ht@markup.co.uk>
parents:
diff changeset
51 self.tables.append(table)
0003fe7b6b67 beginning work on class structure for excel annotation
Henry S. Thompson <ht@markup.co.uk>
parents:
diff changeset
52
71
54bb53434887 begin work on decoder that allows identifiers as keys and values
Henry S. Thompson <ht@markup.co.uk>
parents: 70
diff changeset
53 @from_object()
54bb53434887 begin work on decoder that allows identifiers as keys and values
Henry S. Thompson <ht@markup.co.uk>
parents: 70
diff changeset
54 @to_object(exclude_nulls=True,suppress=["parent"])
70
0003fe7b6b67 beginning work on class structure for excel annotation
Henry S. Thompson <ht@markup.co.uk>
parents:
diff changeset
55 class Region(object):
71
54bb53434887 begin work on decoder that allows identifiers as keys and values
Henry S. Thompson <ht@markup.co.uk>
parents: 70
diff changeset
56 def __init__(self,name,parent=None,ranges=[],content=[]):
54bb53434887 begin work on decoder that allows identifiers as keys and values
Henry S. Thompson <ht@markup.co.uk>
parents: 70
diff changeset
57 if parent is not None:
54bb53434887 begin work on decoder that allows identifiers as keys and values
Henry S. Thompson <ht@markup.co.uk>
parents: 70
diff changeset
58 assert(isinstance(parent,(Region,Sheet)))
54bb53434887 begin work on decoder that allows identifiers as keys and values
Henry S. Thompson <ht@markup.co.uk>
parents: 70
diff changeset
59 self.parent=parent
70
0003fe7b6b67 beginning work on class structure for excel annotation
Henry S. Thompson <ht@markup.co.uk>
parents:
diff changeset
60 ranges=list(ranges)
0003fe7b6b67 beginning work on class structure for excel annotation
Henry S. Thompson <ht@markup.co.uk>
parents:
diff changeset
61 assert(all(isinstance(s,Range) for s in ranges))
0003fe7b6b67 beginning work on class structure for excel annotation
Henry S. Thompson <ht@markup.co.uk>
parents:
diff changeset
62 self.ranges=ranges
71
54bb53434887 begin work on decoder that allows identifiers as keys and values
Henry S. Thompson <ht@markup.co.uk>
parents: 70
diff changeset
63 content=list(content)
54bb53434887 begin work on decoder that allows identifiers as keys and values
Henry S. Thompson <ht@markup.co.uk>
parents: 70
diff changeset
64 assert(all(isinstance(s,(Region,string_types)) for s in content))
54bb53434887 begin work on decoder that allows identifiers as keys and values
Henry S. Thompson <ht@markup.co.uk>
parents: 70
diff changeset
65 self.content=content
70
0003fe7b6b67 beginning work on class structure for excel annotation
Henry S. Thompson <ht@markup.co.uk>
parents:
diff changeset
66 assert(isinstance(name,string_types))
0003fe7b6b67 beginning work on class structure for excel annotation
Henry S. Thompson <ht@markup.co.uk>
parents:
diff changeset
67 self.name=name
71
54bb53434887 begin work on decoder that allows identifiers as keys and values
Henry S. Thompson <ht@markup.co.uk>
parents: 70
diff changeset
68 if parent is not None:
54bb53434887 begin work on decoder that allows identifiers as keys and values
Henry S. Thompson <ht@markup.co.uk>
parents: 70
diff changeset
69 parent.addRegion(self)
54bb53434887 begin work on decoder that allows identifiers as keys and values
Henry S. Thompson <ht@markup.co.uk>
parents: 70
diff changeset
70
54bb53434887 begin work on decoder that allows identifiers as keys and values
Henry S. Thompson <ht@markup.co.uk>
parents: 70
diff changeset
71 def addRegion(self,content):
54bb53434887 begin work on decoder that allows identifiers as keys and values
Henry S. Thompson <ht@markup.co.uk>
parents: 70
diff changeset
72 assert(isinstance(content,(Region,string_types)))
54bb53434887 begin work on decoder that allows identifiers as keys and values
Henry S. Thompson <ht@markup.co.uk>
parents: 70
diff changeset
73 self.content.append(content)
54bb53434887 begin work on decoder that allows identifiers as keys and values
Henry S. Thompson <ht@markup.co.uk>
parents: 70
diff changeset
74
54bb53434887 begin work on decoder that allows identifiers as keys and values
Henry S. Thompson <ht@markup.co.uk>
parents: 70
diff changeset
75 @from_object()
54bb53434887 begin work on decoder that allows identifiers as keys and values
Henry S. Thompson <ht@markup.co.uk>
parents: 70
diff changeset
76 @to_object(exclude_nulls=True,suppress=["sheet"])
54bb53434887 begin work on decoder that allows identifiers as keys and values
Henry S. Thompson <ht@markup.co.uk>
parents: 70
diff changeset
77 class Table(Region):
54bb53434887 begin work on decoder that allows identifiers as keys and values
Henry S. Thompson <ht@markup.co.uk>
parents: 70
diff changeset
78 def __init__(self,name,sheet=None,ranges=[],labels=[],data=[]):
54bb53434887 begin work on decoder that allows identifiers as keys and values
Henry S. Thompson <ht@markup.co.uk>
parents: 70
diff changeset
79 super(Table,self).__init__(name,sheet,ranges)
54bb53434887 begin work on decoder that allows identifiers as keys and values
Henry S. Thompson <ht@markup.co.uk>
parents: 70
diff changeset
80 labels=list(labels)
54bb53434887 begin work on decoder that allows identifiers as keys and values
Henry S. Thompson <ht@markup.co.uk>
parents: 70
diff changeset
81 assert(all(isinstance(s,Label) for s in labels))
54bb53434887 begin work on decoder that allows identifiers as keys and values
Henry S. Thompson <ht@markup.co.uk>
parents: 70
diff changeset
82 self.labels=labels
54bb53434887 begin work on decoder that allows identifiers as keys and values
Henry S. Thompson <ht@markup.co.uk>
parents: 70
diff changeset
83 data=list(data)
54bb53434887 begin work on decoder that allows identifiers as keys and values
Henry S. Thompson <ht@markup.co.uk>
parents: 70
diff changeset
84 assert(all(isinstance(s,Region) for s in data))
54bb53434887 begin work on decoder that allows identifiers as keys and values
Henry S. Thompson <ht@markup.co.uk>
parents: 70
diff changeset
85 self.data=data
70
0003fe7b6b67 beginning work on class structure for excel annotation
Henry S. Thompson <ht@markup.co.uk>
parents:
diff changeset
86 if sheet is not None:
71
54bb53434887 begin work on decoder that allows identifiers as keys and values
Henry S. Thompson <ht@markup.co.uk>
parents: 70
diff changeset
87 assert(isinstance(sheet,Sheet))
70
0003fe7b6b67 beginning work on class structure for excel annotation
Henry S. Thompson <ht@markup.co.uk>
parents:
diff changeset
88 sheet.addTable(self)
71
54bb53434887 begin work on decoder that allows identifiers as keys and values
Henry S. Thompson <ht@markup.co.uk>
parents: 70
diff changeset
89 self.sheet=sheet
54bb53434887 begin work on decoder that allows identifiers as keys and values
Henry S. Thompson <ht@markup.co.uk>
parents: 70
diff changeset
90
54bb53434887 begin work on decoder that allows identifiers as keys and values
Henry S. Thompson <ht@markup.co.uk>
parents: 70
diff changeset
91 def addLabel(self,label):
54bb53434887 begin work on decoder that allows identifiers as keys and values
Henry S. Thompson <ht@markup.co.uk>
parents: 70
diff changeset
92 assert(isinstance(label,Label))
54bb53434887 begin work on decoder that allows identifiers as keys and values
Henry S. Thompson <ht@markup.co.uk>
parents: 70
diff changeset
93 self.labels.append(label)
54bb53434887 begin work on decoder that allows identifiers as keys and values
Henry S. Thompson <ht@markup.co.uk>
parents: 70
diff changeset
94
54bb53434887 begin work on decoder that allows identifiers as keys and values
Henry S. Thompson <ht@markup.co.uk>
parents: 70
diff changeset
95 def addData(self,data):
54bb53434887 begin work on decoder that allows identifiers as keys and values
Henry S. Thompson <ht@markup.co.uk>
parents: 70
diff changeset
96 assert(isinstance(data,Data))
54bb53434887 begin work on decoder that allows identifiers as keys and values
Henry S. Thompson <ht@markup.co.uk>
parents: 70
diff changeset
97 self.data.append(data)
54bb53434887 begin work on decoder that allows identifiers as keys and values
Henry S. Thompson <ht@markup.co.uk>
parents: 70
diff changeset
98
54bb53434887 begin work on decoder that allows identifiers as keys and values
Henry S. Thompson <ht@markup.co.uk>
parents: 70
diff changeset
99 def addRegion(self,region):
54bb53434887 begin work on decoder that allows identifiers as keys and values
Henry S. Thompson <ht@markup.co.uk>
parents: 70
diff changeset
100 assert(isinstance(region,(Label,Data)))
54bb53434887 begin work on decoder that allows identifiers as keys and values
Henry S. Thompson <ht@markup.co.uk>
parents: 70
diff changeset
101 if isinstance(region,Label):
54bb53434887 begin work on decoder that allows identifiers as keys and values
Henry S. Thompson <ht@markup.co.uk>
parents: 70
diff changeset
102 self.addLabel(region)
54bb53434887 begin work on decoder that allows identifiers as keys and values
Henry S. Thompson <ht@markup.co.uk>
parents: 70
diff changeset
103 else:
54bb53434887 begin work on decoder that allows identifiers as keys and values
Henry S. Thompson <ht@markup.co.uk>
parents: 70
diff changeset
104 self.addData(region)
54bb53434887 begin work on decoder that allows identifiers as keys and values
Henry S. Thompson <ht@markup.co.uk>
parents: 70
diff changeset
105
54bb53434887 begin work on decoder that allows identifiers as keys and values
Henry S. Thompson <ht@markup.co.uk>
parents: 70
diff changeset
106 def lt(filename):
54bb53434887 begin work on decoder that allows identifiers as keys and values
Henry S. Thompson <ht@markup.co.uk>
parents: 70
diff changeset
107 with open(filename,'r') as js:
54bb53434887 begin work on decoder that allows identifiers as keys and values
Henry S. Thompson <ht@markup.co.uk>
parents: 70
diff changeset
108 return json.loads(js.read(),cls=eDecoder.eDecoder)