symuid-list 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. import symuid
  4. def symuid_list(path, path_ignore_regex, filter_expression, sort_expression, limit, prefix):
  5. # use generators until sort is required
  6. attr_it = ({'path': track.path,
  7. 'comment': track.comment,
  8. 'play_count': track.get_play_count_sum()}
  9. for track in symuid.Track.walk(path, path_ignore_regex))
  10. if filter_expression:
  11. attr_it = filter(lambda a: eval(filter_expression, a), attr_it)
  12. if sort_expression:
  13. attr_it = list(attr_it)
  14. attr_it.sort(key=lambda a: eval(sort_expression, a))
  15. for i, attr in enumerate(attr_it):
  16. if limit and i == limit:
  17. break
  18. print(prefix + attr['path'])
  19. def _init_argparser():
  20. import argparse
  21. import re
  22. argparser = argparse.ArgumentParser(description=None)
  23. argparser.add_argument('path')
  24. argparser.add_argument(
  25. '--path-ignore-regex',
  26. default=symuid.Track.PATH_DEFAULT_IGNORE_REGEX,
  27. nargs=1,
  28. metavar='pattern',
  29. dest='path_ignore_regex',
  30. type=lambda pattern: re.compile(pattern),
  31. help='(default: %(default)s)',
  32. )
  33. argparser.add_argument(
  34. '--filter',
  35. metavar='expression',
  36. dest='filter_expression',
  37. help='(example: {!r} or {!r})'.format(
  38. "play_count > 16 and path.endswith('.mp3')",
  39. "comment is None or len(comment) < 16",
  40. ),
  41. )
  42. argparser.add_argument(
  43. '--sort',
  44. metavar='expression',
  45. dest='sort_expression',
  46. help='(example: {!r} or {!r})'.format(
  47. "play_count * -1",
  48. "(play_count, len(path))",
  49. ),
  50. )
  51. argparser.add_argument(
  52. '--limit',
  53. type=int,
  54. help='(default: none)',
  55. )
  56. argparser.add_argument(
  57. '--prefix',
  58. type=str,
  59. default='',
  60. help='add prefix to each resulting path (default: %(default)r)',
  61. )
  62. return argparser
  63. def main(argv):
  64. argparser = _init_argparser()
  65. args = argparser.parse_args(argv[1:])
  66. symuid_list(**vars(args))
  67. return 0
  68. if __name__ == "__main__":
  69. import sys
  70. sys.exit(main(sys.argv))