From 2646dc68c8b89cbc395ad27de085add94827eb3e Mon Sep 17 00:00:00 2001 From: Frank Brehm Date: Wed, 15 Mar 2017 14:13:09 +0100 Subject: [PATCH] Start evaluating output of du --- pp_lib/format_du.py | 52 +++++++++++++++++++++++++++++++++++++--- pp_lib/global_version.py | 2 +- 2 files changed, 50 insertions(+), 4 deletions(-) diff --git a/pp_lib/format_du.py b/pp_lib/format_du.py index 804310e..6bec32f 100644 --- a/pp_lib/format_du.py +++ b/pp_lib/format_du.py @@ -13,6 +13,7 @@ import logging import textwrap import sys +# Third party modules import six # Own modules @@ -25,7 +26,7 @@ try: except ImportError: from .global_version import __version__ as my_version -__version__ = '0.2.1' +__version__ = '0.3.1' LOG = logging.getLogger(__name__) @@ -144,13 +145,58 @@ class FormatDuApp(PpApplication): self.exit(1) self.precision = self.args.precision - # ------------------------------------------------------------------------- def _run(self): """The underlaying startpoint of the application.""" - LOG.info("Starting ...") + fh = None + opened = False + open_args = {} + if six.PY3: + open_args['encoding'] = 'utf-8' + open_args['errors'] = 'surrogateescape' + filename = None + + try: + if self.args.file and self.args.file != '-': + fh = open(self.args.file, 'r', **open_args) + opened = True + filename = '{!r}'.format(self.args.file) + else: + fh = sys.stdin + filename = '' + + LOG.debug("Reading DU info from {}.".format(filename)) + self.read_file(fh) + + finally: + if opened: + fh.close() + + # ------------------------------------------------------------------------- + def read_file(self, fh): + + line = None + eof = False + + while not eof: + line = fh.readline() + if not line: + eof = True + break + line = line.strip() + if not line: + continue + self.eval_line(line) + + if self.verbose > 1: + LOG.debug("Finished reading.") + + # ------------------------------------------------------------------------- + def eval_line(self, line): + if self.verbose > 2: + LOG.debug("Evaluating line {!r} ...".format(line)) # ============================================================================= diff --git a/pp_lib/global_version.py b/pp_lib/global_version.py index f7d71aa..6acf14e 100644 --- a/pp_lib/global_version.py +++ b/pp_lib/global_version.py @@ -9,7 +9,7 @@ __author__ = 'Frank Brehm ' __contact__ = 'frank.brehm@pixelpark.com' -__version__ = '0.1.1' +__version__ = '0.2.1' __license__ = 'LGPL3+' # vim: fileencoding=utf-8 filetype=python ts=4 -- 2.39.5