From 87e2c76468b24665ea59a708d0ddbe69c558b54d Mon Sep 17 00:00:00 2001 From: Salvo 'LtWorf' Tomaselli Date: Fri, 6 Nov 2015 16:03:58 +0100 Subject: [PATCH] Silent mode if using scripts When using a script, the command line mode prints less debug output and less things in general, to avoid cluttering the output with unwanted things. --- relational_gui.py | 3 ++- relational_readline/linegui.py | 29 +++++++++++++++++++++-------- 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/relational_gui.py b/relational_gui.py index fd8c6e5..44808f2 100755 --- a/relational_gui.py +++ b/relational_gui.py @@ -121,9 +121,10 @@ if __name__ == "__main__": form.show() sys.exit(app.exec_()) else: - printver(False) try: import relational_readline.linegui + if relational_readline.linegui.TTY: + printver(False) except: print ( "Module relational_readline is missing.\nPlease install relational-cli package.", diff --git a/relational_readline/linegui.py b/relational_readline/linegui.py index 6931d7a..08aaad9 100644 --- a/relational_readline/linegui.py +++ b/relational_readline/linegui.py @@ -30,6 +30,15 @@ from xtermcolor import colorize PROMPT_COLOR = 0xffff00 ERROR_COLOR = 0xff0000 +TTY = os.isatty(0) and os.isatty(1) + +def printtty(*args, **kwargs): + ''' + Prints only if stdout and stdin are a tty + ''' + if TTY: + print(*args,**kwargs) + class SimpleCompleter(object): @@ -121,7 +130,7 @@ def load_relation(filename, defname=None): relations[defname] = relation.relation(filename) completer.add_completion(defname) - print (colorize("Loaded relation %s" % defname, 0x00ff00)) + printtty(colorize("Loaded relation %s" % defname, 0x00ff00)) return defname except Exception as e: print (colorize(e, ERROR_COLOR), file=sys.stderr) @@ -287,7 +296,7 @@ def exec_query(command): pyquery = parser.parse(query) result = pyquery(relations) - print (colorize("-> query: %s" % pyquery, 0x00ff00)) + printtty(colorize("-> query: %s" % pyquery, 0x00ff00)) if printrel: print () @@ -301,8 +310,8 @@ def exec_query(command): def main(files=[]): - print (colorize('> ', PROMPT_COLOR) + "; Type HELP to get the HELP") - print (colorize('> ', PROMPT_COLOR) + + printtty(colorize('> ', PROMPT_COLOR) + "; Type HELP to get the HELP") + printtty(colorize('> ', PROMPT_COLOR) + "; Completion is activated using the tab (if supported by the terminal)") for i in files: @@ -316,14 +325,18 @@ def main(files=[]): while True: try: - line = input(colorize('> ', PROMPT_COLOR)) + + line = input(colorize('> ' if TTY else '', PROMPT_COLOR)) if isinstance(line, str) and len(line) > 0: exec_line(line) except KeyboardInterrupt: - print ('^C\n') - continue + if TTY: + print ('^C\n') + continue + else: + break except EOFError: - print () + printtty() sys.exit(0)