Clean up indentation from docstrings. Any whitespace that can be uniformly removed from the second line onwards is removed.
(doc)
| 861 | return cleandoc(doc) |
| 862 | |
| 863 | def cleandoc(doc): |
| 864 | """Clean up indentation from docstrings. |
| 865 | |
| 866 | Any whitespace that can be uniformly removed from the second line |
| 867 | onwards is removed.""" |
| 868 | try: |
| 869 | lines = doc.expandtabs().split('\n') |
| 870 | except UnicodeError: |
| 871 | return None |
| 872 | else: |
| 873 | # Find minimum indentation of any non-blank lines after first line. |
| 874 | margin = sys.maxsize |
| 875 | for line in lines[1:]: |
| 876 | content = len(line.lstrip()) |
| 877 | if content: |
| 878 | indent = len(line) - content |
| 879 | margin = min(margin, indent) |
| 880 | # Remove indentation. |
| 881 | if lines: |
| 882 | lines[0] = lines[0].lstrip() |
| 883 | if margin < sys.maxsize: |
| 884 | for i in range(1, len(lines)): lines[i] = lines[i][margin:] |
| 885 | # Remove any trailing or leading blank lines. |
| 886 | while lines and not lines[-1]: |
| 887 | lines.pop() |
| 888 | while lines and not lines[0]: |
| 889 | lines.pop(0) |
| 890 | return '\n'.join(lines) |
| 891 | |
| 892 | def getfile(object): |
| 893 | """Work out which source or compiled file an object was defined in.""" |
no test coverage detected