Source code for node_regex_example

# This file is part of Sympathy for Data.
# Copyright (c) 2023, Combine Control Systems AB
#
# Sympathy for Data is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, version 3 of the License.
#
# Sympathy for Data is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Sympathy for Data.  If not, see <http://www.gnu.org/licenses/>.
import re

from sympathy.api import node as synode
from sympathy.api.nodeconfig import Port, Ports, Tag, Tags

from sylib.library.nodes.examples.node_examples import EXAMPLE_NODEIDS


[docs]class RegexExample(synode.Node): """ This example node demonstrates how to work with regular expressions (regex). For more information about how to write regex, see :ref:`appendix_regex`. Specify a *Pattern* to be matched in the source *Text*. Set the mode to be either *Search* or *Replace*. When in *Search* mode, the first match will be returned if it can be found anywhere in the text. In case of no match, the node will return an empty string. When in 'Replace' mode, all matches will be replaced by the *Replacement* string. There will be no replacement in case of no match, and the node will return the original text. """ name = 'Regex example' description = 'Node demonstrating how to work with regex.' icon = 'example.svg' nodeid = 'org.sysess.sympathy.examples.regex' author = 'Jin Guo' version = '1.0' tags = Tags(Tag.Development.Example) related = EXAMPLE_NODEIDS parameters = synode.parameters() parameters.set_string('mode', label='Mode', value='Search', description='Search for the first match of the ' 'pattern anywhere in the string, or ' 'replace at all matches by replacement.', editor=synode.editors.combo_editor( options=['Search', 'Replace'])) parameters.set_string('src_text', label='Text', value='', description='Source text to be matched by pattern.', editor=synode.editors.textedit_editor()) parameters.set_string('pattern', label='Pattern', description='Regex pattern.', editor=synode.editors.lineedit_editor()) parameters.set_string('repl', label='Replacement', description='Replacement string.', editor=synode.editors.lineedit_editor()) controllers = ( synode.controller( when=synode.field('mode', 'value', 'Replace'), action=synode.field('repl', 'enabled'))) outputs = Ports( [Port.Custom('text', 'Output Text', name='output', preview=True)]) def execute(self, ctx): mode = ctx.parameters['mode'].value src_text = ctx.parameters['src_text'].value pattern = re.compile(ctx.parameters['pattern'].value) if mode == 'Search': match = pattern.search(src_text) output_text = match.group() if match else '' else: repl = ctx.parameters['repl'].value output_text = pattern.sub(repl, src_text) output = ctx.output['output'] output.set(output_text)