Skip to content

Commit f6f0572

Browse files
committed
fix(cli): add simple content CLI
1 parent 9e5d6a9 commit f6f0572

File tree

2 files changed

+126
-0
lines changed

2 files changed

+126
-0
lines changed

pyrekordbox/cli/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@
33
# Date: 2025-03-01
44

55
from .cli import cli
6+
from .content import content_cli
67
from .playlists import playlist_cli

pyrekordbox/cli/content.py

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
# -*- coding: utf-8 -*-
2+
# Author: Dylan Jones
3+
# Date: 2025-04-07
4+
5+
import functools
6+
import json as json_
7+
from typing import Tuple
8+
9+
import click
10+
11+
from pyrekordbox import Rekordbox6Database
12+
13+
from .cli import cli
14+
15+
16+
def format_opt(func):
17+
"""Click option decorator for human-readable output."""
18+
19+
@click.option("--format", "-f", is_flag=True, help="Format output as human readable string.")
20+
@functools.wraps(func)
21+
def wrapper(*args, **kwargs):
22+
return func(*args, **kwargs)
23+
24+
return wrapper
25+
26+
27+
def indent_opt(func):
28+
"""Click option decorator for commands using indentaion."""
29+
30+
@click.option("--indent", "-i", type=int, default=None, help="Indentation level.")
31+
@functools.wraps(func)
32+
def wrapper(*args, **kwargs):
33+
return func(*args, **kwargs)
34+
35+
return wrapper
36+
37+
38+
def content_id_arg(func):
39+
"""Click argument decorator for commands using a content ID."""
40+
41+
@click.argument("content_id", type=str, nargs=1)
42+
@functools.wraps(func)
43+
def wrapper(*args, **kwargs):
44+
return func(*args, **kwargs)
45+
46+
return wrapper
47+
48+
49+
def content_items_args(func):
50+
"""Click argument decorator for commands using content items."""
51+
52+
@click.argument("items", type=str, nargs=-1)
53+
@functools.wraps(func)
54+
def wrapper(*args, **kwargs):
55+
return func(*args, **kwargs)
56+
57+
return wrapper
58+
59+
60+
@cli.group(name="content", help="Manage content.")
61+
def content_cli():
62+
pass
63+
64+
65+
# noinspection PyShadowingBuiltins
66+
@content_cli.command(name="list", help="List all content.")
67+
@format_opt
68+
@indent_opt
69+
def list_content(format: bool, indent: int = None):
70+
"""List all content in the database."""
71+
db = Rekordbox6Database()
72+
content = db.get_content()
73+
if format:
74+
for item in content:
75+
click.echo(f"ID: {item.ID}, Title: {item.Title}, Artist: {item.ArtistName}")
76+
else:
77+
data = [
78+
{
79+
"ID": item.ID,
80+
"Title": item.Title,
81+
"Artist": item.ArtistName,
82+
"Album": item.AlbumName,
83+
}
84+
for item in content
85+
]
86+
string = json_.dumps(data, indent=indent)
87+
click.echo(string)
88+
89+
90+
@content_cli.command(name="update-path", help="Update the path of a content item.")
91+
@content_id_arg
92+
@click.argument("path", type=str, nargs=1)
93+
def update_content_path(content_id: str, path: str):
94+
"""Update the path of a content item.
95+
96+
Example:
97+
pyrekordbox content update-path 1234 'path/to/content'
98+
"""
99+
db = Rekordbox6Database()
100+
content = db.get_content(ID=content_id).one()
101+
db.update_content_path(content, path, save=True, commit=True)
102+
103+
104+
@content_cli.command(name="update-paths", help="Update the paths of multiple content paths.")
105+
@click.argument("items", type=str, nargs=-1)
106+
def update_content_paths(items: Tuple[str]):
107+
r"""Update the paths of multiple content paths.
108+
109+
The items are a list of JSON strings representing the content to update.
110+
Each item should have a 'ID' key with the ID of the content and a 'Path' key with the new path.
111+
112+
**Important**: On Windows, double quotes must be escaped with a backslash.
113+
114+
Example:
115+
pyrekordbox content update-paths [{\"id\": 1234, \"path\": \"path/to/content\"}]
116+
"""
117+
s = " ".join(items)
118+
data = json_.loads(s)
119+
120+
db = Rekordbox6Database()
121+
for item in data:
122+
content_id = item["id"]
123+
path = item["path"]
124+
content = db.get_content(ID=content_id).one()
125+
db.update_content_path(content, path, save=True, commit=True)

0 commit comments

Comments
 (0)