Tag mapper for adding books from the GUI

This commit is contained in:
Kovid Goyal 2015-09-24 20:53:19 +05:30
parent 28dcce5418
commit b63766fdc5
5 changed files with 45 additions and 9 deletions

View File

@ -298,6 +298,9 @@ class Adder(QObject):
break break
if mi.application_id == '__calibre_dummy__': if mi.application_id == '__calibre_dummy__':
mi.application_id = None mi.application_id = None
if gprefs.get('tag_map_on_add_rules'):
from calibre.ebooks.metadata.tag_mapper import map_tags
mi.tags = map_tags(mi.tags, gprefs['tag_map_on_add_rules'])
self.pd.msg = mi.title self.pd.msg = mi.title

View File

@ -62,15 +62,15 @@ class Worker(Thread):
files = [x for x in os.listdir(self.path) if files = [x for x in os.listdir(self.path) if
# Must not be in the process of being added to the db # Must not be in the process of being added to the db
x not in self.staging x not in self.staging and
# Firefox creates 0 byte placeholder files when downloading # Firefox creates 0 byte placeholder files when downloading
and os.stat(os.path.join(self.path, x)).st_size > 0 os.stat(os.path.join(self.path, x)).st_size > 0 and
# Must be a file # Must be a file
and os.path.isfile(os.path.join(self.path, x)) os.path.isfile(os.path.join(self.path, x)) and
# Must have read and write permissions # Must have read and write permissions
and os.access(os.path.join(self.path, x), os.R_OK|os.W_OK) os.access(os.path.join(self.path, x), os.R_OK|os.W_OK) and
# Must be a known ebook file type # Must be a known ebook file type
and os.path.splitext(x)[1][1:].lower() in self.allowed os.path.splitext(x)[1][1:].lower() in self.allowed
] ]
data = {} data = {}
# Give any in progress copies time to complete # Give any in progress copies time to complete
@ -201,8 +201,11 @@ class AutoAdder(QObject):
mi = os.path.join(tdir, 'metadata.opf') mi = os.path.join(tdir, 'metadata.opf')
if not os.access(mi, os.R_OK): if not os.access(mi, os.R_OK):
continue continue
mi = [OPF(open(mi, 'rb'), tdir, mi = OPF(open(mi, 'rb'), tdir, populate_spine=False).to_book_metadata()
populate_spine=False).to_book_metadata()] if gprefs.get('tag_map_on_add_rules'):
from calibre.ebooks.metadata.tag_mapper import map_tags
mi.tags = map_tags(mi.tags, gprefs['tag_map_on_add_rules'])
mi = [mi]
dups, ids = m.add_books(paths, dups, ids = m.add_books(paths,
[os.path.splitext(fname)[1][1:].upper()], mi, [os.path.splitext(fname)[1][1:].upper()], mi,
add_duplicates=not gprefs['auto_add_check_for_duplicates'], add_duplicates=not gprefs['auto_add_check_for_duplicates'],
@ -268,4 +271,3 @@ class AutoAdder(QObject):
def do_auto_convert(self, added_ids): def do_auto_convert(self, added_ids):
gui = self.parent() gui = self.parent()
gui.iactions['Convert Books'].auto_convert_auto_add(added_ids) gui.iactions['Convert Books'].auto_convert_auto_add(added_ids)

View File

@ -50,6 +50,17 @@ class ConfigWidget(ConfigWidgetBase, Ui_Form):
for signal in ('Activated', 'Changed', 'DoubleClicked', 'Clicked'): for signal in ('Activated', 'Changed', 'DoubleClicked', 'Clicked'):
signal = getattr(self.opt_blocked_auto_formats, 'item'+signal) signal = getattr(self.opt_blocked_auto_formats, 'item'+signal)
signal.connect(self.blocked_auto_formats_changed) signal.connect(self.blocked_auto_formats_changed)
self.tag_map_rules = None
self.tag_map_rules_button.clicked.connect(self.change_tag_map_rules)
def change_tag_map_rules(self):
from calibre.gui2.tag_mapper import RulesDialog
d = RulesDialog(self)
if gprefs.get('tag_map_on_add_rules'):
d.rules = gprefs['tag_map_on_add_rules']
if d.exec_() == d.Accepted:
self.tag_map_rules = d.rules
self.changed_signal.emit()
def choose_aa_path(self): def choose_aa_path(self):
path = choose_dir(self, 'auto add path choose', path = choose_dir(self, 'auto add path choose',
@ -64,6 +75,7 @@ class ConfigWidget(ConfigWidgetBase, Ui_Form):
self.filename_pattern.blockSignals(False) self.filename_pattern.blockSignals(False)
self.init_blocked_auto_formats() self.init_blocked_auto_formats()
self.opt_automerge.setEnabled(self.opt_add_formats_to_existing.isChecked()) self.opt_automerge.setEnabled(self.opt_add_formats_to_existing.isChecked())
self.tag_map_rules = None
# Blocked auto formats {{{ # Blocked auto formats {{{
def blocked_auto_formats_changed(self, *args): def blocked_auto_formats_changed(self, *args):
@ -133,6 +145,11 @@ class ConfigWidget(ConfigWidgetBase, Ui_Form):
changed = set(fmts) != set(old) changed = set(fmts) != set(old)
if changed: if changed:
gprefs['blocked_auto_formats'] = self.current_blocked_auto_formats gprefs['blocked_auto_formats'] = self.current_blocked_auto_formats
if self.tag_map_rules is not None:
if self.tag_map_rules:
gprefs['tag_map_on_add_rules'] = self.tag_map_rules
else:
gprefs.pop('tag_map_on_add_rules', None)
ret = ConfigWidgetBase.commit(self) ret = ConfigWidgetBase.commit(self)
return changed or ret return changed or ret

View File

@ -68,7 +68,7 @@
</property> </property>
</widget> </widget>
</item> </item>
<item row="8" column="0" colspan="3"> <item row="9" column="0" colspan="3">
<widget class="QGroupBox" name="metadata_box"> <widget class="QGroupBox" name="metadata_box">
<property name="title"> <property name="title">
<string>&amp;Configure metadata from file name</string> <string>&amp;Configure metadata from file name</string>
@ -175,6 +175,16 @@ Title match ignores leading indefinite articles (&quot;the&quot;, &quot;a&quot;,
</property> </property>
</widget> </widget>
</item> </item>
<item row="8" column="0">
<widget class="QPushButton" name="tag_map_rules_button">
<property name="toolTip">
<string>Create rules that can filter or transform tags on added books automatically as soon as they are added. </string>
</property>
<property name="text">
<string>Create &amp;rules to filter/transform tags</string>
</property>
</widget>
</item>
</layout> </layout>
</widget> </widget>
<widget class="QWidget" name="tab_4"> <widget class="QWidget" name="tab_4">

View File

@ -336,6 +336,10 @@ class RulesDialog(Dialog):
self.edit_widget.rules = rules self.edit_widget.rules = rules
def save_ruleset(self): def save_ruleset(self):
if not self.rules:
error_dialog(self, _('No rules'), _(
'Cannot save as no rules have been created'), show=True)
return
text, ok = QInputDialog.getText(self, _('Save ruleset as'), _( text, ok = QInputDialog.getText(self, _('Save ruleset as'), _(
'Enter a name for this ruleset:'), text=self.loaded_ruleset or '') 'Enter a name for this ruleset:'), text=self.loaded_ruleset or '')
if ok and text: if ok and text: