]> git.phdru.name Git - sqlconvert.git/blobdiff - mysql2sql/print_tokens.py
Use encoding (default is utf-8) and unicode
[sqlconvert.git] / mysql2sql / print_tokens.py
index 05af1c1ebbed5697e77c7b6329c743e05f59cfd5..3e2b0d5cab08be97bba84a777fe52ceddb422fca 100644 (file)
@@ -1,20 +1,16 @@
 
 import sys
-from sqlparse.sql import TokenList
 
 
-def print_subtree(token_list, ident=0):
-    for token in token_list:
-        print " "*ident, repr(token)
-        if isinstance(token, TokenList):
-            print_subtree(token, ident+4)
+def print_tokens(token_list, outfile=sys.stdout, encoding=None):
+    if encoding:
+        outfile = getattr(outfile, 'buffer', outfile)
+    for token in token_list.flatten():
+        normalized = token.normalized
+        if encoding:
+            normalized = normalized.encode(encoding)
+        outfile.write(normalized)
 
 
-def print_tokens(token_list, level=0):
-    for token in token_list:
-        if not isinstance(token, TokenList):
-            sys.stdout.write(token.normalized)
-        if isinstance(token, TokenList):
-            print_tokens(token, level+1)
-    if level == 0:
-        print ';'
+def tlist2str(token_list):
+    return u''.join(token.normalized for token in token_list.flatten())