symuid-list 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. import symuid
  4. def symuid_list(path, path_ignore_regex, filter_expression, sort_expression):
  5. selection = []
  6. for track in symuid.Track.walk(root_path=path, path_ignore_regex=path_ignore_regex):
  7. if filter_expression or sort_expression:
  8. track_attr = {'path': track.path, 'play_count': track.get_play_count_sum()}
  9. if not filter_expression or eval(filter_expression, track_attr):
  10. if sort_expression:
  11. selection.append((eval(sort_expression, track_attr), track.path))
  12. else:
  13. print(track.path)
  14. if sort_expression:
  15. selection.sort()
  16. for sort_key, track_path in selection:
  17. print(track_path)
  18. def _init_argparser():
  19. import argparse
  20. import re
  21. argparser = argparse.ArgumentParser(description=None)
  22. argparser.add_argument('path')
  23. argparser.add_argument(
  24. '--path-ignore-regex',
  25. default=symuid.Track.PATH_DEFAULT_IGNORE_REGEX,
  26. nargs=1,
  27. metavar='pattern',
  28. dest='path_ignore_regex',
  29. type=lambda pattern: re.compile(pattern),
  30. help='(default: %(default)s)',
  31. )
  32. argparser.add_argument(
  33. '--filter',
  34. metavar='expression',
  35. dest='filter_expression',
  36. help='(example: {!r})'.format(
  37. "play_count > 16 and path.endswith('.mp3')",
  38. ),
  39. )
  40. argparser.add_argument(
  41. '--sort',
  42. metavar='expression',
  43. dest='sort_expression',
  44. help='(example: {!r} or {!r})'.format(
  45. "play_count * -1",
  46. "(play_count, len(path))",
  47. ),
  48. )
  49. return argparser
  50. def main(argv):
  51. argparser = _init_argparser()
  52. args = argparser.parse_args(argv[1:])
  53. symuid_list(**vars(args))
  54. return 0
  55. if __name__ == "__main__":
  56. import sys
  57. sys.exit(main(sys.argv))