diff options
| author | 2020-05-06 22:39:31 -0700 | |
|---|---|---|
| committer | 2020-06-13 11:21:06 -0700 | |
| commit | 6ec3c712113d350cc027a503ebb0951cfa2fd65a (patch) | |
| tree | 8a46bf0d4a8a0020f31d2c9e684b8f26f27fa413 /bot/cogs/codeblock/parsing.py | |
| parent | Code block: use same lang specifier as the user for the py example (diff) | |
Code block: add trace logging
Diffstat (limited to '')
| -rw-r--r-- | bot/cogs/codeblock/parsing.py | 11 | 
1 files changed, 11 insertions, 0 deletions
| diff --git a/bot/cogs/codeblock/parsing.py b/bot/cogs/codeblock/parsing.py index 9adb4e0ab..7409653d7 100644 --- a/bot/cogs/codeblock/parsing.py +++ b/bot/cogs/codeblock/parsing.py @@ -51,6 +51,8 @@ def find_code_blocks(message: str) -> Sequence[CodeBlock]:      return an empty sequence. This is based on the assumption that if the user managed to get      one code block right, they already know how to fix the rest themselves.      """ +    log.trace("Finding all code blocks in a message.") +      code_blocks = []      for match in RE_CODE_BLOCK.finditer(message):          # Used to ensure non-matched groups have an empty string as the default value. @@ -58,16 +60,20 @@ def find_code_blocks(message: str) -> Sequence[CodeBlock]:          language = groups["lang"].strip()  # Strip the newline cause it's included in the group.          if groups["tick"] == BACKTICK and language: +            log.trace("Message has a valid code block with a language; returning empty tuple.")              return ()          elif len(groups["code"].split("\n", 3)) > 3:              code_block = CodeBlock(groups["code"], language, groups["tick"])              code_blocks.append(code_block) +        else: +            log.trace("Skipped a code block shorter than 4 lines.")      return code_blocks  def is_python_code(content: str) -> bool:      """Return True if `content` is valid Python consisting of more than just expressions.""" +    log.trace("Checking if content is Python code.")      try:          # Attempt to parse the message into an AST node.          # Invalid Python code will raise a SyntaxError. @@ -80,6 +86,7 @@ def is_python_code(content: str) -> bool:      # This check is to avoid all nodes being parsed as expressions.      # (e.g. words over multiple lines)      if not all(isinstance(node, ast.Expr) for node in tree.body): +        log.trace("Code is valid python.")          return True      else:          log.trace("Code consists only of expressions.") @@ -88,12 +95,16 @@ def is_python_code(content: str) -> bool:  def is_repl_code(content: str, threshold: int = 3) -> bool:      """Return True if `content` has at least `threshold` number of Python REPL-like lines.""" +    log.trace(f"Checking if content is Python REPL code using a threshold of {threshold}.") +      repl_lines = 0      for line in content.splitlines():          if line.startswith(">>> ") or line.startswith("... "):              repl_lines += 1          if repl_lines == threshold: +            log.trace("Content is Python REPL code.")              return True +    log.trace("Content is not Python REPL code.")      return False | 
