v0.9.1
convert.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 
4 import sys
5 import argparse
6 import subprocess
7 from os import path
8 from itertools import ifilterfalse
9 import multiprocessing as mp
10 from multiprocessing import Value
11 import os
12 
13 def print_progress(iteration, total, decimals=1, bar_length=50):
14  str_format = "{0:." + str(decimals) + "f}"
15  percents = str_format.format(100 * (iteration / float(total)))
16  filled_length = int(round(bar_length * iteration / float(total)))
17  bar = '█' * filled_length + '-' * (bar_length - filled_length)
18 
19  sys.stdout.write('\r |%s| %s%s (%s of %s)' %
20  (bar, percents, '%', str(iteration), str(total)))
21 
22  if iteration == total:
23  sys.stdout.write('\n')
24  sys.stdout.flush()
25 
26 
27 def is_not_h5m(file):
28  return not file.endswith('h5m')
29 
30 
32  file_vtk = path.splitext(file)[0] + ".vtk"
33  if path.exists(file_vtk):
34  return path.getmtime(file) < path.getmtime(file_vtk)
35  return False
36 
37 
38 def mb_convert(file):
39  file = path.splitext(file)[0]
40  p = subprocess.Popen(["mbconvert", file + ".h5m", file + ".vtk"],
41  stdout=subprocess.PIPE, stderr=subprocess.PIPE)
42  (out, err) = p.communicate()
43  if p.returncode:
44  print "\n", err
45  n.value += 1
46  print_progress(n.value, N.value)
47 
48 
49 if __name__ == '__main__':
50  parser = argparse.ArgumentParser()
51  parser.add_argument(
52  "file", help="list of h5m files or a regexp mask", nargs='+')
53  parser.add_argument("-np", help="number of processes", type=int, default=1)
54  args = parser.parse_args()
55 
56  file_list = list(ifilterfalse(is_not_h5m, args.file))
57  if not len(file_list):
58  print "No h5m files found for the given name/mask"
59  exit()
60 
61  file_list = list(ifilterfalse(is_older_than_vtk, file_list))
62  if not len(file_list):
63  print "All h5m files are older than corresponding vtk files"
64  exit()
65 
66  N = Value('i', len(file_list))
67  n = Value('i', 0)
68 
69  pool = mp.Pool(args.np)
70 
71  print_progress(n.value, N.value)
72  pool.map(mb_convert, file_list)
73 
74  exit()
def mb_convert(file)
Definition: convert.py:38
def is_older_than_vtk(file)
Definition: convert.py:31
def is_not_h5m(file)
Definition: convert.py:27
def print_progress(iteration, total, decimals=1, bar_length=50)
Definition: convert.py:13