1
0
Fork 0
mirror of https://github.com/shlomif/PySolFC.git synced 2025-04-05 00:02:29 -04:00

add the write tags methods.

This commit is contained in:
Shlomi Fish 2019-04-08 13:09:02 +03:00
parent 7a12c8b21f
commit aa1907a567
2 changed files with 32 additions and 1 deletions

View file

@ -13,6 +13,11 @@
class KpatEmitter:
"""docstring for KpatEmitter"""
def _ind_out(self, text):
"""docstring for _out"""
self._out("\t" * self._indent)
self._out(text)
def _out(self, text):
"""docstring for _out"""
self.f.write(text)
@ -20,8 +25,22 @@ class KpatEmitter:
def __init__(self, f):
self.f = f
self._out("""<?xml version="1.0" encoding="UTF-8"?>\n""")
self._tags = []
self._indent = 0
def writeEmptyTag(self, name, attrs):
self._out(
self._ind_out(
"<" + name + "".join([" "+x[0]+"=\""+x[1]+"\"" for x in attrs])
+ "/>\n")
def writeStartTag(self, name, attrs):
self._ind_out(
"<" + name + "".join([" "+x[0]+"=\""+x[1]+"\"" for x in attrs])
+ ">\n")
self._tags.append({'name': name})
self._indent += 1
def endTag(self):
"""docstring for endTag"""
self._indent -= 1
self._ind_out("</{}>\n".format(self._tags.pop()['name']))

View file

@ -16,3 +16,15 @@ class MyTests(unittest.TestCase):
f.getvalue(),
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
+ "<foo one=\"val1\" two=\"val2\"/>\n")
f = cStringIO()
e = KpatEmitter(f)
self.assertTrue(e)
e.writeStartTag("foo", [("one", "val1"), ("two", "val2")])
e.writeEmptyTag("flutter", [])
e.endTag()
self.assertEqual(
f.getvalue(),
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
+ "<foo one=\"val1\" two=\"val2\">\n"
+ "\t<flutter/>\n"
+ "</foo>\n")