1#  Copyright (C) 2024 The Android Open Source Project
2#
3#  Licensed under the Apache License, Version 2.0 (the "License");
4#  you may not use this file except in compliance with the License.
5#  You may obtain a copy of the License at
6#
7#       http://www.apache.org/licenses/LICENSE-2.0
8#
9#  Unless required by applicable law or agreed to in writing, software
10#  distributed under the License is distributed on an "AS IS" BASIS,
11#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12#  See the License for the specific language governing permissions and
13#  limitations under the License.
14#
15#  Licensed under the Apache License, Version 2.0 (the "License");
16#  you may not use this file except in compliance with the License.
17#  You may obtain a copy of the License at
18#
19#       http://www.apache.org/licenses/LICENSE-2.0
20#
21#  Unless required by applicable law or agreed to in writing, software
22#  distributed under the License is distributed on an "AS IS" BASIS,
23#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
24#  See the License for the specific language governing permissions and
25#  limitations under the License.
26
27import re
28
29"""
30Script used to insert a list of strings in a certain format after the target string.
31"""
32def insert_after_string(output_file, target_string_file, list, format_file):
33    format_string = extract_format_string(format_file)
34
35    string_to_insert = ""
36    for new_string in list:
37        string_to_insert += format_string.format(new_string)
38
39    target_string = extract_target_string(target_string_file)
40    try:
41        with open(output_file, 'r+') as file:
42            file_content = file.read()
43            match = re.search(target_string, file_content, re.DOTALL)
44            if match:
45                file.seek(match.end())
46                remaining_content = file.read()
47                file.seek(match.end())
48                file.write('\n' + string_to_insert.rstrip() + remaining_content)
49            else:
50                print(f"Target string '{target_string}' not found in file.")
51
52    except FileNotFoundError:
53        print(f"Error: Output file '{output_file}' not found.")
54    except Exception as e:
55        print(f"An error occurred: {e}")
56
57def extract_format_string(format_file):
58    try:
59        with open(format_file, 'r') as file:
60            format_string = ""
61            for line in file.readlines():
62                format_string += line
63            return format_string
64
65    except FileNotFoundError:
66        print(f"Error: File '{format_file}' not found.")
67        return "{}"
68    except Exception as e:
69        print(f"An error occurred: {e}")
70        return "{}"
71
72def extract_target_string(target_string_file):
73    try:
74        with open(target_string_file, 'r') as file:
75            target_string = ""
76            for line in file.readlines():
77                target_string += line.rstrip()
78            return "({})".format(target_string)
79
80    except FileNotFoundError:
81        print(f"Error: File '{format_file}' not found.")
82        return ""
83    except Exception as e:
84        print(f"An error occurred: {e}")
85        return ""
86