Debian Codegraph @

src/calibre/gui2/series_mapper.py

raw
1 #!/usr/bin/env python
2 # License: GPL v3 Copyright: 2018, Kovid Goyal <kovid at kovidgoyal.net>
3
4 from collections import OrderedDict
5
6 from calibre.ebooks.metadata.tag_mapper import map_tags
7 from calibre.gui2 import Application, elided_text
8 from calibre.gui2.tag_mapper import RuleEdit as RuleEditBase
9 from calibre.gui2.tag_mapper import RuleEditDialog as RuleEditDialogBase
10 from calibre.gui2.tag_mapper import RuleItem as RuleItemBase
11 from calibre.gui2.tag_mapper import Rules as RulesBase
12 from calibre.gui2.tag_mapper import RulesDialog as RulesDialogBase
13 from calibre.gui2.tag_mapper import Tester as TesterBase
14 from calibre.utils.config import JSONConfig
15 from calibre.utils.localization import _
16
17 series_maps = JSONConfig('series-mapping-rules')
18
19
20 class RuleEdit(RuleEditBase):
21 ACTION_MAP = OrderedDict((
22 ('replace', _('Change')),
23 ('capitalize', _('Capitalize')),
24 ('titlecase', _('Title-case')),
25 ('lower', _('Lower-case')),
26 ('upper', _('Upper-case')),
27 ))
28
29 MATCH_TYPE_MAP = OrderedDict((
30 ('one_of', _('is')),
31 ('not_one_of', _('is not')),
32 ('has', _('contains')),
33 ('matches', _('matches regex pattern')),
34 ('not_matches', _('does not match regex pattern')),
35 ))
36
37 MSG = _('Create the rule below, the rule can be used to modify series')
38 SUBJECT = _('the series, if the series name')
39 VALUE_ERROR = _('You must provide a value for the series name to match')
40 REPLACE_TEXT = _('with the name:')
41 SINGLE_EDIT_FIELD_NAME = 'series'
42
43 @property
44 def can_use_tag_editor(self):
45 return False
46
47 def update_state(self):
48 a = self.action.currentData()
49 replace = a == 'replace'
50 self.la3.setVisible(replace), self.replace.setVisible(replace)
51 m = self.match_type.currentData()
52 is_match = 'matches' in m
53 self.regex_help.setVisible(is_match)
54
55 @property
56 def rule(self):
57 return {
58 'action': self.action.currentData(),
59 'match_type': self.match_type.currentData(),
60 'query': self.query.text().strip(),
61 'replace': self.replace.text().strip(),
62 }
63
64 @rule.setter
65 def rule(self, rule):
66 def sc(name):
67 c = getattr(self, name)
68 idx = c.findData(str(rule.get(name, '')))
69 idx = max(idx, 0)
70 c.setCurrentIndex(idx)
71
72 sc('match_type'), sc('action')
73 self.query.setText(str(rule.get('query', '')).strip())
74 self.replace.setText(str(rule.get('replace', '')).strip())
75
76
77 class RuleEditDialog(RuleEditDialogBase):
78 PREFS_NAME = 'edit-series-mapping-rule'
79 RuleEditClass = RuleEdit
80
81
82 class RuleItem(RuleItemBase):
83 @staticmethod
84 def text_from_rule(rule, parent):
85 query = elided_text(rule['query'], font=parent.font(), width=200, pos='right')
86 text = _('<b>{action}</b> the series name, if it <i>{match_type}</i>: <b>{query}</b>').format(
87 action=RuleEdit.ACTION_MAP[rule['action']],
88 match_type=RuleEdit.MATCH_TYPE_MAP[rule['match_type']],
89 query=query,
90 )
91 if rule['action'] == 'replace':
92 text += '<br>' + _('to the name') + ' <b>{}</b>'.format(rule['replace'])
93 return '<div style="white-space: nowrap">' + text + '</div>'
94
95
96 class Rules(RulesBase):
97 RuleItemClass = RuleItem
98 RuleEditDialogClass = RuleEditDialog
99 MSG = _(
100 'You can specify rules to manipulate series names here.'
101 ' Click the "Add Rule" button'
102 ' below to get started. The rules will be processed in order for every series.'
103 )
104
105
106 class Tester(TesterBase):
107 DIALOG_TITLE = _('Test series mapping rules')
108 PREFS_NAME = 'test-series-mapping-rules'
109 LABEL = _('Enter a series name to test:')
110 PLACEHOLDER = _('Enter series and click the "Test" button')
111 EMPTY_RESULT = '<p>&nbsp;</p>'
112
113 def do_test(self):
114 series = self.value.strip()
115 ans = map_tags([series], self.rules, separator='')
116 self.result_widget.setText((ans or ('',))[0])
117
118
119 class RulesDialog(RulesDialogBase):
120 DIALOG_TITLE = _('Edit series mapping rules')
121 PREFS_NAME = 'edit-series-mapping-rules'
122 RulesClass = Rules
123 TesterClass = Tester
124 PREFS_OBJECT = series_maps
125
126
127 if __name__ == '__main__':
128 app = Application([])
129 d = RulesDialog()
130 d.rules = [
131 {'action': 'replace', 'query': 'alice Bob, moose', 'match_type': 'one_of', 'replace': 'Alice Bob, Moose'},
132 ]
133 d.exec()
134 from pprint import pprint
135
136 pprint(d.rules)
137 del d, app