Style changes

This commit is contained in:
Salvo 'LtWorf' Tomaselli 2017-06-24 10:36:01 +02:00
parent c1326c23e4
commit 83ef5986e2
No known key found for this signature in database
GPG Key ID: B3A7CF0C801886CF

View File

@ -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)
@ -41,7 +42,7 @@ def printtty(*args, **kwargs):
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)]
@ -117,21 +116,21 @@ def load_relation(filename, defname=None):
"%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
if defname is None:
f = filename.split('/') f = filename.split('/')
if defname == None: defname = f[-1].lower()
defname = f[len(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)
@ -140,7 +139,6 @@ def load_relation(filename, defname=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(
'To execute a query:\n[relation =] query\nIf the 1st part is omitted, the result will be stored in the relation last_.') 'HELP command\n'
print ( 'To execute a query:\n'
'To prevent from printing the relation, append a ; to the end of the query.') '[relation =] query\n'
print ( 'If the 1st part is omitted, the result will be stored in the relation last_.\n'
'To insert relational operators, type _OPNAME, they will be internally replaced with the correct symbol.') 'To prevent from printing the relation, append a ; to the end of the query.\n'
print ( 'To insert relational operators, type _OPNAME, they will be internally replaced with the correct symbol.\n'
'Rember: the tab key is enabled and can be very helpful if you can\'t remember something.') 'Rember: completion 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):
@ -241,7 +230,6 @@ def exec_line(command):
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:
@ -252,19 +240,23 @@ def exec_line(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,7 +284,7 @@ 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()
@ -321,7 +313,6 @@ 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)