Skip to content

Commit

Permalink
Merge pull request #674 from gisce/allow-pipe-mode
Browse files Browse the repository at this point in the history
Allow to add a PIPE mode to only write progress
  • Loading branch information
ecarreras authored Dec 5, 2024
2 parents 7f32610 + 1f5b4c5 commit 06600a3
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
11 changes: 9 additions & 2 deletions libcnmc/core/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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),)
Expand Down
21 changes: 21 additions & 0 deletions libcnmc/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-
import os
import multiprocessing
import sys
import tempfile

from shapely import wkt
Expand Down Expand Up @@ -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()

0 comments on commit 06600a3

Please sign in to comment.