From fdab63f0b6d686f0e0e1c888c0cb0899a614f58c Mon Sep 17 00:00:00 2001 From: Cosimo Lupo Date: Thu, 16 Nov 2017 15:40:52 +0000 Subject: [PATCH] [feaLib.ast] add __str__ to all AST elements which calls the asFea() method, so one can e.g. print(doc.statements[0]) --- Lib/fontTools/feaLib/ast.py | 30 ++++++++++++++---------------- Tests/feaLib/parser_test.py | 1 + 2 files changed, 15 insertions(+), 16 deletions(-) diff --git a/Lib/fontTools/feaLib/ast.py b/Lib/fontTools/feaLib/ast.py index 0241f94ba..5f7ad2931 100644 --- a/Lib/fontTools/feaLib/ast.py +++ b/Lib/fontTools/feaLib/ast.py @@ -47,7 +47,8 @@ def asFea(g): return g -class Statement(object): +class Element(object): + def __init__(self, location): self.location = location @@ -55,28 +56,25 @@ class Statement(object): pass def asFea(self, indent=""): - pass + raise NotImplementedError + + def __str__(self): + return self.asFea() -class Expression(object): - def __init__(self, location): - self.location = location - - def build(self, builder): - pass - - def asFea(self, indent=""): - pass +class Statement(Element): + pass -class Comment(object): +class Expression(Element): + pass + + +class Comment(Element): def __init__(self, location, text): - self.location = location + super(Comment, self).__init__(location) self.text = text - def build(self, builder): - pass - def asFea(self, indent=""): return self.text diff --git a/Tests/feaLib/parser_test.py b/Tests/feaLib/parser_test.py index b9606406c..54814dc4d 100644 --- a/Tests/feaLib/parser_test.py +++ b/Tests/feaLib/parser_test.py @@ -81,6 +81,7 @@ class ParserTest(unittest.TestCase): c2 = doc.statements[1].statements[1] self.assertEqual(type(c1), ast.Comment) self.assertEqual(c1.text, "# Initial") + self.assertEqual(str(c1), "# Initial") self.assertEqual(type(c2), ast.Comment) self.assertEqual(c2.text, "# simple") self.assertEqual(doc.statements[1].name, "test")