From 797f5a3bfe79621a5fd86052d87bcaf4e2ba437e Mon Sep 17 00:00:00 2001 From: zevav Date: Wed, 20 Feb 2019 11:39:16 -0500 Subject: [PATCH] move getting trasncripts logic to helpers --- setup.py | 2 +- tatt/exceptions.py | 7 +++++++ tatt/helpers.py | 17 +++++++++++++-- tatt/transcribe.py | 52 ++++++++++++++++++++-------------------------- 4 files changed, 46 insertions(+), 32 deletions(-) diff --git a/setup.py b/setup.py index 51f0ce8..53d30a4 100644 --- a/setup.py +++ b/setup.py @@ -6,7 +6,7 @@ with open('README.md') as file: setup( name="tatt", - version="0.942", + version="0.943", py_modules=['tatt'], url='https://github.com/zevaverbach/tatt', install_requires=[ diff --git a/tatt/exceptions.py b/tatt/exceptions.py index 4994388..cebdf42 100644 --- a/tatt/exceptions.py +++ b/tatt/exceptions.py @@ -6,3 +6,10 @@ class ConfigError(Exception): class AlreadyExistsError(Exception): pass + +class DoesntExistError(Exception): + pass + + +class NotAvailable(Exception): + pass diff --git a/tatt/helpers.py b/tatt/helpers.py index aa7f4b9..5ba3bbd 100644 --- a/tatt/helpers.py +++ b/tatt/helpers.py @@ -1,5 +1,4 @@ -from tatt import config -from tatt import vendors +from tatt import config, exceptions, vendors LB = '\n' TAB = '\t' @@ -25,6 +24,20 @@ def make_string_all_services(free_only=False): return all_services_string + '\n' +def get_job(job_name): + job = helpers.get_transcription_jobs_dict().get(name) + if not job: + raise exceptions.DoesntExistError + if job['status'].lower() != 'completed': + raise exceptions.NotAvailable(f'transcript status is {job["status"]}') + + +def get_transcript(job_name): + job = get_job(job_name) + service = get_service(job['service_name']) + return service.retrieve_transcript(name) + + def get_service(service_name): module = vendors.SERVICES[service_name] return getattr(module, config.SERVICE_CLASS_NAME) diff --git a/tatt/transcribe.py b/tatt/transcribe.py index 49c7bd5..a081247 100644 --- a/tatt/transcribe.py +++ b/tatt/transcribe.py @@ -14,22 +14,23 @@ def cli(): @cli.command() -@click.option('-f', '--file', is_flag=True, help='save to file') +@click.option('-s', '--save', is_flag=True, help='save to file') @click.argument('name') -def get(name, file): +def get(name, save): """Downloads and/or saves completed transcript.""" - job = helpers.get_transcription_jobs_dict().get(name) - if not job: + try: + transcript = get_transcript(name) + except exceptions.DoesntExistError: raise click.ClickException(f'no such transcript {name}') - if job['status'].lower() != 'completed': - raise click.ClickException(f'transcript status is {job["status"]}') - service = helpers.get_service(job['service_name']) - if not file: - pprint(service.retrieve_transcript(name)) + except exceptions.NotAvailable as e: + raise click.ClickException(str(e)) + + if not save: + click.echo(transcript) else: with open(f'{name}.json', 'w') as fout: - fout.write(json.dumps(service.retrieve_transcript(name))) - print(f'Okay, downloaded {name}.json') + fout.write(json.dumps(transcript)) + click.echo(f'Okay, downloaded {name}.json') @@ -61,34 +62,27 @@ def services(free_only): @cli.command() -@click.option('-d', '--dry-run', is_flag=True, help=( - 'Do a dry run without actually submitting the media file for transcription')) @click.argument('media_filepath', type=str) @click.argument('service_name', type=str) def this(dry_run, media_filepath, service_name): """Sends a media file to be transcribed.""" - if service_name not in vendors.SERVICES: - print() + try: + service = helpers.get_service(service_name) + except KeyError as e: raise click.ClickException( f'No such service! {print_all_services(print_=False)}') - service = helpers.get_service(service_name) try: s = service(media_filepath) except exceptions.ConfigError as e: raise click.ClickException(str(e)) - if dry_run: - print('If this weren\'t a dry run, I would transcribe ' - f'{media_filepath} using {service_name}') - pprint(vars(s)) - else: - print( - f'Okay, transcribing {media_filepath} using {service_name}...') + click.echo( + f'Okay, transcribing {media_filepath} using {service_name}...') - try: - job_num = s.transcribe() - except exceptions.AlreadyExistsError as e: - raise click.ClickException(str(e)) - print(f'Okay, job {job_num} is being transcribed. Use "get" ' - 'command to download it.') + try: + job_num = s.transcribe() + except exceptions.AlreadyExistsError as e: + raise click.ClickException(str(e)) + click.echo(f'Okay, job {job_num} is being transcribed. Use "get" ' + 'command to download it.')