| 1 |
from typing import Collection, List |
| 2 |
from sys import maxsize |
| 3 |
|
| 4 |
__all__ = [ |
| 5 |
"dedent_block_string_lines", |
| 6 |
"is_printable_as_block_string", |
| 7 |
"print_block_string", |
| 8 |
] |
| 9 |
|
| 10 |
|
| 11 |
def dedent_block_string_lines(lines: Collection[str]) -> List[str]: |
| 12 |
"""Produce the value of a block string from its parsed raw value. |
| 13 |
|
| 14 |
This function works similar to CoffeeScript's block string, |
| 15 |
Python's docstring trim or Ruby's strip_heredoc. |
| 16 |
|
| 17 |
It implements the GraphQL spec's BlockStringValue() static algorithm. |
| 18 |
|
| 19 |
Note that this is very similar to Python's inspect.cleandoc() function. |
| 20 |
The difference is that the latter also expands tabs to spaces and |
| 21 |
removes whitespace at the beginning of the first line. Python also has |
| 22 |
textwrap.dedent() which uses a completely different algorithm. |
| 23 |
|
| 24 |
For internal use only. |
| 25 |
""" |
| 26 |
common_indent = maxsize |
| 27 |
first_non_empty_line = None |
| 28 |
last_non_empty_line = -1 |
| 29 |
|
| 30 |
for i, line in enumerate(lines): |
| 31 |
indent = leading_white_space(line) |
| 32 |
|
| 33 |
if indent == len(line): |
| 34 |
continue # skip empty lines |
| 35 |
|
| 36 |
if first_non_empty_line is None: |
| 37 |
first_non_empty_line = i |
| 38 |
last_non_empty_line = i |
| 39 |
|
| 40 |
if i and indent < common_indent: |
| 41 |
common_indent = indent |
| 42 |
|
| 43 |
if first_non_empty_line is None: |
| 44 |
first_non_empty_line = 0 |
| 45 |
|
| 46 |
return [ # Remove common indentation from all lines but first. |
| 47 |
line[common_indent:] if i else line for i, line in enumerate(lines) |
| 48 |
][ # Remove leading and trailing blank lines. |
| 49 |
first_non_empty_line : last_non_empty_line + 1 |
| 50 |
] |
| 51 |
|
| 52 |
|
| 53 |
def leading_white_space(s: str) -> int: |
| 54 |
i = 0 |
| 55 |
for c in s: |
| 56 |
if c not in " \t": |
| 57 |
return i |
| 58 |
i += 1 |
| 59 |
return i |
| 60 |
|
| 61 |
|
| 62 |
def is_printable_as_block_string(value: str) -> bool: |
| 63 |
"""Check whether the given string is printable as a block string. |
| 64 |
|
| 65 |
For internal use only. |
| 66 |
""" |
| 67 |
if not isinstance(value, str): |
| 68 |
value = str(value) # resolve lazy string proxy object |
| 69 |
|
| 70 |
if not value: |
| 71 |
return True # emtpy string is printable |
| 72 |
|
| 73 |
is_empty_line = True |
| 74 |
has_indent = False |
| 75 |
has_common_indent = True |
| 76 |
seen_non_empty_line = False |
| 77 |
|
| 78 |
for c in value: |
| 79 |
if c == "\n": |
| 80 |
if is_empty_line and not seen_non_empty_line: |
| 81 |
return False # has leading new line |
| 82 |
seen_non_empty_line = True |
| 83 |
is_empty_line = True |
| 84 |
has_indent = False |
| 85 |
elif c in " \t": |
| 86 |
has_indent = has_indent or is_empty_line |
| 87 |
elif c <= "\x0f": |
| 88 |
return False |
| 89 |
else: |
| 90 |
has_common_indent = has_common_indent and has_indent |
| 91 |
is_empty_line = False |
| 92 |
|
| 93 |
if is_empty_line: |
| 94 |
return False # has trailing empty lines |
| 95 |
|
| 96 |
if has_common_indent and seen_non_empty_line: |
| 97 |
return False # has internal indent |
| 98 |
|
| 99 |
return True |
| 100 |
|
| 101 |
|
| 102 |
def print_block_string(value: str, minimize: bool = False) -> str: |
| 103 |
"""Print a block string in the indented block form. |
| 104 |
|
| 105 |
Prints a block string in the indented block form by adding a leading and |
| 106 |
trailing blank line. However, if a block string starts with whitespace and |
| 107 |
is a single-line, adding a leading blank line would strip that whitespace. |
| 108 |
|
| 109 |
For internal use only. |
| 110 |
""" |
| 111 |
if not isinstance(value, str): |
| 112 |
value = str(value) # resolve lazy string proxy object |
| 113 |
|
| 114 |
escaped_value = value.replace('"""', '\\"""') |
| 115 |
|
| 116 |
# Expand a block string's raw value into independent lines. |
| 117 |
lines = escaped_value.splitlines() or [""] |
| 118 |
num_lines = len(lines) |
| 119 |
is_single_line = num_lines == 1 |
| 120 |
|
| 121 |
# If common indentation is found, |
| 122 |
# we can fix some of those cases by adding a leading new line. |
| 123 |
force_leading_new_line = num_lines > 1 and all( |
| 124 |
not line or line[0] in " \t" for line in lines[1:] |
| 125 |
) |
| 126 |
|
| 127 |
# Trailing triple quotes just looks confusing but doesn't force trailing new line. |
| 128 |
has_trailing_triple_quotes = escaped_value.endswith('\\"""') |
| 129 |
|
| 130 |
# Trailing quote (single or double) or slash forces trailing new line |
| 131 |
has_trailing_quote = value.endswith('"') and not has_trailing_triple_quotes |
| 132 |
has_trailing_slash = value.endswith("\\") |
| 133 |
force_trailing_new_line = has_trailing_quote or has_trailing_slash |
| 134 |
|
| 135 |
print_as_multiple_lines = not minimize and ( |
| 136 |
# add leading and trailing new lines only if it improves readability |
| 137 |
not is_single_line |
| 138 |
or len(value) > 70 |
| 139 |
or force_trailing_new_line |
| 140 |
or force_leading_new_line |
| 141 |
or has_trailing_triple_quotes |
| 142 |
) |
| 143 |
|
| 144 |
# Format a multi-line block quote to account for leading space. |
| 145 |
skip_leading_new_line = is_single_line and value and value[0] in " \t" |
| 146 |
before = ( |
| 147 |
"\n" |
| 148 |
if print_as_multiple_lines |
| 149 |
and not skip_leading_new_line |
| 150 |
or force_leading_new_line |
| 151 |
else "" |
| 152 |
) |
| 153 |
after = "\n" if print_as_multiple_lines or force_trailing_new_line else "" |
| 154 |
|
| 155 |
return f'"""{before}{escaped_value}{after}"""' |