From 14738704ede6dfa6ac79f362a9c1f7f40f470cdc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20Eduardo?= Date: Mon, 11 Sep 2017 17:47:17 -0500 Subject: rename mvc at lvc --- lvc/ui/console.py | 120 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 120 insertions(+) create mode 100644 lvc/ui/console.py (limited to 'lvc/ui/console.py') diff --git a/lvc/ui/console.py b/lvc/ui/console.py new file mode 100644 index 0000000..e7eec9c --- /dev/null +++ b/lvc/ui/console.py @@ -0,0 +1,120 @@ +import json +import operator +import optparse +import time +import sys + +import lvc +from lvc.widgets import app +from lvc.widgets import initialize + +parser = optparse.OptionParser( + usage='%prog [-l] [--list-converters] [-c ]', + version='%prog ' + lvc.VERSION, + prog='python -m lvc.ui.console') +parser.add_option('-j', '--json', action='store_true', + dest='json', + help='Output JSON documents, rather than text.') +parser.add_option('-l', '--list-converters', action='store_true', + dest='list_converters', + help="Print a list of supported converter types.") +parser.add_option('-c', '--converter', dest='converter', + help="Specify the type of conversion to make.") + +class Application(lvc.Application): + + def run(self): + (options, args) = parser.parse_args() + + if options.list_converters: + for c in sorted(self.converter_manager.list_converters(), + key=operator.attrgetter('name')): + if options.json: + print json.dumps({'name': c.name, + 'identifier': c.identifier}) + else: + print '%s (-c %s)' % ( + c.name, + c.identifier) + return + + try: + self.converter_manager.get_by_id(options.converter) + except KeyError: + message = '%r is not a valid converter type.' % ( + options.converter,) + if options.json: + print json.dumps({'error': message}) + else: + print 'ERROR:', message + print 'Use "%s -l" to get a list of valid converters.' % ( + parser.prog,) + print + parser.print_help() + sys.exit(1) + + any_failed = False + + def changed(c): + if c.status == 'failed': + any_failed = True + if options.json: + output = { + 'filename': c.video.filename, + 'output': c.output, + 'status': c.status, + 'duration': c.duration, + 'progress': c.progress, + 'percent': (c.progress_percent * 100 if c.progress_percent + else 0), + } + if c.error is not None: + output['error'] = c.error + print json.dumps(output) + else: + if c.status == 'initialized': + line = 'starting (output: %s)' % (c.output,) + elif c.status == 'converting': + if c.progress_percent is not None: + line = 'converting (%i%% complete, %is remaining)' % ( + c.progress_percent * 100, c.eta) + else: + line = 'converting (0% complete, unknown remaining)' + elif c.status == 'staging': + line = 'staging' + elif c.status == 'failed': + line = 'failed (error: %r)' % (c.error,) + elif c.status == 'finished': + line = 'finished (output: %s)' % (c.output,) + else: + line = c.status + print '%s: %s' % (c.video.filename, line) + + for filename in args: + try: + c = app.start_conversion(filename, options.converter) + except ValueError: + message = 'could not parse %r' % filename + if options.json: + any_failed = True + print json.dumps({'status': 'failed', 'error': message, + 'filename': filename}) + else: + print 'ERROR:', message + continue + changed(c) + c.listen(changed) + + # XXX real mainloop + while self.conversion_manager.running: + self.conversion_manager.check_notifications() + time.sleep(1) + self.conversion_manager.check_notifications() # one last time + + sys.exit(0 if not any_failed else 1) + +if __name__ == "__main__": + initialize(None) + app.widgetapp = Application() + app.widgetapp.startup() + app.widgetapp.run() -- cgit v1.2.3