|
| 1 | +from __future__ import division, print_function |
| 2 | + |
| 3 | +import argparse |
| 4 | +import re |
| 5 | + |
| 6 | +COMMENT_SECTION_START_RE = re.compile(r"^\s+/\*\*$") # /** |
| 7 | +DEPRECATED_RE = re.compile( # * @param type $name DEPRECATED: Message |
| 8 | + r"^\s*\*\s+@param\s+(?P<type>\S+)\s+(?P<property_name>\S+)\s+DEPRECATED:\s+(?P<message>.+)$" |
| 9 | +) |
| 10 | +SETTER_FUNCTION_RE = re.compile(r"^\s*public\s+function\s+set\S+") # public function setSomething(value) |
| 11 | +FUNCTION_BODY_STARTED_RE = re.compile(r"^(?P<indent>\s*){$") # { |
| 12 | + |
| 13 | + |
| 14 | +def main(input_file): |
| 15 | + filename = input_file.name |
| 16 | + content = [line.rstrip() for line in input_file.readlines()] |
| 17 | + input_file.close() |
| 18 | + |
| 19 | + with open(filename, "wt", newline="\n") as result: |
| 20 | + comment_section_started = False |
| 21 | + deprecation_message = None |
| 22 | + setter_function_declaration = False |
| 23 | + |
| 24 | + for line in content: |
| 25 | + if COMMENT_SECTION_START_RE.match(line): |
| 26 | + comment_section_started = True |
| 27 | + |
| 28 | + deprecated_match = DEPRECATED_RE.match(line) |
| 29 | + if comment_section_started and deprecated_match: |
| 30 | + # deprecation message found |
| 31 | + deprecation_message = deprecated_match.groupdict()["message"].strip() |
| 32 | + property_name = deprecated_match.groupdict()["property_name"] |
| 33 | + |
| 34 | + if comment_section_started and deprecation_message and SETTER_FUNCTION_RE.match(line): |
| 35 | + setter_function_declaration = True |
| 36 | + |
| 37 | + match_with_indent = FUNCTION_BODY_STARTED_RE.match(line) |
| 38 | + if match_with_indent: |
| 39 | + # Function body started |
| 40 | + if setter_function_declaration and deprecation_message: |
| 41 | + result.write(line + "\n") |
| 42 | + |
| 43 | + indent = match_with_indent.groupdict()["indent"] |
| 44 | + escaped_message = "".join(r"\'" if c == "'" else c for c in deprecation_message) |
| 45 | + result.write( |
| 46 | + f"{indent}{(' ' * 4)}" |
| 47 | + f"trigger_error('Property {property_name} is deprecated. {escaped_message}'" |
| 48 | + ", E_USER_DEPRECATED);\n" |
| 49 | + ) |
| 50 | + |
| 51 | + comment_section_started = False |
| 52 | + setter_function_declaration = False |
| 53 | + deprecation_message = None |
| 54 | + |
| 55 | + continue |
| 56 | + |
| 57 | + comment_section_started = False |
| 58 | + setter_function_declaration = False |
| 59 | + deprecation_message = None |
| 60 | + |
| 61 | + result.write(line + "\n") |
| 62 | + |
| 63 | + |
| 64 | +def parse_args(): |
| 65 | + parser = argparse.ArgumentParser() |
| 66 | + parser.add_argument("input_file", type=argparse.FileType("rt")) |
| 67 | + args = parser.parse_args() |
| 68 | + return vars(args) |
| 69 | + |
| 70 | + |
| 71 | +if __name__ == "__main__": |
| 72 | + main(**parse_args()) |
0 commit comments