Skip to content

Commit

Permalink
Fix matching list by ID
Browse files Browse the repository at this point in the history
  • Loading branch information
ihabunek committed Oct 1, 2024
1 parent a568980 commit 89ea460
Showing 1 changed file with 19 additions and 16 deletions.
35 changes: 19 additions & 16 deletions toot/cli/lists.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import click
import json as pyjson
from typing import Optional

import click

from toot import api, config
from toot.cli import Context, cli, pass_context, json_option
from toot.entities import from_dict_list, List
from toot.cli import Context, cli, json_option, pass_context
from toot.entities import List, from_dict_list
from toot.output import print_list_accounts, print_lists, print_warning


Expand Down Expand Up @@ -49,7 +51,7 @@ def list(ctx: Context, json: bool):
@click.option("--id", help="List ID if not title is given")
@json_option
@pass_context
def accounts(ctx: Context, title: str, id: str, json: bool):
def accounts(ctx: Context, title: str, id: Optional[str], json: bool):
"""List the accounts in a list"""
list_id = _get_list_id(ctx, title, id)
response = api.get_list_accounts(ctx.app, ctx.user, list_id)
Expand Down Expand Up @@ -84,7 +86,7 @@ def create(ctx: Context, title: str, replies_policy: str, json: bool):
@click.option("--id", help="List ID if not title is given")
@json_option
@pass_context
def delete(ctx: Context, title: str, id: str, json: bool):
def delete(ctx: Context, title: str, id: Optional[str], json: bool):
"""Delete a list"""
list_id = _get_list_id(ctx, title, id)
response = api.delete_list(ctx.app, ctx.user, list_id)
Expand All @@ -100,7 +102,7 @@ def delete(ctx: Context, title: str, id: str, json: bool):
@click.option("--id", help="List ID if not title is given")
@json_option
@pass_context
def add(ctx: Context, title: str, account: str, id: str, json: bool):
def add(ctx: Context, title: str, account: str, id: Optional[str], json: bool):
"""Add an account to a list"""
list_id = _get_list_id(ctx, title, id)
found_account = api.find_account(ctx.app, ctx.user, account)
Expand Down Expand Up @@ -133,7 +135,7 @@ def add(ctx: Context, title: str, account: str, id: str, json: bool):
@click.option("--id", help="List ID if not title is given")
@json_option
@pass_context
def remove(ctx: Context, title: str, account: str, id: str, json: bool):
def remove(ctx: Context, title: str, account: str, id: Optional[str], json: bool):
"""Remove an account from a list"""
list_id = _get_list_id(ctx, title, id)
found_account = api.find_account(ctx.app, ctx.user, account)
Expand All @@ -151,7 +153,7 @@ def remove(ctx: Context, title: str, account: str, id: str, json: bool):
@click.argument("title", required=False)
@click.option("--id", help="List ID if not title is given")
@pass_context
def list_accounts(ctx: Context, title: str, id: str):
def list_accounts(ctx: Context, title: str, id: Optional[str]):
"""List the accounts in a list"""
print_warning("`toot list_accounts` is deprecated in favour of `toot lists accounts`")
list_id = _get_list_id(ctx, title, id)
Expand Down Expand Up @@ -179,7 +181,7 @@ def list_create(ctx: Context, title: str, replies_policy: str):
@click.argument("title", required=False)
@click.option("--id", help="List ID if not title is given")
@pass_context
def list_delete(ctx: Context, title: str, id: str):
def list_delete(ctx: Context, title: str, id: Optional[str]):
"""Delete a list"""
print_warning("`toot list_delete` is deprecated in favour of `toot lists delete`")
list_id = _get_list_id(ctx, title, id)
Expand All @@ -192,7 +194,7 @@ def list_delete(ctx: Context, title: str, id: str):
@click.argument("account")
@click.option("--id", help="List ID if not title is given")
@pass_context
def list_add(ctx: Context, title: str, account: str, id: str):
def list_add(ctx: Context, title: str, account: str, id: Optional[str]):
"""Add an account to a list"""
print_warning("`toot list_add` is deprecated in favour of `toot lists add`")
list_id = _get_list_id(ctx, title, id)
Expand Down Expand Up @@ -222,7 +224,7 @@ def list_add(ctx: Context, title: str, account: str, id: str):
@click.argument("account")
@click.option("--id", help="List ID if not title is given")
@pass_context
def list_remove(ctx: Context, title: str, account: str, id: str):
def list_remove(ctx: Context, title: Optional[str], account: str, id: Optional[str]):
"""Remove an account from a list"""
print_warning("`toot list_remove` is deprecated in favour of `toot lists remove`")
list_id = _get_list_id(ctx, title, id)
Expand All @@ -231,15 +233,12 @@ def list_remove(ctx: Context, title: str, account: str, id: str):
click.secho(f"✓ Removed account \"{account}\"", fg="green")


def _get_list_id(ctx: Context, title, list_id):
def _get_list_id(ctx: Context, title: Optional[str], list_id: Optional[str]):
if not list_id and not title:
raise click.ClickException("Please specify list title or ID")

lists = api.get_lists(ctx.app, ctx.user)
matched_ids = [
list["id"] for list in lists
if list["title"].lower() == title.lower() or list["id"] == list_id
]
matched_ids = [list["id"] for list in lists if _matches(list, title, list_id)]

if not matched_ids:
raise click.ClickException("List not found")
Expand All @@ -248,3 +247,7 @@ def _get_list_id(ctx: Context, title, list_id):
raise click.ClickException("Found multiple lists with the same title, please specify the ID instead")

return matched_ids[0]


def _matches(list, title, list_id):
return (title and title.lower() == list["title"].lower()) or (list_id and list_id == list["id"])

0 comments on commit 89ea460

Please sign in to comment.