diff 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
line wrap: on
line diff
--- a/excel.py	Mon Jul 03 16:25:49 2017 +0100
+++ b/excel.py	Wed Jul 05 18:26:27 2017 +0100
@@ -55,26 +55,33 @@
 @from_object()
 @to_object(exclude_nulls=True,suppress=["parent"])
 class Region(object):
-  def __init__(self,name,parent=None,ranges=[],content=[]):
+  def __init__(self,name,bbox,parent=None):
+    assert(isinstance(name,string_types))
+    self.name=name
+    assert(isinstance(bbox,string_types))
+    self.bbox=Range(bbox)
     if parent is not None:
       assert(isinstance(parent,(Region,Sheet)))
     self.parent=parent
-    ranges=list(ranges)
-    assert(all(isinstance(s,string_types) for s in ranges))
-    self.ranges=[Range(s) for s in ranges]
-    content=list(content)
-    assert(all(isinstance(s,(Region,string_types)) for s in content))
-    self.content=content
-    assert(isinstance(name,string_types))
-    self.name=name
     if parent is not None:
       parent.addRegion(self)
 
+class CompoundRegion(Region):
+  def __init__(self,name,bbox,content,parent=None):
+    Region.__init__(self,name,bbox,parent)
+    content=list(content)
+    assert(all(isinstance(s,Region) for s in content))
+    self.content=content
+
   def addRegion(self,content):
-    assert(isinstance(content,(Region,string_types)))
-    # convert
+    assert(isinstance(content,Region))
     self.content.append(content)
 
+class SimpleRegion(Region):
+  def __init__(self,name,bbox,hasNulls=False,parent=None):
+    Region.__init__(self,name,bbox,parent)
+    self.hasNulls=hasNulls
+
 cell=r'(\$?)([A-Z]+)(\$?)([1-9][0-9]*)'
 RANGE=re.compile(cell+(r'(:%s)?'%cell))
 C=0 # Column
@@ -117,42 +124,32 @@
   return '%s%s'%('$' if dollar else '',str(val))
 
 @from_object()
-@to_object(exclude_nulls=True)
-class M(Region):
-  def __init__(self,name,sheet=None,ranges=[],labels=[],data=[]):
-    super(M,self).__init__(name,sheet,ranges,labels,data)
-
-@from_object()
-@to_object(exclude_nulls=True)
-class RC(Region):
-  def __init__(self,name,sheet=None,ranges=[],labels=[],data=[]):
-    super(RC,self).__init__(name,sheet,ranges,labels,data)
-
-@from_object()
 @to_object(exclude_nulls=True,suppress=["sheet"])
-class Table(M):
-  def __init__(self,name,shape='mixed',sheet=None,ranges=[],labels=[],data=[]):
-    super(Table,self).__init__(name,sheet,ranges)
+class Table(CompoundRegion):
+  def __init__(self,name,bbox,data,shape='mixed',sheet=None,labels=[]):
+    data=list(data)
+    if sheet is not None:
+      assert(isinstance(sheet,Sheet))
+    CompoundRegion.__init__(self,name,bbox,data,sheet)
     assert(shape in ('columns','rows','mixed'))
     self.shape=shape # if columns or rows, that's what correspond to DB columns
-    labels=list(labels)
-    assert(all(isinstance(s,Label) for s in labels))
+    if labels is not None:
+      labels=list(labels)
+      assert(all(isinstance(s,Label) for s in labels))
+      for l in labels:
+        self.addRegion(l)
     self.labels=labels
-    data=list(data)
-    assert(all(isinstance(s,Region) for s in data))
     self.data=data
-    if sheet is not None:
-      assert(isinstance(sheet,Sheet))
-      sheet.addTable(self)
-    self.sheet=sheet
 
   def addLabel(self,label):
     assert(isinstance(label,Label))
     self.labels.append(label)
+    Region.addRegion(self,label)
 
   def addData(self,data):
     assert(isinstance(data,Data))
     self.data.append(data)
+    Region.addRegion(self.data)
 
   def addRegion(self,region):
     assert(isinstance(region,(Label,Data)))
@@ -161,6 +158,25 @@
     else:
       self.addData(region)
 
+class Label(Region):
+  '''Abstract base class, see {Compound,Simple}Label'''
+  pass
+
+@from_object()
+@to_object(exclude_nulls=True,suppress=["parent"])
+class CompoundLabel(Label,CompoundRegion):
+  def __init__(self,name,bbox,content,parent=None):
+    assert(all(isinstance(s,Label) for s in content))
+    CompoundRegion.__init__(self,name,bbox,content,parent)
+
+@from_object()
+@to_object(exclude_nulls=True,suppress=["parent"])
+class SimpleLabel(Label,SimpleRegion):
+  def __init__(self,name,bbox,type='string',parent=None):
+    SimpleRegion.__init__(self,name,bbox,parent)
+    assert(isinstance(type,string_types))
+    self.type=type
+
 def lt(filename):
   with open(filename,'r') as js:
     return loader(js.read(),cls=eDecoder.eDecoder)