X-Git-Url: https://git.phdru.name/?a=blobdiff_plain;ds=sidebyside;f=mysql2sql%2Fprint_tokens.py;h=3e2b0d5cab08be97bba84a777fe52ceddb422fca;hb=4c93c3d89685aba33fc45082022373eb93b6583e;hp=5d104c37d0d159e0f4f8e0fdeacc97dcd1523c82;hpb=28b15069b60bba71d6b3b8a038353039aaf9b2d6;p=sqlconvert.git diff --git a/mysql2sql/print_tokens.py b/mysql2sql/print_tokens.py index 5d104c3..3e2b0d5 100644 --- a/mysql2sql/print_tokens.py +++ b/mysql2sql/print_tokens.py @@ -1,24 +1,16 @@ 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_tokens(token_list, outfile=sys.stdout, level=0): - for token in token_list: - if isinstance(token, TokenList): - print_tokens(token, outfile, level+1) - else: - outfile.write(token.normalized) +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 get_tokens_str(token_list): - sio = StringIO() - print_tokens(token_list, outfile=sio) - return sio.getvalue() +def tlist2str(token_list): + return u''.join(token.normalized for token in token_list.flatten())