From d4f6057110847272083adff86baf7a06dafc0535 Mon Sep 17 00:00:00 2001 From: Mark Wooding Date: Wed, 30 Mar 2022 19:46:52 +0100 Subject: [PATCH 1/1] mkm3u: Use `-1' throughout as a sentinel title/chapter number. This will make it easier to integrate with statically typed systems like databases. It already simplifies the duration-caching machinery. --- mkm3u | 47 ++++++++++++++++++++++------------------------- 1 file changed, 22 insertions(+), 25 deletions(-) diff --git a/mkm3u b/mkm3u index 7df1891..368546a 100755 --- a/mkm3u +++ b/mkm3u @@ -141,22 +141,22 @@ class Source (object): def _duration(me, title, start_chapter, end_chapter): return -1 - def url_and_duration(me, title = None, - start_chapter = None, end_chapter = None): - if title == "-": + + def url_and_duration(me, title = -1, start_chapter = -1, end_chapter = -1): + if title == -1: if me.TITLEP: raise ExpectedError("missing title number") - if start_chapter is not None or end_chapter is not None: + if start_chapter != -1 or end_chapter != -1: raise ExpectedError("can't specify chapter without title") suffix = "" elif not me.TITLEP: raise ExpectedError("can't specify title with `%s'" % me.fn) - elif start_chapter is None: - if end_chapter is not None: + elif start_chapter == -1: + if end_chapter != -1: raise ExpectedError("can't specify end chapter without start chapter") suffix = "#%d" % title elif not me.CHAPTERP: raise ExpectedError("can't specify chapter with `%s'" % me.fn) - elif end_chapter is None: + elif end_chapter == -1: suffix = "#%d:%d" % (title, start_chapter) else: suffix = "#%d:%d-%d:%d" % (title, start_chapter, title, end_chapter - 1) @@ -172,8 +172,7 @@ class Source (object): SELECT device, inode, size, mtime, duration FROM duration WHERE path = ? AND title = ? AND start_chapter = ? AND end_chapter = ? - """, [me.fn, title, start_chapter is None and -1 or start_chapter, - end_chapter is None and -1 or end_chapter]) + """, [me.fn, title, start_chapter, end_chapter]) row = c.fetchone() foundp = False if row is None: @@ -183,8 +182,7 @@ class Source (object): (path, title, start_chapter, end_chapter, device, inode, size, mtime, duration) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?) - """, [me.fn, title, start_chapter is None and -1 or start_chapter, - end_chapter is None and -1 or end_chapter, + """, [me.fn, title, start_chapter, end_chapter, st.st_dev, st.st_ino, st.st_size, st.st_mtime, duration]) else: @@ -199,26 +197,25 @@ class Source (object): SET device = ?, inode = ?, size = ?, mtime = ?, duration = ? WHERE path = ? AND title = ? AND start_chapter = ? AND end_chapter = ? - """, [st.st_dev, st.st_dev, st.st_size, st.st_mtime, duration, - me.fn, title, start_chapter is None and -1 or start_chapter, - end_chapter is None and -1 or end_chapter]) + """, [st.st_dev, st.st_dev, st.st_size, st.st_mtime, duration, + me.fn, title, start_chapter, end_chapter]) DB.commit() - if end_chapter is not None: + if end_chapter != -1: keys = [(title, ch) for ch in range(start_chapter, end_chapter)] set = me.used_chapters else: keys, set = [title], me.used_titles for k in keys: if k in set: - if title == "-": + if title == -1: raise ExpectedError("`%s' already used" % me.fn) - elif end_chapter is None: + elif end_chapter == -1: raise ExpectedError("`%s' title %d already used" % (me.fn, title)) else: raise ExpectedError("`%s' title %d chapter %d already used" % (me.fn, title, k[1])) - if end_chapter is not None: + if end_chapter != -1: for ch in range(start_chapter, end_chapter): me.used_chapters.add((title, ch)) return me.PREFIX + ROOT + urlencode(me.fn) + suffix, duration @@ -237,11 +234,11 @@ class VideoDisc (Source): if not 1 <= title <= ntitle: raise ExpectedError("bad title %d for `%s': must be in 1 .. %d" % (title, me.fn, ntitle)) - if start_chapter is None: + if start_chapter == -1: durq = "duration:%d" % title else: nch = int(program_output(["dvd-info", path, "chapters:%d" % title])) - if end_chapter is None: end_chapter = nch + if end_chapter == -1: end_chapter = nch else: end_chapter -= 1 if not 1 <= start_chapter <= end_chapter <= nch: raise ExpectedError("bad chapter range %d .. %d for `%s' title %d: " @@ -407,7 +404,7 @@ class Chapter (object): class Episode (object): def __init__(me, season, i, neps, title, src, series_title_p = True, - tno = None, startch = None, endch = None): + tno = -1, startch = -1, endch = -1): me.season = season me.i, me.neps, me.title = i, neps, title me.chapters = [] @@ -732,13 +729,13 @@ class EpisodeListParser (object): def _process_episode(me, ww): opts = ww.nextword(); check(opts is not None, "missing title/options") - ti = None; sname = None; neps = 1; epi = None; loch = hich = None + ti = -1; sname = None; neps = 1; epi = None; loch = hich = -1 explen, expvar, explicitlen = me._explen, me._expvar, False series_title_p = True for k, v in me._keyvals(opts): if k is None: if v.isdigit(): ti = int(v) - elif v == "-": ti = "-" + elif v == "-": ti = -1 else: sname = v elif k == "s": sname = v elif k == "n": neps = getint(v) @@ -751,7 +748,7 @@ class EpisodeListParser (object): explicitlen = True elif k == "ch": try: sep = v.index("-") - except ValueError: loch, hich = getint(v), None + except ValueError: loch, hich = getint(v), -1 else: loch, hich = getint(v[:sep]), getint(v[sep + 1:]) + 1 else: raise ExpectedError("unknown episode option `%s'" % k) check(ti is not None, "missing title number") @@ -763,7 +760,7 @@ class EpisodeListParser (object): season = series.ensure_season() if epi is None: epi = season.ep_i - if ti == "-": + if ti == -1: check(season.implicitp or season.i is None, "audio source, but explicit non-movie season") dir = lookup(me._audirs, series.name, -- 2.11.0