]> 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 142391fbe05196497b25396a571072765d099ea5..3e2b0d5cab08be97bba84a777fe52ceddb422fca 100644 (file)
@@ -2,10 +2,15 @@
 import sys
 
 
-def print_tokens(token_list, outfile=sys.stdout):
+def print_tokens(token_list, outfile=sys.stdout, encoding=None):
+    if encoding:
+        outfile = getattr(outfile, 'buffer', outfile)
     for token in token_list.flatten():
-        outfile.write(token.normalized)
+        normalized = token.normalized
+        if encoding:
+            normalized = normalized.encode(encoding)
+        outfile.write(normalized)
 
 
 def tlist2str(token_list):
-    return ''.join(token.normalized for token in token_list.flatten())
+    return u''.join(token.normalized for token in token_list.flatten())