From 17bb144fcd60c98769bd972f271116666202e535 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jaime=20Marqui=CC=81nez=20Ferra=CC=81ndiz?= Date: Sat, 7 Mar 2015 15:35:49 +0100 Subject: [PATCH] Add a simple command line interface Can be run as 'pycaption' or by calling 'python -m pycaption' --- pycaption/__main__.py | 4 ++++ pycaption/cli.py | 45 +++++++++++++++++++++++++++++++++++++++++++ setup.py | 5 +++++ 3 files changed, 54 insertions(+) create mode 100644 pycaption/__main__.py create mode 100644 pycaption/cli.py diff --git a/pycaption/__main__.py b/pycaption/__main__.py new file mode 100644 index 00000000..71b440f4 --- /dev/null +++ b/pycaption/__main__.py @@ -0,0 +1,4 @@ +from .cli import main + +if __name__ == '__main__': + main() diff --git a/pycaption/cli.py b/pycaption/cli.py new file mode 100644 index 00000000..ffd5656f --- /dev/null +++ b/pycaption/cli.py @@ -0,0 +1,45 @@ +import argparse + +from . import ( + CaptionConverter, + detect_format, + DFXPWriter, + SAMIWriter, + SCCWriter, + SRTWriter, + WebVTTWriter, +) + + +def main(): + writers = { + 'dfxp': DFXPWriter, + 'sami': SAMIWriter, + 'scc': SCCWriter, + 'srt': SRTWriter, + 'ttml': DFXPWriter, + 'vtt': WebVTTWriter, + 'webvtt': WebVTTWriter, + } + parser = argparse.ArgumentParser( + 'pycaption', description='Convert captions to different formats') + + parser.add_argument( + 'input', type=argparse.FileType('r'), metavar='FILE', + help='Input file') + parser.add_argument( + '--output', '-o', type=argparse.FileType('w'), default='-', + metavar='FILE', help='Output file (default is stdout)') + parser.add_argument( + '--to', '-t', choices=writers.keys(), default='srt', metavar='FORMAT', + help='Output format, one of: %(choices)s. Default is "%(default)s"') + args = parser.parse_args() + + caps = args.input.read().decode() + reader = detect_format(caps) + if reader is None: + parser.exit(1, 'Input format not supported\n') + writer = writers[args.to] + converter = CaptionConverter() + converter.read(caps, reader()) + args.output.write(converter.write(writer())) diff --git a/setup.py b/setup.py index 8eb00208..dcc6d22f 100644 --- a/setup.py +++ b/setup.py @@ -23,6 +23,11 @@ install_requires=dependencies, packages=find_packages(), include_package_data=True, + entry_points={ + 'console_scripts': [ + 'pycaption = pycaption.cli:main', + ], + }, classifiers=[ 'Topic :: Multimedia :: Video', ],