1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162 |
- import symuid
- def symuid_list(path, path_ignore_regex, filter_expression, sort_expression):
- # use generators until sort is required
- attr_it = ({'path': track.path, '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 attr in attr_it:
- print(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})'.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))
|