Created
August 19, 2021 19:55
-
-
Save agumonkey/93fd94dcbcba7dcece8657ead8567691 to your computer and use it in GitHub Desktop.
oosax.py -- trying to play with sax again to make DOM tooling [WIP]
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import xml | |
from xml import sax | |
class Todo: | |
pass | |
class Node: | |
def __init__(self, name, attrs): | |
self.name = name | |
self.attrs = attrs | |
self.children = [] | |
def push(self, node): | |
self.children.append(node) | |
def __repr__(self): | |
return f'<{self.name} [{len(self.children)}]>' | |
class Tab(xml.sax.ContentHandler): | |
def __init__(self): | |
self.nodes = 0 | |
self.root = Node('root', {}) | |
self.stack = [self.root] | |
def __repr__(self): | |
return f'<{self.root} #{self.nodes}>' | |
def startDocument(self): | |
print('BEGIN') | |
def endDocument(self): | |
print(f'END[{self.nodes}]') | |
# def startElement(self, name, attrs): | |
# print('+', name) | |
# if name == 'table:table-row': | |
# self.rows.append([]) | |
# if name == 'table:table-cell': | |
# self.rows[-1].append() | |
# self.nodes += 1 | |
def startElement(self, name, attrs): | |
print('sh', name) | |
self.stack.append(Node(name,attrs)) | |
self.nodes += 1 | |
def endElement(self, name): | |
print('re', name) | |
last = self.stack.pop() | |
self.stack[-1].push(last) | |
class Sel: | |
def __init__(self, q): | |
self.q = q | |
def all(self, root): | |
''' | |
dfs++ over root with q | |
''' | |
return Todo | |
def test(fn='/home/self/Documents/ods.xml'): | |
t = Tab() | |
xml.sax.parse(fn, t) | |
return t | |
class Indenter: | |
def __init__(self, z=0, i=1, c=' '): | |
self.z = z | |
self.i = i | |
self.c = c | |
def ind(self): | |
return self.c * self.z | |
def down(self): | |
return Indenter(self.z+self.i, self.i, self.c) | |
def walk(t, c=lambda n:n.children, f=lambda s,v:print(s.ind(),v), s=Indenter()): | |
f(s,t) | |
for e in c(t): | |
walk(e, c, f, s.down()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment