Make it possible to parse text or file

This commit is contained in:
Lukáš Kucharczyk 2022-05-04 14:43:44 +02:00
parent b0cd7b6b09
commit 71054bc821
1 changed files with 42 additions and 24 deletions

View File

@ -39,18 +39,36 @@ def cli():
pass 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""" """Parses text for Jira or Bugzilla issue numbers and creates entries from them"""
if input_text == "": if input_text == "":
click.echo("No text provided for parsing.") 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: for line in input_text:
line = line.strip() 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): if results := re.findall(search_pattern, line, re.IGNORECASE):
logging.debug(f"Found matches: {results}") logging.debug(f"Found matches: {results}")
if len(results) != 1: if len(results) != 1:
logging.debug("More than 1 match found.") logging.debug("More than 1 match found.")
else: else:
logging.debug("Only 1 match found.") logging.debug("1 match found.")
parentheses_items: list = [] parentheses_items: list = []
# FIXME: add links to other mentioned sections, for now just link the first one # FIXME: add links to other mentioned sections, for now just link the first one
section_id = format_section_id(results[0]) section_id = format_section_id(results[0])
@ -68,8 +86,8 @@ def parse_text(input_text: str = ""):
@cli.command() @cli.command()
@click.argument("filename", type=click.File()) @click.argument("filename", type=click.File())
def parse(filename: IO): def parse_file(filename: IO):
parse_text(filename.readlines()) parse(filename.readlines())
if __name__ == "__main__": if __name__ == "__main__":