Skip to content

Commit fcb5514

Browse files
committed
standardize placement of command setup
This groups together all of the optparse setup calls separately from the functions. This comes at the expense of showing the command-line options above the code that interprets them. Making me wish for a more declarative CLI setup style...
1 parent 03bea61 commit fcb5514

File tree

1 file changed

+89
-104
lines changed

1 file changed

+89
-104
lines changed

beets/ui/commands.py

Lines changed: 89 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,6 @@ def _do_query(lib, query, album, also_items=True):
7979

8080
# fields: Shows a list of available fields for queries and format strings.
8181

82-
fields_cmd = ui.Subcommand(
83-
'fields',
84-
help='show fields available for queries and format strings'
85-
)
86-
87-
8882
def fields_func(lib, opts, args):
8983
def _print_rows(names):
9084
print(" " + "\n ".join(names))
@@ -109,6 +103,11 @@ def _show_plugin_fields(album):
109103
_print_rows(library.Album._fields.keys())
110104
_show_plugin_fields(True)
111105

106+
107+
fields_cmd = ui.Subcommand(
108+
'fields',
109+
help='show fields available for queries and format strings'
110+
)
112111
fields_cmd.func = fields_func
113112
default_commands.append(fields_cmd)
114113

@@ -796,6 +795,27 @@ def import_files(lib, paths, query):
796795
# Emit event.
797796
plugins.send('import', lib=lib, paths=paths)
798797

798+
799+
def import_func(lib, opts, args):
800+
config['import'].set_args(opts)
801+
802+
# Special case: --copy flag suppresses import_move (which would
803+
# otherwise take precedence).
804+
if opts.copy:
805+
config['import']['move'] = False
806+
807+
if opts.library:
808+
query = decargs(args)
809+
paths = []
810+
else:
811+
query = None
812+
paths = args
813+
if not paths:
814+
raise ui.UserError('no path specified')
815+
816+
import_files(lib, paths, query)
817+
818+
799819
import_cmd = ui.Subcommand(
800820
'import', help='import new music', aliases=('imp', 'im')
801821
)
@@ -867,26 +887,6 @@ def import_files(lib, paths, query):
867887
'-g', '--group-albums', dest='group_albums', action='store_true',
868888
help='group tracks in a folder into seperate albums'
869889
)
870-
871-
872-
def import_func(lib, opts, args):
873-
config['import'].set_args(opts)
874-
875-
# Special case: --copy flag suppresses import_move (which would
876-
# otherwise take precedence).
877-
if opts.copy:
878-
config['import']['move'] = False
879-
880-
if opts.library:
881-
query = decargs(args)
882-
paths = []
883-
else:
884-
query = None
885-
paths = args
886-
if not paths:
887-
raise ui.UserError('no path specified')
888-
889-
import_files(lib, paths, query)
890890
import_cmd.func = import_func
891891
default_commands.append(import_cmd)
892892

@@ -906,6 +906,14 @@ def list_items(lib, query, album, fmt):
906906
ui.print_obj(item, lib, tmpl)
907907

908908

909+
def list_func(lib, opts, args):
910+
if opts.path:
911+
fmt = '$path'
912+
else:
913+
fmt = opts.format
914+
list_items(lib, decargs(args), opts.album, fmt)
915+
916+
909917
list_cmd = ui.Subcommand('list', help='query the library', aliases=('ls',))
910918
list_cmd.parser.add_option(
911919
'-a', '--album', action='store_true',
@@ -919,14 +927,6 @@ def list_items(lib, query, album, fmt):
919927
'-f', '--format', action='store',
920928
help='print with custom format', default=None
921929
)
922-
923-
924-
def list_func(lib, opts, args):
925-
if opts.path:
926-
fmt = '$path'
927-
else:
928-
fmt = opts.format
929-
list_items(lib, decargs(args), opts.album, fmt)
930930
list_cmd.func = list_func
931931
default_commands.append(list_cmd)
932932

@@ -1020,6 +1020,10 @@ def update_items(lib, query, album, move, pretend):
10201020
album.move()
10211021

10221022

1023+
def update_func(lib, opts, args):
1024+
update_items(lib, decargs(args), opts.album, opts.move, opts.pretend)
1025+
1026+
10231027
update_cmd = ui.Subcommand(
10241028
'update', help='update the library', aliases=('upd', 'up',)
10251029
)
@@ -1039,12 +1043,6 @@ def update_items(lib, query, album, move, pretend):
10391043
'-f', '--format', action='store',
10401044
help='print with custom format', default=None
10411045
)
1042-
1043-
1044-
def update_func(lib, opts, args):
1045-
update_items(lib, decargs(args), opts.album, opts.move, opts.pretend)
1046-
1047-
10481046
update_cmd.func = update_func
10491047
default_commands.append(update_cmd)
10501048

@@ -1078,6 +1076,10 @@ def remove_items(lib, query, album, delete):
10781076
obj.remove(delete)
10791077

10801078

1079+
def remove_func(lib, opts, args):
1080+
remove_items(lib, decargs(args), opts.album, opts.delete)
1081+
1082+
10811083
remove_cmd = ui.Subcommand(
10821084
'remove', help='remove matching items from the library', aliases=('rm',)
10831085
)
@@ -1089,12 +1091,6 @@ def remove_items(lib, query, album, delete):
10891091
'-a', '--album', action='store_true',
10901092
help='match albums instead of tracks'
10911093
)
1092-
1093-
1094-
def remove_func(lib, opts, args):
1095-
remove_items(lib, decargs(args), opts.album, opts.delete)
1096-
1097-
10981094
remove_cmd.func = remove_func
10991095
default_commands.append(remove_cmd)
11001096

@@ -1133,19 +1129,17 @@ def show_stats(lib, query, exact):
11331129
size_str, len(artists), len(albums)))
11341130

11351131

1132+
def stats_func(lib, opts, args):
1133+
show_stats(lib, decargs(args), opts.exact)
1134+
1135+
11361136
stats_cmd = ui.Subcommand(
11371137
'stats', help='show statistics about the library or a query'
11381138
)
11391139
stats_cmd.parser.add_option(
11401140
'-e', '--exact', action='store_true',
11411141
help='get exact file sizes'
11421142
)
1143-
1144-
1145-
def stats_func(lib, opts, args):
1146-
show_stats(lib, decargs(args), opts.exact)
1147-
1148-
11491143
stats_cmd.func = stats_func
11501144
default_commands.append(stats_cmd)
11511145

@@ -1256,6 +1250,17 @@ def modify_parse_args(args):
12561250
query.append(arg)
12571251
return query, mods, dels
12581252

1253+
1254+
def modify_func(lib, opts, args):
1255+
query, mods, dels = modify_parse_args(decargs(args))
1256+
if not mods and not dels:
1257+
raise ui.UserError('no modifications specified')
1258+
write = opts.write if opts.write is not None else \
1259+
config['import']['write'].get(bool)
1260+
modify_items(lib, mods, dels, query, write, opts.move, opts.album,
1261+
not opts.yes)
1262+
1263+
12591264
modify_cmd = ui.Subcommand(
12601265
'modify', help='change metadata fields', aliases=('mod',)
12611266
)
@@ -1283,18 +1288,6 @@ def modify_parse_args(args):
12831288
'-f', '--format', action='store',
12841289
help='print with custom format', default=None
12851290
)
1286-
1287-
1288-
def modify_func(lib, opts, args):
1289-
query, mods, dels = modify_parse_args(decargs(args))
1290-
if not mods and not dels:
1291-
raise ui.UserError('no modifications specified')
1292-
write = opts.write if opts.write is not None else \
1293-
config['import']['write'].get(bool)
1294-
modify_items(lib, mods, dels, query, write, opts.move, opts.album,
1295-
not opts.yes)
1296-
1297-
12981291
modify_cmd.func = modify_func
12991292
default_commands.append(modify_cmd)
13001293

@@ -1319,6 +1312,16 @@ def move_items(lib, dest, query, copy, album):
13191312
obj.store()
13201313

13211314

1315+
def move_func(lib, opts, args):
1316+
dest = opts.dest
1317+
if dest is not None:
1318+
dest = normpath(dest)
1319+
if not os.path.isdir(dest):
1320+
raise ui.UserError('no such directory: %s' % dest)
1321+
1322+
move_items(lib, dest, decargs(args), opts.copy, opts.album)
1323+
1324+
13221325
move_cmd = ui.Subcommand(
13231326
'move', help='move or copy items', aliases=('mv',)
13241327
)
@@ -1334,18 +1337,6 @@ def move_items(lib, dest, query, copy, album):
13341337
'-a', '--album', default=False, action='store_true',
13351338
help='match whole albums instead of tracks'
13361339
)
1337-
1338-
1339-
def move_func(lib, opts, args):
1340-
dest = opts.dest
1341-
if dest is not None:
1342-
dest = normpath(dest)
1343-
if not os.path.isdir(dest):
1344-
raise ui.UserError('no such directory: %s' % dest)
1345-
1346-
move_items(lib, dest, decargs(args), opts.copy, opts.album)
1347-
1348-
13491340
move_cmd.func = move_func
13501341
default_commands.append(move_cmd)
13511342

@@ -1383,39 +1374,21 @@ def write_items(lib, query, pretend):
13831374
item.try_write()
13841375

13851376

1377+
def write_func(lib, opts, args):
1378+
write_items(lib, decargs(args), opts.pretend)
1379+
1380+
13861381
write_cmd = ui.Subcommand('write', help='write tag information to files')
13871382
write_cmd.parser.add_option(
13881383
'-p', '--pretend', action='store_true',
13891384
help="show all changes but do nothing"
13901385
)
1391-
1392-
1393-
def write_func(lib, opts, args):
1394-
write_items(lib, decargs(args), opts.pretend)
1395-
1396-
13971386
write_cmd.func = write_func
13981387
default_commands.append(write_cmd)
13991388

14001389

14011390
# config: Show and edit user configuration.
14021391

1403-
config_cmd = ui.Subcommand('config',
1404-
help='show or edit the user configuration')
1405-
config_cmd.parser.add_option(
1406-
'-p', '--paths', action='store_true',
1407-
help='show files that configuration was loaded from'
1408-
)
1409-
config_cmd.parser.add_option(
1410-
'-e', '--edit', action='store_true',
1411-
help='edit user configuration with $EDITOR'
1412-
)
1413-
config_cmd.parser.add_option(
1414-
'-d', '--defaults', action='store_true',
1415-
help='include the default configuration'
1416-
)
1417-
1418-
14191392
def config_func(lib, opts, args):
14201393
# Make sure lazy configuration is loaded
14211394
config.resolve()
@@ -1466,18 +1439,26 @@ def config_func(lib, opts, args):
14661439
print(config.dump(full=opts.defaults))
14671440

14681441

1442+
config_cmd = ui.Subcommand('config',
1443+
help='show or edit the user configuration')
1444+
config_cmd.parser.add_option(
1445+
'-p', '--paths', action='store_true',
1446+
help='show files that configuration was loaded from'
1447+
)
1448+
config_cmd.parser.add_option(
1449+
'-e', '--edit', action='store_true',
1450+
help='edit user configuration with $EDITOR'
1451+
)
1452+
config_cmd.parser.add_option(
1453+
'-d', '--defaults', action='store_true',
1454+
help='include the default configuration'
1455+
)
14691456
config_cmd.func = config_func
14701457
default_commands.append(config_cmd)
14711458

14721459

14731460
# completion: print completion script
14741461

1475-
completion_cmd = ui.Subcommand(
1476-
'completion',
1477-
help='print shell script that provides command line completion'
1478-
)
1479-
1480-
14811462
def print_completion(*args):
14821463
for line in completion_script(default_commands + plugins.commands()):
14831464
print(line, end='')
@@ -1564,6 +1545,10 @@ def completion_script(commands):
15641545
yield '}\n'
15651546

15661547

1548+
completion_cmd = ui.Subcommand(
1549+
'completion',
1550+
help='print shell script that provides command line completion'
1551+
)
15671552
completion_cmd.func = print_completion
15681553
completion_cmd.hide = True
15691554
default_commands.append(completion_cmd)

0 commit comments

Comments
 (0)