]> git.phdru.name Git - sqlconvert.git/commitdiff
Output tokens to an output stream instead of printing
authorOleg Broytman <phd@phdru.name>
Sat, 23 Jul 2016 03:17:22 +0000 (06:17 +0300)
committerOleg Broytman <phd@phdru.name>
Sat, 23 Jul 2016 03:17:22 +0000 (06:17 +0300)
Convert a list of tokens tokens to a string.

mysql2sql/print_tokens.py

index 05af1c1ebbed5697e77c7b6329c743e05f59cfd5..bfcaad9fca3097757a49a2c4c38037f7ab7c34ec 100644 (file)
@@ -1,20 +1,35 @@
 
 import sys
+try:
+    from cStringIO import StringIO
+except ImportError:
+    try:
+        from StringIO import StringIO
+    except ImportError:
+        from io import StringIO
 from sqlparse.sql import TokenList
 
 
-def print_subtree(token_list, ident=0):
+def print_subtree(token_list, outfile=sys.stdout, ident=0):
     for token in token_list:
-        print " "*ident, repr(token)
+        outfile.write(" "*ident)
+        outfile.write(repr(token))
+        outfile.write("\n")
         if isinstance(token, TokenList):
-            print_subtree(token, ident+4)
+            print_subtree(token, outfile, ident+4)
 
 
-def print_tokens(token_list, level=0):
+def print_tokens(token_list, outfile=sys.stdout, level=0):
     for token in token_list:
         if not isinstance(token, TokenList):
-            sys.stdout.write(token.normalized)
+            outfile.write(token.normalized)
         if isinstance(token, TokenList):
-            print_tokens(token, level+1)
+            print_tokens(token, outfile, level+1)
     if level == 0:
-        print ';'
+        outfile.write(';\n')
+
+
+def get_tokens_str(token_list):
+    sio = StringIO()
+    print_tokens(token_list, outfile=sio)
+    return sio.getvalue()