"""Compatibility patches for core.sr.ht 0.78.6 + todo.sr.ht 0.77.5.
core.sr.ht 0.78.6 refactored the OAuth API:
- AbstractOAuthService removed, replaced by OAuthService (different signature)
- DelegatedScope removed, replaced by OAuthScope (different signature)
- SrhtFlask no longer accepts oauth_service=, uses user_class= instead
"""
import glob
# 1. Patch srht/oauth/__init__.py: add compat aliases
OAUTH_PATCH = '''
# --- compat shim for todo.sr.ht 0.77.5 ---
class AbstractOAuthService(OAuthService):
"""Compat wrapper: old API accepted (client_id, client_secret, delegated_scopes=, token_class=, user_class=)"""
def __init__(self, client_id=None, client_secret=None,
delegated_scopes=None, token_class=None, user_class=None, **kwargs):
super().__init__("todo.sr.ht",
user_class=user_class,
oauthtoken_class=token_class)
self.delegated_scopes = delegated_scopes or []
class DelegatedScope:
def __init__(self, name, description=None, write=False):
self.name = name
self.description = description
self.write = write
# --- end compat shim ---
'''
for f in glob.glob("/usr/lib/python3.*/site-packages/srht/oauth/__init__.py"):
with open(f) as fh:
content = fh.read()
if "DelegatedScope" not in content:
with open(f, "a") as fh:
fh.write(OAUTH_PATCH)
print(f"Patched {f}: added AbstractOAuthService + DelegatedScope compat")
else:
print(f"Already patched: {f}")
# 2. Patch todosrht/flask.py: use new SrhtFlask API
# Old: super().__init__("todo.sr.ht", __name__, oauth_service=TodoOAuthService())
# New: super().__init__("todo.sr.ht", __name__, user_class=User, legacy_oauthtoken_class=OAuthToken)
for f in glob.glob("/usr/lib/python3.*/site-packages/todosrht/flask.py"):
with open(f) as fh:
content = fh.read()
if "oauth_service=TodoOAuthService()" in content:
content = content.replace(
"oauth_service=TodoOAuthService())",
"user_class=User, legacy_oauthtoken_class=OAuthToken)"
)
# Add OAuthToken import
content = content.replace(
"from todosrht.types import TicketAccess, TicketStatus, TicketResolution, User",
"from todosrht.types import TicketAccess, TicketStatus, TicketResolution, User, OAuthToken"
)
with open(f, "w") as fh:
fh.write(content)
print(f"Patched {f}: switched to new SrhtFlask API")
else:
print(f"Already patched: {f}")