|
16 | 16 | import dataclasses as dc
|
17 | 17 | import enum
|
18 | 18 | import functools
|
19 |
| -import hashlib |
20 | 19 | import inspect
|
21 | 20 | import io
|
22 | 21 | import itertools
|
@@ -1792,21 +1791,6 @@ def render_function(
|
1792 | 1791 | return clinic.get_destination('block').dump()
|
1793 | 1792 |
|
1794 | 1793 |
|
1795 |
| -def create_regex( |
1796 |
| - before: str, |
1797 |
| - after: str, |
1798 |
| - word: bool = True, |
1799 |
| - whole_line: bool = True |
1800 |
| -) -> re.Pattern[str]: |
1801 |
| - """Create an re object for matching marker lines.""" |
1802 |
| - group_re = r"\w+" if word else ".+" |
1803 |
| - pattern = r'{}({}){}' |
1804 |
| - if whole_line: |
1805 |
| - pattern = '^' + pattern + '$' |
1806 |
| - pattern = pattern.format(re.escape(before), group_re, re.escape(after)) |
1807 |
| - return re.compile(pattern) |
1808 |
| - |
1809 |
| - |
1810 | 1794 | @dc.dataclass(slots=True, repr=False)
|
1811 | 1795 | class Block:
|
1812 | 1796 | r"""
|
@@ -1905,8 +1889,9 @@ def __init__(
|
1905 | 1889 | self.language = language
|
1906 | 1890 | before, _, after = language.start_line.partition('{dsl_name}')
|
1907 | 1891 | assert _ == '{dsl_name}'
|
1908 |
| - self.find_start_re = create_regex(before, after, whole_line=False) |
1909 |
| - self.start_re = create_regex(before, after) |
| 1892 | + self.find_start_re = libclinic.create_regex(before, after, |
| 1893 | + whole_line=False) |
| 1894 | + self.start_re = libclinic.create_regex(before, after) |
1910 | 1895 | self.verify = verify
|
1911 | 1896 | self.last_checksum_re: re.Pattern[str] | None = None
|
1912 | 1897 | self.last_dsl_name: str | None = None
|
@@ -1995,7 +1980,7 @@ def is_stop_line(line: str) -> bool:
|
1995 | 1980 | else:
|
1996 | 1981 | before, _, after = self.language.checksum_line.format(dsl_name=dsl_name, arguments='{arguments}').partition('{arguments}')
|
1997 | 1982 | assert _ == '{arguments}'
|
1998 |
| - checksum_re = create_regex(before, after, word=False) |
| 1983 | + checksum_re = libclinic.create_regex(before, after, word=False) |
1999 | 1984 | self.last_dsl_name = dsl_name
|
2000 | 1985 | self.last_checksum_re = checksum_re
|
2001 | 1986 | assert checksum_re is not None
|
@@ -2029,7 +2014,7 @@ def is_stop_line(line: str) -> bool:
|
2029 | 2014 | else:
|
2030 | 2015 | checksum = d['checksum']
|
2031 | 2016 |
|
2032 |
| - computed = compute_checksum(output, len(checksum)) |
| 2017 | + computed = libclinic.compute_checksum(output, len(checksum)) |
2033 | 2018 | if checksum != computed:
|
2034 | 2019 | fail("Checksum mismatch! "
|
2035 | 2020 | f"Expected {checksum!r}, computed {computed!r}. "
|
@@ -2142,8 +2127,8 @@ def print_block(
|
2142 | 2127 | write(output)
|
2143 | 2128 |
|
2144 | 2129 | arguments = "output={output} input={input}".format(
|
2145 |
| - output=compute_checksum(output, 16), |
2146 |
| - input=compute_checksum(input, 16) |
| 2130 | + output=libclinic.compute_checksum(output, 16), |
| 2131 | + input=libclinic.compute_checksum(input, 16) |
2147 | 2132 | )
|
2148 | 2133 | write(self.language.checksum_line.format(dsl_name=dsl_name, arguments=arguments))
|
2149 | 2134 | write("\n")
|
@@ -2245,27 +2230,6 @@ def dump(self) -> str:
|
2245 | 2230 | extensions['py'] = PythonLanguage
|
2246 | 2231 |
|
2247 | 2232 |
|
2248 |
| -def write_file(filename: str, new_contents: str) -> None: |
2249 |
| - try: |
2250 |
| - with open(filename, encoding="utf-8") as fp: |
2251 |
| - old_contents = fp.read() |
2252 |
| - |
2253 |
| - if old_contents == new_contents: |
2254 |
| - # no change: avoid modifying the file modification time |
2255 |
| - return |
2256 |
| - except FileNotFoundError: |
2257 |
| - pass |
2258 |
| - # Atomic write using a temporary file and os.replace() |
2259 |
| - filename_new = f"{filename}.new" |
2260 |
| - with open(filename_new, "w", encoding="utf-8") as fp: |
2261 |
| - fp.write(new_contents) |
2262 |
| - try: |
2263 |
| - os.replace(filename_new, filename) |
2264 |
| - except: |
2265 |
| - os.unlink(filename_new) |
2266 |
| - raise |
2267 |
| - |
2268 |
| - |
2269 | 2233 | ClassDict = dict[str, "Class"]
|
2270 | 2234 | DestinationDict = dict[str, Destination]
|
2271 | 2235 | ModuleDict = dict[str, "Module"]
|
@@ -2505,7 +2469,8 @@ def parse(self, input: str) -> str:
|
2505 | 2469 | core_includes=True,
|
2506 | 2470 | limited_capi=self.limited_capi,
|
2507 | 2471 | header_includes=self.includes)
|
2508 |
| - write_file(destination.filename, printer_2.f.getvalue()) |
| 2472 | + libclinic.write_file(destination.filename, |
| 2473 | + printer_2.f.getvalue()) |
2509 | 2474 | continue
|
2510 | 2475 |
|
2511 | 2476 | return printer.f.getvalue()
|
@@ -2578,18 +2543,7 @@ def parse_file(
|
2578 | 2543 | limited_capi=limited_capi)
|
2579 | 2544 | cooked = clinic.parse(raw)
|
2580 | 2545 |
|
2581 |
| - write_file(output, cooked) |
2582 |
| - |
2583 |
| - |
2584 |
| -def compute_checksum( |
2585 |
| - input: str | None, |
2586 |
| - length: int | None = None |
2587 |
| -) -> str: |
2588 |
| - input = input or '' |
2589 |
| - s = hashlib.sha1(input.encode('utf-8')).hexdigest() |
2590 |
| - if length: |
2591 |
| - s = s[:length] |
2592 |
| - return s |
| 2546 | + libclinic.write_file(output, cooked) |
2593 | 2547 |
|
2594 | 2548 |
|
2595 | 2549 | class PythonParser:
|
|
0 commit comments