Browse Source

added symuid-list

Fabian Peter Hammerle 5 years ago
parent
commit
4a52ce6d10
2 changed files with 75 additions and 3 deletions
  1. 65 0
      symuid-list
  2. 10 3
      symuid/__init__.py

+ 65 - 0
symuid-list

@@ -0,0 +1,65 @@
+#!/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))

+ 10 - 3
symuid/__init__.py

@@ -64,6 +64,8 @@ class Track:
             label += ':' + player
             if library_id:
                 label += ':' + library_id
+        elif library_id:
+            raise Exception((player, library_id))
         for k, c in self._iface.get_free_ints(label):
             player, library_id, register_ts_dec = k.split(':')[2:]
             yield PlayCount(
@@ -93,6 +95,9 @@ class Track:
             assert len(counts) == 1, counts
             return counts[0]
 
+    def get_play_count_sum(self, player=None, library_id=None):
+        return sum(c.count for c in self._get_latest_play_counts(player, library_id))
+
     def register_play_count(self, player, library_id, register_dt, play_count, tag_set_cb=None):
         # TODO accept PlayCount as param
         assert isinstance(register_dt, dt.datetime), register_dt
@@ -110,14 +115,16 @@ class Track:
             raise Exception((current_count, play_count))
 
     @classmethod
-    def walk(cls, root_path, path_ignore_regex, ignored_cb, unsupported_cb):
+    def walk(cls, root_path, path_ignore_regex, ignored_cb=None, unsupported_cb=None):
         for dirpath, dirnames, filenames in os.walk(root_path):
             for filename in filenames:
                 track_path = os.path.join(dirpath, filename)
                 if path_ignore_regex.search(track_path):
-                    ignored_cb(track_path)
+                    if ignored_cb is not None:
+                        ignored_cb(track_path)
                 else:
                     try:
                         yield cls(track_path)
                     except NotImplementedError as e:
-                        unsupported_cb(track_path, e)
+                        if unsupported_cb is not None:
+                            unsupported_cb(track_path, e)