Skip to content

Commit

Permalink
Rename vague API class for specificity
Browse files Browse the repository at this point in the history
  • Loading branch information
rossjrw committed May 10, 2024
1 parent e28aa93 commit 013e120
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 55 deletions.
11 changes: 5 additions & 6 deletions notifier/config/remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@
from typing import Any, Dict, List, Tuple, cast

import boto3
import requests
import tomlkit
from tomlkit.exceptions import TOMLKitError

from notifier.database.drivers.base import BaseDatabaseDriver
from notifier.database.utils import try_cache
from notifier.types import LocalConfig, SupportedWikiConfig
from notifier.wikiconnection import Connection
from notifier.wikidot import Wikidot

logger = logging.getLogger(__name__)

Expand All @@ -25,22 +24,22 @@
def get_global_config(
local_config: LocalConfig,
database: BaseDatabaseDriver,
connection: Connection,
wikidot: Wikidot,
) -> None:
"""Retrieve remote global config for wikis."""
try_cache(
get=lambda: fetch_supported_wikis(local_config, connection),
get=lambda: fetch_supported_wikis(local_config, wikidot),
store=database.store_supported_wikis,
do_not_store=[],
)


def fetch_supported_wikis(
local_config: LocalConfig, connection: Connection
local_config: LocalConfig, wikidot: Wikidot
) -> List[SupportedWikiConfig]:
"""Fetch the list of supported wikis from the configuration wiki."""
configs = []
for config_soup in connection.listpages(
for config_soup in wikidot.listpages(
local_config["config_wiki"],
category=local_config["wiki_config_category"],
module_body=wiki_config_listpages_body,
Expand Down
14 changes: 7 additions & 7 deletions notifier/config/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
Subscription,
SubscriptionCardinality,
)
from notifier.wikiconnection import Connection
from notifier.wikidot import Wikidot

logger = logging.getLogger(__name__)

Expand All @@ -40,22 +40,22 @@
def get_user_config(
local_config: LocalConfig,
database: BaseDatabaseDriver,
connection: Connection,
wikidot: Wikidot,
) -> None:
"""Retrieve remote user config."""
try_cache(
get=lambda: find_valid_user_configs(local_config, connection),
get=lambda: find_valid_user_configs(local_config, wikidot),
store=database.store_user_configs,
do_not_store=[],
)


def find_valid_user_configs(
local_config: LocalConfig, connection: Connection
local_config: LocalConfig, wikidot: Wikidot
) -> List[RawUserConfig]:
"""Fetches user configs and returns those that are valid."""
configs: List[RawUserConfig] = []
for slug, config in fetch_user_configs(local_config, connection):
for slug, config in fetch_user_configs(local_config, wikidot):
if not user_config_is_valid(slug, config):
# Only accept configs for the user who created the page
logger.warning(
Expand All @@ -78,15 +78,15 @@ def user_config_is_valid(slug: str, config: RawUserConfig) -> bool:


def fetch_user_configs(
local_config: LocalConfig, connection: Connection
local_config: LocalConfig, wikidot: Wikidot
) -> List[Tuple[str, RawUserConfig]]:
"""Fetches a list of user configurations from the configuration wiki.
User configurations are stored on the dedicated Wikidot site. They are
cached in the database.
"""
configs: List[Tuple[str, RawUserConfig]] = []
for config_soup in connection.listpages(
for config_soup in wikidot.listpages(
local_config["config_wiki"],
category=local_config["user_config_category"],
module_body=user_config_listpages_body,
Expand Down
22 changes: 11 additions & 11 deletions notifier/deletions.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from notifier.database.drivers.base import BaseDatabaseDriver
from notifier import timing
from notifier.types import LocalConfig, PostMeta
from notifier.wikiconnection import Connection, ThreadNotExists
from notifier.wikidot import Wikidot, ThreadNotExists

logger = logging.getLogger(__name__)

Expand All @@ -32,7 +32,7 @@


def clear_deleted_posts(
database: BaseDatabaseDriver, connection: Connection
database: BaseDatabaseDriver, wikidot: Wikidot
) -> None:
"""Check for posts that have been deleted on the remote and delete them here too.
Expand All @@ -47,13 +47,13 @@ def clear_deleted_posts(
now_hour_ts = int(datetime.timestamp(now_hour))

posts = database.get_posts_to_check_for_deletion(now_hour_ts)
delete_posts(posts, database, connection)
delete_posts(posts, database, wikidot)


def delete_posts(
posts: List[PostMeta],
database: BaseDatabaseDriver,
connection: Connection,
wikidot: Wikidot,
) -> None:
"""Sync post deletion states.
Expand All @@ -77,7 +77,7 @@ def delete_posts(

try:
# Throws ThreadNotExists if the thread doesn't exist
thread_meta, thread_posts = connection.thread(
thread_meta, thread_posts = wikidot.thread(
post["wiki_id"], post["thread_id"], post["post_id"]
)

Expand Down Expand Up @@ -143,22 +143,22 @@ def delete_posts(


def rename_invalid_user_config_pages(
local_config: LocalConfig, connection: Connection
local_config: LocalConfig, wikidot: Wikidot
) -> None:
"""Prepares invalid user config pages for deletion."""
logger.info("Finding invalid user configs to prepare for deletion")
# Get all user configs and filter out any that are valid
invalid_configs = [
(slug, config)
for slug, config in fetch_user_configs(local_config, connection)
for slug, config in fetch_user_configs(local_config, wikidot)
if not user_config_is_valid(slug, config)
]
logger.debug(
"Found invalid configs to rename %s", {"count": len(invalid_configs)}
)
for slug, config in invalid_configs:
try:
connection.rename_page(
wikidot.rename_page(
local_config["config_wiki"], slug, f"deleted:{uuid4()}"
)
except Exception as error:
Expand All @@ -171,19 +171,19 @@ def rename_invalid_user_config_pages(


def delete_prepared_invalid_user_pages(
local_config: LocalConfig, connection: Connection
local_config: LocalConfig, wikidot: Wikidot
) -> None:
"""Deletes prepared invalid user config pages."""
logger.info("Finding pages marked for deletion")
pages_to_delete = connection.listpages(
pages_to_delete = wikidot.listpages(
local_config["config_wiki"],
category="deleted",
module_body="%%fullname%%",
)
for page in pages_to_delete:
slug = page.get_text()
try:
connection.delete_page(local_config["config_wiki"], slug)
wikidot.delete_page(local_config["config_wiki"], slug)
except Exception as error:
logger.error(
"Couldn't delete page %s",
Expand Down
12 changes: 6 additions & 6 deletions notifier/newposts.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from notifier.config.user import parse_thread_url
from notifier.database.drivers.base import BaseDatabaseDriver
from notifier.types import RawPost, RawThreadMeta
from notifier.wikiconnection import Connection
from notifier.wikidot import Wikidot

logger = logging.getLogger(__name__)

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

def get_new_posts(
database: BaseDatabaseDriver,
connection: Connection,
wikidot: Wikidot,
limit_wikis: Optional[List[str]] = None,
) -> None:
"""For each configured wiki, retrieve and store new posts.
Expand All @@ -36,7 +36,7 @@ def get_new_posts(
continue
logger.info("Getting new posts %s", {"for wiki_id": wiki["id"]})
try:
fetch_posts_with_context(wiki["id"], database, connection)
fetch_posts_with_context(wiki["id"], database, wikidot)
except Exception as error:
logger.error(
"Failed getting new posts %s",
Expand All @@ -47,7 +47,7 @@ def get_new_posts(


def fetch_posts_with_context(
wiki_id: str, database: BaseDatabaseDriver, connection: Connection
wiki_id: str, database: BaseDatabaseDriver, wikidot: Wikidot
) -> None:
"""Look up new posts for a wiki and then attach their context. Stores
the posts in the cache.
Expand Down Expand Up @@ -110,7 +110,7 @@ def fetch_posts_with_context(
"post_id": post_id,
},
)
thread_meta, thread_page_posts = connection.thread(
thread_meta, thread_page_posts = wikidot.thread(
wiki_id, thread_id, post_id
)
post = next(
Expand Down Expand Up @@ -165,7 +165,7 @@ def fetch_posts_with_context(
"Downloading first thread page %s",
{"wiki_id": wiki_id, "thread_id": thread_id},
)
thread_first_post = connection.thread(wiki_id, thread_id)[1][0]
thread_first_post = wikidot.thread(wiki_id, thread_id)[1][0]
database.store_context_thread(
{
"thread_id": thread_id,
Expand Down
Loading

0 comments on commit 013e120

Please sign in to comment.