Coverage for src / competitive_verifier / oj / verify / languages / special_comments.py: 100%

31 statements  

« prev     ^ index     » next       coverage.py v7.13.1, created at 2026-03-05 16:00 +0000

1# Python Version: 3.x 

2import functools 

3import pathlib 

4import re 

5from collections.abc import Iterable, Mapping 

6from logging import getLogger 

7 

8from competitive_verifier.util import read_text_normalized 

9 

10logger = getLogger(__name__) 

11 

12 

13# special comments like Vim and Python: see https://www.python.org/dev/peps/pep-0263/ 

14@functools.cache 

15def list_special_comments(path: pathlib.Path) -> Mapping[str, str]: 

16 pattern = re.compile( 

17 r"\b(?:verify-helper|verification-helper|competitive-verifier):\s*([0-9A-Za-z_]+)(?:\s(.*))?$" 

18 ) 

19 attributes: dict[str, str] = {} 

20 for line in read_text_normalized(path).splitlines(): 

21 matched = pattern.search(line) 

22 if matched: 

23 key = matched.group(1) 

24 value = (matched.group(2) or "").strip() 

25 attributes[key] = value 

26 return attributes 

27 

28 

29def _unquote(s: str) -> str: 

30 if s.startswith(("'", '"', "`")): 

31 end_quote_pos = s.rfind(s[0]) 

32 if end_quote_pos == 0: 

33 # Remove opening quote from the URL like `"https://atcoder.jp/` 

34 return s[1:] 

35 # Remove quotes and trailing superfluous chars around the URL 

36 return s[1:end_quote_pos] 

37 return s 

38 

39 

40@functools.cache 

41def list_embedded_urls(path: pathlib.Path) -> Iterable[str]: 

42 pattern = re.compile( 

43 r"""['"`]?https?://\S*""" 

44 ) # use a broad pattern. There are no needs to make match strict. 

45 content = read_text_normalized(path) 

46 # The URL may be written like `"https://atcoder.jp/"`. In this case, we need to remove `"`s around the URL. 

47 # We also need to remove trailing superfluous chars in a case like `{"url":"https://atcoder.jp/"}`. 

48 urls = {_unquote(url) for url in pattern.findall(content)} 

49 return sorted(urls)