1# -*- coding:utf-8 -*-
2"""
3/***************************************************************************
4Module to generate prepared APIs for calltips and auto-completion.
5                             -------------------
6begin                : 2013-08-29
7copyright            : (C) 2013 Larry Shaffer
8email                : larrys (at) dakotacarto (dot) com
9 ***************************************************************************/
10
11/***************************************************************************
12 *                                                                         *
13 *   This program is free software; you can redistribute it and/or modify  *
14 *   it under the terms of the GNU General Public License as published by  *
15 *   the Free Software Foundation; either version 2 of the License, or     *
16 *   (at your option) any later version.                                   *
17 *                                                                         *
18 ***************************************************************************/
19Portions of this file contain code from Eric4 APIsManager module.
20"""
21
22import sys
23import os
24
25from qgis.PyQt.Qsci import QsciLexerPython, QsciAPIs
26from qgis.PyQt.QtWidgets import QApplication
27from qgis.PyQt.QtCore import QObject
28
29
30class PrepareAPIs(QObject):
31
32    def __init__(self, api_lexer, api_files, pap_file):
33        QObject.__init__(self)
34        self._api = None
35        self._api_files = api_files
36        self._api_lexer = api_lexer
37        self._pap_file = pap_file
38
39    def _clearLexer(self):
40        self.qlexer = None
41
42    def _stopPreparation(self):
43        if self._api is not None:
44            self._api.cancelPreparation()
45        self._api = None
46        sys.exit(1)
47
48    def _preparationFinished(self):
49        self._clearLexer()
50        try:
51            if os.path.exists(self._pap_file):
52                os.remove(self._pap_file)
53            prepd = self._api.savePrepared(str(self._pap_file))
54            self._api = None
55            sys.exit(0 if prepd else 1)
56        except Exception as err:
57            self._api = None
58            sys.exit(1)
59
60    def prepareAPI(self):
61        try:
62            self._api = QsciAPIs(self._api_lexer)
63            self._api.apiPreparationFinished.connect(self._preparationFinished)
64            for api_file in self._api_files:
65                self._api.load(str(api_file))
66            self._api.prepare()
67        except Exception as err:
68            self._api = None
69            sys.exit(1)
70
71
72if __name__ == '__main__':
73    if len(sys.argv) != 4:
74        print('Usage: python <script> <pap_file> <apis_src_dir> <api_bin_dir>')
75        sys.exit(1)
76    pap_file = sys.argv[1]
77    api_src_dir = sys.argv[2]
78    api_bin_dir = sys.argv[3]
79
80    api_files = [
81        os.path.join(api_bin_dir, 'PyQGIS.api'),
82        os.path.join(api_src_dir, 'Python-3.6.api'),
83        os.path.join(api_src_dir, 'PyQt5.api'),
84        os.path.join(api_src_dir, 'OSGeo_GEOS-3.6.2.api'),
85        os.path.join(api_src_dir, 'OSGeo_GDAL-OGR-2.2.3.api')
86    ]
87    # print api_files.__repr__()
88    # print pap_file.__repr__()
89
90    app = QApplication(sys.argv, False)  # just start a non-gui console app
91    api_lexer = QsciLexerPython()
92    prepap = PrepareAPIs(api_lexer, api_files, pap_file)
93    prepap.prepareAPI()
94
95    sys.exit(app.exec_())
96