From 4c3c4ff03183b914750e889202435bc558933898 Mon Sep 17 00:00:00 2001 From: "James E. Blair" Date: Sun, 20 Nov 2016 10:54:45 -0800 Subject: Fail nicer if figlet/cowsay not installed If figlet or cowsay are not installed, print a warning at startup and continue. --- presentty/text.py | 54 ++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 36 insertions(+), 18 deletions(-) diff --git a/presentty/text.py b/presentty/text.py index e0a88fc..92c0a26 100644 --- a/presentty/text.py +++ b/presentty/text.py @@ -29,15 +29,24 @@ class FigletText(urwid.WidgetWrap): super(FigletText, self).__init__(widget) def _run(self): - p = subprocess.Popen(['figlet'], - stdin=subprocess.PIPE, - stdout=subprocess.PIPE, - stderr=subprocess.PIPE) - p.stdin.write(self.text) - p.stdin.close() - data = p.stdout.read() - p.stderr.read() - p.wait() + try: + p = subprocess.Popen(['figlet'], + stdin=subprocess.PIPE, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE) + except OSError, e: + if e.errno == 2: + print("ERROR: figlet is used but is not installed.") + else: + print("ERROR: unable to run figlet: %s" % e) + raw_input("Press ENTER to continue.") + data = "[Unable to run figlet]" + else: + p.stdin.write(self.text) + p.stdin.close() + data = p.stdout.read() + p.stderr.read() + p.wait() return data class CowsayText(urwid.WidgetWrap): @@ -52,15 +61,24 @@ class CowsayText(urwid.WidgetWrap): super(CowsayText, self).__init__(widget) def _run(self): - p = subprocess.Popen(['cowsay'], - stdin=subprocess.PIPE, - stdout=subprocess.PIPE, - stderr=subprocess.PIPE) - p.stdin.write(self.text) - p.stdin.close() - data = p.stdout.read() - p.stderr.read() - p.wait() + try: + p = subprocess.Popen(['cowsay'], + stdin=subprocess.PIPE, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE) + except OSError, e: + if e.errno == 2: + print("ERROR: cowsay is used but is not installed.") + else: + print("ERROR: unable to run cowsay: %s" % e) + raw_input("Press ENTER to continue.") + data = "[Unable to run cowsay]" + else: + p.stdin.write(self.text) + p.stdin.close() + data = p.stdout.read() + p.stderr.read() + p.wait() return data def main(): -- cgit v1.2.3