Skip to content

Openverse is a search engine for openly-licensed media. This monorepo includes all application code.

Use Python 3.10 everywhere (#656)

sarayourfriend1d407e813db5
diff --git a/.github/workflows/ci_cd.yml b/.github/workflows/ci_cd.yml
index be26648ff..026a1d7ba 100644
--- a/.github/workflows/ci_cd.yml
+++ b/.github/workflows/ci_cd.yml
@@ -18,16 +18,24 @@ jobs:
steps:
- name: Checkout repository
uses: actions/checkout@v2
+
- uses: extractions/setup-just@v1
+
+ - name: Get Python version
+ id: python-version
+ run: echo "::set-output name=value::$(just py-version)"
+
- name: Setup Python
uses: actions/setup-python@v2
with:
- python-version: "3.9"
+ python-version: "${{ steps.python-version.outputs.value }}"
+
- name: Cache pre-commit envs
uses: actions/cache@v2
with:
path: ~/.cache/pre-commit
key: ${{ runner.os }}-pre-commit-${{ hashFiles('.pre-commit-config.yaml') }}
+
- name: Run pre-commit to lint files
run: |
pip install pre-commit
@@ -40,6 +48,12 @@ jobs:
- name: Checkout repository
uses: actions/checkout@v2
+ - uses: extractions/setup-just@v1
+
+ - name: Get Python version
+ id: python-version
+ run: echo "::set-output name=value::$(just py-version)"
+
- name: Setup Docker Buildx
uses: docker/setup-buildx-action@v1
with:
@@ -54,6 +68,8 @@ jobs:
cache-from: type=gha,scope=catalog
cache-to: type=gha,scope=catalog
outputs: type=docker,dest=/tmp/catalog.tar
+ build-args: |
+ PROJECT_PY_VERSION=${{ steps.python-version.outputs.value }}
- name: Upload image
uses: actions/upload-artifact@v2
diff --git a/docker-compose.override.yml b/docker-compose.override.yml
index 537e67176..caca2b483 100644
--- a/docker-compose.override.yml
+++ b/docker-compose.override.yml
@@ -55,6 +55,7 @@ services:
context: .
args:
- REQUIREMENTS_FILE=requirements_dev.txt
+ - PROJECT_PY_VERSION=${PROJECT_PY_VERSION}
dockerfile: docker/airflow/Dockerfile
restart: on-failure
# This command ensures an admin account is created on startup
diff --git a/docker/airflow/Dockerfile b/docker/airflow/Dockerfile
index 12aee757b..4eeacc5e6 100644
--- a/docker/airflow/Dockerfile
+++ b/docker/airflow/Dockerfile
@@ -1,4 +1,8 @@
-FROM python:3.10-slim
+# Cannot use $PYTHON_VERSION because that is overwritten by the base image
+# with the patch version (we only care about major and minor version)
+ARG PROJECT_PY_VERSION
+
+FROM python:${PROJECT_PY_VERSION}-slim
# Build-time arguments, with sensible defaults
ARG REQUIREMENTS_FILE=requirements_prod.txt
@@ -45,8 +49,11 @@ WORKDIR ${AIRFLOW_HOME}
COPY ${REQUIREMENTS_FILE} requirements_prod.txt ${AIRFLOW_HOME}/
COPY docker/airflow/wait_for_db.py /opt/airflow/
+# args go out of scope when a new build stage starts so it must be redeclared here
+ARG PROJECT_PY_VERSION
+
# https://airflow.apache.org/docs/apache-airflow/stable/installation/installing-from-pypi.html#constraints-files
-ARG CONSTRAINTS_FILE="https://raw.githubusercontent.com/apache/airflow/constraints-${AIRFLOW_VERSION}/constraints-3.10.txt"
+ARG CONSTRAINTS_FILE="https://raw.githubusercontent.com/apache/airflow/constraints-${AIRFLOW_VERSION}/constraints-${PROJECT_PY_VERSION}.txt"
RUN pip install --user -r ${REQUIREMENTS_FILE} -c ${CONSTRAINTS_FILE}
diff --git a/justfile b/justfile
index e239f3a7d..57bf3dbce 100644
--- a/justfile
+++ b/justfile
@@ -10,9 +10,24 @@ DOCKER_FILES := "--file=docker-compose.yml" + (
)
SERVICE := "webserver"
+export PROJECT_PY_VERSION := `grep '# PYTHON' requirements_prod.txt | awk -F= '{print $2}'`
+
+# Print the required Python version
+@py-version:
+ echo $PROJECT_PY_VERSION
+
+# Check the installed Python version matches the required Python version and fail if not
+check-py-version:
+ #! /usr/bin/env sh
+ installed_python_version=`python -c 'import sys; print(f"{sys.version_info[0]}.{sys.version_info[1]}")'`
+ if [ "$PROJECT_PY_VERSION" != "$installed_python_version" ]
+ then
+ printf "Detected Python version $installed_python_version but $PROJECT_PY_VERSION is required.\n" > /dev/stderr
+ exit 1
+ fi
# Install dependencies into the current environment
-install:
+install: check-py-version
pip install -r requirements.txt -r requirements_dev.txt
pre-commit install
diff --git a/openverse_catalog/templates/README.md b/openverse_catalog/templates/README.md
deleted file mode 100644
index 4290686ac..000000000
--- a/openverse_catalog/templates/README.md
+++ /dev/null
@@ -1,26 +0,0 @@
-## Adding new provider API script
-
-Openverse Catalog uses APIs of sites that share openly-licensed media to collect the data about the media and save it to the database.
-We call the scripts that pull the data from these APIs "Provider API scripts".
-You can find examples in [`provider_api_scripts` folder](../dags/providers/provider_api_scripts).
-
-To add a Provider API script using this template, you will need to have Python 3 installed on your machine (preferably, version 3.9). You will also need to know the name of provider, and the type of media you are going to collect (`image` or `audio`).
-
-To add a script for collecting audio data from provider named "MyProvider", open your terminal and run
-```bash
-python3 openverse_catalog/templates/create_api_script.py MyProvider -m audio
-```
-You should see output similar to this:
-```bash
-Creating files in /path/to/openverse-catalog
-API script: openverse-catalog/openverse_catalog/dags/provider_api_scripts/myprovider.py
-Airflow DAG: openverse-catalog/openverse_catalog/dags/myprovider_workflow.py
-API script test: openverse-catalog/tests/dags/provider_api_scripts/test_myprovider.py
-
-```
-The following files have been created:
-1. Airflow workflow file. You will probably NOT need to edit it.
-2. `myprovider.py` script. This is a template that simplifies creating an API provider script by providing the basic structure. The scripts use small and easily-testable functions. Follow the instructions within the script comments, and complete all the TODOs. Make sure to look at sample `.json` files that will be saved for testing.
-3. `test_myprovider.py`. This is a skeleton for your tests. Write tests for the functions in your Provider API script, using the `json` files with sample API responses.
-
-**NOTE**: You will also need to add the Airflow workflow file to the `WORKFLOWS` list in the [DAG parsing test](../../tests/dags/test_dag_parsing.py).
diff --git a/openverse_catalog/templates/__init__.py b/openverse_catalog/templates/__init__.py
deleted file mode 100644
index e69de29bb..000000000
diff --git a/openverse_catalog/templates/create_api_script.py b/openverse_catalog/templates/create_api_script.py
deleted file mode 100644
index 4f22177f5..000000000
--- a/openverse_catalog/templates/create_api_script.py
+++ /dev/null
@@ -1,117 +0,0 @@
-import argparse
-from pathlib import Path
-
-
-IMAGE_STORE_INIT = "image_store = ImageStore(provider=PROVIDER)"
-AUDIO_STORE_INIT = "audio_store = AudioStore(provider=PROVIDER)"
-TEMPLATES_PATH = Path(__file__).parent
-REPO_PATH = TEMPLATES_PATH.parents[1]
-PROJECT_PATH = REPO_PATH.parent
-
-
-def _get_filled_template(template_path: Path, provider: str, media_type: str):
- with template_path.open("r", encoding="utf-8") as template:
- template_string = template.read()
- script_string = (
- template_string.replace("{provider_title_case}", provider.title())
- .replace("{provider_upper_case}", provider.upper())
- .replace("{provider}", provider.lower())
- )
- if media_type == "audio":
- media_store_init = AUDIO_STORE_INIT
- media_store = "audio_store"
- else:
- media_store_init = IMAGE_STORE_INIT
- media_store = "image_store"
- script_string = (
- script_string.replace("media_store_init", media_store_init)
- .replace("{media_store}", media_store)
- .replace("{media_type}", media_type)
- )
-
- return script_string
-
-
-def _render_file(
- target: Path,
- template_path: Path,
- provider: str,
- media_type: str,
- name: str,
-):
- with target.open("w", encoding="utf-8") as target_file:
- filled_template = _get_filled_template(template_path, provider, media_type)
- target_file.write(filled_template)
- print(f"{name + ':':<18} {target.relative_to(PROJECT_PATH)}")
-
-
-def fill_template(provider, media_type):
- print(f"Creating files in {REPO_PATH}")
-
- dags_path = TEMPLATES_PATH.parent / "dags" / "providers"
- api_path = dags_path / "provider_api_scripts"
- filename = provider.replace(" ", "_").lower()
-
- # Render the API file itself
- script_template_path = TEMPLATES_PATH / "template_provider.py_template"
- api_script_path = api_path / f"{filename}.py"
- _render_file(
- api_script_path, script_template_path, provider, media_type, "API script"
- )
-
- # Render the DAG workflow
- workflow_template_path = TEMPLATES_PATH / "workflow.py_template"
- workflow_path = dags_path / f"{filename}_workflow.py"
- _render_file(
- workflow_path,
- workflow_template_path,
- provider,
- media_type,
- "Airflow DAG",
- )
-
- # Render the tests
- script_template_path = TEMPLATES_PATH / "template_test.py_template"
- tests_path = REPO_PATH / "tests"
- # Mirror the directory structure, but under the "tests" top level directory
- test_script_path = tests_path.joinpath(*api_path.parts[-3:]) / f"test_{filename}.py"
- _render_file(
- test_script_path, script_template_path, provider, media_type, "API script test"
- )
-
- print(
- """
-NOTE: You will also need to add the Airflow workflow file to the WORKFLOWS list in the \
-DAG parsing test file (openverse-catalog/tests/dags/test_dag_parsing.py).
-"""
- )
-
-
-def main():
- parser = argparse.ArgumentParser(
- description="Create a new provider API script",
- add_help=True,
- )
- parser.add_argument(
- "provider", help='Create the script for this provider (eg. "Wikimedia").'
- )
- parser.add_argument(
- "-m",
- "--media",
- type=str,
- choices=["image", "audio"],
- help="Script will collect media of this type"
- " ('audio'/'image'). Default value is 'image'",
- )
- args = parser.parse_args()
- provider = args.provider
- media_type = args.media
- if media_type not in ["audio", "image"]:
- print("No media type given, assuming it's `image`")
- media_type = "image"
-
- fill_template(provider, media_type)
-
-
-if __name__ == "__main__":
- main()
diff --git a/openverse_catalog/templates/template_provider.py_template b/openverse_catalog/templates/template_provider.py_template
deleted file mode 100644
index 5b6f7ca29..000000000
--- a/openverse_catalog/templates/template_provider.py_template
+++ /dev/null
@@ -1,393 +0,0 @@
-"""
-Content Provider: {provider_title_case}
-
-ETL Process: Use the API to identify all openly licensed media.
-
-Output: TSV file containing the media metadata.
-
-Notes: {{API URL}}
- No rate limit specified.
-"""
-import json
-import os
-import logging
-from pathlib import Path
-from urllib.parse import urlparse
-
-from common.licenses import get_license_info, get_license_info_from_license_pair
-from common.loader import provider_details as prov
-from common.requester import DelayedRequester
-from common.storage.image import ImageStore
-from common.storage.audio import AudioStore
-
-"""
-This is template for an API script. Broadly, there are several steps:
-1. Download batches of information for the query for openly-licensed media
-2. For each item in batch, extract the necessary meta data.
-3. Save the metadata using ImageStore.add_item or AudioStore.add_item methods
-
-Try to write small functions that are easier to test. Don't forget to
-write tests, too!
-
-You can test your script during development by running it:
-`python -m <your_script_name>.py`
-
-To extract information from html, you can use lxml.html.
-Recommended examples:
-- For scripts requiring a start date: `wikimedia_commons.py`
-- For scripts without a start date: `science_museum.py`.
-
-ImageStore/AudioStore are the classes that clean the media metadata
-and save it to the disk in form of a tsv file.
-They will save the file to '/tmp' folder on your computer when you run
-this script as is
-"""
-
-logging.basicConfig(
- format='%(asctime)s - %(name)s - %(levelname)s: %(message)s',
- level=logging.INFO
-)
-logger = logging.getLogger(__name__)
-
-LIMIT = 0 # number of items per page in API response
-DELAY = 1 # in seconds
-RETRIES = 3
-HOST = '{{host URL}}'
-ENDPOINT = f'https://{HOST}/{{API PATH}}'
-# TODO: Add Provider constant to the
-# `openverse_catalog/dags/util/loader/provider_details.py` file
-PROVIDER = prov.{provider_upper_case}_DEFAULT_PROVIDER
-# TODO: Add the API key to `openverse_catalog/env.template`
-# Do not hardcode your API key value!
-API_KEY = os.getenv("{provider_upper_case}", "nokeyprovided")
-
-# TODO: Add any headers necessary for API request
-HEADERS = {
- "Accept": "application/json",
- "api_key": API_KEY,
-}
-# TODO: Add parameters that are necessary for each API request
-DEFAULT_QUERY_PARAMS = {
- 'format': 'json',
- 'license': 'open-cc-licenses',
-}
-
-delayed_requester = DelayedRequester(DELAY)
-media_store_init
-
-
-saved_json_counter = {
- 'full_response': 0,
- 'empty_response': 0,
- 'full_item': 0,
- 'no_{media_type}_url': 0,
- 'no_foreign_landing_url': 0,
- 'no_license': 0,
-}
-
-
-def check_and_save_json_for_test(name, data):
- parent = Path(__file__).parent
- test_resources_path = parent / 'tests' / 'resources' / '{provider}'
- if not Path.is_dir(test_resources_path):
- Path.mkdir(test_resources_path)
- if saved_json_counter[name] == 0:
- with open(f"{name}.json", "w+", encoding="utf-8") as outf:
- json.dump(data, outf, indent=2)
- saved_json_counter[name] += 1
-
-
-# TODO: Date parameter is necessary for providers with a lot of
-# content. For others, we simply ingest all of the content
-# every time.
-def main():
- """
- This script pulls the data for a given date from the {provider_title_case},
- and writes it into a .TSV file to be eventually read
- into our DB.
-
- {{ TODO: Remove if date is not used }}
- Required Arguments:
-
- date: Date String in the form YYYY-MM-DD. This is the date for
- which running the script will pull data. }}
- """
-
- logger.info("Begin: {provider_title_case} script")
- {media_type}_count = _get_items()
- {media_type}_store.commit()
- logger.info(f"Total {media_type}s pulled: {{media_type}_count}")
- logger.info('Terminated!')
-
-
-def _get_query_params(
- page_number=0,
- default_query_params=None,
-):
- if default_query_params is None:
- default_query_params = DEFAULT_QUERY_PARAMS
- query_params = default_query_params.copy()
- query_params["page"] = str(page_number)
- return query_params
-
-
-def _get_items():
- item_count = 0
- page_number = 0
- should_continue = True
- while should_continue:
- query_params = _get_query_params(page_number=page_number)
-
- batch_data = _get_batch_json(
- query_params=query_params
- )
- if isinstance(batch_data, list) and len(batch_data) > 0:
- item_count = _process_item_batch(batch_data)
- page_number += 1
- else:
- should_continue = False
- return item_count
-
-
-def _get_batch_json(
- endpoint=ENDPOINT,
- headers=None,
- retries=RETRIES,
- query_params=None
-):
- if headers is None:
- headers = HEADERS
- response_json = delayed_requester.get_response_json(
- endpoint,
- retries,
- query_params,
- headers=headers
- )
- if response_json is None:
- return None
- else:
- data = response_json.get("data")
- if data:
- check_and_save_json_for_test('full_response', data)
- else:
- check_and_save_json_for_test('empty_response', data)
- return data
-
-
-def _process_item_batch(items_batch):
- for item in items_batch:
- # For testing purposes, you would need to save json data for single
- # media objects. To make sure that you test edge cases,
- # we add the code that saves a json file per each condition:
- # full, and without one of the required properties.
- # TODO: save the resulting json files (if any) in the
- # `provider_api_scripts/tests/resources/<provider_name>` folder
- # TODO: remove the code for saving json files from the final script
-
- item_meta_data = _extract_item_data(item)
- if item_meta_data is None:
- continue
- {media_store}.add_item(**item_meta_data)
- return {media_store}.total_items
-
-
-def _extract_item_data(media_data):
- """
- Extract data for individual item
- You can view the documentation about media parameters in
- `AudioStore.add_item` or `ImageStore.add_item` method
- docstrings.
- Required properties:
- - foreign_landing_url
- - image_url / audio_url
- - item_license
-
- Optional properties:
- - foreign_identifier
- - title
- - creator
- - creator_url
- - thumbnail_url
- - metadata
- - tags
- - watermarked (false by default)
-
- Optional properties for images:
- - width
- - height
-
- Optional properties for audio:
- - duration
- - bit_rate
- - sample_rate
- - category
- - genre (list of genres)
- - audio_set (JSON field)
- - alternative audio fields
- """
- # TODO: remove the code for saving json files from the final script
-
- try:
- foreign_landing_url = media_data["links"][0]["url"]
- except (TypeError, KeyError, AttributeError):
- print(f"Found no foreign landing url:")
- print(f"{json.dumps(media_data, indent=2)}")
- check_and_save_json_for_test('no_foreign_landing_url', media_data)
- return None
- # TODO: Choose correct line for media type and delete the other:
- # TODO: Delete the unnecessary _get_media_info function
- # audio_url, duration = _get_audio_info(media_data)
- # image_url, height, width = _get_image_info(media_data)
- if {media_type}_url is None:
- print(f"Found no media url:")
- print(f"{json.dumps(media_data, indent=2)}")
- check_and_save_json_for_test('no_{media_type}_url', media_data)
- return None
- item_license = _get_license(media_data)
- if item_license is None:
- print(f"Found no item license:")
- print(f"{json.dumps(media_data, indent=2)}")
- check_and_save_json_for_test('no_license', media_data)
- return None
- foreign_identifier = _get_foreign_identifier(media_data)
- title = _get_title(media_data)
- creator, creator_url = _get_creator_data(media_data)
- thumbnail = _get_thumbnail_url(media_data)
- metadata = _get_metadata(media_data)
- tags = _get_tags(media_data)
- check_and_save_json_for_test('full_item', media_data)
-
- return {
- 'title': title,
- 'creator': creator,
- 'creator_url': creator_url,
- 'foreign_identifier': foreign_identifier,
- 'foreign_landing_url': foreign_landing_url,
- '{media_type}_url': {media_type}_url,
- 'height': height,
- 'width': width,
- 'thumbnail_url': thumbnail,
- 'license_': item_license.license,
- 'license_version': item_license.version,
- 'meta_data': metadata,
- 'raw_tags': tags
- }
-
-
-def _get_foreign_identifier(media_data):
- try:
- return media_data['some_key'][0]['uid']
- except(TypeError, IndexError, AttributeError):
- return None
-
-
-def _get_image_info(media_data):
- width = media_data.get('width')
- height = media_data.get('height')
- image_url = media_data.get('image_url')
- return image_url, width, height
-
-def _get_audio_info(media_data):
- duration = media_data.get('duration')
- audio_url = media_data.get('audio_url')
- return audio_url, duration
-
-
-def _get_thumbnail_url(media_data):
- # TODO: Add correct implementation of _get_thumbnail_url
- return media_data.get('thumbnail', {}).get('url', None)
-
-
-def _get_creator_data(item):
- # TODO: Add correct implementation of _get_creator_data
- creator = item.get('creator_key').strip()
- creator_url = _cleanse_url(
- item.get('creator_key', {}).get('url')
- )
- return creator, creator_url
-
-
-def _get_title(item):
- # TODO: Add correct implementation of _get_title
- title = item.get('title')
- return title
-
-
-def _get_metadata(item):
- """
- Metadata may include: description, date created and modified at source,
- categories, popularity statistics.
- """
- # TODO: Add function to extract metadata from the item dictionary
- # Do not includes keys without value
- metadata = {}
- some_other_key_value = item.get('some_other_key')
- if some_other_key_value is not None:
- metadata['some_other_key'] = some_other_key_value
- return metadata
-
-
-def _get_tags(item):
- # TODO: Add correct implementation of _get_tags
- return item.get('tags')
-
-
-def _get_license(item):
- """
- To parse the item license, use `get_license_info` function. It
- returns a namedtuple LicenseInfo(license_url, license, version)
-
- It requires either:
- 1) a`license_url` (eg. `https://creativecommons.org/licenses/by/4.0/`)
- or
- 2)`license_name` and `license_version`
-
- `license_name` can be one of the following:
- [ 'by', 'by-nc', 'by-nc-nd', 'by-nc-sa', 'by-nd', 'by-sa',
- 'devnations', 'sampling', 'sampling+',
- 'publicdomain', 'pdm', 'cc0' ]
-
- To view all possible licenses, look at licenses/constants.
- To validate that the license_name and license_version you get
- are correct, you can use `get_license_info_from_license_pair(
- license_name, license_version)`
- """
- # TODO: add correct implementation of _get_license
- # If the provider gives license url:
- item_license_url = item.get('license_url')
- item_license = get_license_info(license_url=item_license_url)
-
- # If the provider gives license name and license version
- # Note: `publicdomain` does not have version, pass 'N/A' instead
- item_license_name = item.get('license_name')
- item_license_version = item.get('license_version')
- item_license = get_license_info(
- license_=item_license_name,
- license_version=item_license_version
- )
- if item_license.license is None:
- return None
- return item_license
-
-
-def _cleanse_url(url_string):
- """
- Check to make sure that a url is valid, and prepend a protocol if needed
- """
-
- parse_result = urlparse(url_string)
-
- if parse_result.netloc == HOST:
- parse_result = urlparse(url_string, scheme='https')
- elif not parse_result.scheme:
- parse_result = urlparse(url_string, scheme='http')
-
- if parse_result.netloc or parse_result.path:
- return parse_result.geturl()
-
-
-if __name__ == '__main__':
- main()
-
-# TODO: Remove unnecessary comments
-# TODO: Lint your code with pycodestyle
diff --git a/openverse_catalog/templates/template_test.py_template b/openverse_catalog/templates/template_test.py_template
deleted file mode 100644
index 9aff6c209..000000000
--- a/openverse_catalog/templates/template_test.py_template
+++ /dev/null
@@ -1,172 +0,0 @@
-# TODO: Test the small functions you created,
-# trying to find different edge cases (missing keys,
-# different data types returned, Nones, etc),
-# especially the ones found in the `json` files
-# with API responses.
-
-
-# Mock the functions that require internet access,
-# such as url verification, so that the tests can run
-# faster, and even offline.
-import json
-import logging
-from pathlib import Path
-from unittest.mock import patch
-
-from providers.provider_api_scripts import {provider}
-
-RESOURCES = Path(__file__).parent / 'tests/resources/{provider}'
-SAMPLE_MEDIA_DATA = RESOURCES / '{media_type}_data_example.json'
-
-logging.basicConfig(
- format='%(asctime)s - %(name)s - %(levelname)s: %(message)s',
- level=logging.DEBUG,
-)
-
-
-def test_get_{media_type}_pages_returns_correctly_with_none_json():
- expect_result = None
- with patch.object(
- {provider}.delayed_requester,
- 'get_response_json',
- return_value=None
- ):
- actual_result = {provider}._get_batch_json()
- assert actual_result == expect_result
-
-
-def test_get_{media_type}_pages_returns_correctly_with_no_results():
- expect_result = None
- with patch.object(
- {provider}.delayed_requester,
- 'get_response_json',
- return_value={}
- ):
- actual_result = {provider}._get_batch_json()
- assert actual_result == expect_result
-
-
-def test_get_query_params_adds_offset():
- actual_qp = {provider}._get_query_params(
- offset=200
- )
- expected_qp = {'offset': 200}
- assert actual_qp['offset'] == expected_qp['offset']
-
-
-def test_get_query_params_leaves_other_keys():
- actual_qp = {provider}._get_query_params(
- offset=200, default_query_params={'test': 'value'}
- )
- assert actual_qp['test'] == 'value'
- assert len(actual_qp.keys()) == 2
-
-
-def test_get_items():
- with open(RESOURCES / 'page1.json') as f:
- first_response = json.load(f)
- with patch.object(
- {provider},
- '_get_batch_json',
- side_effect=[first_response, []]
- ):
- expected_{media_type}_count = 3
- actual_{media_type}_count = {provider}._get_items()
- assert expected_{media_type}_count == actual_{media_type}_count
-
-
-def test_process_item_batch_handles_example_batch():
- with open(SAMPLE_MEDIA_DATA) as f:
- items_batch = [json.load(f)]
- with patch.object(
- {provider}.{media_type}_store,
- 'add_item',
- return_value=1
- ) as mock_add:
- {provider}._process_item_batch(items_batch)
- mock_add.assert_called_once()
- _, actual_call_args = mock_add.call_args_list[0]
- expected_call_args = {
- }
- assert actual_call_args == expected_call_args
-
-
-def test_extract_{media_type}_data_returns_none_when_media_data_none():
- actual_{media_type}_info = {provider}._extract_item_data(None)
- expected_{media_type}_info = None
- assert actual_{media_type}_info is expected_{media_type}_info
-
-
-def test_extract_{media_type}_data_returns_none_when_no_foreign_id():
- with open(SAMPLE_MEDIA_DATA) as f:
- {media_type}_data = json.load(f)
- {media_type}_data.pop('foreign_id', None)
- actual_{media_type}_info = {provider}._extract_item_data({media_type}_data)
- expected_{media_type}_info = None
- assert actual_{media_type}_info is expected_{media_type}_info
-
-
-def test_extract_{media_type}_data_returns_none_when_no_{media_type}_url():
- with open(SAMPLE_MEDIA_DATA) as f:
- {media_type}_data = json.load(f)
- {media_type}_data.pop('{media_type}_url', None)
- actual_{media_type}_info = {provider}._extract_item_data({media_type}_data)
- assert actual_{media_type}_info is None
-
-
-def test_extract_{media_type}_data_returns_none_when_no_license():
- with open(SAMPLE_MEDIA_DATA) as f:
- {media_type}_data = json.load(f)
- {media_type}_data.pop('license_url', None)
- actual_{media_type}_info = {provider}._extract_item_data({media_type}_data)
- assert actual_{media_type}_info is None
-
-
-def test_get_creator_data():
- with open(SAMPLE_MEDIA_DATA) as f:
- {media_type}_data = json.load(f)
- actual_creator, actual_creator_url = {provider}._get_creator_data({media_type}_data)
- expected_creator = ''
- expected_creator_url = ''
-
- assert actual_creator == expected_creator
- assert actual_creator_url == expected_creator_url
-
-
-def test_get_creator_data_handles_no_url():
- with open(SAMPLE_MEDIA_DATA) as f:
- {media_type}_data = json.load(f)
- {media_type}_data.pop('artist_url', None)
- expected_creator = ''
-
- actual_creator, actual_creator_url = {provider}._get_creator_data({media_type}_data)
- assert actual_creator == expected_creator
- assert actual_creator_url is None
-
-
-def test_get_creator_data_returns_none_when_no_artist():
- with open(SAMPLE_MEDIA_DATA) as f:
- {media_type}_data = json.load(f)
- {media_type}_data.pop('artist_name', None)
- actual_creator, actual_creator_url = {provider}._get_creator_data({media_type}_data)
-
- assert actual_creator is None
- assert actual_creator_url is None
-
-
-def test_extract_{media_type}_data_handles_example_dict():
- with open(SAMPLE_MEDIA_DATA) as f:
- {media_type}_data = json.load(f)
-
- actual_{media_type}_info = {provider}._extract_item_data({media_type}_data)
- expected_{media_type}_info = {}
- assert actual_{media_type}_info == expected_{media_type}_info
-
-
-def test_get_tags():
- item_data = {
- "tags": ['tag1', 'tag2']
- }
- expected_tags = ['tag1', 'tag2']
- actual_tags = {provider}._get_tags(item_data)
- assert expected_tags == actual_tags
diff --git a/openverse_catalog/templates/workflow.py_template b/openverse_catalog/templates/workflow.py_template
deleted file mode 100644
index abe1134cf..000000000
--- a/openverse_catalog/templates/workflow.py_template
+++ /dev/null
@@ -1,26 +0,0 @@
-"""
-This file configures the Apache Airflow DAG to (re)ingest {provider_title_case} data.
-"""
-# airflow DAG (necessary for Airflow to find this file)
-from datetime import datetime
-import logging
-
-from provider_api_scripts import {provider}
-from providers.provider_dag_factory import create_provider_api_workflow
-
-
-logging.basicConfig(
- format='%(asctime)s: [%(levelname)s - DAG Loader] %(message)s',
- level=logging.DEBUG)
-logger = logging.getLogger(__name__)
-
-DAG_ID = "{provider}_workflow"
-
-globals()[DAG_ID] = create_provider_api_workflow(
- DAG_ID,
- {provider}.main,
- start_date=datetime(1970, 1, 1),
- max_active_tasks=1,
- schedule_string='@monthly',
- dated=False,
-)
diff --git a/requirements_prod.txt b/requirements_prod.txt
index bfb8a6231..f753eec98 100644
--- a/requirements_prod.txt
+++ b/requirements_prod.txt
@@ -1,3 +1,4 @@
+# PYTHON=3.10
apache-airflow[amazon,postgres,http]==2.3.3
lxml
psycopg2-binary
diff --git a/tests/templates/test_create_api_script.py b/tests/templates/test_create_api_script.py
deleted file mode 100644
index b860af46e..000000000
--- a/tests/templates/test_create_api_script.py
+++ /dev/null
@@ -1,28 +0,0 @@
-from pathlib import Path
-
-from openverse_catalog.templates import create_api_script
-
-
-def test_files_created():
- provider = "Foobar Industries"
- media_type = "image"
- dags_path = create_api_script.TEMPLATES_PATH.parent / "dags" / "providers"
- expected_provider = dags_path / "provider_api_scripts" / "foobar_industries.py"
- expected_workflow = dags_path / "foobar_industries_workflow.py"
- expected_test = (
- Path(__file__).parents[1]
- / "dags"
- / "providers"
- / "provider_api_scripts"
- / "test_foobar_industries.py"
- )
- try:
- create_api_script.fill_template(provider, media_type)
- assert expected_provider.exists()
- assert expected_workflow.exists()
- assert expected_test.exists()
- finally:
- # Clean up
- expected_provider.unlink(missing_ok=True)
- expected_workflow.unlink(missing_ok=True)
- expected_test.unlink(missing_ok=True)