diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000..2ff802d --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,22 @@ +name: Run tests + +on: + pull_request: + branches: [master] + +jobs: + tests: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - name: Set up Python 3.8 + uses: actions/setup-python@v2 + with: + python-version: 3.8 + - name: Upgrade pip + run: python -m pip install --upgrade pip setuptools + - name: Install dependencies + run: | + pip install 'tutor[dev]>=16.0.0,<17.0.0' + - name: Test lint, types, and format + run: make test diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..2adc5ab --- /dev/null +++ b/Makefile @@ -0,0 +1,34 @@ +.DEFAULT_GOAL := help +.PHONY: docs +SRC_DIRS = ./tutordiscovery +BLACK_OPTS = --exclude templates ${SRC_DIRS} + +# Warning: These checks are run on every PR. +test: test-lint test-types test-format # Run some static checks. + +test-format: ## Run code formatting tests. + black --check --diff $(BLACK_OPTS) + +test-lint: ## Run code linting tests + pylint --errors-only --enable=unused-import,unused-argument --ignore=templates --ignore=docs/_ext ${SRC_DIRS} + +test-types: ## Run type checks. + mypy --exclude=templates --ignore-missing-imports --implicit-reexport --strict ${SRC_DIRS} + +format: ## Format code automatically. + black $(BLACK_OPTS) + +isort: ## Sort imports. This target is not mandatory because the output may be incompatible with black formatting. Provided for convenience purposes. + isort --skip=templates ${SRC_DIRS} + +changelog-entry: ## Create a new changelog entry. + scriv create + +changelog: ## Collect changelog entries in the CHANGELOG.md file. + scriv collect + +ESCAPE =  +help: ## Print this help. + @grep -E '^([a-zA-Z_-]+:.*?## .*|######* .+)$$' Makefile \ + | sed 's/######* \(.*\)/@ $(ESCAPE)[1;31m\1$(ESCAPE)[0m/g' | tr '@' '\n' \ + | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[33m%-30s\033[0m %s\n", $$1, $$2}' diff --git a/README.rst b/README.rst index d681d2e..0c47cd0 100644 --- a/README.rst +++ b/README.rst @@ -71,7 +71,7 @@ Then run the below command, this command will cause errors every time as it trie tutor local run lms ./manage.py lms cache_programs -If you don't want the errors, then make use of an extra argumet to the command .i.e. ``--domain``. This argument will be equal to ``local.overhang.io`` if you are running tutor local and ``local.overhang.io:8000`` if you are running tutor dev:: +If you don't want the errors, then make use of an extra argument to the command .i.e. ``--domain``. This argument will be equal to ``local.overhang.io`` if you are running tutor local and ``local.overhang.io:8000`` if you are running tutor dev:: tutor local run lms ./manage.py lms cache_programs --domain="local.overhang.io" or tutor dev run lms ./manage.py lms cache_programs --domain="local.overhang.io:8000" diff --git a/changelog.d/20231005_153602_codewithemad.md b/changelog.d/20231005_153602_codewithemad.md new file mode 100644 index 0000000..952a383 --- /dev/null +++ b/changelog.d/20231005_153602_codewithemad.md @@ -0,0 +1 @@ +- [Improvement] Added Typing to code, Makefile and test action to the repository and formatted code with Black and isort. (by @CodeWithEmad) \ No newline at end of file diff --git a/tutordiscovery/__about__.py b/tutordiscovery/__about__.py index d945113..610c111 100644 --- a/tutordiscovery/__about__.py +++ b/tutordiscovery/__about__.py @@ -1,2 +1 @@ __version__ = "16.0.1" - diff --git a/tutordiscovery/plugin.py b/tutordiscovery/plugin.py index a64e904..58b297e 100644 --- a/tutordiscovery/plugin.py +++ b/tutordiscovery/plugin.py @@ -1,10 +1,10 @@ from __future__ import annotations -from glob import glob import os -import pkg_resources import typing as t +from glob import glob +import pkg_resources from tutor import hooks as tutor_hooks from tutor.__about__ import __version_suffix__ @@ -15,15 +15,10 @@ __version__ += "-" + __version_suffix__ HERE = os.path.abspath(os.path.dirname(__file__)) +REPO_NAME = "course-discovery" +APP_NAME = "discovery" - -config = { - "unique": { - "MYSQL_PASSWORD": "{{ 8|random_string }}", - "SECRET_KEY": "{{ 20|random_string }}", - "OAUTH2_SECRET": "{{ 8|random_string }}", - "OAUTH2_SECRET_SSO": "{{ 8|random_string }}", - }, +config: t.Dict[str, t.Dict[str, t.Any]] = { "defaults": { "VERSION": __version__, "DOCKER_IMAGE": "{{ DOCKER_REGISTRY}}overhangio/openedx-discovery:{{ DISCOVERY_VERSION }}", @@ -41,6 +36,12 @@ "REPOSITORY": "https://github.com/openedx/course-discovery.git", "REPOSITORY_VERSION": "{{ OPENEDX_COMMON_VERSION }}", }, + "unique": { + "MYSQL_PASSWORD": "{{ 8|random_string }}", + "SECRET_KEY": "{{ 20|random_string }}", + "OAUTH2_SECRET": "{{ 8|random_string }}", + "OAUTH2_SECRET_SSO": "{{ 8|random_string }}", + }, } # Initialization tasks @@ -86,13 +87,11 @@ ) -REPO_NAME = "course-discovery" -APP_NAME = "discovery" - - # Automount /openedx/discovery folder from the container @tutor_hooks.Filters.COMPOSE_MOUNTS.add() -def _mount_course_discovery(mounts, name): +def _mount_course_discovery( + mounts: list[tuple[str, str]], name: str +) -> list[tuple[str, str]]: if name == REPO_NAME: mounts.append((APP_NAME, "/openedx/discovery")) return mounts @@ -100,7 +99,9 @@ def _mount_course_discovery(mounts, name): # Bind-mount repo at build-time, both for prod and dev images @tutor_hooks.Filters.IMAGES_BUILD_MOUNTS.add() -def _mount_course_discovery_on_build(mounts: list[tuple[str, str]], host_path: str) -> list[tuple[str, str]]: +def _mount_course_discovery_on_build( + mounts: list[tuple[str, str]], host_path: str +) -> list[tuple[str, str]]: path_basename = os.path.basename(host_path) if path_basename == REPO_NAME: mounts.append((APP_NAME, f"{APP_NAME}-src")) @@ -108,7 +109,6 @@ def _mount_course_discovery_on_build(mounts: list[tuple[str, str]], host_path: s return mounts -####### Boilerplate code # Add the "templates" folder as a template root tutor_hooks.Filters.ENV_TEMPLATE_ROOTS.add_item( pkg_resources.resource_filename("tutordiscovery", "templates") @@ -144,7 +144,9 @@ def _mount_course_discovery_on_build(mounts: list[tuple[str, str]], host_path: s @tutor_hooks.Filters.APP_PUBLIC_HOSTS.add() -def _print_discovery_public_hosts(hosts: list[str], context_name: t.Literal["local", "dev"]) -> list[str]: +def _print_discovery_public_hosts( + hosts: list[str], context_name: t.Literal["local", "dev"] +) -> list[str]: if context_name == "dev": hosts += ["{{ DISCOVERY_HOST }}:8381"] else: