(self, pysource, multiline)
| 4524 | return ''.join(self.code_buffer) |
| 4525 | |
| 4526 | def read_code(self, pysource, multiline): |
| 4527 | code_line, comment = '', '' |
| 4528 | offset = 0 |
| 4529 | while True: |
| 4530 | m = self.re_tok.search(pysource, pos=offset) |
| 4531 | if not m: |
| 4532 | code_line += pysource[offset:] |
| 4533 | offset = len(pysource) |
| 4534 | self.write_code(code_line.strip(), comment) |
| 4535 | break |
| 4536 | code_line += pysource[offset:m.start()] |
| 4537 | offset = m.end() |
| 4538 | _str, _com, _po, _pc, _blk1, _blk2, _end, _cend, _nl = m.groups() |
| 4539 | if self.paren_depth > 0 and (_blk1 or _blk2): # a if b else c |
| 4540 | code_line += _blk1 or _blk2 |
| 4541 | continue |
| 4542 | if _str: # Python string |
| 4543 | code_line += _str |
| 4544 | elif _com: # Python comment (up to EOL) |
| 4545 | comment = _com |
| 4546 | if multiline and _com.strip().endswith(self._tokens[1]): |
| 4547 | multiline = False # Allow end-of-block in comments |
| 4548 | elif _po: # open parenthesis |
| 4549 | self.paren_depth += 1 |
| 4550 | code_line += _po |
| 4551 | elif _pc: # close parenthesis |
| 4552 | if self.paren_depth > 0: |
| 4553 | # we could check for matching parentheses here, but it's |
| 4554 | # easier to leave that to python - just check counts |
| 4555 | self.paren_depth -= 1 |
| 4556 | code_line += _pc |
| 4557 | elif _blk1: # Start-block keyword (if/for/while/def/try/...) |
| 4558 | code_line = _blk1 |
| 4559 | self.indent += 1 |
| 4560 | self.indent_mod -= 1 |
| 4561 | elif _blk2: # Continue-block keyword (else/elif/except/...) |
| 4562 | code_line = _blk2 |
| 4563 | self.indent_mod -= 1 |
| 4564 | elif _cend: # The end-code-block template token (usually '%>') |
| 4565 | if multiline: multiline = False |
| 4566 | else: code_line += _cend |
| 4567 | elif _end: |
| 4568 | self.indent -= 1 |
| 4569 | self.indent_mod += 1 |
| 4570 | else: # \n |
| 4571 | self.write_code(code_line.strip(), comment) |
| 4572 | self.lineno += 1 |
| 4573 | code_line, comment, self.indent_mod = '', '', 0 |
| 4574 | if not multiline: |
| 4575 | break |
| 4576 | |
| 4577 | return offset |
| 4578 | |
| 4579 | def flush_text(self): |
| 4580 | text = ''.join(self.text_buffer) |
no test coverage detected