Converted command line interface to Python3

master
Salvo 'LtWorf' Tomaselli 2015-02-19 12:56:21 +07:00
parent a355762fc4
commit 991b0d9942
2 changed files with 48 additions and 44 deletions

@ -415,7 +415,7 @@ def parse(expr):
if __name__ == "__main__": if __name__ == "__main__":
while True: while True:
e = str(raw_input("Expression: ")) e = input("Expression: ")
print (parse(e)) print (parse(e))
# b=u"σ age>1 and skill=='C' (peopleᐅᐊskills)" # b=u"σ age>1 and skill=='C' (peopleᐅᐊskills)"

@ -105,8 +105,8 @@ completer = SimpleCompleter(
def load_relation(filename, defname=None): def load_relation(filename, defname=None):
if not os.path.isfile(filename): if not os.path.isfile(filename):
print >> sys.stderr, colorize( print (colorize(
"%s is not a file" % filename, ERROR_COLOR) "%s is not a file" % filename, ERROR_COLOR), file=sys.stderr)
return None return None
f = filename.split('/') f = filename.split('/')
@ -116,17 +116,17 @@ def load_relation(filename, defname=None):
defname = defname[:-4] defname = defname[:-4]
if not rtypes.is_valid_relation_name(defname): if not rtypes.is_valid_relation_name(defname):
print >> sys.stderr, colorize( print (colorize(
"%s is not a valid relation name" % defname, ERROR_COLOR) "%s is not a valid relation name" % defname, ERROR_COLOR), file=sys.stderr)
return return
try: try:
relations[defname] = relation.relation(filename) relations[defname] = relation.relation(filename)
completer.add_completion(defname) completer.add_completion(defname)
print colorize("Loaded relation %s" % defname, 0x00ff00) print (colorize("Loaded relation %s" % defname, 0x00ff00))
return defname return defname
except Exception, e: except Exception as e:
print >>sys.stderr, colorize(e, ERROR_COLOR) print (colorize(e, ERROR_COLOR), file=sys.stderr)
return None return None
@ -139,7 +139,7 @@ def survey():
fields = ('System', 'Country', 'School', 'Age', 'How did you find', fields = ('System', 'Country', 'School', 'Age', 'How did you find',
'email (only if you want a reply)', 'Comments') 'email (only if you want a reply)', 'Comments')
for i in fields: for i in fields:
a = raw_input('%s: ' % i) a = input('%s: ' % i)
post[i] = a post[i] = a
maintenance.send_survey(post) maintenance.send_survey(post)
@ -148,33 +148,37 @@ def help(command):
'''Prints help on the various functions''' '''Prints help on the various functions'''
p = command.split(' ', 1) p = command.split(' ', 1)
if len(p) == 1: if len(p) == 1:
print 'HELP command' print ('HELP command')
print 'To execute a query:\n[relation =] query\nIf the 1st part is omitted, the result will be stored in the relation last_.' print (
print 'To prevent from printing the relation, append a ; to the end of the query.' 'To execute a query:\n[relation =] query\nIf the 1st part is omitted, the result will be stored in the relation last_.')
print 'To insert relational operators, type _OPNAME, they will be internally replaced with the correct symbol.' print (
print 'Rember: the tab key is enabled and can be very helpful if you can\'t remember something.' 'To prevent from printing the relation, append a ; to the end of the query.')
print (
'To insert relational operators, type _OPNAME, they will be internally replaced with the correct symbol.')
print (
'Rember: the tab key is enabled and can be very helpful if you can\'t remember something.')
return return
cmd = p[1] cmd = p[1]
if cmd == 'QUIT': if cmd == 'QUIT':
print 'Quits the program' print ('Quits the program')
elif cmd == 'LIST': elif cmd == 'LIST':
print "Lists the relations loaded" print ('Lists the relations loaded')
elif cmd == 'LOAD': elif cmd == 'LOAD':
print "LOAD filename [relationame]" print ('LOAD filename [relationame]')
print "Loads a relation into memory" print ('Loads a relation into memory')
elif cmd == 'UNLOAD': elif cmd == 'UNLOAD':
print "UNLOAD relationame" print ('UNLOAD relationame')
print "Unloads a relation from memory" print ('Unloads a relation from memory')
elif cmd == 'SAVE': elif cmd == 'SAVE':
print "SAVE filename relationame" print ('SAVE filename relationame')
print "Saves a relation in a file" print ('Saves a relation in a file')
elif cmd == 'HELP': elif cmd == 'HELP':
print "Prints the help on a command" print ('Prints the help on a command')
elif cmd == 'SURVEY': elif cmd == 'SURVEY':
print "Fill and send a survey" print ('Fill and send a survey')
else: else:
print "Unknown command: %s" % cmd print ('Unknown command: %s' % cmd)
def exec_line(command): def exec_line(command):
@ -189,13 +193,13 @@ def exec_line(command):
elif command == 'LIST': # Lists all the loaded relations elif command == 'LIST': # Lists all the loaded relations
for i in relations: for i in relations:
if not i.startswith('_'): if not i.startswith('_'):
print i print (i)
elif command == 'SURVEY': elif command == 'SURVEY':
survey() survey()
elif command.startswith('LOAD '): # Loads a relation elif command.startswith('LOAD '): # Loads a relation
pars = command.split(' ') pars = command.split(' ')
if len(pars) == 1: if len(pars) == 1:
print colorize("Missing parameter", ERROR_COLOR) print (colorize("Missing parameter", ERROR_COLOR))
return return
filename = pars[1] filename = pars[1]
@ -208,31 +212,31 @@ def exec_line(command):
elif command.startswith('UNLOAD '): elif command.startswith('UNLOAD '):
pars = command.split(' ') pars = command.split(' ')
if len(pars) < 2: if len(pars) < 2:
print colorize("Missing parameter", ERROR_COLOR) print (colorize("Missing parameter", ERROR_COLOR))
return return
if pars[1] in relations: if pars[1] in relations:
del relations[pars[1]] del relations[pars[1]]
completer.remove_completion(pars[1]) completer.remove_completion(pars[1])
else: else:
print colorize("No such relation %s" % pars[1], ERROR_COLOR) print (colorize("No such relation %s" % pars[1], ERROR_COLOR))
pass pass
elif command.startswith('SAVE '): elif command.startswith('SAVE '):
pars = command.split(' ') pars = command.split(' ')
if len(pars) != 3: if len(pars) != 3:
print colorize("Missing parameter", ERROR_COLOR) print (colorize("Missing parameter", ERROR_COLOR))
return return
filename = pars[1] filename = pars[1]
defname = pars[2] defname = pars[2]
if defname not in relations: if defname not in relations:
print colorize("No such relation %s" % defname, ERROR_COLOR) print (colorize("No such relation %s" % defname, ERROR_COLOR))
return return
try: try:
relations[defname].save(filename) relations[defname].save(filename)
except Exception, e: except Exception as e:
print colorize(e, ERROR_COLOR) print (colorize(e, ERROR_COLOR))
else: else:
exec_query(command) exec_query(command)
@ -259,7 +263,6 @@ def exec_query(command):
'''This function executes a query and prints the result on the screen '''This function executes a query and prints the result on the screen
if the command terminates with ";" the result will not be printed if the command terminates with ";" the result will not be printed
''' '''
command = unicode(command, 'utf-8')
# If it terminates with ; doesn't print the result # If it terminates with ; doesn't print the result
if command.endswith(';'): if command.endswith(';'):
@ -286,22 +289,23 @@ def exec_query(command):
pyquery = parser.parse(query) pyquery = parser.parse(query)
result = eval(pyquery, relations) result = eval(pyquery, relations)
print colorize("-> query: %s" % pyquery.encode('utf-8'), 0x00ff00) print (colorize("-> query: %s" % pyquery.encode('utf-8'), 0x00ff00))
if printrel: if printrel:
print print ()
print result print (result)
relations[relname] = result relations[relname] = result
completer.add_completion(relname) completer.add_completion(relname)
except Exception, e: except Exception as e:
print colorize(str(e), ERROR_COLOR) print (colorize(str(e), ERROR_COLOR))
def main(files=[]): def main(files=[]):
print colorize('> ', PROMPT_COLOR) + "; Type HELP to get the HELP" print (colorize('> ', PROMPT_COLOR) + "; Type HELP to get the HELP")
print colorize('> ', PROMPT_COLOR) + "; Completion is activated using the tab (if supported by the terminal)" print (colorize('> ', PROMPT_COLOR) +
"; Completion is activated using the tab (if supported by the terminal)")
for i in files: for i in files:
load_relation(i) load_relation(i)
@ -314,14 +318,14 @@ def main(files=[]):
while True: while True:
try: try:
line = raw_input(colorize('> ', PROMPT_COLOR)) line = input(colorize('> ', PROMPT_COLOR))
if isinstance(line, str) and len(line) > 0: if isinstance(line, str) and len(line) > 0:
exec_line(line) exec_line(line)
except KeyboardInterrupt: except KeyboardInterrupt:
print print ()
continue continue
except EOFError: except EOFError:
print print ()
sys.exit(0) sys.exit(0)