2010-06-21 08:37:41 +07:00
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
# coding=UTF-8
|
|
|
|
|
# Relational
|
|
|
|
|
# Copyright (C) 2010 Salvo "LtWorf" Tomaselli
|
|
|
|
|
#
|
|
|
|
|
# Relational is free software: you can redistribute it and/or modify
|
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
# (at your option) any later version.
|
|
|
|
|
#
|
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
|
#
|
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
#
|
|
|
|
|
# author Salvo "LtWorf" Tomaselli <tiposchi@tiscali.it>
|
|
|
|
|
# Initial readline code from http://www.doughellmann.com/PyMOTW/readline/index.html
|
|
|
|
|
|
|
|
|
|
import readline
|
|
|
|
|
import logging
|
|
|
|
|
import os.path
|
2010-06-21 12:05:31 +07:00
|
|
|
|
import os
|
2010-06-21 08:37:41 +07:00
|
|
|
|
import sys
|
|
|
|
|
|
2011-03-21 02:43:57 +07:00
|
|
|
|
from relational import relation, parser, rtypes
|
2011-04-01 05:28:47 +07:00
|
|
|
|
from termcolor import colored
|
|
|
|
|
|
2010-06-21 08:37:41 +07:00
|
|
|
|
|
|
|
|
|
class SimpleCompleter(object):
|
|
|
|
|
'''Handles completion'''
|
|
|
|
|
|
|
|
|
|
def __init__(self, options):
|
|
|
|
|
'''Takes a list of valid completion options'''
|
|
|
|
|
self.options = sorted(options)
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
def add_completion(self,option):
|
|
|
|
|
'''Adds one string to the list of the valid completion options'''
|
2010-06-21 08:53:48 +07:00
|
|
|
|
if option not in self.options:
|
|
|
|
|
self.options.append(option)
|
|
|
|
|
self.options.sort()
|
2010-06-21 08:37:41 +07:00
|
|
|
|
|
|
|
|
|
def remove_completion(self,option):
|
|
|
|
|
'''Removes one completion from the list of the valid completion options'''
|
2010-06-21 13:08:02 +07:00
|
|
|
|
if option in self.options:
|
|
|
|
|
self.options.remove(option)
|
2010-06-21 08:37:41 +07:00
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
def complete(self, text, state):
|
|
|
|
|
response = None
|
|
|
|
|
if state == 0:
|
|
|
|
|
# This is the first time for this text, so build a match list.
|
|
|
|
|
if text:
|
2010-06-21 12:57:15 +07:00
|
|
|
|
|
|
|
|
|
self.matches =[s
|
2010-06-21 08:37:41 +07:00
|
|
|
|
for s in self.options
|
|
|
|
|
if s and s.startswith(text)]
|
2010-06-21 12:05:31 +07:00
|
|
|
|
|
|
|
|
|
#Add the completion for files here
|
|
|
|
|
try:
|
2010-06-21 12:57:15 +07:00
|
|
|
|
d=os.path.dirname(text)
|
|
|
|
|
listf=os.listdir(d)
|
|
|
|
|
|
|
|
|
|
d+="/"
|
2010-06-21 12:05:31 +07:00
|
|
|
|
except:
|
2010-06-21 12:57:15 +07:00
|
|
|
|
d=""
|
2010-06-21 12:05:31 +07:00
|
|
|
|
listf=os.listdir('.')
|
|
|
|
|
|
|
|
|
|
for i in listf:
|
2010-06-21 12:57:15 +07:00
|
|
|
|
i=(d+i).replace('//','/')
|
2010-06-21 12:05:31 +07:00
|
|
|
|
if i.startswith(text):
|
|
|
|
|
if os.path.isdir(i):
|
2010-06-21 12:57:15 +07:00
|
|
|
|
i=i+"/"
|
2010-06-21 12:05:31 +07:00
|
|
|
|
self.matches.append(i)
|
|
|
|
|
|
2010-06-21 08:37:41 +07:00
|
|
|
|
logging.debug('%s matches: %s', repr(text), self.matches)
|
|
|
|
|
else:
|
|
|
|
|
self.matches = self.options[:]
|
|
|
|
|
logging.debug('(empty input) matches: %s', self.matches)
|
|
|
|
|
|
|
|
|
|
# Return the state'th item from the match list,
|
|
|
|
|
# if we have that many.
|
|
|
|
|
try:
|
|
|
|
|
response = self.matches[state]
|
|
|
|
|
except IndexError:
|
|
|
|
|
response = None
|
|
|
|
|
logging.debug('complete(%s, %s) => %s',
|
|
|
|
|
repr(text), state, repr(response))
|
|
|
|
|
return response
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
relations={}
|
2011-06-14 11:21:15 +07:00
|
|
|
|
completer=SimpleCompleter(['SURVEY','LIST','LOAD ','UNLOAD ','HELP ','QUIT','SAVE ','_PRODUCT ','_UNION ','_INTERSECTION ','_DIFFERENCE ','_JOIN ','_LJOIN ','_RJOIN ','_FJOIN ','_PROJECTION ','_RENAME_TO ','_SELECTION ','_RENAME ','_DIVISION '])
|
2010-06-21 08:37:41 +07:00
|
|
|
|
|
|
|
|
|
def load_relation(filename,defname=None):
|
|
|
|
|
if not os.path.isfile(filename):
|
2011-04-01 05:28:47 +07:00
|
|
|
|
print >> sys.stderr, colored("%s is not a file" % filename,'red')
|
2010-06-21 08:37:41 +07:00
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
f=filename.split('/')
|
|
|
|
|
if defname==None:
|
|
|
|
|
defname=f[len(f)-1].lower()
|
2010-09-22 08:12:02 +07:00
|
|
|
|
if defname.endswith(".csv"): #removes the extension
|
2010-06-21 08:37:41 +07:00
|
|
|
|
defname=defname[:-4]
|
2011-03-21 02:43:57 +07:00
|
|
|
|
|
|
|
|
|
if not rtypes.is_valid_relation_name(defname):
|
2011-04-01 05:28:47 +07:00
|
|
|
|
print >> sys.stderr, colored("%s is not a valid relation name" % defname,'red')
|
2011-03-21 02:43:57 +07:00
|
|
|
|
return
|
2010-06-21 08:37:41 +07:00
|
|
|
|
try:
|
|
|
|
|
relations[defname]=relation.relation(filename)
|
2010-06-21 09:30:27 +07:00
|
|
|
|
|
|
|
|
|
completer.add_completion(defname)
|
2011-04-01 05:28:47 +07:00
|
|
|
|
print colored("Loaded relation %s"% defname,'green',attrs=['bold'])
|
2010-06-21 08:37:41 +07:00
|
|
|
|
return defname
|
|
|
|
|
except Exception, e:
|
2011-04-01 05:28:47 +07:00
|
|
|
|
print >>sys.stderr,colored(e,'red')
|
2010-06-21 08:37:41 +07:00
|
|
|
|
return None
|
2010-06-21 12:05:31 +07:00
|
|
|
|
|
2011-06-14 11:21:15 +07:00
|
|
|
|
def survey():
|
|
|
|
|
'''performs a survey'''
|
|
|
|
|
from relational import maintenance
|
|
|
|
|
|
|
|
|
|
post= {'software':'Relational algebra (cli)','version':version}
|
|
|
|
|
|
|
|
|
|
fields=('System','Country','School','Age','How did you find','email (only if you want a reply)','Comments')
|
|
|
|
|
for i in fields:
|
|
|
|
|
a=raw_input('%s: '%i)
|
|
|
|
|
post[i]=a
|
|
|
|
|
maintenance.send_survey(post)
|
|
|
|
|
|
2010-06-21 12:05:31 +07:00
|
|
|
|
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.'
|
2011-03-14 16:51:22 +07:00
|
|
|
|
print 'Rember: the tab key is enabled and can be very helpful if you can\'t remember something.'
|
2010-06-21 12:05:31 +07:00
|
|
|
|
return
|
|
|
|
|
cmd=p[1]
|
2010-06-21 08:37:41 +07:00
|
|
|
|
|
2010-06-21 12:05:31 +07:00
|
|
|
|
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"
|
2011-06-14 11:21:15 +07:00
|
|
|
|
elif cmd=='SURVEY':
|
|
|
|
|
print "Fill and send a survey"
|
2010-06-21 12:05:31 +07:00
|
|
|
|
else:
|
|
|
|
|
print "Unknown command: %s" %cmd
|
|
|
|
|
|
|
|
|
|
|
2010-06-21 08:37:41 +07:00
|
|
|
|
def exec_line(command):
|
|
|
|
|
command=command.strip()
|
2011-04-01 05:28:47 +07:00
|
|
|
|
|
|
|
|
|
if command.startswith(';'):
|
|
|
|
|
return
|
|
|
|
|
elif command=='QUIT':
|
2010-06-21 08:37:41 +07:00
|
|
|
|
sys.exit(0)
|
2010-06-21 12:05:31 +07:00
|
|
|
|
elif command.startswith('HELP'):
|
|
|
|
|
help(command)
|
2010-06-21 08:37:41 +07:00
|
|
|
|
elif command=='LIST': #Lists all the loaded relations
|
|
|
|
|
for i in relations:
|
2010-06-21 09:12:43 +07:00
|
|
|
|
if not i.startswith('_'):
|
|
|
|
|
print i
|
2011-06-14 11:21:15 +07:00
|
|
|
|
elif command=='SURVEY':
|
|
|
|
|
survey()
|
2011-04-01 05:28:47 +07:00
|
|
|
|
elif command.startswith('LOAD '): #Loads a relation
|
2010-06-21 08:37:41 +07:00
|
|
|
|
pars=command.split(' ')
|
2010-06-21 13:08:02 +07:00
|
|
|
|
if len(pars)==1:
|
2011-04-01 05:28:47 +07:00
|
|
|
|
print colored("Missing parameter",'red')
|
2010-06-21 13:08:02 +07:00
|
|
|
|
return
|
|
|
|
|
|
2010-06-21 08:37:41 +07:00
|
|
|
|
filename=pars[1]
|
|
|
|
|
if len(pars)>2:
|
|
|
|
|
defname=pars[2]
|
|
|
|
|
else:
|
|
|
|
|
defname=None
|
2010-06-21 09:30:27 +07:00
|
|
|
|
load_relation(filename,defname)
|
2010-06-21 08:37:41 +07:00
|
|
|
|
|
2011-04-01 05:28:47 +07:00
|
|
|
|
elif command.startswith('UNLOAD '):
|
2010-06-21 13:08:02 +07:00
|
|
|
|
pars=command.split(' ')
|
|
|
|
|
if len(pars)<2:
|
2011-04-01 05:28:47 +07:00
|
|
|
|
print colored("Missing parameter",'red')
|
2010-06-21 13:08:02 +07:00
|
|
|
|
return
|
|
|
|
|
if pars[1] in relations:
|
|
|
|
|
del relations[pars[1]]
|
|
|
|
|
completer.remove_completion(pars[1])
|
|
|
|
|
else:
|
2011-04-01 05:28:47 +07:00
|
|
|
|
print colored("No such relation %s" % pars[1],'red')
|
2010-06-21 08:37:41 +07:00
|
|
|
|
pass
|
2011-04-01 05:28:47 +07:00
|
|
|
|
elif command.startswith('SAVE '):
|
2010-06-21 16:02:47 +07:00
|
|
|
|
pars=command.split(' ')
|
|
|
|
|
if len(pars)!=3:
|
2011-04-01 05:28:47 +07:00
|
|
|
|
print colored("Missing parameter",'red')
|
2010-06-21 16:02:47 +07:00
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
filename=pars[1]
|
|
|
|
|
defname=pars[2]
|
|
|
|
|
|
|
|
|
|
if defname not in relations:
|
2011-04-01 05:28:47 +07:00
|
|
|
|
print colored("No such relation %s" % defname,'red')
|
2010-06-21 16:02:47 +07:00
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
relations[defname].save(filename)
|
|
|
|
|
except Exception,e:
|
2011-04-01 05:28:47 +07:00
|
|
|
|
print colored(e,'red')
|
2010-06-21 08:37:41 +07:00
|
|
|
|
else:
|
2010-06-21 16:02:47 +07:00
|
|
|
|
exec_query(command)
|
2010-06-21 08:53:48 +07:00
|
|
|
|
|
2010-06-21 09:12:43 +07:00
|
|
|
|
def replacements(query):
|
2010-06-21 09:20:09 +07:00
|
|
|
|
'''This funcion replaces ascii easy operators with the correct ones'''
|
2010-06-21 09:12:43 +07:00
|
|
|
|
query=query.replace( '_PRODUCT' , '*')
|
|
|
|
|
query=query.replace( '_UNION' , 'ᑌ')
|
|
|
|
|
query=query.replace( '_INTERSECTION' , 'ᑎ')
|
|
|
|
|
query=query.replace( '_DIFFERENCE' , '-')
|
|
|
|
|
query=query.replace( '_JOIN' , 'ᐅᐊ')
|
|
|
|
|
query=query.replace( '_LJOIN' , 'ᐅLEFTᐊ')
|
|
|
|
|
query=query.replace( '_RJOIN' , 'ᐅRIGHTᐊ')
|
|
|
|
|
query=query.replace( '_FJOIN' , 'ᐅFULLᐊ')
|
|
|
|
|
query=query.replace( '_PROJECTION' , 'π')
|
|
|
|
|
query=query.replace( '_RENAME_TO' , '➡')
|
|
|
|
|
query=query.replace( '_SELECTION' , 'σ')
|
|
|
|
|
query=query.replace( '_RENAME' , 'ρ')
|
2010-07-02 08:50:06 +07:00
|
|
|
|
query=query.replace( '_DIVISION' , '÷')
|
2010-06-21 09:12:43 +07:00
|
|
|
|
return query
|
|
|
|
|
|
2010-06-21 08:53:48 +07:00
|
|
|
|
def exec_query(command):
|
2010-10-26 08:54:34 +07:00
|
|
|
|
'''This function executes a query and prints the result on the screen
|
|
|
|
|
if the command terminates with ";" the result will not be printed
|
|
|
|
|
'''
|
2010-06-21 08:53:48 +07:00
|
|
|
|
|
2010-06-21 09:24:43 +07:00
|
|
|
|
#If it terminates with ; doesn't print the result
|
|
|
|
|
if command.endswith(';'):
|
|
|
|
|
command=command[:-1]
|
|
|
|
|
printrel=False
|
|
|
|
|
else:
|
|
|
|
|
printrel=True
|
|
|
|
|
|
2010-06-21 09:20:09 +07:00
|
|
|
|
#Performs replacements for weird operators
|
|
|
|
|
command=replacements(command)
|
2010-06-21 08:53:48 +07:00
|
|
|
|
|
2010-06-21 09:20:09 +07:00
|
|
|
|
#Finds the name in where to save the query
|
2010-06-21 08:53:48 +07:00
|
|
|
|
parts=command.split('=',1)
|
|
|
|
|
|
2011-03-21 02:43:57 +07:00
|
|
|
|
if len(parts)>1 and rtypes.is_valid_relation_name(parts[0]):
|
|
|
|
|
relname=parts[0]
|
|
|
|
|
query=parts[1]
|
2010-06-21 08:53:48 +07:00
|
|
|
|
else:
|
|
|
|
|
relname='last_'
|
|
|
|
|
query=command
|
|
|
|
|
|
2011-10-17 16:26:01 +07:00
|
|
|
|
query=unicode(query,'utf-8')
|
2010-06-21 08:53:48 +07:00
|
|
|
|
#Execute query
|
|
|
|
|
try:
|
|
|
|
|
pyquery=parser.parse(query)
|
|
|
|
|
result=eval(pyquery,relations)
|
2011-04-01 05:28:47 +07:00
|
|
|
|
print colored("-> query: %s" % pyquery,'green')
|
2010-06-21 09:24:43 +07:00
|
|
|
|
|
|
|
|
|
if printrel:
|
2010-06-21 12:05:31 +07:00
|
|
|
|
print
|
2010-06-21 09:24:43 +07:00
|
|
|
|
print result
|
2010-06-21 08:53:48 +07:00
|
|
|
|
|
|
|
|
|
relations[relname]=result
|
|
|
|
|
|
|
|
|
|
completer.add_completion(relname)
|
|
|
|
|
except Exception, e:
|
2011-04-01 05:28:47 +07:00
|
|
|
|
print colored(e,'red')
|
2010-06-21 08:53:48 +07:00
|
|
|
|
|
2010-06-21 08:37:41 +07:00
|
|
|
|
def main(files=[]):
|
2011-04-01 05:28:47 +07:00
|
|
|
|
print colored('> ','blue') + "; Type HELP to get the HELP"
|
|
|
|
|
print colored('> ','blue') + "; Completion is activated using the tab (if supported by the terminal)"
|
2010-06-21 09:30:27 +07:00
|
|
|
|
|
|
|
|
|
for i in files:
|
|
|
|
|
load_relation(i)
|
|
|
|
|
|
2010-06-21 08:37:41 +07:00
|
|
|
|
readline.set_completer(completer.complete)
|
|
|
|
|
|
|
|
|
|
readline.parse_and_bind('tab: complete')
|
2010-06-21 12:05:31 +07:00
|
|
|
|
readline.parse_and_bind('set editing-mode emacs')
|
2010-06-21 12:57:15 +07:00
|
|
|
|
readline.set_completer_delims(" ")
|
|
|
|
|
|
2010-06-21 08:37:41 +07:00
|
|
|
|
|
|
|
|
|
while True:
|
2010-06-21 09:12:43 +07:00
|
|
|
|
try:
|
2011-04-01 05:28:47 +07:00
|
|
|
|
line = raw_input(colored('> ','blue'))
|
|
|
|
|
if isinstance(line,str) and len(line)>0:
|
2010-06-21 12:05:31 +07:00
|
|
|
|
exec_line(line)
|
|
|
|
|
except EOFError:
|
2010-06-21 09:12:43 +07:00
|
|
|
|
print
|
|
|
|
|
sys.exit(0)
|
2010-06-21 08:37:41 +07:00
|
|
|
|
|
|
|
|
|
|
2010-06-21 09:30:27 +07:00
|
|
|
|
if __name__ == "__main__":
|
2011-04-01 05:28:47 +07:00
|
|
|
|
main()
|
|
|
|
|
|