How to use the flynt.state.verbose function in flynt

To help you get started, we’ve selected a few flynt examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github ikamensh / flynt / src / flynt / transform / transform.py View on Github external
) -> Tuple[str, bool]:
    """Convert a block of code to an f-string

    Args:
        code: The code to convert.
        quote_type: the quote type to use for the transformed result

    Returns:
       Tuple: resulting code, boolean: was it changed?
    """

    try:
        tree = ast.parse(code)
        converted, changed, str_in_str = fstringify_node(copy.deepcopy(tree))
    except (SyntaxError, FlyntException, Exception) as e:
        if state.verbose:
            if isinstance(e, ConversionRefused):
                print(f"Not converting code '{code}': {e}")
                print(e)
            else:
                print(f"Exception {e} during conversion of code '{code}'")
                traceback.print_exc()
        state.invalid_conversions += 1
        return code, False
    else:
        if changed:
            new_code = astor.to_source(converted)
            new_code = new_code.strip()
            new_code = set_quote_type(
                new_code, quote_type if not str_in_str else QuoteTypes.double
            )
            new_code = new_code.replace("\n", "\\n")
github ikamensh / flynt / src / flynt / cli.py View on Github external
parser.add_argument(
        "src", action="store", nargs="+", help="source file(s) or directory"
    )

    args = parser.parse_args()

    if args.transform_concats:
        if sys.version_info < (3, 8):
            raise Exception(
                f"""Transforming string concatenations is only possible with flynt 
                installed to a python3.8+ interpreter. Currently using {sys.version_info}."""
            )

    state.aggressive = args.aggressive
    state.verbose = args.verbose
    state.quiet = args.quiet

    return fstringify(
        args.src,
        multiline=not args.no_multiline,
        len_limit=int(args.line_length),
        fail_on_changes=args.fail_on_change,
        transform_concat=args.transform_concats,
    )
github ikamensh / flynt / src / flynt / lexer / split.py View on Github external
reuse = chunk.append(t)

            if chunk.complete:

                yield chunk
                chunk = Chunk()
                if reuse:
                    reuse = chunk.append(t)
                    # assert not reuse
                    if chunk.complete:
                        yield chunk
                        chunk = Chunk()

        yield chunk
    except tokenize.TokenError as e:
        if state.verbose:
            traceback.print_exc()
            print(e)
github ikamensh / flynt / src / flynt / process.py View on Github external
def try_chunk(self, chunk):

        for line in self.src_lines[chunk.start_line : chunk.start_line + chunk.n_lines]:
            if noqa_regex.findall(line):
                # user does not wish for this line to be converted.
                return

        try:
            if chunk.string_in_string:
                quote_type = qt.double
            else:
                quote_type = chunk.quote_type

        except FlyntException as e:
            if state.verbose:
                print(f"Exception {e} during conversion of code '{str(chunk)}'")
                traceback.print_exc()

        else:
            converted, changed = self.transform_func(str(chunk), quote_type=quote_type)
            if changed:
                contract_lines = chunk.n_lines - 1
                if contract_lines == 0:
                    line = self.src_lines[chunk.start_line]
                    rest = line[chunk.end_idx:]
                else:
                    next_line = self.src_lines[chunk.start_line + contract_lines]
                    rest = next_line[chunk.end_idx:]
                self.maybe_replace(chunk, contract_lines, converted, rest)
github ikamensh / flynt / src / flynt / api.py View on Github external
try:
        new_code, changes = fstringify_code_by_line(
            contents, multiline=multiline, len_limit=len_limit
        )
        if transform_concat:
            new_code, concat_changes = fstringify_concats(
                new_code, multiline=multiline, len_limit=len_limit
            )
            changes += concat_changes
            state.concat_changes += concat_changes

    except Exception as e:
        if not state.quiet:
            print(f"Skipping fstrings transform of file {filename} due to {e}")
            if state.verbose:
                traceback.print_exc()
        return default_result()

    if new_code == contents:
        return default_result()

    try:
        ast_after = ast.parse(new_code)
    except SyntaxError:
        print(f"Faulty result during conversion on {filename} - skipping.")
        if state.verbose:
            print(new_code)
            traceback.print_exc()
        return default_result()

    if not len(ast_before.body) == len(ast_after.body):