1
0

Ruff format
All checks were successful
Test Python / Test (push) Successful in 21s

This commit is contained in:
Jon Michael Aanes 2024-06-01 20:46:38 +02:00
parent 2afb80fc1e
commit 2f6dc95a7f
Signed by: Jmaa
SSH Key Fingerprint: SHA256:Ab0GfHGCblESJx7JRE4fj4bFy/KRpeLhi41y4pF3sNA
4 changed files with 21 additions and 10 deletions

View File

@ -15,6 +15,7 @@ PACKAGE_NAME = 'socials_util'
with open('README.md') as f: with open('README.md') as f:
readme = f.read() readme = f.read()
def parse_version_file(text: str) -> str: def parse_version_file(text: str) -> str:
match = re.match(r'^__version__\s*=\s*(["\'])([\d\.]+)\1$', text) match = re.match(r'^__version__\s*=\s*(["\'])([\d\.]+)\1$', text)
if match is None: if match is None:
@ -22,9 +23,11 @@ def parse_version_file(text: str) -> str:
raise Exception(msg) raise Exception(msg)
return match.group(2) return match.group(2)
with open(PACKAGE_NAME + '/_version.py') as f: with open(PACKAGE_NAME + '/_version.py') as f:
version = parse_version_file(f.read()) version = parse_version_file(f.read())
def parse_requirements(text: str) -> list[str]: def parse_requirements(text: str) -> list[str]:
return text.strip().split('\n') return text.strip().split('\n')

View File

@ -188,7 +188,9 @@ WIKIDATA_PROPERTIES: dict[SocialSiteId | int, WikidataInfo] = {
def re_social_subdomain(main_domain: str) -> str: def re_social_subdomain(main_domain: str) -> str:
return r'^(?:https?:\/\/)?(?:www\.)?([\w_-]+)\.' + re.escape(main_domain) + r'(\/.*)?$' return (
r'^(?:https?:\/\/)?(?:www\.)?([\w_-]+)\.' + re.escape(main_domain) + r'(\/.*)?$'
)
RE_ID = r'@?([^\s/]+)' RE_ID = r'@?([^\s/]+)'
@ -264,7 +266,9 @@ PIXIV_SKETCH_USER_NICKNAME_URL = re_social_path_adv('sketch.pixiv.net', RE_ID)
URL_PARSE_CARRD_PAGE = re_social_subdomain('carrd.co') URL_PARSE_CARRD_PAGE = re_social_subdomain('carrd.co')
URL_PARSE_YOUTUBE_CHANNEL_HANDLE_1 = re_social_path_adv( URL_PARSE_YOUTUBE_CHANNEL_HANDLE_1 = re_social_path_adv(
'youtube.com', RE_ID, RE_ANY_SUBPATH 'youtube.com',
RE_ID,
RE_ANY_SUBPATH,
) )
URL_PARSE_YOUTUBE_CHANNEL_HANDLE_2 = re_social_path_adv('youtube.com', 'c', RE_ID) URL_PARSE_YOUTUBE_CHANNEL_HANDLE_2 = re_social_path_adv('youtube.com', 'c', RE_ID)
URL_PARSE_YOUTUBE_CHANNEL_ID = re_social_path_adv('youtube.com', 'channel', RE_ID) URL_PARSE_YOUTUBE_CHANNEL_ID = re_social_path_adv('youtube.com', 'channel', RE_ID)
@ -419,6 +423,7 @@ WELL_KNOWN_MASTODON_INSTANCES: frozenset[str] = frozenset(
DISALLOWED_IDENTIFIERS: frozenset[str] = frozenset({'www', 'intent', 'user'}) DISALLOWED_IDENTIFIERS: frozenset[str] = frozenset({'www', 'intent', 'user'})
def determine_social_from_url_internally( def determine_social_from_url_internally(
url: str, url: str,
) -> tuple[SocialSiteId | None, str | None]: ) -> tuple[SocialSiteId | None, str | None]:

View File

@ -1,6 +1,6 @@
import pytest import pytest
from socials_util import determine_social_from_url, SocialSiteId, SocialLink from socials_util import SocialLink, SocialSiteId, determine_social_from_url
PARSABLE_SOCIAL_IDS_COMBINED: list[tuple[str, object, str]] = [ PARSABLE_SOCIAL_IDS_COMBINED: list[tuple[str, object, str]] = [
# Tumblr formats # Tumblr formats
@ -181,15 +181,17 @@ PARSABLE_SOCIAL_IDS_COMBINED: list[tuple[str, object, str]] = [
] ]
NOT_PARSABLE = [ NOT_PARSABLE = [
# Twitter intents are not supported # Twitter intents are not supported
'twitter.com/intent/user?user_id=123', 'twitter.com/intent/user?user_id=123',
'https://twitter.com/intent/user?user_id=123', 'https://twitter.com/intent/user?user_id=123',
'https://twitter.com/intent/user', 'https://twitter.com/intent/user',
'https://twitter.com/intent', 'https://twitter.com/intent',
] ]
@pytest.mark.parametrize( @pytest.mark.parametrize(
'url,expected_social_site_id,expected_social_id', PARSABLE_SOCIAL_IDS_COMBINED 'url,expected_social_site_id,expected_social_id',
PARSABLE_SOCIAL_IDS_COMBINED,
) )
def test_parse_social_ids(url, expected_social_site_id, expected_social_id): def test_parse_social_ids(url, expected_social_site_id, expected_social_id):
social_link: SocialLink | None = determine_social_from_url(url) social_link: SocialLink | None = determine_social_from_url(url)
@ -199,6 +201,7 @@ def test_parse_social_ids(url, expected_social_site_id, expected_social_id):
expected_social_site_id, expected_social_site_id,
), url ), url
@pytest.mark.parametrize('url', NOT_PARSABLE) @pytest.mark.parametrize('url', NOT_PARSABLE)
def test_not_parsable(url: str): def test_not_parsable(url: str):
assert determine_social_from_url(url) is None assert determine_social_from_url(url) is None