When reading metadata from file names, allow setting the comments field as well, in addition to title/authros/publisher/series/etc. Fixes #1332582 [some add book regexp bugs and suggestion](https://bugs.launchpad.net/calibre/+bug/1332582)

This commit is contained in:
Kovid Goyal 2014-07-02 18:19:30 +05:30
parent 84205d79cd
commit 7a2a5bbdd9
3 changed files with 37 additions and 6 deletions

View File

@ -184,6 +184,11 @@ def metadata_from_filename(name, pat=None, fallback_pat=None):
mi.pubdate = parse_only_date(pubdate) mi.pubdate = parse_only_date(pubdate)
except: except:
pass pass
try:
comments = match.group('comments')
mi.comments = comments
except (IndexError, ValueError):
pass
if mi.is_null('title'): if mi.is_null('title'):
mi.title = name mi.title = name

View File

@ -101,8 +101,8 @@
<rect> <rect>
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>305</width> <width>301</width>
<height>263</height> <height>284</height>
</rect> </rect>
</property> </property>
<layout class="QGridLayout" name="gridLayout_2"> <layout class="QGridLayout" name="gridLayout_2">
@ -116,7 +116,7 @@
<item row="0" column="1"> <item row="0" column="1">
<widget class="QLineEdit" name="title"> <widget class="QLineEdit" name="title">
<property name="toolTip"> <property name="toolTip">
<string>Regular expression (?P&lt;title&gt;)</string> <string>Regular expression (?P&amp;lt;title&amp;gt;)</string>
</property> </property>
<property name="text"> <property name="text">
<string>No match</string> <string>No match</string>
@ -246,6 +246,26 @@
</property> </property>
</widget> </widget>
</item> </item>
<item row="7" column="0">
<widget class="QLabel" name="label_10">
<property name="text">
<string>Comments:</string>
</property>
</widget>
</item>
<item row="7" column="1">
<widget class="QLineEdit" name="comments">
<property name="toolTip">
<string>Regular expression (?P&lt;comments&gt;)</string>
</property>
<property name="text">
<string>No match</string>
</property>
<property name="readOnly">
<bool>true</bool>
</property>
</widget>
</item>
</layout> </layout>
</widget> </widget>
</widget> </widget>

View File

@ -71,7 +71,7 @@ class FilenamePattern(QWidget, Ui_Form): # {{{
self.re.lineEdit().textChanged.connect(lambda x: self.changed_signal.emit()) self.re.lineEdit().textChanged.connect(lambda x: self.changed_signal.emit())
def initialize(self, defaults=False): def initialize(self, defaults=False):
# Get all itmes in the combobox. If we are resting # Get all items in the combobox. If we are resting
# to defaults we don't want to lose what the user # to defaults we don't want to lose what the user
# has added. # has added.
val_hist = [unicode(self.re.lineEdit().text())] + [unicode(self.re.itemText(i)) for i in xrange(self.re.count())] val_hist = [unicode(self.re.lineEdit().text())] + [unicode(self.re.itemText(i)) for i in xrange(self.re.count())]
@ -95,6 +95,7 @@ class FilenamePattern(QWidget, Ui_Form): # {{{
self.re.setCurrentIndex(0) self.re.setCurrentIndex(0)
def do_test(self): def do_test(self):
from calibre.ebooks.metadata import authors_to_string
from calibre.ebooks.metadata.meta import metadata_from_filename from calibre.ebooks.metadata.meta import metadata_from_filename
fname = unicode(self.filename.text()) fname = unicode(self.filename.text())
ext = os.path.splitext(fname)[1][1:].lower() ext = os.path.splitext(fname)[1][1:].lower()
@ -116,7 +117,7 @@ class FilenamePattern(QWidget, Ui_Form): # {{{
else: else:
self.title.setText(_('No match')) self.title.setText(_('No match'))
if mi.authors: if mi.authors:
self.authors.setText(', '.join(mi.authors)) self.authors.setText(authors_to_string(mi.authors))
else: else:
self.authors.setText(_('No match')) self.authors.setText(_('No match'))
@ -132,11 +133,16 @@ class FilenamePattern(QWidget, Ui_Form): # {{{
if mi.publisher: if mi.publisher:
self.publisher.setText(mi.publisher) self.publisher.setText(mi.publisher)
else:
self.publisher.setText(_('No match'))
if mi.pubdate: if mi.pubdate:
self.pubdate.setText(mi.pubdate.strftime('%Y-%m-%d')) self.pubdate.setText(mi.pubdate.strftime('%Y-%m-%d'))
else:
self.pubdate.setText(_('No match'))
self.isbn.setText(_('No match') if mi.isbn is None else str(mi.isbn)) self.isbn.setText(_('No match') if mi.isbn is None else str(mi.isbn))
self.comments.setText(mi.comments if mi.comments else _('No match'))
def pattern(self): def pattern(self):
pat = unicode(self.re.lineEdit().text()) pat = unicode(self.re.lineEdit().text())
@ -148,7 +154,7 @@ class FilenamePattern(QWidget, Ui_Form): # {{{
history = [] history = []
history_pats = [unicode(self.re.lineEdit().text())] + [unicode(self.re.itemText(i)) for i in xrange(self.re.count())] history_pats = [unicode(self.re.lineEdit().text())] + [unicode(self.re.itemText(i)) for i in xrange(self.re.count())]
for p in history_pats[:14]: for p in history_pats[:24]:
# Ensure we don't have duplicate items. # Ensure we don't have duplicate items.
if p and p not in history: if p and p not in history:
history.append(p) history.append(p)