Style changes
This commit is contained in:
parent
c1326c23e4
commit
83ef5986e2
@ -30,6 +30,7 @@ from xtermcolor import colorize
|
|||||||
|
|
||||||
PROMPT_COLOR = 0xffff00
|
PROMPT_COLOR = 0xffff00
|
||||||
ERROR_COLOR = 0xff0000
|
ERROR_COLOR = 0xff0000
|
||||||
|
COLOR_GREEN = 0x00ff00
|
||||||
|
|
||||||
TTY = os.isatty(0) and os.isatty(1)
|
TTY = os.isatty(0) and os.isatty(1)
|
||||||
|
|
||||||
@ -38,10 +39,10 @@ def printtty(*args, **kwargs):
|
|||||||
Prints only if stdout and stdin are a tty
|
Prints only if stdout and stdin are a tty
|
||||||
'''
|
'''
|
||||||
if TTY:
|
if TTY:
|
||||||
print(*args,**kwargs)
|
print(*args, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
class SimpleCompleter(object):
|
class SimpleCompleter:
|
||||||
|
|
||||||
'''Handles completion'''
|
'''Handles completion'''
|
||||||
|
|
||||||
@ -60,14 +61,12 @@ class SimpleCompleter(object):
|
|||||||
'''Removes one completion from the list of the valid completion options'''
|
'''Removes one completion from the list of the valid completion options'''
|
||||||
if option in self.options:
|
if option in self.options:
|
||||||
self.options.remove(option)
|
self.options.remove(option)
|
||||||
pass
|
|
||||||
|
|
||||||
def complete(self, text, state):
|
def complete(self, text, state):
|
||||||
response = None
|
response = None
|
||||||
if state == 0:
|
if state == 0:
|
||||||
# This is the first time for this text, so build a match list.
|
# This is the first time for this text, so build a match list.
|
||||||
if text:
|
if text:
|
||||||
|
|
||||||
self.matches = [s
|
self.matches = [s
|
||||||
for s in self.options
|
for s in self.options
|
||||||
if s and s.startswith(text)]
|
if s and s.startswith(text)]
|
||||||
@ -113,34 +112,33 @@ 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 (colorize(
|
print(colorize(
|
||||||
"%s is not a file" % filename, ERROR_COLOR), file=sys.stderr)
|
"%s is not a file" % filename, ERROR_COLOR), file=sys.stderr)
|
||||||
return None
|
return None
|
||||||
|
|
||||||
f = filename.split('/')
|
if defname is None:
|
||||||
if defname == None:
|
f = filename.split('/')
|
||||||
defname = f[len(f) - 1].lower()
|
defname = f[-1].lower()
|
||||||
if defname.endswith(".csv"): # removes the extension
|
if defname.endswith(".csv"): # removes the extension
|
||||||
defname = defname[:-4]
|
defname = defname[:-4]
|
||||||
|
|
||||||
if not rtypes.is_valid_relation_name(defname):
|
if not rtypes.is_valid_relation_name(defname):
|
||||||
print (colorize(
|
print(colorize(
|
||||||
"%s is not a valid relation name" % defname, ERROR_COLOR), file=sys.stderr)
|
"%s is not a valid relation name" % defname, ERROR_COLOR), file=sys.stderr)
|
||||||
return
|
return None
|
||||||
try:
|
try:
|
||||||
relations[defname] = relation.relation(filename)
|
relations[defname] = relation.relation(filename)
|
||||||
|
|
||||||
completer.add_completion(defname)
|
completer.add_completion(defname)
|
||||||
printtty(colorize("Loaded relation %s" % defname, 0x00ff00))
|
printtty(colorize("Loaded relation %s" % defname, COLOR_GREEN))
|
||||||
return defname
|
return defname
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print (colorize(e, ERROR_COLOR), file=sys.stderr)
|
print(colorize(e, ERROR_COLOR), file=sys.stderr)
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
def survey():
|
def survey():
|
||||||
'''performs a survey'''
|
'''performs a survey'''
|
||||||
|
|
||||||
post = {'software': 'Relational algebra (cli)', 'version': version}
|
post = {'software': 'Relational algebra (cli)', 'version': version}
|
||||||
|
|
||||||
fields = ('System', 'Country', 'School', 'Age', 'How did you find',
|
fields = ('System', 'Country', 'School', 'Age', 'How did you find',
|
||||||
@ -157,37 +155,28 @@ 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(
|
||||||
print (
|
'HELP command\n'
|
||||||
'To execute a query:\n[relation =] query\nIf the 1st part is omitted, the result will be stored in the relation last_.')
|
'To execute a query:\n'
|
||||||
print (
|
'[relation =] query\n'
|
||||||
'To prevent from printing the relation, append a ; to the end of the query.')
|
'If the 1st part is omitted, the result will be stored in the relation last_.\n'
|
||||||
print (
|
'To prevent from printing the relation, append a ; to the end of the query.\n'
|
||||||
'To insert relational operators, type _OPNAME, they will be internally replaced with the correct symbol.')
|
'To insert relational operators, type _OPNAME, they will be internally replaced with the correct symbol.\n'
|
||||||
print (
|
'Rember: completion is enabled and can be very helpful if you can\'t remember something.'
|
||||||
'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':
|
cmdhelp = {
|
||||||
print ('Quits the program')
|
'QUIT': 'Quits the program',
|
||||||
elif cmd == 'LIST':
|
'LIST': 'Lists the relations loaded',
|
||||||
print ('Lists the relations loaded')
|
'LOAD': 'LOAD filename [relationame]\nLoads a relation into memory',
|
||||||
elif cmd == 'LOAD':
|
'UNLOAD': 'UNLOAD relationame\nUnloads a relation from memory',
|
||||||
print ('LOAD filename [relationame]')
|
'SAVE': 'SAVE filename relationame\nSaves a relation in a file',
|
||||||
print ('Loads a relation into memory')
|
'HELP': 'Prints the help on a command',
|
||||||
elif cmd == 'UNLOAD':
|
'SURVEY': 'Fill and send a survey',
|
||||||
print ('UNLOAD relationame')
|
}
|
||||||
print ('Unloads a relation from memory')
|
print (cmdhelp.get(cmd, 'Unknown command: %s' % cmd))
|
||||||
elif cmd == 'SAVE':
|
|
||||||
print ('SAVE filename relationame')
|
|
||||||
print ('Saves a relation in a file')
|
|
||||||
elif cmd == 'HELP':
|
|
||||||
print ('Prints the help on a command')
|
|
||||||
elif cmd == 'SURVEY':
|
|
||||||
print ('Fill and send a survey')
|
|
||||||
else:
|
|
||||||
print ('Unknown command: %s' % cmd)
|
|
||||||
|
|
||||||
|
|
||||||
def exec_line(command):
|
def exec_line(command):
|
||||||
@ -202,13 +191,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]
|
||||||
@ -221,50 +210,53 @@ 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 as e:
|
except Exception as e:
|
||||||
print (colorize(e, ERROR_COLOR))
|
print(colorize(e, ERROR_COLOR))
|
||||||
else:
|
else:
|
||||||
exec_query(command)
|
exec_query(command)
|
||||||
|
|
||||||
|
|
||||||
def replacements(query):
|
def replacements(query):
|
||||||
'''This funcion replaces ascii easy operators with the correct ones'''
|
'''This funcion replaces ascii easy operators with the correct ones'''
|
||||||
query = query.replace(u'_PRODUCT', parser.PRODUCT)
|
rules = (
|
||||||
query = query.replace(u'_UNION', parser.UNION)
|
('_PRODUCT', parser.PRODUCT),
|
||||||
query = query.replace(u'_INTERSECTION', parser.INTERSECTION)
|
('_UNION', parser.UNION),
|
||||||
query = query.replace(u'_DIFFERENCE', parser.DIFFERENCE)
|
('_INTERSECTION', parser.INTERSECTION),
|
||||||
query = query.replace(u'_JOIN', parser.JOIN)
|
('_DIFFERENCE', parser.DIFFERENCE),
|
||||||
query = query.replace(u'_LJOIN', parser.JOIN_LEFT)
|
('_JOIN', parser.JOIN),
|
||||||
query = query.replace(u'_RJOIN', parser.JOIN_RIGHT)
|
('_LJOIN', parser.JOIN_LEFT),
|
||||||
query = query.replace(u'_FJOIN', parser.JOIN_FULL)
|
('_RJOIN', parser.JOIN_RIGHT),
|
||||||
query = query.replace(u'_PROJECTION', parser.PROJECTION)
|
('_FJOIN', parser.JOIN_FULL),
|
||||||
query = query.replace(u'_RENAME_TO', parser.ARROW)
|
('_PROJECTION', parser.PROJECTION),
|
||||||
query = query.replace(u'_SELECTION', parser.SELECTION)
|
('_RENAME_TO', parser.ARROW),
|
||||||
query = query.replace(u'_RENAME', parser.RENAME)
|
('_SELECTION', parser.SELECTION),
|
||||||
query = query.replace(u'_DIVISION', parser.DIVISION)
|
('_RENAME', parser.RENAME),
|
||||||
|
('_DIVISION', parser.DIVISION),
|
||||||
|
)
|
||||||
|
for asciiop, op in rules:
|
||||||
|
query = query.replace(asciiop, op)
|
||||||
return query
|
return query
|
||||||
|
|
||||||
|
|
||||||
@ -292,17 +284,17 @@ def exec_query(command):
|
|||||||
pyquery = parser.parse(query)
|
pyquery = parser.parse(query)
|
||||||
result = pyquery(relations)
|
result = pyquery(relations)
|
||||||
|
|
||||||
printtty(colorize("-> query: %s" % pyquery, 0x00ff00))
|
printtty(colorize("-> query: %s" % pyquery, COLOR_GREEN))
|
||||||
|
|
||||||
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 as e:
|
except Exception as e:
|
||||||
print (colorize(str(e), ERROR_COLOR))
|
print(colorize(str(e), ERROR_COLOR))
|
||||||
|
|
||||||
|
|
||||||
def main(files=[]):
|
def main(files=[]):
|
||||||
@ -321,13 +313,12 @@ def main(files=[]):
|
|||||||
|
|
||||||
while True:
|
while True:
|
||||||
try:
|
try:
|
||||||
|
|
||||||
line = input(colorize('> ' if TTY else '', PROMPT_COLOR))
|
line = input(colorize('> ' if TTY else '', 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:
|
||||||
if TTY:
|
if TTY:
|
||||||
print ('^C\n')
|
print('^C\n')
|
||||||
continue
|
continue
|
||||||
else:
|
else:
|
||||||
break
|
break
|
||||||
|
Loading…
x
Reference in New Issue
Block a user