Using print() function instead of print statements to allow Python3

This commit is contained in:
Thomas Goirand 2014-09-25 16:31:15 +08:00
parent 52e5b29c11
commit 1d71d1a05f
2 changed files with 20 additions and 20 deletions

View File

@ -35,14 +35,14 @@ PROMPT = "yaql> "
@context_aware @context_aware
def main(context, show_tokens): def main(context, show_tokens):
print "Yet Another Query Language - command-line query tool" print("Yet Another Query Language - command-line query tool")
print "Version {0}".format(version) print("Version {0}".format(version))
print "Copyright (c) 2014 Mirantis, Inc" print("Copyright (c) 2014 Mirantis, Inc")
print print("")
if not context.get_data(): if not context.get_data():
print "No data loaded into context " print("No data loaded into context ")
print "Type '@load data-file.json' to load data" print("Type '@load data-file.json' to load data")
print print("")
readline.parse_and_bind('') readline.parse_and_bind('')
@ -57,7 +57,7 @@ def main(context, show_tokens):
if comm[0] == '@': if comm[0] == '@':
func_name, args = parse_service_command(comm) func_name, args = parse_service_command(comm)
if func_name not in SERVICE_FUNCTIONS: if func_name not in SERVICE_FUNCTIONS:
print "Unknown command " + func_name print("Unknown command " + func_name)
else: else:
SERVICE_FUNCTIONS[func_name](args, context) SERVICE_FUNCTIONS[func_name](args, context)
continue continue
@ -70,42 +70,42 @@ def main(context, show_tokens):
if not tok: if not tok:
break break
tokens.append(tok) tokens.append(tok)
print "Tokens: " + str(tokens) print("Tokens: " + str(tokens))
expr = yaql.parse(comm) expr = yaql.parse(comm)
except YaqlParsingException as ex: except YaqlParsingException as ex:
if ex.position: if ex.position:
pointer_string = (" " * (ex.position + len(PROMPT))) + '^' pointer_string = (" " * (ex.position + len(PROMPT))) + '^'
print pointer_string print(pointer_string)
print ex.message print(ex.message)
continue continue
try: try:
res = expr.evaluate(context=Context(context)) res = expr.evaluate(context=Context(context))
if isinstance(res, types.GeneratorType): if isinstance(res, types.GeneratorType):
res = limit(res) res = limit(res)
print json.dumps(res, indent=4) print(json.dumps(res, indent=4))
except Exception as ex: except Exception as ex:
print "Execution exception:" print("Execution exception:")
if hasattr(ex, 'message'): if hasattr(ex, 'message'):
print ex.message print(ex.message)
else: else:
print "Unknown" print("Unknown")
def load_data(data_file, context): def load_data(data_file, context):
try: try:
json_str = open(os.path.expanduser(data_file)).read() json_str = open(os.path.expanduser(data_file)).read()
except IOError as e: except IOError as e:
print "Unable to read data file '{0}': {1}".format(data_file, print("Unable to read data file '{0}': {1}".format(data_file,
e.strerror) e.strerror))
return return
try: try:
decoder = JSONDecoder() decoder = JSONDecoder()
data = decoder.decode(json_str) data = decoder.decode(json_str)
except Exception as e: except Exception as e:
print "Unable to parse data: " + e.message print("Unable to parse data: " + e.message)
return return
context.set_data(data) context.set_data(data)
print "Data from file '{0}' loaded into context".format(data_file) print("Data from file '{0}' loaded into context".format(data_file))
def regexp(self, pattern): def regexp(self, pattern):

View File

@ -32,7 +32,7 @@ def main():
decoder = JSONDecoder() decoder = JSONDecoder()
data = decoder.decode(json_str) data = decoder.decode(json_str)
except: except:
print "Unable to load data from " + options.data print("Unable to load data from " + options.data)
return return
else: else:
data = None data = None