Example #1
0
def no_skipping(code):
    raw_code_lines = code.split("\n")
    no_skip_range = []
    scopes_by_idx = {}
    for positions in get_str_bin_op_lines(code):
        # for start, end in positions:
        if not positions:
            continue
        start, end = positions
        start_idx = start - 1
        raw_scope = raw_code_lines[start_idx:end]
        if not raw_scope:
            continue

        for line in raw_scope:
            if line:
                indent = get_indent(line)
                break

        strip_scope = map(lambda x: x.strip(), raw_scope)
        scopes_by_idx[start_idx] = dict(
            raw_scope=raw_scope,
            strip_scope=strip_scope,
            indent=indent,
        )
        no_skip_range += list(range(start_idx, end))
    return no_skip_range, scopes_by_idx
Example #2
0
def fstringify_code_by_line(code, stats=False, debug=False):
    raw_code_lines = code.split("\n")
    no_skip_range = []
    scopes_by_idx = {}
    for positions in get_str_bin_op_lines(code):
        # for start, end in positions:
        if not positions:
            continue
        start, end = positions
        start_idx = start - 1
        raw_scope = raw_code_lines[start_idx:end]
        if not raw_scope:
            continue

        strip_scope = map(lambda x: x.strip(), raw_scope)
        scopes_by_idx[start_idx] = dict(
            raw_scope=raw_scope,
            strip_scope=strip_scope,
            indent=get_indent(raw_scope[0]),
        )
        no_skip_range += list(range(start_idx, end))

    result_lines = []
    for line_idx, raw_line in enumerate(raw_code_lines):
        lineno = line_idx + 1
        indented = get_indent(raw_line)

        if line_idx not in no_skip_range:
            result_lines.append(raw_line)
            continue

        if line_idx not in scopes_by_idx:
            continue

        scoped = scopes_by_idx[line_idx]
        code_line, meta = fstringify_code("\n".join(scoped["strip_scope"]),
                                          include_meta=True,
                                          debug=debug)

        if not meta["changed"]:
            if debug:
                print("~~~~NOT CHANGED", scoped["raw_scope"], "meta", meta)
            result_lines += scoped["raw_scope"]
            continue

        code_line = force_double_quote_fstring(code_line)
        code_line_parts = code_line.strip().split("\n")

        indie = ""
        indent = scoped["indent"]
        for idx, cline in enumerate(code_line_parts):
            code_line_strip = cline.lstrip()  # if change_add else cline
            if idx == 0:
                indie = indent + code_line_strip
            else:
                if (indie.endswith(",") or indie.endswith("else")
                        or indie.endswith("for") or indie.endswith("in")
                        or indie.endswith("not")):
                    indie += " "

                indie += cline.strip()
                # else:
                #     indie += cline.strip()

        result_lines.append(indie)

    final_code = "\n".join(result_lines)
    return final_code
Example #3
0
 def test_get_indent(self):
     self.assertCodeEqual("    ", get_indent("    code"))
     self.assertCodeEqual("", get_indent("code"))
Example #4
0
def test_get_indent():
    line = "        attrs = {'r': '%d' % row_idx}"
    result = get_indent(line)
    assert result == "        "