Source code for lib.string_helpers

# -*- coding: utf-8 -*-

# Copyright Martin Manns
# Distributed under the terms of the GNU General Public License

# --------------------------------------------------------------------
# pyspread is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# pyspread is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with pyspread.  If not, see <>.
# --------------------------------------------------------------------



 * :func:`quote`
 * :func:`wrap_text`


import textwrap

[docs]def quote(code: str) -> str: """Quote code :param code: Code to be quoted :return: Quoted code if not already quoted and quoting possible """ starts_and_ends = [ ("'", "'"), ('"', '"'), ("u'", "'"), ('u"', '"'), ("b'", "'"), ('b"', '"'), ("r'", "'"), ('r"', '"'), ] if code is None or not (isinstance(code, bytes) or isinstance(code, str)): return code code = code.strip() if code and not (code[0], code[-1]) in starts_and_ends: return repr(code) else: return code
[docs]def wrap_text(text, width=80, maxlen=2000): """Wrap text to line width :param text: The text to be wrapped :param width: Width of the text to be wrapped :param maxlen: Maximum total text length before text in truncated and extended by [...]. If None then truncation is disabled. :return: Wrapped text """ if text is None: return if maxlen is not None and len(text) > maxlen: text = text[:maxlen] + "..." return "\n".join(textwrap.wrap(text, width=width))