- Added attempt of completion on files

- Added help
- Better handling of input termination or empty lines


git-svn-id: http://galileo.dmi.unict.it/svn/relational/trunk@233 014f5005-505e-4b48-8d0a-63407b615a7c
This commit is contained in:
LtWorf 2010-06-21 17:05:31 +00:00
parent 261bd4469f
commit 9a51c38be6
3 changed files with 59 additions and 10 deletions

View File

@ -101,4 +101,6 @@
- When a query fails, shows the message of the exception (Rev220)
- Improved tokenizer for select in optimizations, now can accept operators in identifiers (Rev220)
- Uses getopt to handle the command line in a more standard way
- Organized code so the ui can be either qt or curses
- Organized code so the ui can be either qt or curses
- Does not depend on QT anymore
- Added readline user interface

View File

@ -104,7 +104,7 @@ if __name__ == "__main__":
Form.show()
sys.exit(app.exec_())
else: #TODO load with readline interface
else:
import relational_readline.linegui
relational_readline.linegui.main(files)
pass

View File

@ -22,6 +22,7 @@
import readline
import logging
import os.path
import os
import sys
from relational import relation, parser, optimizer
@ -46,6 +47,8 @@ class SimpleCompleter(object):
pass
def complete(self, text, state):
#print test, state
response = None
if state == 0:
# This is the first time for this text, so build a match list.
@ -53,6 +56,19 @@ class SimpleCompleter(object):
self.matches = [s
for s in self.options
if s and s.startswith(text)]
#Add the completion for files here
try:
listf=os.listdir(os.path.dirname(text))
except:
listf=os.listdir('.')
for i in listf:
if i.startswith(text):
if os.path.isdir(i):
i+="/"
self.matches.append(i)
logging.debug('%s matches: %s', repr(text), self.matches)
else:
self.matches = self.options[:]
@ -70,7 +86,7 @@ class SimpleCompleter(object):
relations={}
completer=SimpleCompleter(['LIST','LOAD ','UNLOAD ','HELP','QUIT','SAVE ','_PRODUCT ','_UNION ','_INTERSECTION ','_DIFFERENCE ','_JOIN ','_LJOIN ','_RJOIN ','_FJOIN ','_PROJECTION ','_RENAME_TO ','_SELECTION ','_RENAME '])
completer=SimpleCompleter(['LIST','LOAD ','UNLOAD ','HELP ','QUIT','SAVE ','_PRODUCT ','_UNION ','_INTERSECTION ','_DIFFERENCE ','_JOIN ','_LJOIN ','_RJOIN ','_FJOIN ','_PROJECTION ','_RENAME_TO ','_SELECTION ','_RENAME '])
def load_relation(filename,defname=None):
@ -93,14 +109,43 @@ def load_relation(filename,defname=None):
except Exception, e:
print e
return None
def help(command):
'''Prints help on the various functions'''
p=command.split(' ',1)
if len(p)==1:
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 '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.'
return
cmd=p[1]
if cmd=='QUIT':
print 'Quits the program'
elif cmd=='LIST':
print "Lists the relations loaded"
elif cmd=='LOAD':
print "LOAD filename [relationame]"
print "Loads a relation into memory"
elif cmd=='UNLOAD':
print "UNLOAD relationame"
print "Unloads a relation from memory"
elif cmd=='SAVE':
print "SAVE filename relationame"
print "Saves a relation in a file"
elif cmd=='HELP':
print "Prints the help on a command"
else:
print "Unknown command: %s" %cmd
def exec_line(command):
command=command.strip()
if command=='QUIT':
sys.exit(0)
elif command=='HELP':
#//TODO
pass
elif command.startswith('HELP'):
help(command)
elif command=='LIST': #Lists all the loaded relations
for i in relations:
if not i.startswith('_'):
@ -176,6 +221,7 @@ def exec_query(command):
print "-> query: %s" % pyquery
if printrel:
print
print result
relations[relname]=result
@ -192,13 +238,14 @@ def main(files=[]):
readline.set_completer(completer.complete)
readline.parse_and_bind('tab: complete')
readline.parse_and_bind('set editing-mode vi')
readline.parse_and_bind('set editing-mode emacs')
while True:
try:
line = raw_input('> ')
exec_line(line)
except:
if isinstance(line,str) and len(line)>0:
exec_line(line)
except EOFError:
print
sys.exit(0)