Skip to content

Commit

Permalink
remove nostdout function
Browse files Browse the repository at this point in the history
  • Loading branch information
diomekes committed Dec 30, 2016
1 parent 2511255 commit ff0abbe
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 23 deletions.
23 changes: 4 additions & 19 deletions beetsplug/play.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,8 @@
from beets import config
from beets import ui
from beets import util
import os
import sys
from os.path import relpath
from tempfile import NamedTemporaryFile
import contextlib
from beetsplug import random

# Indicate where arguments should be inserted into the command string.
Expand Down Expand Up @@ -127,8 +124,8 @@ def play_music(self, lib, opts, args):

# Send query to random plugin.
if opts.random:
with nostdout():
selection = random.random_func(lib, opts, args)
selection = random.random_func(lib, opts, args,
print_list=False)
else:
selection = lib.albums(ui.decargs(args))

Expand All @@ -145,8 +142,8 @@ def play_music(self, lib, opts, args):
# Perform item query and add tracks to playlist.
else:
if opts.random:
with nostdout():
selection = random.random_func(lib, opts, args)
selection = random.random_func(lib, opts, args,
print_list=False)
else:
selection = lib.items(ui.decargs(args))

Expand Down Expand Up @@ -193,15 +190,3 @@ def _create_tmp_playlist(self, paths_list):
m3u.write(item + b'\n')
m3u.close()
return m3u.name


@contextlib.contextmanager
def nostdout():
"""Send stdout to devnull, used to capture random plugin's printed list
"""
new_target = open(os.devnull, "w")
old_target, sys.stdout = sys.stdout, new_target
try:
yield new_target
finally:
sys.stdout = old_target
11 changes: 7 additions & 4 deletions beetsplug/random.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def random_objs(objs, album, number=1, time=None, equal_chance=False):
return _take(perm, number)


def random_func(lib, opts, args):
def random_func(lib, opts, args, print_list=True):
"""Select some random items or albums and print the results.
"""
# Fetch all the objects matching the query into a list.
Expand All @@ -125,11 +125,14 @@ def random_func(lib, opts, args):
# Print a random subset.
objs = random_objs(objs, opts.album, opts.number, opts.time,
opts.equal_chance)
for obj in objs:
print_(format(obj))

if print_list:
for obj in objs:
print_(format(obj))

# Return random subset to be used by other plugins.
return objs
else:
return objs

random_cmd = Subcommand('random',
help=u'choose a random track or album')
Expand Down

0 comments on commit ff0abbe

Please sign in to comment.