From 1f5b4c584729bdd4e18f11981bf33597ab1ef15f Mon Sep 17 00:00:00 2001 From: Eduard Carreras Date: Wed, 4 Dec 2024 23:15:35 +0100 Subject: [PATCH] Allow to add a PIPE mode to only write progress --- libcnmc/core/__init__.py | 11 +++++++++-- libcnmc/utils.py | 21 +++++++++++++++++++++ 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/libcnmc/core/__init__.py b/libcnmc/core/__init__.py index 897e918a..3647f009 100644 --- a/libcnmc/core/__init__.py +++ b/libcnmc/core/__init__.py @@ -63,10 +63,13 @@ def __init__(self, **kwargs): self.output_m = multiprocessing.JoinableQueue() self.progress_q = multiprocessing.Queue() self.quiet = kwargs.pop('quiet', False) + self.pipe = os.environ.get('CNMC_PIPE') self.interactive = kwargs.pop('interactive', False) self.report_name = '' self.base_object = '' self.file_header = [] + if self.pipe: + self.quiet = True if 'SENTRY_DSN' in os.environ and Client: try: raven = Client() @@ -119,7 +122,11 @@ def progress(self, total): widgets = ['Informe %s: ' % self.report_name, Percentage(), ' ', Bar(), ' ', ETA()] if total: - pbar = ProgressBar(widgets=widgets, maxval=total).start() + if self.pipe: + from libcnmc.utils import PipeProgressBar + pbar = PipeProgressBar(maxval=total).start() + else: + pbar = ProgressBar(widgets=widgets, maxval=total).start() done = 0 while done < total: self.progress_q.get() @@ -219,7 +226,7 @@ def calc(self): start = datetime.now() processes = [multiprocessing.Process(target=self.consumer) for _ in range(0, self.num_proc)] - if not self.quiet: + if not self.quiet or self.pipe: processes += [ multiprocessing.Process( target=self.progress, args=(len(sequence),) diff --git a/libcnmc/utils.py b/libcnmc/utils.py index 20ddf6cc..9c482366 100644 --- a/libcnmc/utils.py +++ b/libcnmc/utils.py @@ -1,6 +1,7 @@ # -*- coding: utf-8 -*- import os import multiprocessing +import sys import tempfile from shapely import wkt @@ -641,3 +642,23 @@ def get_serveis_aux(o, re_id): serveis_aux = cups_data['cups'] return serveis_aux + + +class PipeProgressBar(object): + def __init__(self, maxval): + self.maxval = maxval + self.current = 0 + + def start(self): + self.current = 0 + return self + + def update(self, done): + self.current = done + sys.stdout.write("{}\n".format(int(((self.current * 1.0) / self.maxval) * 100))) + sys.stdout.flush() + + def finish(self): + self.current = self.maxval + sys.stdout.write("100\n") + sys.stdout.flush()