1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- #!/usr/bin/env python3
- # -*- coding: utf-8 -*-
- import symuid
- def symuid_list(path, path_ignore_regex, filter_expression, sort_expression):
- selection = []
- for track in symuid.Track.walk(root_path=path, path_ignore_regex=path_ignore_regex):
- if filter_expression or sort_expression:
- track_attr = {'path': track.path, 'play_count': track.get_play_count_sum()}
- if not filter_expression or eval(filter_expression, track_attr):
- if sort_expression:
- selection.append((eval(sort_expression, track_attr), track.path))
- else:
- print(track.path)
- if sort_expression:
- selection.sort()
- for sort_key, track_path in selection:
- print(track_path)
- def _init_argparser():
- import argparse
- import re
- argparser = argparse.ArgumentParser(description=None)
- argparser.add_argument('path')
- argparser.add_argument(
- '--path-ignore-regex',
- default=symuid.Track.PATH_DEFAULT_IGNORE_REGEX,
- nargs=1,
- metavar='pattern',
- dest='path_ignore_regex',
- type=lambda pattern: re.compile(pattern),
- help='(default: %(default)s)',
- )
- argparser.add_argument(
- '--filter',
- metavar='expression',
- dest='filter_expression',
- help='(example: {!r})'.format(
- "play_count > 16 and path.endswith('.mp3')",
- ),
- )
- argparser.add_argument(
- '--sort',
- metavar='expression',
- dest='sort_expression',
- help='(example: {!r} or {!r})'.format(
- "play_count * -1",
- "(play_count, len(path))",
- ),
- )
- return argparser
- def main(argv):
- argparser = _init_argparser()
- args = argparser.parse_args(argv[1:])
- symuid_list(**vars(args))
- return 0
- if __name__ == "__main__":
- import sys
- sys.exit(main(sys.argv))
|