From 69c1cd6460b9c7e6a37a01e086d26b49ecb1798c Mon Sep 17 00:00:00 2001 From: Matthieu Coudron Date: Wed, 17 Dec 2014 23:51:10 +0100 Subject: [PATCH] Various pep8 and comments removal + converted shell module to use run_through_shell command --- i3pystatus/__init__.py | 5 ++--- i3pystatus/core/command.py | 25 ++++++++++--------------- i3pystatus/mail/__init__.py | 6 +----- i3pystatus/shell.py | 19 +++---------------- 4 files changed, 16 insertions(+), 39 deletions(-) diff --git a/i3pystatus/__init__.py b/i3pystatus/__init__.py index df270a2..8845385 100644 --- a/i3pystatus/__init__.py +++ b/i3pystatus/__init__.py @@ -8,13 +8,12 @@ from i3pystatus.core.util import formatp import logging import os -h=logging.FileHandler(".i3pystatus-" + str(os.getpid())); +h = logging.FileHandler(".i3pystatus-" + str(os.getpid()), delay=True) logger = logging.getLogger("i3pystatus") logger.addHandler(h) logger.setLevel(logging.DEBUG) -#~/.i3pystatus-- -logger.error("Start !") + __path__ = extend_path(__path__, __name__) diff --git a/i3pystatus/core/command.py b/i3pystatus/core/command.py index a1eb41a..e0e8e47 100644 --- a/i3pystatus/core/command.py +++ b/i3pystatus/core/command.py @@ -1,28 +1,23 @@ -from subprocess import check_output, CalledProcessError +from subprocess import CalledProcessError import subprocess from i3pystatus import logger -# class Command(Object): -# ,*args -def run_through_shell(command,enable_log): + +def run_through_shell(command, enable_log, enable_shell=False): """ Retrieves output of shell command Returns tuple boolean (success)/ string (error msg, output) """ - result=False + result = False try: - #stderr=subprocess.STDOUT - #stderr=subprocess.PIPE - # with subprocess.Popen() as proc: - # logger.error(proc.stderr.read()) - proc = subprocess.Popen(command,stderr=subprocess.PIPE,stdout=subprocess.PIPE) - + proc = subprocess.Popen(command, stderr=subprocess.PIPE, stdout=subprocess.PIPE, shell=enable_shell) + out, stderr = proc.communicate() if stderr and enable_log: logger.error(stderr) - # out = check_output(command, stderr=subprocess.STDOUT, shell=True) - result=True - # color = self.color + + result = True + except CalledProcessError as e: out = e.output # color = self.error_color @@ -34,4 +29,4 @@ def run_through_shell(command,enable_log): except: out = "" - return result, out + return out, result diff --git a/i3pystatus/mail/__init__.py b/i3pystatus/mail/__init__.py index cc9658b..afe6c25 100644 --- a/i3pystatus/mail/__init__.py +++ b/i3pystatus/mail/__init__.py @@ -1,7 +1,4 @@ -import subprocess - from i3pystatus import SettingsBase, IntervalModule -from i3pystatus.core.util import internet, require from i3pystatus.core.command import run_through_shell @@ -70,8 +67,7 @@ class Mail(IntervalModule): def on_leftclick(self): if self.email_client: - run_through_shell(self.email_client,self.enable_log) - + run_through_shell(self.email_client, self.enable_log) def on_rightclick(self): self.run() diff --git a/i3pystatus/shell.py b/i3pystatus/shell.py index 571521f..12de74b 100644 --- a/i3pystatus/shell.py +++ b/i3pystatus/shell.py @@ -1,5 +1,5 @@ from i3pystatus import IntervalModule -from subprocess import check_output, CalledProcessError +from i3pystatus.core.command import run_through_shell class Shell(IntervalModule): @@ -19,21 +19,8 @@ class Shell(IntervalModule): required = ("command",) def run(self): - try: - out = check_output(self.command, shell=True) - color = self.color - except CalledProcessError as e: - out = e.output - color = self.error_color - - out = out.decode("UTF-8").replace("\n", " ") - try: - if out[-1] == " ": - out = out[:-1] - except: - out = "" - + out, success = run_through_shell(self.command, self.enable_log, enable_shell=True) self.output = { "full_text": out, - "color": color + "color": self.color if success else self.error_color }