From 51caa0ea042444f996f8649a1a2fbfe640697bbf Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Fri, 21 Jul 2017 11:16:44 +0530 Subject: [PATCH] Allow Export/Import of custom list templates --- src/calibre/gui2/preferences/server.py | 34 ++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/src/calibre/gui2/preferences/server.py b/src/calibre/gui2/preferences/server.py index 31a4a49ffc..f52fd70f63 100644 --- a/src/calibre/gui2/preferences/server.py +++ b/src/calibre/gui2/preferences/server.py @@ -18,7 +18,8 @@ from PyQt5.Qt import ( from calibre import as_unicode from calibre.gui2 import ( - config, error_dialog, gprefs, info_dialog, open_url, warning_dialog + choose_files, choose_save_file, config, error_dialog, gprefs, info_dialog, + open_url, warning_dialog ) from calibre.gui2.preferences import AbortCommit, ConfigWidgetBase, test_widget from calibre.srv.code import custom_list_template as default_custom_list_template @@ -778,6 +779,29 @@ class CustomList(QWidget): # {{{ self.template = t = QPlainTextEdit(self) l.addRow(t) t.textChanged.connect(self.changed_signal) + self.imex = bb = QDialogButtonBox(self) + b = bb.addButton(_('&Import template'), bb.ActionRole) + b.clicked.connect(self.import_template) + b = bb.addButton(_('E&xport template'), bb.ActionRole) + b.clicked.connect(self.export_template) + l.addRow(bb) + + def import_template(self): + paths = choose_files(self, 'custom-list-template', _('Choose template file'), + filters=[(_('Template files'), ['json'])], all_files=False, select_only_single_file=True) + if paths: + with lopen(paths[0], 'rb') as f: + raw = f.read() + self.current_template = self.deserialize(raw) + + def export_template(self): + path = choose_save_file( + self, 'custom-list-template', _('Choose template file'), + filters=[(_('Template files'), ['json'])], initial_filename='custom-list-template.json') + if path: + raw = self.serialize(self.current_template) + with lopen(path, 'wb') as f: + f.write(raw) def thumbnail_state_changed(self): is_enabled = bool(self.thumbnail.isChecked()) @@ -810,6 +834,12 @@ class CustomList(QWidget): # {{{ self.comments_fields.setText(', '.join(template.get('comments_fields') or ())) self.template.setPlainText('\n'.join(template.get('lines') or ())) + def serialize(self, template): + return json.dumps(template, sort_keys=True, indent=4, separators=(',', ': '), ensure_ascii=True) + + def deserialize(self, raw): + return json.loads(raw) + def restore_defaults(self): self.current_template = self.default_template @@ -822,7 +852,7 @@ class CustomList(QWidget): # {{{ if err.errno != errno.ENOENT: raise else: - raw = json.dumps(template, sort_keys=True, indent=4, separators=(',', ': ')) + raw = self.serialize(template) with lopen(custom_list_template.path, 'wb') as f: f.write(raw) return True