changeset 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
files excel.py kdir.js
diffstat 2 files changed, 91 insertions(+), 34 deletions(-) [+]
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)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/kdir.js	Wed Jul 05 18:26:27 2017 +0100
@@ -0,0 +1,41 @@
+{__type__:Book,
+ source:"kdir.xlsx",
+ sheets:[
+   {__type__:Sheet,
+    name:"Sheet1",
+    tables:[
+      {__type__:Table,
+       name:"_T000",
+       bbox:A$3:$J$11,
+       shape:rows,
+       labels:[
+	 {__type__:CompoundLabel,
+	  name:"_L000", bbox:C$3:$J$5,
+	  content:[{__type__:SimpleLabel,
+		    name:"_L001", bbox:C$3:$E$3},
+		   {__type__:SimpleLabel,
+		    name:"_L002", bbox:I$3:$J$4},
+		   {__type__:SimpleLabel,
+		    name:"_L003", bbox:C$4:$H$4},
+		   {__type__:SimpleLabel,
+		    name:"_L004", bbox:C$5:$J$5}]}],
+       data:[
+	   {__type__:Key,
+	    name:"_K001", bbox:A$6:$A$11,
+	    content:[
+		{__type__:Key,
+		 name:"_K001a", bbox:A$6:$A$8},
+		{__type__:Key,
+		 name:"_K001b", bbox:A$9:$A$11}]},
+	   {__type__:Key,
+	    name:"_K002", bbox:B$6:$B$11},
+	   {__type__:Value,static:True,
+	    name:"_V000", bbox:C$6:$C$11},
+	   {__type__:Value,static:False,
+	    name:"_V010", bbox:I$6:$I$11}
+       ]}
+    ],
+    misc:[{__type__:Region,
+	   name:"_X000", bbox:A$1:$J$1},
+	  {__type__:Region,
+	   name:"_X001", bbox:A$13:$E$13}]}]}