console.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. import json
  2. import operator
  3. import optparse
  4. import time
  5. import sys
  6. import lvc
  7. from lvc.variables import (
  8. __version__
  9. )
  10. from lvc.widgets import app
  11. from lvc.widgets import initialize
  12. parser = optparse.OptionParser(
  13. usage='%prog [-l] [--list-converters] [-c <converter> <filenames..>]',
  14. version='%prog ' + __version__,
  15. prog='python -m lvc.ui.console')
  16. parser.add_option('-j', '--json', action='store_true',
  17. dest='json',
  18. help='Output JSON documents, rather than text.')
  19. parser.add_option('-l', '--list-converters', action='store_true',
  20. dest='list_converters',
  21. help="Print a list of supported converter types.")
  22. parser.add_option('-c', '--converter', dest='converter',
  23. help="Specify the type of conversion to make.")
  24. class Application(lvc.Application):
  25. def run(self):
  26. (options, args) = parser.parse_args()
  27. if options.list_converters:
  28. for c in sorted(self.converter_manager.list_converters(),
  29. key=operator.attrgetter('name')):
  30. if options.json:
  31. print(json.dumps({'name': c.name,
  32. 'identifier': c.identifier}))
  33. else:
  34. print('%s (-c %s)' % (
  35. c.name,
  36. c.identifier))
  37. return
  38. try:
  39. self.converter_manager.get_by_id(options.converter)
  40. except KeyError:
  41. message = '%r is not a valid converter type.' % (
  42. options.converter,)
  43. if options.json:
  44. print(json.dumps({'error': message}))
  45. else:
  46. print('ERROR:', message)
  47. print('Use "%s -l" to get a list of valid converters.' % (
  48. parser.prog,))
  49. print('')
  50. parser.print_help()
  51. sys.exit(1)
  52. any_failed = False
  53. def changed(c):
  54. if c.status == 'failed':
  55. any_failed = True
  56. if options.json:
  57. output = {
  58. 'filename': c.video.filename,
  59. 'output': c.output,
  60. 'status': c.status,
  61. 'duration': c.duration,
  62. 'progress': c.progress,
  63. 'percent': (c.progress_percent * 100 if c.progress_percent
  64. else 0),
  65. }
  66. if c.error is not None:
  67. output['error'] = c.error
  68. print(json.dumps(output))
  69. else:
  70. if c.status == 'initialized':
  71. line = 'starting (output: %s)' % (c.output,)
  72. elif c.status == 'converting':
  73. if c.progress_percent is not None:
  74. line = 'converting (%i%% complete, %is remaining)' % (
  75. c.progress_percent * 100, c.eta)
  76. else:
  77. line = 'converting (0% complete, unknown remaining)'
  78. elif c.status == 'staging':
  79. line = 'staging'
  80. elif c.status == 'failed':
  81. line = 'failed (error: %r)' % (c.error,)
  82. elif c.status == 'finished':
  83. line = 'finished (output: %s)' % (c.output,)
  84. else:
  85. line = c.status
  86. print('%s: %s' % (c.video.filename, line))
  87. for filename in args:
  88. try:
  89. c = self.start_conversion(filename, options.converter)
  90. except ValueError:
  91. message = 'could not parse %r' % filename
  92. if options.json:
  93. any_failed = True
  94. print(json.dumps({'status': 'failed', 'error': message,
  95. 'filename': filename}))
  96. else:
  97. print('ERROR:', message)
  98. continue
  99. changed(c)
  100. c.listen(changed)
  101. # XXX real mainloop
  102. while self.conversion_manager.running:
  103. self.conversion_manager.check_notifications()
  104. time.sleep(1)
  105. self.conversion_manager.check_notifications() # one last time
  106. sys.exit(0 if not any_failed else 1)
  107. if __name__ == "__main__":
  108. initialize(None)
  109. app.widgetapp = Application()
  110. app.widgetapp.startup()
  111. app.widgetapp.run()