diff --git a/server/entertainment_decider/common.py b/server/entertainment_decider/common.py index e6028f3..a3614c9 100644 --- a/server/entertainment_decider/common.py +++ b/server/entertainment_decider/common.py @@ -1,36 +1,5 @@ -import sys from typing import ( Optional, Sequence, TypeVar, ) - - -# 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) diff --git a/server/entertainment_decider/common/__init__.py b/server/entertainment_decider/common/__init__.py index b5f8668..0699a53 100644 --- a/server/entertainment_decider/common/__init__.py +++ b/server/entertainment_decider/common/__init__.py @@ -11,6 +11,9 @@ from ._itertools import ( from ._setting_handler import ( update_bool_value, ) +from ._string import ( + trim, +) from ._subprocess import ( call, ) @@ -24,5 +27,6 @@ __all__ = [ "iter_lookahead", "limit_iter", "to_just_number", + "trim", "update_bool_value", ] diff --git a/server/entertainment_decider/common/_string.py b/server/entertainment_decider/common/_string.py new file mode 100644 index 0000000..ab78f0c --- /dev/null +++ b/server/entertainment_decider/common/_string.py @@ -0,0 +1,31 @@ +import sys + + +# 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)