Skip to content

Commit 1709020

Browse files
gh-113317: Move FormatCounterFormatter into libclinic (#114066)
1 parent 4de4e65 commit 1709020

File tree

3 files changed

+31
-25
lines changed

3 files changed

+31
-25
lines changed

Tools/clinic/clinic.py

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import pprint
2424
import re
2525
import shlex
26-
import string
2726
import sys
2827
import textwrap
2928

@@ -270,24 +269,6 @@ def __init__(self) -> None:
270269
self.unlock: list[str] = []
271270

272271

273-
class FormatCounterFormatter(string.Formatter):
274-
"""
275-
This counts how many instances of each formatter
276-
"replacement string" appear in the format string.
277-
278-
e.g. after evaluating "string {a}, {b}, {c}, {a}"
279-
the counts dict would now look like
280-
{'a': 2, 'b': 1, 'c': 1}
281-
"""
282-
def __init__(self) -> None:
283-
self.counts = collections.Counter[str]()
284-
285-
def get_value(
286-
self, key: str, args: object, kwargs: object # type: ignore[override]
287-
) -> Literal['']:
288-
self.counts[key] += 1
289-
return ''
290-
291272
class Language(metaclass=abc.ABCMeta):
292273

293274
start_line = ""
@@ -341,7 +322,7 @@ def assert_only_one(
341322
fields = ['dsl_name']
342323
fields.extend(additional_fields)
343324
line: str = getattr(self, attr)
344-
fcf = FormatCounterFormatter()
325+
fcf = libclinic.FormatCounterFormatter()
345326
fcf.format(line)
346327
def local_fail(should_be_there_but_isnt: bool) -> None:
347328
if should_be_there_but_isnt:

Tools/clinic/libclinic/__init__.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@
1616
wrapped_c_string_literal,
1717
)
1818
from .utils import (
19-
create_regex,
19+
FormatCounterFormatter,
2020
compute_checksum,
21+
create_regex,
2122
write_file,
2223
)
2324

@@ -39,8 +40,9 @@
3940
"wrapped_c_string_literal",
4041

4142
# Utility functions
42-
"create_regex",
43+
"FormatCounterFormatter",
4344
"compute_checksum",
45+
"create_regex",
4446
"write_file",
4547
]
4648

Tools/clinic/libclinic/utils.py

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
import collections
12
import hashlib
2-
import re
33
import os
4+
import re
5+
import string
6+
from typing import Literal
47

58

69
def write_file(filename: str, new_contents: str) -> None:
@@ -39,7 +42,27 @@ def create_regex(
3942
group_re = r"\w+" if word else ".+"
4043
before = re.escape(before)
4144
after = re.escape(after)
42-
pattern = fr"{before}({group_re}){after}"
45+
pattern = rf"{before}({group_re}){after}"
4346
if whole_line:
44-
pattern = fr"^{pattern}$"
47+
pattern = rf"^{pattern}$"
4548
return re.compile(pattern)
49+
50+
51+
class FormatCounterFormatter(string.Formatter):
52+
"""
53+
This counts how many instances of each formatter
54+
"replacement string" appear in the format string.
55+
56+
e.g. after evaluating "string {a}, {b}, {c}, {a}"
57+
the counts dict would now look like
58+
{'a': 2, 'b': 1, 'c': 1}
59+
"""
60+
61+
def __init__(self) -> None:
62+
self.counts = collections.Counter[str]()
63+
64+
def get_value(
65+
self, key: str, args: object, kwargs: object # type: ignore[override]
66+
) -> Literal[""]:
67+
self.counts[key] += 1
68+
return ""

0 commit comments

Comments
 (0)