"""Module for creating final asset filename according to name conventions.""" from assets.constants import asset_legacy FILENAME_TEMPLATE_TRACK = '{upc}_{tuid}' FILENAME_TEMPLATE_TRACK_INCONTENT = '{upc}_{volume_number}_{track_number}' FILENAME_TEMPLATE_TRACK_SEARCH = '{filename}.%' FILENAME_TEMPLATE_IMAGE = '{upc}' def _get_filename_with_extension(filename, asset_type): """Add extension to the filename in depend of asset type. Args: filename (str): Initial filename. asset_type (str): Asset type. Returns: string: filename name with extension or without (if asset type is None) """ if asset_type is None: return filename if asset_type[0] == '.': asset_type = asset_type[1:] return '{filename}.{ext}'.format(filename=filename, ext=asset_type.lower()) def get_track_filename(product, track, asset_type=None): """Return common track asset filename. Args: product (dict): Dictionary with product info. track (dict): Dictionary with track info. asset_type (str): Asset type name (will be used as file extension if it's not None Returns: string: Track filename. """ template = FILENAME_TEMPLATE_TRACK if product.get('status') == asset_legacy.PRODUCT_STATUS_IN_CONTENT: template = FILENAME_TEMPLATE_TRACK_INCONTENT filename = template.format( upc=product.get('upc'), tuid=track.get('tuid'), volume_number=track.get('volume_number'), track_number=track.get('track_number')) return _get_filename_with_extension(filename, asset_type) def get_track_search_filename(product, track): """Return track asset filename pattern for search in the database. Args: product (dict): Dictionary with product info. track (dict): Dictionary with track info. Returns: string: Track filename search pattern. """ filename = get_track_filename(product, track) return FILENAME_TEMPLATE_TRACK_SEARCH.format(filename=filename) def get_image_filename(product, asset_type=None): """Return common image asset filename. Args: product (dict): Dictionary with product info. asset_type (str): Asset type name (will be used as file extension if it's not None Returns: string: Image filename """ filename = FILENAME_TEMPLATE_IMAGE.format(upc=product.get('upc')) return _get_filename_with_extension(filename, asset_type)