def select(self, text: str): if not self._dialog: self._dialog = PluginConfigPreviewDialog(self._context, self.config.clone(), "Reformat Text", self._codec.reformat, qtawesome.icon("fa.edit")) self._dialog.setInput(text) self._dialog_return_code = self._dialog.exec_() if self._dialog_return_code != QDialog.Accepted: # User clicked the Cancel-Button. raise AbortedException("Aborted") self.config.update(self._dialog.config) return self.run(text)
class Plugin(ScriptPlugin): """ Opens a dialog to filter text by certain conditions. """ class Option(object): Filter_Term = PluginConfig.Option.Label("filter_term", "Filter:") Should_Match_Case = PluginConfig.Option.Label("should_match_case", "Match Case") Should_Invert_Match = PluginConfig.Option.Label( "should_invert_match", "Invert Lines") Is_Regex = PluginConfig.Option.Label("is_regex", "Regex") class FilterCodec: def run(self, config: PluginConfig, text: str): lines = [] filter_term = config.get(Plugin.Option.Filter_Term).value is_regex = config.get(Plugin.Option.Is_Regex).value should_match_case = config.get( Plugin.Option.Should_Match_Case).value for text_line in text.splitlines(): try: if self._should_filter(text_line, config): if is_regex and should_match_case: match = re.match(filter_term, text_line) lines.append(match.group(0)) elif is_regex: match = re.match(filter_term, text_line, flags=re.IGNORECASE) lines.append(match.group(0)) else: lines.append(text_line) except Exception as e: # Ignore exceptions - most likely an error in the regex filter string pass return os.linesep.join(lines) def _should_filter(self, text_line: str, config: PluginConfig): filter_term = config.get(Plugin.Option.Filter_Term).value is_regex = config.get(Plugin.Option.Is_Regex).value should_match_case = config.get( Plugin.Option.Should_Match_Case).value should_invert_match = config.get( Plugin.Option.Should_Invert_Match).value if is_regex and should_match_case: result = re.match(filter_term, text_line, flags=re.IGNORECASE) is not None elif is_regex: result = re.match(filter_term, text_line) is not None elif should_match_case: result = filter_term in text_line else: result = filter_term.lower() in text_line.lower() if should_invert_match: return not result else: return result def __init__(self, context): # Name, Author, Dependencies super().__init__('Filter Lines', "Thomas Engel", [], context) self._context = context self._logger = context.logger self._codec = Plugin.FilterCodec() self.config.add( PluginConfig.Option.String(label=Plugin.Option.Filter_Term, value="", description="term to filter by", is_required=True)) self.config.add( PluginConfig.Option.Boolean( label=Plugin.Option.Should_Match_Case, value=True, description="defines whether filter should match case", is_required=False)) self.config.add( PluginConfig.Option.Boolean( label=Plugin.Option.Should_Invert_Match, value=False, description="defines whether filter should invert match", is_required=False)) self.config.add( PluginConfig.Option.Boolean( label=Plugin.Option.Is_Regex, value=False, description="defines whether filter term is a regex", is_required=False)) self._dialog = None self._dialog_return_code = QDialog.Accepted def select(self, text: str): if not self._dialog: self._dialog = PluginConfigPreviewDialog( self._context, self.config.clone(), "Filter Lines", self._codec.run, qtawesome.icon("fa.filter")) self._dialog.setInput(text) self._dialog_return_code = self._dialog.exec_() if self._dialog_return_code != QDialog.Accepted: # User clicked the Cancel-Button. raise AbortedException("Aborted") self.config.update(self._dialog.config) return self.run(text) def title(self): return "Filter lines by '{}' using {}".format( self.config.get(Plugin.Option.Filter_Term).value, self._getOptionAsHumanReadableString()) def _getOptionAsHumanReadableString(self): options = [] if self.config.get(Plugin.Option.Should_Match_Case).value: options.append('Match Case') else: options.append('Ignore Case') if self.config.get(Plugin.Option.Is_Regex).value: options.append('Regular Expression') if self.config.get(Plugin.Option.Should_Invert_Match).value: options.append('Invert Match') return self._join_options_as_human_readable_string(options) def run(self, text: str): return self._codec.run(self.config, text)
class Plugin(ScriptPlugin): """ Opens a dialog to filter xml text by certain xpath expression. Example: Input: <a><b>text</b></a> Expression: //b Output: <b>text</b> """ class Option(object): Expression = PluginConfig.Option.Label("xpath_expression", "XPath:") class Codec: def run(self, config: PluginConfig, text: str): try: from lxml import etree expression = config.get(Plugin.Option.Expression).value root = etree.fromstring(text) return "".join( etree.tostring(item).decode('utf-8', errors="surrogateescape") for item in root.xpath(expression)) except XMLSyntaxError as e: raise Exception("Error decoding XML!") except Exception as e: # Ignore exceptions - most likely an error in the xpath expression pass def __init__(self, context): # Name, Author, Dependencies super().__init__('XPath', "Thomas Engel", ["lxml"], context) self._context = context self._logger = context.logger self._codec = Plugin.Codec() self.config.add( PluginConfig.Option.String( label=Plugin.Option.Expression, value="", description="xpath expression to filter by", is_required=True)) self._dialog = None self._dialog_return_code = QDialog.Accepted def select(self, text: str): if not self._dialog: self._dialog = PluginConfigPreviewDialog( self._context, self.config.clone(), "XPath", self._codec.run, qtawesome.icon("fa.filter")) self._dialog.setInput(text) self._dialog_return_code = self._dialog.exec_() if self._dialog_return_code != QDialog.Accepted: # User clicked the Cancel-Button. raise AbortedException("Aborted") self.config.update(self._dialog.config) return self.run(text) def title(self): return "Filter by XPath expression '{}'".format( self.config.get(Plugin.Option.Expression).value) def run(self, text: str): return self._codec.run(self.config, text)
class Plugin(ScriptPlugin): """ Opens a dialog to filter xml text by certain jq expression. Example: Input: {"a": [{"b": "c"}, {"b": "d"}]} Expression: .a[].b Output: "c" "d" """ class Option(object): Expression = PluginConfig.Option.Label("expression", "Expression:") class Codec: def run(self, config: PluginConfig, text: str): try: import pyjq as pyjq expression = config.get(Plugin.Option.Expression).value return os.linesep.join([ json.dumps(item) for item in pyjq.all(expression, json.loads(text)) ]) except JSONDecodeError as e: raise Exception("Error decoding json!") except Exception as e: # Ignore exceptions - most likely an error in the jq expression pass def __init__(self, context): # Name, Author, Dependencies super().__init__('JQ', "Thomas Engel", ["pyjq"], context) self._context = context self._logger = context.logger self._codec = Plugin.Codec() self.config.add( PluginConfig.Option.String( label=Plugin.Option.Expression, value="", description="jq expression to filter by", is_required=True)) self._dialog = None self._dialog_return_code = QDialog.Accepted def select(self, text: str): if not self._dialog: self._dialog = PluginConfigPreviewDialog( self._context, self.config.clone(), "jq", self._codec.run, qtawesome.icon("fa.filter")) self._dialog.setInput(text) self._dialog_return_code = self._dialog.exec_() if self._dialog_return_code != QDialog.Accepted: # User clicked the Cancel-Button. raise AbortedException("Aborted") self.config.update(self._dialog.config) return self.run(text) def title(self): return "Filter by jq expression '{}'".format( self.config.get(Plugin.Option.Expression).value) def run(self, text: str): return self._codec.run(self.config, text)
class Plugin(ScriptPlugin): """ Reformats the input. Example: Input: 123 456 Parameters: Format = "{1} {0}" Split by = " " Regex = False Handle Newline = True Output: 456 123 """ class Option(object): Format = PluginConfig.Option.Label("format_string", "Format:") SplitChars = PluginConfig.Option.Label("split_chars", "Split by:") IsRegex = PluginConfig.Option.Label("is_regex", "Regex") HandleNewlines = PluginConfig.Option.Label("handle_newlines", "Handle Newlines") def __init__(self, context): # Name, Author, Dependencies super().__init__('Reformat Text', "Thomas Engel", [], context) def _validate_split_chars(config, codec, input): def _validate_regex(): try: re.compile(config.get(Plugin.Option.SplitChars).value) return True except: return False if not config.get(Plugin.Option.SplitChars).value: return "Split by should not be empty!" if config.get(Plugin.Option.IsRegex).value and not _validate_regex(): return "Invalid regular expression!" def _validate_format(config, codec, input): try: codec(config, input) except: return "Invalid format string!" self.config.add(PluginConfig.Option.String( label=Plugin.Option.Format, value="", description="the format string to be used", is_required=True ), validator=_validate_format) self.config.add(PluginConfig.Option.String( label=Plugin.Option.SplitChars, value=" ", description="the characters used to split the text in individual parts (default=' ')", is_required=False ), validator=_validate_split_chars) self.config.add(PluginConfig.Option.Boolean( label=Plugin.Option.IsRegex, value=False, description="defines whether the split chars is a regular expression (default=False)", is_required=False )) self.config.add(PluginConfig.Option.Boolean( label=Plugin.Option.HandleNewlines, value=True, description="defines whether the operation should be applied for each individual line (default=True)", is_required=False )) self._dialog = None self._codec = ReformatCodec() def select(self, text: str): if not self._dialog: self._dialog = PluginConfigPreviewDialog(self._context, self.config.clone(), "Reformat Text", self._codec.reformat, qtawesome.icon("fa.edit")) self._dialog.setInput(text) self._dialog_return_code = self._dialog.exec_() if self._dialog_return_code != QDialog.Accepted: # User clicked the Cancel-Button. raise AbortedException("Aborted") self.config.update(self._dialog.config) return self.run(text) def title(self): return "Reformat text with '{}' using '{}' as {}delimiter{}".format( self.config.get(Plugin.Option.Format).value, self.config.get(Plugin.Option.SplitChars).value, "regex-" if self.config.get(Plugin.Option.IsRegex).value else "", " (newline sensitive)" if self.config.get(Plugin.Option.HandleNewlines).value else "" ) def run(self, text): if text: return self._codec.reformat(self.config, text) return ''