brass-eye.epls: Add some stanza comments.
[epls] / mkm3u
diff --git a/mkm3u b/mkm3u
index 9b308aa..21c9a4b 100755 (executable)
--- a/mkm3u
+++ b/mkm3u
@@ -149,28 +149,28 @@ class VideoSeason (object):
       raise ExpectedError("season %d episode %d already taken" % (me.i, i))
     me.episodes[i] = disc; disc.neps += 1
 
-def some_group(m, *gg):
-  for g in gg:
-    s = m.group(g)
+def match_group(m, *groups, dflt = None, mustp = False):
+  for g in groups:
+    try: s = m.group(g)
+    except IndexError: continue
     if s is not None: return s
-  return None
+  if mustp: raise ValueError("no match found")
+  else: return dflt
 
 class VideoDir (object):
 
-  _R_ISO_PRE = RX.compile(r""" ^
-        (?: S (?P<si> \d+)
-              (?: \. \ (?P<st> .*) — (?: D \d+ \. \ )? |
-                  D \d+ \. \ |
-                  (?= E \d+ \. \ ) |
-                  \. \ ) |
-            (?P<epnum> \d+) \. \ )
-        (?: (?P<eplist>
-             (?: S \d+ \ )? E \d+ (?: – \d+)?
-             (?: , \ (?: S \d+ \ )? E \d+ (?: – \d+)?)*) |
-            (?P<epname> E \d+) \. \ .* |
-            .*)
-        \. iso $
-  """, RX.X)
+  _R_ISO_PRE = list(map(lambda pats:
+                          list(map(lambda pat:
+                                     RX.compile("^" + pat + r"\.iso$", RX.X),
+                                   pats)),
+    [[r""" S (?P<si> \d+) \. \ (?P<stitle> .*) — (?: D \d+ \. \ )?
+           (?P<epex> .*) """,
+      r""" S (?P<si> \d+) (?: D \d+)? \. \ (?P<epex> .*) """,
+      r""" S (?P<si> \d+) \. \ (?P<epex> E \d+ .*) """,
+      r""" S (?P<si> \d+) (?P<epex> E \d+) \. \ .* """],
+     [r""" (?P<si> \d+) [A-Z]? \. \ (?P<stitle> .*) — (?P<epex> .*) """],
+     [r""" \d+ \. \ (?P<epex> [ES] \d+ .*) """],
+     [r""" (?P<epnum> \d+ ) \. \ .* """]]))
 
   _R_ISO_EP = RX.compile(r""" ^
         (?: S (?P<si> \d+) \ )?
@@ -183,25 +183,34 @@ class VideoDir (object):
     fns.sort()
     season = None
     seasons = {}
+    styles = me._R_ISO_PRE
     for fn in fns:
       path = OS.path.join(dir, fn)
       if not fn.endswith(".iso"): continue
-      m = me._R_ISO_PRE.match(fn)
-      if not m:
-        #print(";; `%s' ignored (regex mismatch)" % path, file = SYS.stderr)
+      #print(";; `%s'" % path, file = SYS.stderr)
+      for sty in styles:
+        for r in sty:
+          m = r.match(fn)
+          if m: styles = [sty]; break
+        else:
+          continue
+        break
+      else:
+        #print(";;\tignored (regex mismatch)", file = SYS.stderr)
         continue
 
-      i = filter(m.group("si"), int)
-      stitle = m.group("st")
-      check(i is not None or stitle is None,
+      si = filter(match_group(m, "si"), int)
+      stitle = match_group(m, "stitle")
+
+      check(si is not None or stitle is None,
             "explicit season title without number in `%s'" % fn)
-      if i is not None:
-        if season is None or i != season.i:
-          check(season is None or i == season.i + 1,
+      if si is not None:
+        if season is None or si != season.i:
+          check(season is None or si == season.i + 1,
                 "season %d /= %d" %
-                  (i, season is None and -1 or season.i + 1))
-          check(i not in seasons, "season %d already seen" % i)
-          seasons[i] = season = VideoSeason(i, stitle)
+                  (si, season is None and -1 or season.i + 1))
+          check(si not in seasons, "season %d already seen" % si)
+          seasons[si] = season = VideoSeason(si, stitle)
         else:
           check(stitle == season.title,
                 "season title `%s' /= `%s'" % (stitle, season.title))
@@ -209,14 +218,9 @@ class VideoDir (object):
       disc = VideoDisc(path)
       ts = season
       any, bad = False, False
-      epname = m.group("epname")
-      epexpr = m.group("eplist")
-      epnum = m.group("epnum")
-      if epname is not None: eplist = [epname]
-      elif epexpr is not None: eplist = epexpr.split(", ")
-      elif epnum is not None: eplist = ["E" + epnum]
-      else: continue
-      #print(";; `%s'" % path, file = SYS.stderr)
+      epnum = match_group(m, "epnum")
+      if epnum is not None: eplist = ["E" + epnum]
+      else: eplist = match_group(m, "epex", mustp = True).split(", ")
       for eprange in eplist:
         mm = me._R_ISO_EP.match(eprange)
         if mm is None:
@@ -235,7 +239,7 @@ class VideoDir (object):
           ts.set_episode_disc(k, disc)
           #print(";;\tepisode %d.%d" % (ts.i, k), file = SYS.stderr)
       if not any:
-        #print(";;\tignored" % path, file = SYS.stderr)
+        #print(";;\tignored", file = SYS.stderr)
         pass
       elif bad:
         raise ExpectedError("bad ep list in `%s'", fn)