Skip to content

Commit

Permalink
Get rid of global state (not lexer state yet) (#155)
Browse files Browse the repository at this point in the history
  • Loading branch information
akx committed Dec 8, 2022
1 parent 90c66b4 commit b3d3337
Show file tree
Hide file tree
Showing 19 changed files with 366 additions and 366 deletions.
58 changes: 16 additions & 42 deletions src/flynt/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@

import astor

from flynt import state
from flynt.cli_messages import farewell_message
from flynt.process import (
fstringify_code_by_line,
fstringify_concats,
fstringify_static_joins,
)
from flynt.state import State

log = logging.getLogger(__name__)

Expand All @@ -24,13 +24,7 @@

def _fstringify_file(
filename: str,
multiline: bool,
len_limit: int,
*,
transform_concat: bool = False,
transform_format: bool = True,
transform_join: bool = False,
transform_percent: bool = True,
state: State,
) -> Tuple[bool, int, int, int]:
"""
:return: tuple: (changes_made, n_changes,
Expand Down Expand Up @@ -59,23 +53,22 @@ def default_result() -> Tuple[bool, int, int, int]:
try:
new_code = contents
changes = 0
if transform_percent or transform_format:
if state.transform_percent or state.transform_format:
new_code, changes = fstringify_code_by_line(
contents,
multiline=multiline,
len_limit=len_limit,
transform_percent=transform_percent,
transform_format=transform_format,
state=state,
)
if transform_concat:
if state.transform_concat:
new_code, concat_changes = fstringify_concats(
new_code, multiline=multiline, len_limit=len_limit
new_code,
state=state,
)
changes += concat_changes
state.concat_changes += concat_changes
if transform_join:
if state.transform_join:
new_code, join_changes = fstringify_static_joins(
new_code, multiline=multiline, len_limit=len_limit
new_code,
state=state,
)
changes += join_changes
state.join_changes += join_changes
Expand Down Expand Up @@ -123,13 +116,7 @@ def default_result() -> Tuple[bool, int, int, int]:

def fstringify_files(
files: List[str],
multiline: bool,
len_limit: int,
transform_concat: bool,
transform_join: bool = False,
*,
transform_format: bool = True,
transform_percent: bool = True,
state: State,
) -> int:
changed_files = 0
total_charcount_original = 0
Expand All @@ -144,12 +131,7 @@ def fstringify_files(
charcount_new,
) = _fstringify_file(
path,
multiline,
len_limit,
transform_concat=transform_concat,
transform_format=transform_format,
transform_join=transform_join,
transform_percent=transform_percent,
state,
)
if changed:
changed_files += 1
Expand All @@ -163,6 +145,7 @@ def fstringify_files(

if not state.quiet:
_print_report(
state,
len(files),
changed_files,
total_charcount_new,
Expand All @@ -175,6 +158,7 @@ def fstringify_files(


def _print_report(
state: State,
found_files: int,
changed_files: int,
total_cc_new: int,
Expand Down Expand Up @@ -241,13 +225,8 @@ def _print_report(

def fstringify(
files_or_paths: List[str],
multiline: bool,
len_limit: int,
state: State,
fail_on_changes: bool = False,
transform_percent: bool = True,
transform_format: bool = True,
transform_concat: bool = False,
transform_join: bool = False,
excluded_files_or_paths: Optional[Collection[str]] = None,
) -> int:
"""determine if a directory or a single file was passed, and f-stringify it."""
Expand All @@ -256,12 +235,7 @@ def fstringify(

status = fstringify_files(
files,
multiline=multiline,
len_limit=len_limit,
transform_percent=transform_percent,
transform_format=transform_format,
transform_concat=transform_concat,
transform_join=transform_join,
state=state,
)

if fail_on_changes:
Expand Down
29 changes: 12 additions & 17 deletions src/flynt/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
import warnings
from typing import List, Optional

from flynt import __version__, state
from flynt import __version__
from flynt.api import fstringify, fstringify_code_by_line
from flynt.pyproject_finder import find_pyproject_toml, parse_pyproject_toml
from flynt.state import State


def main():
Expand Down Expand Up @@ -172,12 +173,18 @@ def run_flynt_cli(arglist: Optional[List[str]] = None) -> int:
level=(logging.DEBUG if args.verbose else logging.CRITICAL),
)

state = State(
aggressive=args.aggressive,
quiet=args.quiet,
dry_run=args.dry_run,
)
if args.verbose:
logging.getLogger("flynt").setLevel(logging.DEBUG)

if args.string:
set_global_state(args)
converted, _ = fstringify_code_by_line(
" ".join(args.src),
multiline=not args.no_multiline,
len_limit=int(args.line_length),
state=state,
)
print(converted)
return 0
Expand All @@ -198,7 +205,6 @@ def run_flynt_cli(arglist: Optional[List[str]] = None) -> int:
)
parser.set_defaults(**cfg)
args = parser.parse_args(arglist)
set_global_state(args)
if not args.quiet:
print(salutation)
if args.verbose:
Expand All @@ -208,17 +214,6 @@ def run_flynt_cli(arglist: Optional[List[str]] = None) -> int:
return fstringify(
args.src,
excluded_files_or_paths=args.exclude,
multiline=not args.no_multiline,
len_limit=int(args.line_length),
fail_on_changes=args.fail_on_change,
transform_percent=args.transform_percent,
transform_format=args.transform_format,
transform_concat=args.transform_concats,
transform_join=args.transform_joins,
state=state,
)


def set_global_state(args: argparse.Namespace) -> None:
state.aggressive = args.aggressive
state.quiet = args.quiet
state.dry_run = args.dry_run
50 changes: 22 additions & 28 deletions src/flynt/process.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from flynt.format import get_quote_type
from flynt.lexer import split
from flynt.lexer.Chunk import Chunk
from flynt.state import State
from flynt.static_join.candidates import join_candidates
from flynt.static_join.transformer import transform_join
from flynt.string_concat.candidates import concat_candidates
Expand Down Expand Up @@ -171,58 +172,51 @@ def add_rest(self) -> None:
self.last_line += 1


def fstringify_code_by_line(
code: str,
multiline: bool = True,
len_limit: Optional[int] = 88,
transform_percent: bool = True,
transform_format: bool = True,
) -> Tuple[str, int]:
def fstringify_code_by_line(code: str, state: State) -> Tuple[str, int]:
"""returns fstringified version of the code and amount of lines edited."""
phunk = partial(
transform_chunk,
transform_format=transform_format,
transform_percent=transform_percent,
)
return _transform_code(
code, split.get_fstringify_chunks, phunk, multiline, len_limit
code, split.get_fstringify_chunks, partial(transform_chunk, state=state), state
)


def fstringify_concats(
code: str,
multiline: bool = True,
len_limit: Optional[int] = 88,
) -> Tuple[str, int]:
def fstringify_concats(code: str, state: State) -> Tuple[str, int]:
"""replace string literal concatenations with f-string expressions."""
return _transform_code(
code, concat_candidates, transform_concat, multiline, len_limit
code,
partial(concat_candidates, state=state),
partial(transform_concat, state=state),
state,
)


def fstringify_static_joins(code: str, multiline=True, len_limit=88) -> Tuple[str, int]:
def fstringify_static_joins(code: str, state: State) -> Tuple[str, int]:
"""replace joins on static content with f-string expressions."""
return _transform_code(code, join_candidates, transform_join, multiline, len_limit)
return _transform_code(
code,
partial(join_candidates, state=state),
partial(transform_join, state=state),
state,
)


def _transform_code(
code: str,
candidates_iter_factory: Callable,
transform_func: Callable,
multiline: bool,
len_limit: Optional[int],
state: State,
) -> Tuple[str, int]:
"""returns fstringified version of the code and amount of lines edited."""

len_limit = _multiline_settings(len_limit, multiline)
len_limit = _multiline_settings(state)
jt = JoinTransformer(code, len_limit, candidates_iter_factory, transform_func)
return jt.fstringify_code_by_line()


def _multiline_settings(len_limit: Optional[int], multiline: bool) -> Optional[int]:
if not multiline:
len_limit = 0
def _multiline_settings(state: State) -> Optional[int]:
# TODO: eradicate this function and system
if not state.multiline:
state.len_limit = 0
lexer.set_single_line()
else:
lexer.set_multiline()
return len_limit
return state.len_limit
45 changes: 24 additions & 21 deletions src/flynt/state.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,33 @@
"""This module contains global state of flynt application instance."""

quiet = False
aggressive = False
dry_run = False
import dataclasses
from typing import Optional

percent_candidates = 0
percent_transforms = 0

call_candidates = 0
call_transforms = 0
@dataclasses.dataclass
class State:
# -- Options
quiet: bool = False
aggressive: bool = False
dry_run: bool = False
multiline: bool = True
len_limit: Optional[int] = None
transform_percent: bool = True
transform_format: bool = True
transform_concat: bool = False
transform_join: bool = False

invalid_conversions = 0
# -- Statistics
percent_candidates: int = 0
percent_transforms: int = 0

concat_candidates = 0
concat_changes = 0
call_candidates: int = 0
call_transforms: int = 0

join_candidates = 0
join_changes = 0
invalid_conversions: int = 0

# Backup of the initial state to support the tests, which should start with a clean state each time.
# Note: this assumes that all state variables are immutable.
_initial_state = dict(globals())
concat_candidates: int = 0
concat_changes: int = 0


def _reset() -> None:
"""
Resets the state variables to the initial values seen above.
"""
globals().update(**_initial_state)
join_candidates: int = 0
join_changes: int = 0
4 changes: 2 additions & 2 deletions src/flynt/static_join/candidates.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import ast
from typing import List

from flynt import state
from flynt.ast_chunk import AstChunk
from flynt.state import State
from flynt.static_join.utils import get_static_join_bits


Expand All @@ -22,7 +22,7 @@ def visit_Call(self, node: ast.Call) -> None:
self.generic_visit(node)


def join_candidates(code: str):
def join_candidates(code: str, state: State):
tree = ast.parse(code)

ch = JoinHound()
Expand Down
4 changes: 2 additions & 2 deletions src/flynt/string_concat/candidates.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import ast
from typing import Generator, List

from flynt import state
from flynt.ast_chunk import AstChunk
from flynt.state import State
from flynt.utils import is_str_literal


Expand Down Expand Up @@ -30,7 +30,7 @@ def visit_BinOp(self, node: ast.BinOp) -> None:
self.generic_visit(node)


def concat_candidates(code: str) -> Generator[AstChunk, None, None]:
def concat_candidates(code: str, state: State) -> Generator[AstChunk, None, None]:
tree = ast.parse(code)

ch = ConcatHound()
Expand Down
Loading

0 comments on commit b3d3337

Please sign in to comment.