From 5ec89c0e9e90b1f25035d2451aa973359c728c34 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Tue, 27 Sep 2011 09:41:55 -0600 Subject: [PATCH] Fix reading metadata from filenames when the author regexp does not match anything --- src/calibre/ebooks/metadata/__init__.py | 2 ++ src/calibre/ebooks/metadata/meta.py | 27 +++++++++++++------------ 2 files changed, 16 insertions(+), 13 deletions(-) diff --git a/src/calibre/ebooks/metadata/__init__.py b/src/calibre/ebooks/metadata/__init__.py index dd90b4c2ec..decba3b780 100644 --- a/src/calibre/ebooks/metadata/__init__.py +++ b/src/calibre/ebooks/metadata/__init__.py @@ -22,6 +22,8 @@ except: _author_pat = re.compile(r'(?i),?\s+(and|with)\s+') def string_to_authors(raw): + if not raw: + return [] raw = raw.replace('&&', u'\uffff') raw = _author_pat.sub('&', raw) authors = [a.strip().replace(u'\uffff', '&') for a in raw.split('&')] diff --git a/src/calibre/ebooks/metadata/meta.py b/src/calibre/ebooks/metadata/meta.py index d82a2268fa..7d8855de14 100644 --- a/src/calibre/ebooks/metadata/meta.py +++ b/src/calibre/ebooks/metadata/meta.py @@ -149,19 +149,20 @@ def metadata_from_filename(name, pat=None): try: au = match.group('author') aus = string_to_authors(au) - mi.authors = aus - if prefs['swap_author_names'] and mi.authors: - def swap(a): - if ',' in a: - parts = a.split(',', 1) - else: - parts = a.split(None, 1) - if len(parts) > 1: - t = parts[-1] - parts = parts[:-1] - parts.insert(0, t) - return ' '.join(parts) - mi.authors = [swap(x) for x in mi.authors] + if aus: + mi.authors = aus + if prefs['swap_author_names'] and mi.authors: + def swap(a): + if ',' in a: + parts = a.split(',', 1) + else: + parts = a.split(None, 1) + if len(parts) > 1: + t = parts[-1] + parts = parts[:-1] + parts.insert(0, t) + return ' '.join(parts) + mi.authors = [swap(x) for x in mi.authors] except (IndexError, ValueError): pass try: