Skip to content

Commit

Permalink
Migrate to importlib and update some dependencies (#189)
Browse files Browse the repository at this point in the history
* Migrate to importlib.

* Update CI
  • Loading branch information
fishinthecalculator committed Jan 28, 2024
1 parent 77a8819 commit e381c1b
Show file tree
Hide file tree
Showing 15 changed files with 219 additions and 228 deletions.
60 changes: 48 additions & 12 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,67 @@ name: CI
# Controls when the workflow will run
on:
pull_request:
paths-ignore:
- 'guix.scm'
- 'manifest.scm'
- 'channels-lock.scm'
- '.envrc'
- '.gitignore'
- 'pre-commit-*.yaml'
- Dockerfile
- README.*
- LICENSE
- 'sample_settings/**'
- 'etc/**'

push:
# Sequence of patterns matched against refs/tags
branches: ["master"]
paths-ignore:
- 'guix.scm'
- 'manifest.scm'
- 'channels-lock.scm'
- '.envrc'
- '.gitignore'
- 'pre-commit-*.yaml'
- Dockerfile
- README.*
- LICENSE
- 'sample_settings/**'
- 'etc/**'

# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
run-tests-dev:
# The type of runner that the job will run on
runs-on: ubuntu-latest

# Steps represent a sequence of tasks that will be executed as part of the job
strategy:
fail-fast: false
matrix:
python-version: ["3.10", "3.11"]
poetry-version: ["1.1.12", "1.7.0"]
os: [ubuntu-latest]
runs-on: ${{ matrix.os }}
steps:
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
- uses: actions/checkout@v2

# Runs a single command using the runners shell
- name: Set up Python 3.10
uses: actions/setup-python@v2
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
- name: Run image
uses: abatilo/actions-poetry@v2
with:
poetry-version: ${{ matrix.poetry-version }}
- name: Setup a local virtual environment
run: |
poetry config virtualenvs.create true --local
poetry config virtualenvs.in-project true --local
- uses: actions/cache@v3
name: Define a cache for the virtual environment based on the dependencies lock file
with:
python-version: "3.10"
path: ./.venv
key: venv-${{ hashFiles('poetry.lock') }}
- name: Install dependencies
run: scripts/install_github_actions_dev_dependencies.sh
- name: Run tests in dev env
run: scripts/run_pipeline_tests.sh
run: scripts/run_pipeline_tests.sh
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ repos:
rev: stable
hooks:
- id: black
language_version: python3.9
language_version: python3.10
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v1.2.3
hooks:
Expand Down
2 changes: 1 addition & 1 deletion channels-lock.scm
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
(name 'guix)
(url "https://git.savannah.gnu.org/git/guix.git")
(commit
"79a3cd34c0318928186a04b6481c4d22c0051d04")
"e7403acb345a59d580607fbfe7ef2aa0c410767a")
(introduction
(make-channel-introduction
"afb9f2752315f131e4ddd44eba02eed403365085"
Expand Down
29 changes: 13 additions & 16 deletions guix.scm
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
(define-module (guix)
#:use-module (guix git-download)
#:use-module (guix build-system python)
#:use-module (guix gexp)
#:use-module (guix packages)
#:use-module (guix utils)
#:use-module (gnu packages databases) ;; for python-tortoise-orm
#:use-module (gnu packages markup) ;; for python-markdownify
#:use-module (gnu packages python)
#:use-module (gnu packages python-web) ;; for python-uvicorn
#:use-module (gnu packages python-xyz) ;; for dynaconf
#:use-module (mobilizon-reshare package)
#:use-module (mobilizon-reshare dependencies)
#:use-module (ice-9 rdelim)
#:use-module (ice-9 popen))
(use-modules (guix git-download)
(guix build-system python)
(guix gexp)
(guix packages)
(guix utils)
(gnu packages markup) ;; for python-markdownify
(mobilizon-reshare package)
(ice-9 rdelim)
(ice-9 popen))

(define %source-dir (getcwd))

Expand All @@ -32,7 +26,10 @@
(package (inherit mobilizon-reshare)
(name "mobilizon-reshare.git")
(version (git-version source-version revision commit))
(source mobilizon-reshare-git-origin))))
(source mobilizon-reshare-git-origin)
(propagated-inputs
(modify-inputs (package-propagated-inputs mobilizon-reshare)
(replace "python-markdownify" python-markdownify))))))

(define-public mobilizon-reshare-scheduler
(package (inherit mobilizon-reshare.git)
Expand Down
2 changes: 1 addition & 1 deletion manifest.scm
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@
(map cadr (package-direct-inputs mobilizon-reshare))
(map specification->package+output
'("git-cal" "man-db" "texinfo"
"python-pre-commit" "cloc"
"pre-commit" "cloc"
"ripgrep" "python-semver"
"fd" "docker-compose" "poetry"))))
26 changes: 13 additions & 13 deletions mobilizon_reshare/config/config.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import importlib.resources
import importlib
import logging
from logging.config import dictConfig
from pathlib import Path
from typing import Optional

import pkg_resources
from appdirs import AppDirs
from dynaconf import Dynaconf, Validator

Expand Down Expand Up @@ -48,17 +47,18 @@ def init_logging(settings: Optional[Dynaconf] = None):
def get_settings_files_paths() -> Optional[str]:

dirs = AppDirs(appname="mobilizon-reshare", version=current_version())
bundled_settings_path = pkg_resources.resource_filename(
"mobilizon_reshare", "settings.toml"
)
for config_path in [
Path(dirs.user_config_dir, "mobilizon_reshare.toml").absolute(),
Path(dirs.site_config_dir, "mobilizon_reshare.toml").absolute(),
bundled_settings_path,
]:
if config_path and Path(config_path).exists():
logger.debug(f"Loading configuration from {config_path}")
return config_path
bundled_settings_ref = importlib.resources.files(
"mobilizon_reshare"
) / "settings.toml"
with importlib.resources.as_file(bundled_settings_ref) as bundled_settings_path:
for config_path in [
Path(dirs.user_config_dir, "mobilizon_reshare.toml").absolute(),
Path(dirs.site_config_dir, "mobilizon_reshare.toml").absolute(),
bundled_settings_path.absolute(),
]:
if config_path and Path(config_path).exists():
logger.debug(f"Loading configuration from {config_path}")
return config_path


def build_settings(validators: Optional[list[Validator]] = None) -> Dynaconf:
Expand Down
47 changes: 37 additions & 10 deletions mobilizon_reshare/publishers/abstract.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import importlib
import inspect
import logging
from abc import ABC, abstractmethod
Expand Down Expand Up @@ -124,6 +125,33 @@ def _validate_message(self, message: str) -> None:
"""
raise NotImplementedError # pragma: no cover

def _get_name(self) -> str:
return self._conf[1]


def _get_template(self, configured_template, default_generator) -> Template:
if configured_template:
return JINJA_ENV.get_template(configured_template)
else:
template_ref = default_generator()
with importlib.resources.as_file(template_ref) as template_path:
return JINJA_ENV.get_template(template_path.as_posix())


def get_default_template_path(self, type=""):
return importlib.resources.files(
"mobilizon_reshare.publishers.templates"
) / f"{self._get_name()}{type}.tmpl.j2"


def get_default_recap_template_path(self):
return self.get_default_template_path(type="_recap")


def get_default_recap_header_template_path(self):
return self.get_default_template_path(type="_recap_header")


def validate_event(self, event: _MobilizonEvent) -> None:
self._validate_event(event)
self._validate_message(self.get_message_from_event(event))
Expand All @@ -148,21 +176,20 @@ def get_message_template(self) -> Template:
"""
Retrieves publisher's message template.
"""
template_path = self.conf.msg_template_path or self.default_template_path
return JINJA_ENV.get_template(template_path)
return self._get_template(self.conf.msg_template_path, self.get_default_template_path)

def get_recap_header(self):
template_path = (
self.conf.recap_header_template_path
or self.default_recap_header_template_path
def get_recap_header(self) -> Template:
return self._get_template(
self.conf.recap_header_template_path,
self.get_default_recap_header_template_path
)
return JINJA_ENV.get_template(template_path).render()


def get_recap_fragment_template(self) -> Template:
template_path = (
self.conf.recap_template_path or self.default_recap_template_path
return self._get_template(
self.conf.recap_template_path,
self.get_default_recap_template_path
)
return JINJA_ENV.get_template(template_path)

def get_recap_fragment(self, event: _MobilizonEvent) -> str:
"""
Expand Down
13 changes: 0 additions & 13 deletions mobilizon_reshare/publishers/platforms/facebook.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from typing import Optional

import facebook
import pkg_resources
from facebook import GraphAPIError

from mobilizon_reshare.dataclasses import MobilizonEvent
Expand All @@ -19,19 +18,7 @@


class FacebookFormatter(AbstractEventFormatter):

_conf = ("publisher", "facebook")
default_template_path = pkg_resources.resource_filename(
"mobilizon_reshare.publishers.templates", "facebook.tmpl.j2"
)

default_recap_template_path = pkg_resources.resource_filename(
"mobilizon_reshare.publishers.templates", "facebook_recap.tmpl.j2"
)

default_recap_header_template_path = pkg_resources.resource_filename(
"mobilizon_reshare.publishers.templates", "facebook_recap_header.tmpl.j2"
)

def _validate_event(self, event: MobilizonEvent) -> None:
text = event.description
Expand Down
13 changes: 0 additions & 13 deletions mobilizon_reshare/publishers/platforms/mastodon.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from typing import Optional
from urllib.parse import urljoin

import pkg_resources
import requests
from requests import Response

Expand All @@ -20,19 +19,7 @@


class MastodonFormatter(AbstractEventFormatter):

_conf = ("publisher", "mastodon")
default_template_path = pkg_resources.resource_filename(
"mobilizon_reshare.publishers.templates", "mastodon.tmpl.j2"
)

default_recap_template_path = pkg_resources.resource_filename(
"mobilizon_reshare.publishers.templates", "mastodon_recap.tmpl.j2"
)

default_recap_header_template_path = pkg_resources.resource_filename(
"mobilizon_reshare.publishers.templates", "mastodon_recap_header.tmpl.j2"
)

def _validate_event(self, event: MobilizonEvent) -> None:
text = event.description
Expand Down
13 changes: 0 additions & 13 deletions mobilizon_reshare/publishers/platforms/telegram.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import re
from typing import Optional

import pkg_resources
import requests
from bs4 import BeautifulSoup
from requests import Response
Expand All @@ -20,18 +19,6 @@


class TelegramFormatter(AbstractEventFormatter):
default_template_path = pkg_resources.resource_filename(
"mobilizon_reshare.publishers.templates", "telegram.tmpl.j2"
)

default_recap_template_path = pkg_resources.resource_filename(
"mobilizon_reshare.publishers.templates", "telegram_recap.tmpl.j2"
)

default_recap_header_template_path = pkg_resources.resource_filename(
"mobilizon_reshare.publishers.templates", "telegram_recap_header.tmpl.j2"
)

_conf = ("publisher", "telegram")

def _validate_event(self, event: MobilizonEvent) -> None:
Expand Down
13 changes: 0 additions & 13 deletions mobilizon_reshare/publishers/platforms/twitter.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from typing import Optional

import pkg_resources
from tweepy import OAuthHandler, API, TweepyException
from tweepy.models import Status

Expand All @@ -17,19 +16,7 @@


class TwitterFormatter(AbstractEventFormatter):

_conf = ("publisher", "twitter")
default_template_path = pkg_resources.resource_filename(
"mobilizon_reshare.publishers.templates", "twitter.tmpl.j2"
)

default_recap_template_path = pkg_resources.resource_filename(
"mobilizon_reshare.publishers.templates", "twitter_recap.tmpl.j2"
)

default_recap_header_template_path = pkg_resources.resource_filename(
"mobilizon_reshare.publishers.templates", "twitter_recap_header.tmpl.j2"
)

def _validate_event(self, event: MobilizonEvent) -> None:
pass # pragma: no cover
Expand Down
13 changes: 0 additions & 13 deletions mobilizon_reshare/publishers/platforms/zulip.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from typing import Optional
from urllib.parse import urljoin

import pkg_resources
import requests
from requests import Response
from requests.auth import HTTPBasicAuth
Expand All @@ -23,19 +22,7 @@


class ZulipFormatter(AbstractEventFormatter):

_conf = ("publisher", "zulip")
default_template_path = pkg_resources.resource_filename(
"mobilizon_reshare.publishers.templates", "zulip.tmpl.j2"
)

default_recap_template_path = pkg_resources.resource_filename(
"mobilizon_reshare.publishers.templates", "zulip_recap.tmpl.j2"
)

default_recap_header_template_path = pkg_resources.resource_filename(
"mobilizon_reshare.publishers.templates", "zulip_recap_header.tmpl.j2"
)

def _validate_event(self, event: MobilizonEvent) -> None:
text = event.description
Expand Down
Loading

0 comments on commit e381c1b

Please sign in to comment.