Now we have a list of (much simpler!) patterns which we try one by one.
This handling oddball filenames significantly easier.
raise ExpectedError("season %d episode %d already taken" % (me.i, i))
me.episodes[i] = disc; disc.neps += 1
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
if s is not None: return s
+ if mustp: raise ValueError("no match found")
+ else: return dflt
- _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 pat: RX.compile("^" + pat + r"\.iso$", RX.X),
+ [r""" S? (?P<si> \d+) [A-Z]? \. \ (?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""" S (?P<si> \d+) (?P<epex> E \d+) \. \ .* """,
+ r""" \d+ \. \ (?P<epex> [ES] \d+ .*) """,
+ r""" (?P<epnum> \d+ ) \. \ .* """]))
_R_ISO_EP = RX.compile(r""" ^
(?: S (?P<si> \d+) \ )?
_R_ISO_EP = RX.compile(r""" ^
(?: S (?P<si> \d+) \ )?
for fn in fns:
path = OS.path.join(dir, fn)
if not fn.endswith(".iso"): continue
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 r in me._R_ISO_PRE:
+ m = r.match(fn)
+ if m: break
+ else:
+ #print(";;\tignored (regex mismatch)", file = SYS.stderr)
- 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)
"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,
- (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))
else:
check(stitle == season.title,
"season title `%s' /= `%s'" % (stitle, season.title))
disc = VideoDisc(path)
ts = season
any, bad = False, False
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:
for eprange in eplist:
mm = me._R_ISO_EP.match(eprange)
if mm is None:
ts.set_episode_disc(k, disc)
#print(";;\tepisode %d.%d" % (ts.i, k), file = SYS.stderr)
if not any:
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)
pass
elif bad:
raise ExpectedError("bad ep list in `%s'", fn)