Browse Source

setup `symuid-sync` cli entry point via setuptools.setup()

Fabian Peter Hammerle 4 years ago
parent
commit
e37a144ca4
2 changed files with 21 additions and 22 deletions
  1. 5 0
      setup.py
  2. 16 22
      symuid/sync.py

+ 5 - 0
setup.py

@@ -4,6 +4,11 @@ setuptools.setup(
     name='symuid',
     use_scm_version=True,
     packages=setuptools.find_packages(),
+    entry_points={
+        'console_scripts': [
+            'symuid-sync = symuid.sync:_main',
+        ],
+    },
     install_requires=[
         'mutagen < 2',
         'pytz',

+ 16 - 22
symuid-sync → symuid/sync.py

@@ -1,6 +1,4 @@
-#!/usr/bin/env python3
-# -*- coding: utf-8 -*-
-
+import argparse
 import re
 import subprocess
 import sys
@@ -8,33 +6,33 @@ import sys
 import symuid
 
 
-def generate_uuid():
+def _generate_uuid():
     return subprocess.check_output(['uuid', '-v', '4', '-F', 'BIN']).strip()
 
 
-def log_path(track_path, msg, stream=sys.stdout):
+def _log_path(track_path, msg, stream=sys.stdout):
     stream.write("{!r}: {}\n".format(track_path, msg))
 
 
-def log_path_error(track_path, msg):
-    log_path(track_path, msg, stream=sys.stderr)
+def _log_path_error(track_path, msg):
+    _log_path(track_path, msg, stream=sys.stderr)
 
 
-def symuid_sync(path, path_ignore_regex, show_ignored=False):
+def sync(path, path_ignore_regex, show_ignored=False):
     for track in symuid.Track.walk(
             root_path=path,
             path_ignore_regex=path_ignore_regex,
-            ignored_cb=lambda p: show_ignored and log_path(p, 'ignored'),
+            ignored_cb=lambda p: show_ignored and _log_path(p, 'ignored'),
             unsupported_cb=lambda p, e:
-                log_path_error(p, 'unsupported type, skipped'),
-        ):
+                _log_path_error(p, 'unsupported type, skipped'),
+    ):
         if track.get_uuid() is None:
-            track.assign_uuid(generate_uuid())
-            log_path(track.path, 'assigned uuid {!r}'.format(track.get_uuid()))
+            track.assign_uuid(_generate_uuid())
+            _log_path(track.path, 'assigned uuid {!r}'.format(
+                track.get_uuid()))
 
 
 def _init_argparser():
-    import argparse
     argparser = argparse.ArgumentParser(description=None)
     argparser.add_argument('path')
     argparser.add_argument(
@@ -43,7 +41,7 @@ def _init_argparser():
         nargs=1,
         metavar='pattern',
         dest='path_ignore_regex',
-        type=lambda pattern: re.compile(pattern),
+        type=re.compile,
         help='(default: %(default)s)',
     )
     argparser.add_argument(
@@ -53,11 +51,7 @@ def _init_argparser():
     return argparser
 
 
-def main(argv):
+def _main():
     argparser = _init_argparser()
-    args = argparser.parse_args(argv[1:])
-    symuid_sync(**vars(args))
-    return 0
-
-if __name__ == "__main__":
-    sys.exit(main(sys.argv))
+    args = argparser.parse_args()
+    sync(**vars(args))