예제 #1
0
파일: parser.py 프로젝트: nhrx/mpdc
def p_expression_collection(p):
    'expression : COLLECTION'
    p[0] = OrderedSet()
    if p[1] in collectionsmanager.c:
        collection = collectionsmanager.c[p[1]]
        if 'expression' in collection:
            p[0] |= parser.parse(collection['expression'],
                    lexer=lex.lex(debug=0, reflags=re.UNICODE|re.IGNORECASE))
        if 'songs' in collection:
            p[0] |= OrderedSet(collection['songs'])
        if enable_command and 'command' in collection:
            try:
                output = subprocess.check_output(collection['command'],
                                                 shell=True)
                p[0] |= OrderedSet(format_mpc_output(output.decode()))
            except subprocess.CalledProcessError:
                warning('Error while executing `command` in collection [{}]'.
                        format(p[1]))
                sys.exit(0)
        if 'sort' in collection:
            p[0] = mpd.set_sort(p[0])
    elif p[1] == 'all':
        p[0] = OrderedSet(mpd.get_all_songs())
    elif p[1] == 'c':
        p[0] = OrderedSet(mpd.get_playlist_songs())
    elif p[1] == 'C':
        c_song = mpd.get_current_song()
        if c_song is not None:
            p[0] = OrderedSet([c_song])
    elif p[1] == 'A':
        c_song = mpd.get_current_song()
        if c_song is not None:
            p[0] = OrderedSet(mpd.find('artist',
                                       mpd.get_tag(c_song, 'artist')))
    elif p[1] == 'B':
        c_song = mpd.get_current_song()
        if c_song is not None:
            p[0] = OrderedSet(mpd.find_multiple(
                              albumartist=mpd.get_tag(c_song, 'albumartist'),
                              album=mpd.get_tag(c_song, 'album')))
            if not p[0]:
                p[0] = OrderedSet(mpd.find_multiple(
                                  artist=mpd.get_tag(c_song, 'artist'),
                                  album=mpd.get_tag(c_song, 'album')))
    else:
        warning('Collection [{}] does not exist'.format(p[1]))
        sys.exit(0)
예제 #2
0
파일: parser.py 프로젝트: madjar/mpdc
def p_expression_collection(p):
    'expression : COLLECTION'
    if p[1] in collections:
        collection = collections[p[1]]
        p[0] = OrderedSet()
        if 'expression' in collection:
            p[0] |= p.parser.parse(collection['expression'],
                                   lexer=lex.lex(debug=0, reflags=re.UNICODE))
        if 'songs' in collection:
            p[0] |= OrderedSet(collection['songs'])
        if enable_command and 'command' in collection:
            try:
                output = check_output(collection['command'], shell=True)
                p[0] |= OrderedSet(format_mpc_output(output.decode()))
            except CalledProcessError:
                warning('Error while executing `command` in collection [%s]' %
                        p[1])
                sys.exit(0)
        if 'sort' in collection:
            p[0] = mpd.set_sort(p[0])

    elif p[1] == 'all':
        p[0] = OrderedSet(mpd.get_all_songs())
    elif p[1] == 'c':
        p[0] = OrderedSet(mpd.get_playlist_songs())
    elif p[1] == 'C':
        p[0] = OrderedSet([mpd.get_current_song()])
    elif p[1] == 'A':
        c_song = mpd.get_current_song()
        p[0] = OrderedSet(mpd.find('artist', mpd.get_tag(c_song, 'artist')))
    elif p[1] == 'B':
        c_song = mpd.get_current_song()
        p[0] = OrderedSet(mpd.find_multiple(
                                        artist=mpd.get_tag(c_song, 'artist'),
                                        album=mpd.get_tag(c_song, 'album')))
    else:
        warning('Collection [%s] doesn\'t exist' % p[1])
        sys.exit(0)
예제 #3
0
def display_songs(filenames, path=None, enable_pager=False):
    lines = []
    if path is None:
        c_w, t_w = columns_width(columns)
        header = ''
        for i, column in enumerate(columns):
            header += colorize(column.title().ljust(c_w[column]),
                               colors[i % len(colors)], True)
        if enable_pager:
            lines.append(header)
            lines.append('-' * t_w)
        else:
            print(header)
            print('—' * t_w)
        current_song = mpd.get_current_song()
    for song in filenames:
        if path is None:
            bold = True if song == current_song else False
            row = ''
            for i, column in enumerate(columns):
                if column == 'filename':
                    tag = song
                else:
                    tag = mpd.get_tag(song, column, empty='<empty>')
                    if column == 'time':
                        m, s = divmod(int(tag), 60)
                        tag = '{}:{:02}'.format(m, s)
                if len(tag) > c_w[column] - 1:
                    tag = tag[:c_w[column] - 2] + '…'
                row += colorize(tag.ljust(c_w[column]),
                                colors[i % len(colors)], bold)
            if enable_pager:
                lines.append(row)
            else:
                print(row)
        else:
            if enable_pager:
                lines.append(os.path.join(path, song))
            else:
                print(os.path.join(path, song))
    if enable_pager:
        pager_p = subprocess.Popen(shlex.split(pager), stdin=subprocess.PIPE)
        pager_p.stdin.write(bytes('\n'.join(lines), 'utf-8'))
        pager_p.stdin.close()
        pager_p.communicate()