comparison excel.py @ 74:7827e686be75 default tip

refactoring again...
author Henry S. Thompson <ht@markup.co.uk>
date Wed, 05 Jul 2017 18:26:27 +0100
parents 4bd5de7ac247
children
comparison
equal deleted inserted replaced
73:4bd5de7ac247 74:7827e686be75
53 self.tables.append(table) 53 self.tables.append(table)
54 54
55 @from_object() 55 @from_object()
56 @to_object(exclude_nulls=True,suppress=["parent"]) 56 @to_object(exclude_nulls=True,suppress=["parent"])
57 class Region(object): 57 class Region(object):
58 def __init__(self,name,parent=None,ranges=[],content=[]): 58 def __init__(self,name,bbox,parent=None):
59 assert(isinstance(name,string_types))
60 self.name=name
61 assert(isinstance(bbox,string_types))
62 self.bbox=Range(bbox)
59 if parent is not None: 63 if parent is not None:
60 assert(isinstance(parent,(Region,Sheet))) 64 assert(isinstance(parent,(Region,Sheet)))
61 self.parent=parent 65 self.parent=parent
62 ranges=list(ranges)
63 assert(all(isinstance(s,string_types) for s in ranges))
64 self.ranges=[Range(s) for s in ranges]
65 content=list(content)
66 assert(all(isinstance(s,(Region,string_types)) for s in content))
67 self.content=content
68 assert(isinstance(name,string_types))
69 self.name=name
70 if parent is not None: 66 if parent is not None:
71 parent.addRegion(self) 67 parent.addRegion(self)
72 68
69 class CompoundRegion(Region):
70 def __init__(self,name,bbox,content,parent=None):
71 Region.__init__(self,name,bbox,parent)
72 content=list(content)
73 assert(all(isinstance(s,Region) for s in content))
74 self.content=content
75
73 def addRegion(self,content): 76 def addRegion(self,content):
74 assert(isinstance(content,(Region,string_types))) 77 assert(isinstance(content,Region))
75 # convert
76 self.content.append(content) 78 self.content.append(content)
79
80 class SimpleRegion(Region):
81 def __init__(self,name,bbox,hasNulls=False,parent=None):
82 Region.__init__(self,name,bbox,parent)
83 self.hasNulls=hasNulls
77 84
78 cell=r'(\$?)([A-Z]+)(\$?)([1-9][0-9]*)' 85 cell=r'(\$?)([A-Z]+)(\$?)([1-9][0-9]*)'
79 RANGE=re.compile(cell+(r'(:%s)?'%cell)) 86 RANGE=re.compile(cell+(r'(:%s)?'%cell))
80 C=0 # Column 87 C=0 # Column
81 R=1 # Row 88 R=1 # Row
115 def _cellStr(dvp): 122 def _cellStr(dvp):
116 (dollar,val)=dvp 123 (dollar,val)=dvp
117 return '%s%s'%('$' if dollar else '',str(val)) 124 return '%s%s'%('$' if dollar else '',str(val))
118 125
119 @from_object() 126 @from_object()
120 @to_object(exclude_nulls=True)
121 class M(Region):
122 def __init__(self,name,sheet=None,ranges=[],labels=[],data=[]):
123 super(M,self).__init__(name,sheet,ranges,labels,data)
124
125 @from_object()
126 @to_object(exclude_nulls=True)
127 class RC(Region):
128 def __init__(self,name,sheet=None,ranges=[],labels=[],data=[]):
129 super(RC,self).__init__(name,sheet,ranges,labels,data)
130
131 @from_object()
132 @to_object(exclude_nulls=True,suppress=["sheet"]) 127 @to_object(exclude_nulls=True,suppress=["sheet"])
133 class Table(M): 128 class Table(CompoundRegion):
134 def __init__(self,name,shape='mixed',sheet=None,ranges=[],labels=[],data=[]): 129 def __init__(self,name,bbox,data,shape='mixed',sheet=None,labels=[]):
135 super(Table,self).__init__(name,sheet,ranges) 130 data=list(data)
131 if sheet is not None:
132 assert(isinstance(sheet,Sheet))
133 CompoundRegion.__init__(self,name,bbox,data,sheet)
136 assert(shape in ('columns','rows','mixed')) 134 assert(shape in ('columns','rows','mixed'))
137 self.shape=shape # if columns or rows, that's what correspond to DB columns 135 self.shape=shape # if columns or rows, that's what correspond to DB columns
138 labels=list(labels) 136 if labels is not None:
139 assert(all(isinstance(s,Label) for s in labels)) 137 labels=list(labels)
138 assert(all(isinstance(s,Label) for s in labels))
139 for l in labels:
140 self.addRegion(l)
140 self.labels=labels 141 self.labels=labels
141 data=list(data)
142 assert(all(isinstance(s,Region) for s in data))
143 self.data=data 142 self.data=data
144 if sheet is not None:
145 assert(isinstance(sheet,Sheet))
146 sheet.addTable(self)
147 self.sheet=sheet
148 143
149 def addLabel(self,label): 144 def addLabel(self,label):
150 assert(isinstance(label,Label)) 145 assert(isinstance(label,Label))
151 self.labels.append(label) 146 self.labels.append(label)
147 Region.addRegion(self,label)
152 148
153 def addData(self,data): 149 def addData(self,data):
154 assert(isinstance(data,Data)) 150 assert(isinstance(data,Data))
155 self.data.append(data) 151 self.data.append(data)
152 Region.addRegion(self.data)
156 153
157 def addRegion(self,region): 154 def addRegion(self,region):
158 assert(isinstance(region,(Label,Data))) 155 assert(isinstance(region,(Label,Data)))
159 if isinstance(region,Label): 156 if isinstance(region,Label):
160 self.addLabel(region) 157 self.addLabel(region)
161 else: 158 else:
162 self.addData(region) 159 self.addData(region)
163 160
161 class Label(Region):
162 '''Abstract base class, see {Compound,Simple}Label'''
163 pass
164
165 @from_object()
166 @to_object(exclude_nulls=True,suppress=["parent"])
167 class CompoundLabel(Label,CompoundRegion):
168 def __init__(self,name,bbox,content,parent=None):
169 assert(all(isinstance(s,Label) for s in content))
170 CompoundRegion.__init__(self,name,bbox,content,parent)
171
172 @from_object()
173 @to_object(exclude_nulls=True,suppress=["parent"])
174 class SimpleLabel(Label,SimpleRegion):
175 def __init__(self,name,bbox,type='string',parent=None):
176 SimpleRegion.__init__(self,name,bbox,parent)
177 assert(isinstance(type,string_types))
178 self.type=type
179
164 def lt(filename): 180 def lt(filename):
165 with open(filename,'r') as js: 181 with open(filename,'r') as js:
166 return loader(js.read(),cls=eDecoder.eDecoder) 182 return loader(js.read(),cls=eDecoder.eDecoder)