6599: Add regular expression option to rules using the match_substring method

svn: r21935
This commit is contained in:
Nick Hall 2013-04-09 13:52:09 +00:00
parent b5997bdb45
commit 59d9bc25c0
11 changed files with 46 additions and 8 deletions

View File

@ -141,8 +141,8 @@ class FilterList(object):
f.write(' comment="%s"' % self.fix(comment))
f.write('>\n')
for rule in the_filter.get_rules():
f.write(' <rule class="%s">\n'
% rule.__class__.__name__)
f.write(' <rule class="%s" use_regex="%s">\n'
% (rule.__class__.__name__, rule.use_regex))
for value in list(rule.values()):
f.write(' <arg value="%s"/>\n' % self.fix(value))
f.write(' </rule>\n')

View File

@ -55,6 +55,7 @@ class FilterParser(handler.ContentHandler):
self.a = []
self.cname = None
self.namespace = 'Person'
self.use_regex = False
def setDocumentLocator(self, locator):
self.locator = locator
@ -83,6 +84,10 @@ class FilterParser(handler.ContentHandler):
self.f.set_comment(attrs['comment'])
self.gfilter_list.add(self.namespace, self.f)
elif tag == "rule":
if attrs.has_key('use_regex'):
self.use_regex = attrs['use_regex'] == 'True'
else:
self.use_regex = False
save_name = attrs['class']
if save_name in old_names_2_class:
self.r = old_names_2_class[save_name]
@ -116,7 +121,7 @@ class FilterParser(handler.ContentHandler):
"Trying to load with subset of arguments.") %\
self.f.get_name())
nargs = len(self.r.labels)
rule = self.r(self.a[0:nargs])
rule = self.r(self.a[0:nargs], self.use_regex)
self.f.add_rule(rule)
else:
if len(self.r.labels) > len(self.a):
@ -125,7 +130,7 @@ class FilterParser(handler.ContentHandler):
"will be upgraded.") %\
self.f.get_name())
try:
rule = self.r(self.a)
rule = self.r(self.a, self.use_regex)
except AssertionError as msg:
print(msg)
print(_("ERROR: filter %s could not be correctly loaded. "

View File

@ -54,6 +54,7 @@ class HasCitationBase(Rule):
name = _('Citations matching parameters')
description = _("Matches citations with particular parameters")
category = _('Citation/source filters')
allow_regex = True
def prepare(self, db):
self.date = None

View File

@ -55,6 +55,7 @@ class HasEventBase(Rule):
name = 'Events matching parameters'
description = "Matches events with particular parameters"
category = _('Event filters')
allow_regex = True
def prepare(self, db):
self.date = None

View File

@ -52,6 +52,7 @@ class HasSourceBase(Rule):
name = 'Sources matching parameters'
description = "Matches sources with particular parameters"
category = _('Citation/source filters')
allow_regex = True
def apply(self,db,source):
if not self.match_substring(0,source.get_title()):

View File

@ -56,6 +56,7 @@ class Rule(object):
name = ''
category = _('Miscellaneous filters')
description = _('No description')
allow_regex = False
def __init__(self, arg, use_regex=False):
self.list = []

View File

@ -53,6 +53,7 @@ class HasCitation(Rule):
name = _('Citations matching parameters')
category = _('General filters')
description = _("Matches citations with particular parameters")
allow_regex = True
def prepare(self, db):
self.date = None

View File

@ -53,6 +53,7 @@ class HasMedia(Rule):
name = _('Media objects matching parameters')
description = _("Matches media objects with particular parameters")
category = _('General filters')
allow_regex = True
def prepare(self,db):
self.date = None

View File

@ -60,6 +60,7 @@ class HasPlace(Rule):
name = _('Places matching parameters')
description = _("Matches places with particular parameters")
category = _('General filters')
allow_regex = True
def apply(self, db, place):
if not self.match_substring(0, place.get_title()):

View File

@ -53,6 +53,7 @@ class HasRepo(Rule):
name = _('Repositories matching parameters')
description = _("Matches Repositories with particular parameters")
category = _('General filters')
allow_regex = True
def prepare(self, dummy_db):
if self.list[1]:

View File

@ -483,7 +483,6 @@ class EditRule(ManagedWindow):
arglist = class_obj.labels
vallist = []
tlist = []
self.page.append((class_obj, vallist, tlist))
pos = 0
l2 = Gtk.Label(label=class_obj.name)
l2.set_alignment(0, 0.5)
@ -558,6 +557,27 @@ class EditRule(ManagedWindow):
table.attach(l, 1, 2, pos, pos+1, Gtk.AttachOptions.FILL, 0, 5, 5)
table.attach(t, 2, 3, pos, pos+1, Gtk.AttachOptions.EXPAND|Gtk.AttachOptions.FILL, 0, 5, 5)
pos += 1
use_regex = None
if class_obj.allow_regex:
use_regex = Gtk.CheckButton(_('Use regular expressions'))
tip = _('Interpret the contents of string fields as regular '
'expressions.\n'
'A decimal point will match any character. '
'A question mark will match zero or one occurences '
'of the previous character or group. '
'An asterisk will match zero or more occurences. '
'A plus sign will match one or more occurences. '
'Use parentheses to group expressions. '
'Specify alternatives using a vertical bar. '
'A caret will match the start of a line. '
'A dollar sign will match the end of a line.')
use_regex.set_tooltip_text(tip)
table.attach(use_regex, 2, 3, pos, pos+1,
Gtk.AttachOptions.FILL, 0, 5, 5)
self.page.append((class_obj, vallist, tlist, use_regex))
# put the table into a scrollable area:
scrolled_win = Gtk.ScrolledWindow()
scrolled_win.add_with_viewport(table)
@ -613,10 +633,12 @@ class EditRule(ManagedWindow):
page = self.class2page[self.active_rule.__class__]
self.notebook.set_current_page(page)
self.display_values(self.active_rule.__class__)
(class_obj, vallist, tlist) = self.page[page]
(class_obj, vallist, tlist, use_regex) = self.page[page]
r = list(self.active_rule.values())
for i in range(0, min(len(tlist), len(r))):
tlist[i].set_text(r[i])
if class_obj.allow_regex:
use_regex.set_active(self.active_rule.use_regex)
self.selection.connect('changed', self.on_node_selected)
self.rname.connect('button-press-event', self._button_press)
@ -700,9 +722,12 @@ class EditRule(ManagedWindow):
try:
page = self.notebook.get_current_page()
(class_obj, vallist, tlist) = self.page[page]
(class_obj, vallist, tlist, use_regex) = self.page[page]
value_list = [cuni(sclass.get_text()) for sclass in tlist]
new_rule = class_obj(value_list)
if class_obj.allow_regex:
new_rule = class_obj(value_list, use_regex.get_active())
else:
new_rule = class_obj(value_list)
self.update_rule(self.active_rule, new_rule)
self.close()