git @ Cat's Eye Technologies kinoje / 6e4cea3
Use tqdm if available. Chris Pressey 4 years ago
5 changed file(s) with 45 addition(s) and 33 deletion(s). Raw diff Collapse all Expand all
11 import os
22 import sys
33
4 from kinoje.utils import Executor, LoggingExecutor, load_config_file
4 from kinoje.utils import Executor, load_config_file
55
66
77 SUPPORTED_OUTPUT_FORMATS = ('.m4v', '.mp4', '.gif')
88
99
1010 class Compiler(object):
11 def __init__(self, config, dirname, outfilename, exe=None):
11 def __init__(self, config, dirname, outfilename, exe=None, tqdm=None):
1212 self.dirname = dirname
1313 self.exe = exe or Executor()
1414 self.outfilename = outfilename
1515 self.config = config
1616 self.frame_fmt = "%08d.png"
17 if not tqdm:
18 def tqdm(x, **kwargs): return x
19 self.tqdm = tqdm
1720
1821 @classmethod
1922 def get_class_for(cls, filename):
3033 raise NotImplementedError
3134
3235 def compile_all(self):
33 return self.compile(self.config['num_frames'])
36 tasks = [lambda: self.compile(self.config['num_frames'])]
37 for task in self.tqdm(tasks):
38 result = task()
39 return result
3440
3541
3642 class GifCompiler(Compiler):
102108 config = load_config_file(options.configfile)
103109 config['shorten_final_frame'] = options.shorten_final_frame
104110
105 exe = LoggingExecutor('compiler.log')
106
107 compiler = Compiler.get_class_for(options.output)(config, options.framesdir, options.output, exe=exe)
111 compiler = Compiler.get_class_for(options.output)(config, options.framesdir, options.output)
108112 compiler.compile_all()
109113
110114 if options.view:
111115 compiler.view()
112
113 exe.close()
55
66 from jinja2 import Template
77
8 from kinoje.utils import Executor, LoggingExecutor, fmod, tween, load_config_file
8 from kinoje.utils import Executor, fmod, tween, load_config_file
99
1010
1111 class Expander(object):
1212 """Takes a directory and a template (Jinja2) and expands the template a number of times,
1313 creating a number of filled-out text files in the directory."""
14 def __init__(self, config, dirname, exe=None):
14 def __init__(self, config, dirname, exe=None, tqdm=None):
1515 self.dirname = dirname
1616 self.template = Template(config['template'])
1717 self.config = config
1818 self.exe = exe or Executor()
19 if not tqdm:
20 def tqdm(x, **kwargs): return x
21 self.tqdm = tqdm
1922
2023 self.fun_context = {}
2124 for key, value in self.config.get('functions', {}).iteritems():
3942 def expand_all(self):
4043 t = self.config['start']
4144 t_step = self.config['t_step']
42 for frame in xrange(self.config['num_frames']):
45 for frame in self.tqdm(xrange(self.config['num_frames'])):
4346 self.fillout_template(frame, t)
4447 t += t_step
4548
5861
5962 config = load_config_file(options.configfile)
6063
61 exe = LoggingExecutor('movie.log')
62
63 expander = Expander(config, options.instantsdir, exe=exe)
64 expander = Expander(config, options.instantsdir)
6465 expander.expand_all()
65
66 exe.close()
22 import os
33 import re
44 import sys
5 from tempfile import mkdtemp
5 from tempfile import mkdtemp, mkstemp
6
7 try:
8 from tqdm import tqdm
9 except ImportError:
10 def tqdm(x, **kwargs): return x
611
712 from kinoje.expander import Expander
813 from kinoje.renderer import Renderer
914 from kinoje.compiler import Compiler
1015
11 from kinoje.utils import Executor, load_config_file
16 from kinoje.utils import LoggingExecutor, load_config_file
1217
1318
1419 SUPPORTED_OUTPUT_FORMATS = ('.m4v', '.mp4', '.gif')
4247
4348 config = load_config_file(options.configfile)
4449
45 exe = Executor()
50 fd, log_filename = mkstemp()
51 exe = LoggingExecutor(log_filename)
4652
4753 instants_dir = mkdtemp()
4854 frames_dir = mkdtemp()
4955
50 expander = Expander(config, instants_dir, exe=exe)
56 print('expanding template to instants...')
57 expander = Expander(config, instants_dir, exe=exe, tqdm=tqdm)
5158 expander.expand_all()
5259
53 renderer = Renderer(config, instants_dir, frames_dir, exe=exe)
60 print('rendering instants to frames...')
61 renderer = Renderer(config, instants_dir, frames_dir, exe=exe, tqdm=tqdm)
5462 renderer.render_all()
5563
56 compiler = Compiler.get_class_for(output_filename)(config, frames_dir, output_filename, exe=exe)
64 print('compiling frames to movie...')
65 compiler = Compiler.get_class_for(output_filename)(config, frames_dir, output_filename, exe=exe, tqdm=tqdm)
5766 compiler.compile_all()
5867
5968 exe.close()
69 os.close(fd)
88 except ImportError:
99 from yaml import Loader
1010
11 from kinoje.utils import Executor, LoggingExecutor, load_config_file
11 from kinoje.utils import Executor, load_config_file
1212
1313
1414 class Renderer(object):
1515 """Takes a source directory filled with text files and a destination directory and
1616 creates one image file in the destination directory from each text file in the source."""
17 def __init__(self, config, src, dest, exe=None):
17 def __init__(self, config, src, dest, exe=None, tqdm=None):
1818 self.command = config['command']
1919 self.libdir = config['libdir']
2020 self.src = src
2222 self.exe = exe or Executor()
2323 self.width = config['width']
2424 self.height = config['height']
25 if not tqdm:
26 def tqdm(x, **kwargs): return x
27 self.tqdm = tqdm
2528
2629 def render_all(self):
27 for filename in sorted(os.listdir(self.src)):
30 for filename in self.tqdm(sorted(os.listdir(self.src))):
2831 full_srcname = os.path.join(self.src, filename)
2932 match = re.match(r'^.*?(\d+).*?$', filename)
3033 frame = int(match.group(1))
6063
6164 config = load_config_file(options.configfile)
6265
63 exe = LoggingExecutor('renderer.log')
64
65 renderer = Renderer(config, options.instantsdir, options.framesdir, exe=exe)
66 renderer = Renderer(config, options.instantsdir, options.framesdir)
6667 renderer.render_all()
67
68 exe.close()
3838 def __init__(self, filename):
3939 self.filename = filename
4040 self.log = open(filename, 'w')
41 print("logging to {}".format(self.filename))
4142
4243 def do_it(self, cmd, **kwargs):
43 print cmd
44 self.log.write('>>> {}\n'.format(cmd))
45 self.log.flush()
4446 try:
4547 check_call(cmd, shell=True, stdout=self.log, stderr=self.log, **kwargs)
4648 except Exception as e:
4749 self.log.close()
48 print str(e)
50 print(str(e))
4951 check_call("tail %s" % self.filename, shell=True)
5052 sys.exit(1)
5153
5557
5658 class Executor(object):
5759 def do_it(self, cmd, **kwargs):
58 print cmd
60 print(cmd)
5961 check_call(cmd, shell=True, **kwargs)
6062
6163 def close(self):