gh-113317: Move FormatCounterFormatter into libclinic (#114066)

This commit is contained in:
Erlend E. Aasland 2024-01-15 00:09:26 +01:00 committed by GitHub
parent 4de4e654e5
commit 1709020e8e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 31 additions and 25 deletions

View file

@ -23,7 +23,6 @@ import os
import pprint import pprint
import re import re
import shlex import shlex
import string
import sys import sys
import textwrap import textwrap
@ -270,24 +269,6 @@ class CRenderData:
self.unlock: list[str] = [] self.unlock: list[str] = []
class FormatCounterFormatter(string.Formatter):
"""
This counts how many instances of each formatter
"replacement string" appear in the format string.
e.g. after evaluating "string {a}, {b}, {c}, {a}"
the counts dict would now look like
{'a': 2, 'b': 1, 'c': 1}
"""
def __init__(self) -> None:
self.counts = collections.Counter[str]()
def get_value(
self, key: str, args: object, kwargs: object # type: ignore[override]
) -> Literal['']:
self.counts[key] += 1
return ''
class Language(metaclass=abc.ABCMeta): class Language(metaclass=abc.ABCMeta):
start_line = "" start_line = ""
@ -341,7 +322,7 @@ class Language(metaclass=abc.ABCMeta):
fields = ['dsl_name'] fields = ['dsl_name']
fields.extend(additional_fields) fields.extend(additional_fields)
line: str = getattr(self, attr) line: str = getattr(self, attr)
fcf = FormatCounterFormatter() fcf = libclinic.FormatCounterFormatter()
fcf.format(line) fcf.format(line)
def local_fail(should_be_there_but_isnt: bool) -> None: def local_fail(should_be_there_but_isnt: bool) -> None:
if should_be_there_but_isnt: if should_be_there_but_isnt:

View file

@ -16,8 +16,9 @@ from .formatting import (
wrapped_c_string_literal, wrapped_c_string_literal,
) )
from .utils import ( from .utils import (
create_regex, FormatCounterFormatter,
compute_checksum, compute_checksum,
create_regex,
write_file, write_file,
) )
@ -39,8 +40,9 @@ __all__ = [
"wrapped_c_string_literal", "wrapped_c_string_literal",
# Utility functions # Utility functions
"create_regex", "FormatCounterFormatter",
"compute_checksum", "compute_checksum",
"create_regex",
"write_file", "write_file",
] ]

View file

@ -1,6 +1,9 @@
import collections
import hashlib import hashlib
import re
import os import os
import re
import string
from typing import Literal
def write_file(filename: str, new_contents: str) -> None: def write_file(filename: str, new_contents: str) -> None:
@ -39,7 +42,27 @@ def create_regex(
group_re = r"\w+" if word else ".+" group_re = r"\w+" if word else ".+"
before = re.escape(before) before = re.escape(before)
after = re.escape(after) after = re.escape(after)
pattern = fr"{before}({group_re}){after}" pattern = rf"{before}({group_re}){after}"
if whole_line: if whole_line:
pattern = fr"^{pattern}$" pattern = rf"^{pattern}$"
return re.compile(pattern) return re.compile(pattern)
class FormatCounterFormatter(string.Formatter):
"""
This counts how many instances of each formatter
"replacement string" appear in the format string.
e.g. after evaluating "string {a}, {b}, {c}, {a}"
the counts dict would now look like
{'a': 2, 'b': 1, 'c': 1}
"""
def __init__(self) -> None:
self.counts = collections.Counter[str]()
def get_value(
self, key: str, args: object, kwargs: object # type: ignore[override]
) -> Literal[""]:
self.counts[key] += 1
return ""