Skip to content

Commit

Permalink
Refactored python 3.12 f-string coping
Browse files Browse the repository at this point in the history
  • Loading branch information
bricoletc authored and mbhall88 committed Jan 31, 2024
1 parent 2b19e6a commit 29235a7
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 19 deletions.
12 changes: 2 additions & 10 deletions snakefmt/parser/parser.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import sys
import tokenize
from abc import ABC, abstractmethod
from typing import NamedTuple, Optional
Expand All @@ -11,6 +10,7 @@
Vocabulary,
add_token_space,
is_newline,
re_add_curly_bracket_if_needed,
)
from snakefmt.types import TAB, Token, TokenIterator, col_nb

Expand Down Expand Up @@ -325,12 +325,4 @@ def get_next_queriable(self, snakefile: TokenIterator) -> Status:
if not pythonable and token.type != tokenize.COMMENT:
pythonable = True
buffer += token.string
if (
token is not None
and sys.version_info >= (3, 12)
and token.type == tokenize.FSTRING_MIDDLE
):
if token.string.endswith("}"):
buffer += "}"
elif token.string.endswith("{"):
buffer += "{"
buffer += re_add_curly_bracket_if_needed(token)
24 changes: 15 additions & 9 deletions snakefmt/parser/syntax.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,20 @@
spacing_triggers[tokenize.OP].add(tokenize.FSTRING_START)


def re_add_curly_bracket_if_needed(token: Token) -> str:
result = ""
if (
token is not None
and sys.version_info >= (3, 12)
and token.type == tokenize.FSTRING_MIDDLE
):
if token.string.endswith("}"):
result = "}"
elif token.string.endswith("{"):
result = "{"
return result


def operator_skip_spacing(prev_token: Token, token: Token) -> bool:
if prev_token.type != tokenize.OP and token.type != tokenize.OP:
return False
Expand Down Expand Up @@ -333,15 +347,7 @@ def parse_params(self, snakefile: TokenIterator):
prev_token = None
while True:
cur_param = self.process_token(cur_param, prev_token)
if (
self.token is not None
and sys.version_info >= (3, 12)
and self.token.type == tokenize.FSTRING_MIDDLE
):
if self.token.string.endswith("}"):
cur_param.value += "}"
elif self.token.string.endswith("{"):
cur_param.value += "{"
cur_param.value += re_add_curly_bracket_if_needed(self.token)
try:
prev_token = self.token
self.token = next(snakefile)
Expand Down

0 comments on commit 29235a7

Please sign in to comment.