(
cls,
code, # type: Text
line_count=None, # type: Optional[int]
)
| 97 | class Code(object): |
| 98 | @classmethod |
| 99 | def parse( |
| 100 | cls, |
| 101 | code, # type: Text |
| 102 | line_count=None, # type: Optional[int] |
| 103 | ): |
| 104 | # type: (...) -> Code |
| 105 | |
| 106 | lines = defaultdict(lambda: True) # type: DefaultDict[int, bool] |
| 107 | root = None # type: Optional[ast.AST] |
| 108 | try: |
| 109 | root = ast.parse(code) |
| 110 | except SyntaxError as e: |
| 111 | TRACER.log( |
| 112 | "Cannot parse script code for robust PEP-723 analysis, continuing with naive " |
| 113 | "analysis: {err}".format(err=e), |
| 114 | ) |
| 115 | if root: |
| 116 | for node in ast.walk(root): |
| 117 | start_line = getattr(node, "lineno", 0) |
| 118 | if start_line: |
| 119 | end_line = getattr(node, "end_lineno", 0) |
| 120 | # For Python<3.8 multiline string literals were represented in Str nodes with |
| 121 | # the lineno being the _last_ line of the string literal. We calculate the first |
| 122 | # line number by subtracting the height of the multiline text block. |
| 123 | if not end_line and node.__class__.__name__ == "Str": |
| 124 | line_count = len(getattr(node, "s", "").splitlines()) |
| 125 | if line_count > 1: |
| 126 | end_line = start_line |
| 127 | start_line = end_line - len(getattr(node, "s", "").splitlines()) |
| 128 | if end_line > start_line: |
| 129 | for offset in range(end_line - start_line): |
| 130 | lines[start_line + offset - 1] = False |
| 131 | else: |
| 132 | lines[start_line - 1] = False |
| 133 | |
| 134 | return cls(lines) |
| 135 | |
| 136 | # True marks a whitespace or comment line. Everything else is code. |
| 137 | lines = attr.ib() # type: DefaultDict[int, bool] |
no test coverage detected