123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- #!/usr/bin/env python3
- # -*- coding: utf-8 -*-
- import symuid
- def symuid_list(path, path_ignore_regex, filter_expression, sort_expression, limit, prefix):
- # use generators until sort is required
- attr_it = ({'path': track.path,
- 'comment': track.comment,
- 'play_count': track.get_play_count_sum()}
- for track in symuid.Track.walk(path, path_ignore_regex))
- if filter_expression:
- attr_it = filter(lambda a: eval(filter_expression, a), attr_it)
- if sort_expression:
- attr_it = list(attr_it)
- attr_it.sort(key=lambda a: eval(sort_expression, a))
- for i, attr in enumerate(attr_it):
- if limit and i == limit:
- break
- print(prefix + attr['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} or {!r})'.format(
- "play_count > 16 and path.endswith('.mp3')",
- "comment is None or len(comment) < 16",
- ),
- )
- argparser.add_argument(
- '--sort',
- metavar='expression',
- dest='sort_expression',
- help='(example: {!r} or {!r})'.format(
- "play_count * -1",
- "(play_count, len(path))",
- ),
- )
- argparser.add_argument(
- '--limit',
- type=int,
- help='(default: none)',
- )
- argparser.add_argument(
- '--prefix',
- type=str,
- default='',
- help='add prefix to each resulting path (default: %(default)r)',
- )
- 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))
|