From d431d6aa0d42ebf2b35999ad1923c34c26e90868 Mon Sep 17 00:00:00 2001 From: zevav Date: Wed, 20 Feb 2019 11:12:45 -0500 Subject: [PATCH] made STT_SERVICES dynamic based on vendors.__init__, updated calling code accordingly --- tatt/config.py | 8 -------- tatt/helpers.py | 34 ++++++++++++++++++++-------------- tatt/transcribe.py | 4 ++-- tatt/vendors/__init__.py | 4 ++++ tatt/vendors/amazon.py | 4 ++-- 5 files changed, 28 insertions(+), 26 deletions(-) diff --git a/tatt/config.py b/tatt/config.py index 40d981f..480f891 100644 --- a/tatt/config.py +++ b/tatt/config.py @@ -3,14 +3,6 @@ from pathlib import Path import sqlite3 -STT_SERVICES = { - 'amazon': { - 'cost_per_minute': .024, - 'free': '60_minutes_per_month_for_the_first_12_months', - }, - } - - AWS_BUCKET_NAME_FMTR_MEDIA = 'tatt-media-{}' AWS_BUCKET_NAME_FMTR_TRANSCRIPT = 'tatt-transcript-{}' diff --git a/tatt/helpers.py b/tatt/helpers.py index ccdb8d1..d6edc79 100644 --- a/tatt/helpers.py +++ b/tatt/helpers.py @@ -1,27 +1,33 @@ from tatt import config from tatt import vendors +LB = '\n' +TAB = '\t' + def make_string_all_services(free_only=False): - all_services_string = ( - '\nHere are all the available ' - f'{"free " if free_only else ""}speech-to-text services:' - '\n\n' - '\n'.join(['{}{}{}{}'.format('\t', service_name, '\t\t', + all_services_string_formatter = ( + "Here are all the available {}speech-to-text-services:\n\n" + ) - f'({info["free"].replace("_", " ")})' - if isinstance(info["free"], str) else "" - - ) + if free_only: + all_services_string = all_services_string_formatter.format("free ") + else: + all_services_string = all_services_string_formatter.format("") + + for service_name, module in vendors.STT_SERVICES.items(): + if free_only and module.cost_per_15_seconds > 0: + continue + all_services_string += ( + f'{TAB}{service_name}{TAB}{TAB}${module.cost_per_15_seconds} per 15 seconds' + ) - for service_name, info in - config.STT_SERVICES.items()]) + '\n' - ) return all_services_string def get_service(service_name): - return getattr(getattr(vendors, service_name), config.SERVICE_CLASS_NAME) + module = vendors.STT_SERVICES[service_name] + return getattr(module, config.SERVICE_CLASS_NAME) def print_transcription_jobs(jobs): @@ -49,7 +55,7 @@ def print_transcription_jobs(jobs): def get_transcription_jobs(service_name=None, name=None, status=None): all_jobs = {} - for stt_name, data in config.STT_SERVICES.items(): + for stt_name in vendors.STT_SERVICES: if service_name is None or service_name == stt_name: service = get_service(stt_name) service._setup() # check for AWS credentials and create buckets diff --git a/tatt/transcribe.py b/tatt/transcribe.py index e9ef3bf..b743ec9 100644 --- a/tatt/transcribe.py +++ b/tatt/transcribe.py @@ -39,7 +39,7 @@ def get(name, file): @click.option('--status', type=str, help="completed | failed | in_progress") def list(name, service, status): """Lists available STT services.""" - if service is not None and service not in config.STT_SERVICES: + if service is not None and service not in vendors.STT_SERVICES: raise click.ClickException(f'no such service: {service}') try: @@ -67,7 +67,7 @@ def services(free_only): @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 config.STT_SERVICES: + if service_name not in vendors.STT_SERVICES: print() raise click.ClickException( f'No such service! {print_all_services(print_=False)}') diff --git a/tatt/vendors/__init__.py b/tatt/vendors/__init__.py index 6f7f7cc..3feb8b8 100644 --- a/tatt/vendors/__init__.py +++ b/tatt/vendors/__init__.py @@ -1 +1,5 @@ from tatt.vendors import amazon + +STT_VENDORS = { + 'amazon': amazon, + } diff --git a/tatt/vendors/amazon.py b/tatt/vendors/amazon.py index 8aac6b0..95571c9 100644 --- a/tatt/vendors/amazon.py +++ b/tatt/vendors/amazon.py @@ -12,6 +12,7 @@ from tatt import exceptions NAME = 'amazon' BUCKET_NAME_MEDIA = config.AWS_BUCKET_NAME_FMTR_MEDIA.format(NAME) BUCKET_NAME_TRANSCRIPT = config.AWS_BUCKET_NAME_FMTR_TRANSCRIPT.format(NAME) +cost_per_15_seconds = .024 / 4 def check_for_config(): @@ -30,7 +31,6 @@ class Transcriber: bucket_names = {'media': BUCKET_NAME_MEDIA, 'transcript': BUCKET_NAME_TRANSCRIPT} - service_name = 'amazon' def __init__(self, filepath): self._setup() @@ -62,7 +62,7 @@ class Transcriber: return self._request_transcription() except tr.exceptions.ConflictException: raise exceptions.AlreadyExistsError( - f'{self.basename} already exists on {self.service_name}') + f'{self.basename} already exists on {NAME}') def _upload_file(self): s3.Bucket(self.bucket_names['media']).upload_file(