1# -*- coding: utf-8 -*-
2
3# Copyright (c) 2007 - 2021 Detlev Offenbach <detlev@die-offenbachs.de>
4#
5
6"""
7Module implementing the Ericdoc plugin.
8"""
9
10import os
11
12from PyQt5.QtCore import QObject, QCoreApplication
13from PyQt5.QtWidgets import QDialog
14
15from E5Gui.E5Application import e5App
16
17from E5Gui.E5Action import E5Action
18
19import Utilities
20import UI.Info
21
22from eric6config import getConfig
23
24# Start-Of-Header
25name = "Ericdoc Plugin"
26author = "Detlev Offenbach <detlev@die-offenbachs.de>"
27autoactivate = True
28deactivateable = True
29version = UI.Info.VersionOnly
30className = "EricdocPlugin"
31packageName = "__core__"
32shortDescription = "Show the Ericdoc dialogs."
33longDescription = (
34    """This plugin implements the Ericdoc dialogs."""
35    """ Ericdoc is used to generate a source code documentation"""
36    """ for Python and Ruby projects."""
37)
38pyqtApi = 2
39# End-Of-Header
40
41error = ""
42
43
44def exeDisplayDataList():
45    """
46    Public method to support the display of some executable info.
47
48    @return dictionary containing the data to query the presence of
49        the executable
50    """
51    dataList = []
52
53    # 1. eric6_doc
54    exe = 'eric6_doc'
55    if Utilities.isWindowsPlatform():
56        exe = os.path.join(getConfig("bindir"), exe + '.cmd')
57        if not os.path.exists(exe):
58            exe = os.path.join(getConfig("bindir"), exe + '.bat')
59    else:
60        exe = os.path.join(getConfig("bindir"), exe)
61    dataList.append({
62        "programEntry": True,
63        "header": QCoreApplication.translate(
64            "EricdocPlugin", "eric Documentation Generator"),
65        "exe": exe,
66        "versionCommand": '--version',
67        "versionStartsWith": 'eric6_',
68        "versionPosition": -3,
69        "version": "",
70        "versionCleanup": None,
71    })
72
73    # 2. Qt Help Generator
74    exe = os.path.join(
75        Utilities.getQtBinariesPath(),
76        Utilities.generateQtToolName('qhelpgenerator')
77    )
78    if Utilities.isWindowsPlatform():
79        exe += '.exe'
80    dataList.append({
81        "programEntry": True,
82        "header": QCoreApplication.translate(
83            "EricdocPlugin", "Qt Help Tools"),
84        "exe": exe,
85        "versionCommand": '-v',
86        "versionStartsWith": 'Qt',
87        "versionPosition": -1,
88        "version": "",
89        "versionCleanup": (0, -1),
90    })
91
92    return dataList
93
94
95class EricdocPlugin(QObject):
96    """
97    Class implementing the Ericdoc plugin.
98    """
99    def __init__(self, ui):
100        """
101        Constructor
102
103        @param ui reference to the user interface object (UI.UserInterface)
104        """
105        super().__init__(ui)
106        self.__ui = ui
107        self.__initialize()
108
109    def __initialize(self):
110        """
111        Private slot to (re)initialize the plugin.
112        """
113        self.__projectAct = None
114
115    def activate(self):
116        """
117        Public method to activate this plugin.
118
119        @return tuple of None and activation status (boolean)
120        """
121        menu = e5App().getObject("Project").getMenu("Apidoc")
122        if menu:
123            self.__projectAct = E5Action(
124                self.tr('Generate documentation (eric6_doc)'),
125                self.tr('Generate &documentation (eric6_doc)'), 0, 0,
126                self, 'doc_eric6_doc')
127            self.__projectAct.setStatusTip(
128                self.tr('Generate API documentation using eric6_doc'))
129            self.__projectAct.setWhatsThis(self.tr(
130                """<b>Generate documentation</b>"""
131                """<p>Generate API documentation using eric6_doc.</p>"""
132            ))
133            self.__projectAct.triggered.connect(self.__doEricdoc)
134            e5App().getObject("Project").addE5Actions([self.__projectAct])
135            menu.addAction(self.__projectAct)
136
137        e5App().getObject("Project").showMenu.connect(self.__projectShowMenu)
138
139        return None, True
140
141    def deactivate(self):
142        """
143        Public method to deactivate this plugin.
144        """
145        e5App().getObject("Project").showMenu.disconnect(
146            self.__projectShowMenu)
147
148        menu = e5App().getObject("Project").getMenu("Apidoc")
149        if menu:
150            menu.removeAction(self.__projectAct)
151            e5App().getObject("Project").removeE5Actions([self.__projectAct])
152        self.__initialize()
153
154    def __projectShowMenu(self, menuName, menu):
155        """
156        Private slot called, when the the project menu or a submenu is
157        about to be shown.
158
159        @param menuName name of the menu to be shown (string)
160        @param menu reference to the menu (QMenu)
161        """
162        if menuName == "Apidoc" and self.__projectAct is not None:
163            self.__projectAct.setEnabled(
164                e5App().getObject("Project").getProjectLanguage() in
165                ["Python", "Python3", "Ruby", "MicroPython"])
166
167    def __doEricdoc(self):
168        """
169        Private slot to perform the eric6_doc api documentation generation.
170        """
171        from DocumentationPlugins.Ericdoc.EricdocConfigDialog import (
172            EricdocConfigDialog
173        )
174        eolTranslation = {
175            '\r': 'cr',
176            '\n': 'lf',
177            '\r\n': 'crlf',
178        }
179        project = e5App().getObject("Project")
180        parms = project.getData('DOCUMENTATIONPARMS', "ERIC4DOC")
181        dlg = EricdocConfigDialog(project, parms)
182        if dlg.exec() == QDialog.DialogCode.Accepted:
183            args, parms = dlg.generateParameters()
184            project.setData('DOCUMENTATIONPARMS', "ERIC4DOC", parms)
185
186            # add parameter for the eol setting
187            if not project.useSystemEol():
188                args.append(
189                    "--eol={0}".format(eolTranslation[project.getEolString()]))
190
191            # now do the call
192            from DocumentationPlugins.Ericdoc.EricdocExecDialog import (
193                EricdocExecDialog
194            )
195            dia = EricdocExecDialog("Ericdoc")
196            res = dia.start(args, project.ppath)
197            if res:
198                dia.exec()
199
200            outdir = Utilities.toNativeSeparators(parms['outputDirectory'])
201            if outdir == '':
202                outdir = 'doc'      # that is eric6_docs default output dir
203
204            # add it to the project data, if it isn't in already
205            outdir = project.getRelativePath(outdir)
206            if outdir not in project.pdata['OTHERS']:
207                project.pdata['OTHERS'].append(outdir)
208                project.setDirty(True)
209                project.othersAdded(outdir)
210
211            if parms['qtHelpEnabled']:
212                outdir = Utilities.toNativeSeparators(
213                    parms['qtHelpOutputDirectory'])
214                if outdir == '':
215                    outdir = 'help'
216                    # that is eric6_docs default QtHelp output dir
217
218                # add it to the project data, if it isn't in already
219                outdir = project.getRelativePath(outdir)
220                if outdir not in project.pdata['OTHERS']:
221                    project.pdata['OTHERS'].append(outdir)
222                    project.setDirty(True)
223                    project.othersAdded(outdir)
224