diff --git a/server/entertainment_decider/common.py b/server/entertainment_decider/common.py index 6712af2..327cad7 100644 --- a/server/entertainment_decider/common.py +++ b/server/entertainment_decider/common.py @@ -1,6 +1,7 @@ from datetime import date, datetime import itertools import subprocess +import sys from typing import ( IO, Iterable, @@ -28,6 +29,36 @@ def call( return proc +# source: https://peps.python.org/pep-0257/#handling-docstring-indentation +def trim(docstring: str) -> str: + """ + Trim strings like docstrings are trimmed following PEP 257 + """ + if not docstring: + return "" + # Convert tabs to spaces (following the normal Python rules) + # and split into a list of lines: + lines = docstring.expandtabs().splitlines() + # Determine minimum indentation (first line doesn't count): + indent = sys.maxsize + for line in lines[1:]: + stripped = line.lstrip() + if stripped: + indent = min(indent, len(line) - len(stripped)) + # Remove indentation (first line is special): + trimmed = [lines[0].strip()] + if indent < sys.maxsize: + for line in lines[1:]: + trimmed.append(line[indent:].rstrip()) + # Strip off trailing and leading blank lines: + while trimmed and not trimmed[-1]: + trimmed.pop() + while trimmed and not trimmed[0]: + trimmed.pop(0) + # Return a single string: + return "\n".join(trimmed) + + def update_bool_value( old_value: bool, new_value: Union[bool, Literal["toggle"]] ) -> bool: