From 71054bc8210cd844b75bc787ec5a6ec4735a5ddc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Kucharczyk?= Date: Wed, 4 May 2022 14:43:44 +0200 Subject: [PATCH] Make it possible to parse text or file --- src/changelog/changelog.py | 66 ++++++++++++++++++++++++-------------- 1 file changed, 42 insertions(+), 24 deletions(-) diff --git a/src/changelog/changelog.py b/src/changelog/changelog.py index 174fb48..1482dce 100644 --- a/src/changelog/changelog.py +++ b/src/changelog/changelog.py @@ -39,37 +39,55 @@ def cli(): pass -def parse_text(input_text: str = ""): +def parse(input_text: str = ""): """Parses text for Jira or Bugzilla issue numbers and creates entries from them""" if input_text == "": - click.echo("No text provided for parsing.") - for line in input_text: - line = line.strip() - if results := re.findall(search_pattern, line, re.IGNORECASE): - logging.debug(f"Found matches: {results}") - if len(results) != 1: - logging.debug("More than 1 match found.") - else: - logging.debug("Only 1 match found.") - parentheses_items: list = [] - # FIXME: add links to other mentioned sections, for now just link the first one - section_id = format_section_id(results[0]) - for match in results: - issue_number = match.split("#").pop() - if "jsc" in match: - parentheses_items.append(make_jira_url(issue_number)) - if "bsc" in match: - parentheses_items.append(make_bugzilla_url(issue_number)) - parentheses_text = ", ".join(parentheses_items) - click.echo(make_entry(section_id, parentheses_text)) + logging.error("No text provided for parsing.") + match input_text: + case [text] if type(text) == str: + logging.debug(f"{text} is a string.") + parse_text(text) + case [*line]: + logging.debug("This is a list of lines.") + for line in input_text: + line = line.strip() + parse_text(line) + case _: + logging.debug("This is nothing I recognize.") + + +@cli.command("parse-text") +@click.argument("text") +def parse_text_command(text): + parse_text(text) + + +def parse_text(line): + if results := re.findall(search_pattern, line, re.IGNORECASE): + logging.debug(f"Found matches: {results}") + if len(results) != 1: + logging.debug("More than 1 match found.") else: - logging.debug(f"Nothing found in line '{line}'. Ignoring.") + logging.debug("1 match found.") + parentheses_items: list = [] + # FIXME: add links to other mentioned sections, for now just link the first one + section_id = format_section_id(results[0]) + for match in results: + issue_number = match.split("#").pop() + if "jsc" in match: + parentheses_items.append(make_jira_url(issue_number)) + if "bsc" in match: + parentheses_items.append(make_bugzilla_url(issue_number)) + parentheses_text = ", ".join(parentheses_items) + click.echo(make_entry(section_id, parentheses_text)) + else: + logging.debug(f"Nothing found in line '{line}'. Ignoring.") @cli.command() @click.argument("filename", type=click.File()) -def parse(filename: IO): - parse_text(filename.readlines()) +def parse_file(filename: IO): + parse(filename.readlines()) if __name__ == "__main__":