Просмотр исходного кода

Pick a new name
Avoid putting code directly into __main__.py, which cannot be imported
Add an option for non-recursion
Add an option to process only files matching a regex

ZiB 10 месяцев назад
Родитель
Сommit
131980a8e9

+ 0 - 63
remux/remux/__main__.py

@@ -1,63 +0,0 @@
-#!/usr/bin/env python3
-import sys, os, argparse, pathlib, logging
-import remux
-
-def main():
-  # Static config
-  debug         = False
-  log_level     = logging.WARN
-
-  # ENV config
-  if 'DEBUG' in os.environ:
-    if os.environ['DEBUG'] == 'INFO':
-      log_level = logging.INFO
-    else:
-      log_level = logging.DEBUG
-
-  # CLI config
-  parser = argparse.ArgumentParser(prog='remux',
-    description="Calls mkvmerge, mkvpropedit and ffprobe in useful ways")
-  parser.add_argument('root', metavar='base_directory', type=pathlib.Path,
-    help='Recursively process all media files in this directory')
-  parser.add_argument('--debug',   '-d', action='store_true')
-  parser.add_argument('--verbose', '-v', action='store_true')
-
-  args = None
-  try:
-    args = parser.parse_args()
-  except:
-    parser.print_help()
-    sys.exit(-1)
-  if args.debug:
-    log_level = logging.DEBUG
-  elif args.verbose:
-    log_level = logging.INFO
-
-  # Sanity
-  if not args.root.is_dir():
-    print(f"Invalid base_directory: {args.root}", file=sys.stderr)
-    parser.print_help()
-    sys.exit(-1)
-
-  # Logging
-  logging.basicConfig(stream=sys.stderr)
-  logger = logging.getLogger('remux')
-  logger.setLevel(log_level)
-
-  # Module init
-  mux  = remux.Mux(logLevel=log_level)
-  scan = remux.Scan(logLevel=log_level)
-  lang = remux.Lang(logLevel=log_level)
-
-  # Walk
-  logger.debug(f"Root: {args.root}")
-  for (parent, dirs, files) in args.root.walk():
-    logger.debug(f"Walking: {parent}")
-
-    media = scan.scan(files, parent)
-    mux.mux(media)
-    lang.lang(media)
-
-# Exec
-if __name__ == "__main__":
-  main()

+ 5 - 2
remux/pyproject.toml → zz_remux/pyproject.toml

@@ -6,7 +6,7 @@ build-backend = "hatchling.build"
 source = "src"
 
 [project]
-name = "remux"
+name = "zz_remux"
 version = "0.0.1"
 
 description = "Runs mkvmerge, mkvpropedit, and ffprobe in useful ways"
@@ -29,6 +29,9 @@ classifiers = [
   "Topic :: Multimedia :: Video :: Conversion",
 ]
 
+[project.scripts]
+zz_remux = "zz_remux.cli:main"
+
 [project.urls]
-source = "https://git.zi3.xyz/zib/Loki/src/main/remux"
+source = "https://git.zi3.xyz/zib/Loki/src/main/zibzab-remux"
 funding = "https://4114.us/give"

+ 0 - 0
remux/remux/__init__.py → zz_remux/zz_remux/__init__.py


+ 3 - 0
zz_remux/zz_remux/__main__.py

@@ -0,0 +1,3 @@
+from .cli import main
+
+main()

+ 89 - 0
zz_remux/zz_remux/cli.py

@@ -0,0 +1,89 @@
+#!/usr/bin/env python3
+import re, sys, os, argparse, pathlib, logging
+import zz_remux as remux
+
+def main():
+  # Static config
+  debug     = False
+  log_level = logging.WARN
+  prog_name = 'zz_remux'
+
+  # ENV config
+  if 'DEBUG' in os.environ:
+    if os.environ['DEBUG'] == 'INFO':
+      log_level = logging.INFO
+    else:
+      log_level = logging.DEBUG
+
+  # CLI config
+  parser = argparse.ArgumentParser(prog=prog_name,
+    description='Calls mkvmerge, mkvpropedit and ffprobe in useful ways')
+  parser.add_argument('root', metavar='base_directory', type=pathlib.Path,
+    help='Recursively process all media files in this directory')
+  parser.add_argument('--match',   '-m',
+    help='Only process files with name stems that match this (case-insensitive) regex')
+  parser.add_argument('--no-recursion', '-n', action='store_true', help='Do not recurse')
+  parser.add_argument('--verbose', '-v', action='store_true', help='Log more')
+  parser.add_argument('--debug',   '-d', action='store_true', help='Log lots more')
+
+  args = None
+  try:
+    args = parser.parse_args()
+  except:
+    parser.print_help()
+    sys.exit(-1)
+  if args.debug:
+    log_level = logging.DEBUG
+  elif args.verbose:
+    log_level = logging.INFO
+
+  if not args.root.is_dir():
+    print(f"Invalid base_directory: {args.root}", file=sys.stderr)
+    parser.print_help()
+    sys.exit(-1)
+  root = args.root
+
+  match = None
+  if args.match:
+    try:
+      match = re.compile(args.match, re.IGNORECASE)
+      args.match = True
+    except:
+      print(f"Unable to compile match regex: {args.match}", file=sys.stderr)
+      parser.print_help()
+      sys.exit(-1)
+
+  recurse = True
+  if args.no_recursion:
+    recurse = False
+
+  del args
+  del parser
+
+  # Logging
+  logging.basicConfig(stream=sys.stderr)
+  logger = logging.getLogger(prog_name)
+  logger.setLevel(log_level)
+
+  # Module init
+  mux  = remux.Mux(logLevel=log_level)
+  scan = remux.Scan(logLevel=log_level)
+  lang = remux.Lang(logLevel=log_level)
+
+  # Walk
+  logger.debug(f"Root: {root}")
+  for (parent, dirs, files) in root.walk():
+    logger.debug(f"Walking: {parent}")
+
+    media = scan.scan(files, parent, match=match)
+    mux.mux(media)
+    lang.lang(media)
+
+    # Non-recursion
+    if not recurse:
+      logger.info("Not recursing")
+      return
+
+# Exec
+if __name__ == "__main__":
+  main()

+ 2 - 2
remux/remux/consts.py → zz_remux/zz_remux/consts.py

@@ -19,8 +19,8 @@ class Consts:
   media_ext     = video_ext + subs_ext
 
   # Tag data
-  mux_tag  = 'zib.mux'
-  mux_vers = 1
+  mux_tag   = 'zib.mux'
+  mux_vers  = 1
   lang_tag  = 'zib.lang'
   lang_vers = 1
 

+ 0 - 0
remux/remux/lang.py → zz_remux/zz_remux/lang.py


+ 0 - 0
remux/remux/mux.py → zz_remux/zz_remux/mux.py


+ 1 - 1
remux/remux/probe.py → zz_remux/zz_remux/probe.py

@@ -29,7 +29,7 @@ class Probe:
     if probe.returncode != 0:
       self.logger.error(f"ffprobe error ({probe.returncode}): {probe.stderr.decode(self.encoding)}")
       self.logger.debug(f"ffprobe stdout: {data}")
-      return steams
+      return streams
 
     # Parse streams
     prefix = None

+ 8 - 2
remux/remux/scan.py → zz_remux/zz_remux/scan.py

@@ -23,7 +23,7 @@ class Scan:
       re.IGNORECASE
     )
 
-  def scan(self, files, parent=pathlib.Path('')):
+  def scan(self, files, parent=pathlib.Path(''), match=None):
     self.logger.info(f"Processing {len(files)} files")
     media = []
 
@@ -38,7 +38,7 @@ class Scan:
       # Parse media paths
       m = self.re_media.search(info['name'])
       if m and len(m.groups()) == 4:
-        info['group']  = m.group(1)
+        info['group'] = m.group(1)
 
         try:
           lang = langcodes.Language.get(m.group(2).lower())
@@ -58,6 +58,12 @@ class Scan:
         self.logger.debug(f"Skipping non-media file: {file}")
         continue
 
+      # Skip files that don't match the user regex
+      if match:
+        if not match.search(info['group']):
+          self.logger.info(f"Skipping non-matching group: {info['group']}")
+          continue
+
       # Read tags
       info['mux_vers']  = self.tags.get_int(info['path'], Consts.mux_tag)
       info['lang_vers'] = self.tags.get_int(info['path'], Consts.lang_tag)

+ 0 - 0
remux/remux/tag.py → zz_remux/zz_remux/tag.py