1#!/usr/bin/python
2############################################################################
3#
4# Copyright (C) 2019 The Qt Company Ltd.
5# Contact: https://www.qt.io/licensing/
6#
7# This file is part of Qt Creator.
8#
9# Commercial License Usage
10# Licensees holding valid commercial Qt licenses may use this file in
11# accordance with the commercial license agreement provided with the
12# Software or, alternatively, in accordance with the terms contained in
13# a written agreement between you and The Qt Company. For licensing terms
14# and conditions see https://www.qt.io/terms-conditions. For further
15# information use the contact form at https://www.qt.io/contact-us.
16#
17# GNU General Public License Usage
18# Alternatively, this file may be used under the terms of the GNU
19# General Public License version 3 as published by the Free Software
20# Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21# included in the packaging of this file. Please review the following
22# information to ensure the GNU General Public License requirements will
23# be met: https://www.gnu.org/licenses/gpl-3.0.html.
24#
25############################################################################
26
27import os
28import sys
29import xml.etree.ElementTree as ET
30
31if len(sys.argv) != 3:
32    print("Please provide a top level directory to scan and a file to write into.")
33    sys.exit(1)
34
35top_dir = sys.argv[1]
36target_file = sys.argv[2]
37
38
39def fix_value(value):
40    value = value.replace('\"', '\\\"')
41    value = value.replace('\n', '\\\n')
42    return value
43
44
45def parse_file(file_path):
46    root = ET.parse(file_path).getroot()
47    result = '#include <QtGlobal>\n\n'
48
49    index = 0
50    for e in root.findall('snippet'):
51        if 'complement' in e.attrib:
52            text = fix_value(e.attrib['complement'])
53            if text:
54                result += 'const char *a{} = QT_TRANSLATE_NOOP3("TextEditor::Internal::Snippets", "{}", "group:\'{}\' trigger:\'{}\'"); // {}\n' \
55                    .format(index, text, e.attrib['group'], e.attrib['trigger'], file_path)
56
57            index += 1
58    return result
59
60result = ''
61
62# traverse root directory, and list directories as dirs and files as files
63for root, _, files in os.walk(top_dir):
64    for file in files:
65        if file.endswith('.xml'):
66            result += parse_file(os.path.join(root, file))
67
68with open(target_file, 'w') as header_file:
69    header_file.write(result)
70