Skip to content
Permalink
Browse files

Merge pull request #619 from slarosa/generate_pap

[FEATURE]  Module to generate prepared APIs file for call tips and auto-completion in Python Console
  • Loading branch information
slarosa committed Jun 12, 2013
2 parents a602112 + 3b4dfd7 commit 57cb57537e74ebbbb3177ed1631994132688a1c1
@@ -7,6 +7,7 @@ SET(PY_CONSOLE_FILES
console_settings.py
console_output.py
console_editor.py
console_compile_apis.py
__init__.py
)

@@ -0,0 +1,95 @@
# -*- coding:utf-8 -*-
"""
/***************************************************************************
Module to generate prepared APIs for calltips and auto-completion.
-------------------
begin : 2012-09-10
copyright : (C) 2012 Larry Shaffer
email : larrys (at) dakotacarto (dot) com
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
Portions of this file contain code from Eric4 APIsManager module.
"""

import sys
import os
import shutil
import fnmatch
import glob

from PyQt4.Qsci import *
from PyQt4.QtGui import *
from PyQt4.QtCore import *

from ui_console_compile_apis import Ui_APIsDialogPythonConsole

class PrepareAPIDialog(QDialog):
def __init__(self, api_lexer, api_files, pap_file, parent=None):
QDialog.__init__(self, parent)
self.ui = Ui_APIsDialogPythonConsole()
self.ui.setupUi(self)
self.setWindowTitle(QCoreApplication.translate("PythonConsole","Compile APIs"))
self.ui.plainTextEdit.setVisible(False)
self.ui.textEdit_Qsci.setVisible(False)
self.adjustSize()
self._api = None
self.ui.buttonBox.rejected.connect(self._stopPreparation)
self._api_files = api_files
self._api_lexer = api_lexer
self._pap_file = pap_file

def _clearLexer(self):
# self.ui.textEdit_Qsci.setLexer(0)
self.qlexer = None

def _stopPreparation(self):
if self._api is not None:
self._api.cancelPreparation()
self._api = None
self._clearLexer()
self.close()

def _preparationFinished(self):
self._clearLexer()
if os.path.exists(self._pap_file):
os.remove(self._pap_file)
self.ui.label.setText(QCoreApplication.translate("PythonConsole","Saving prepared file..."))
prepd = self._api.savePrepared(unicode(self._pap_file))
rslt = self.trUtf8("Error")
if prepd:
rslt = QCoreApplication.translate("PythonConsole","Saved")
self.ui.label.setText('{0} {1}'.format(self.ui.label.text(), rslt))
self._api = None
self.ui.progressBar.setVisible(False)
self.ui.buttonBox.button(QDialogButtonBox.Cancel).setText(
QCoreApplication.translate("PythonConsole","Done"))
self.adjustSize()

def prepareAPI(self):
# self.ui.textEdit_Qsci.setLexer(0)
exec 'self.qlexer = {0}(self.ui.textEdit_Qsci)'.format(self._api_lexer)
# self.ui.textEdit_Qsci.setLexer(self.qlexer)
self._api = QsciAPIs(self.qlexer)
self._api.apiPreparationFinished.connect(self._preparationFinished)
for api_file in self._api_files:
self._api.load(unicode(api_file))
try:
self._api.prepare()
except Exception, err:
self._api = None
self._clearLexer()
self.ui.label.setText(QCoreApplication.translate("PythonConsole","Error preparing file..."))
self.ui.progressBar.setVisible(False)
self.ui.plainTextEdit.setVisible(True)
self.ui.plainTextEdit.insertPlainText(err)
self.ui.buttonBox.button(QDialogButtonBox.Cancel).setText(
self.trUtf8("Done"))
self.adjustSize()
@@ -0,0 +1,98 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>APIsDialogPythonConsole</class>
<widget class="QDialog" name="APIsDialogPythonConsole">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>320</width>
<height>280</height>
</rect>
</property>
<property name="minimumSize">
<size>
<width>320</width>
<height>0</height>
</size>
</property>
<property name="windowTitle">
<string/>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QLabel" name="label">
<property name="minimumSize">
<size>
<width>320</width>
<height>0</height>
</size>
</property>
<property name="text">
<string>Generating prepared API file (please wait)...</string>
</property>
</widget>
</item>
<item>
<widget class="QProgressBar" name="progressBar">
<property name="enabled">
<bool>true</bool>
</property>
<property name="maximum">
<number>0</number>
</property>
<property name="value">
<number>-1</number>
</property>
</widget>
</item>
<item>
<widget class="QPlainTextEdit" name="plainTextEdit">
<property name="font">
<font>
<pointsize>12</pointsize>
</font>
</property>
<property name="readOnly">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<widget class="QsciScintilla" name="textEdit_Qsci">
<property name="toolTip">
<string/>
</property>
<property name="whatsThis">
<string/>
</property>
<property name="verticalScrollBarPolicy">
<enum>Qt::ScrollBarAlwaysOff</enum>
</property>
<property name="horizontalScrollBarPolicy">
<enum>Qt::ScrollBarAlwaysOff</enum>
</property>
</widget>
</item>
<item>
<widget class="QDialogButtonBox" name="buttonBox">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="standardButtons">
<set>QDialogButtonBox::Cancel</set>
</property>
</widget>
</item>
</layout>
</widget>
<customwidgets>
<customwidget>
<class>QsciScintilla</class>
<extends>QFrame</extends>
<header>Qsci/qsciscintilla.h</header>
</customwidget>
</customwidgets>
<resources/>
<connections/>
</ui>
@@ -237,8 +237,11 @@ def setLexers(self):

self.api = QsciAPIs(self.lexer)
chekBoxAPI = self.settings.value("pythonConsole/preloadAPI", True, type=bool)
chekBoxPreparedAPI = self.settings.value("pythonConsole/usePreparedAPIFile", False, type=bool)
if chekBoxAPI:
self.api.loadPrepared( QgsApplication.pkgDataPath() + "/python/qsci_apis/pyqgis_master.pap" )
self.api.loadPrepared(QgsApplication.pkgDataPath() + "/python/qsci_apis/pyqgis_master.pap")
elif chekBoxPreparedAPI:
self.api.loadPrepared(self.settings.value("pythonConsole/preparedAPIFile"))
else:
apiPath = self.settings.value("pythonConsole/userAPI", [])
for i in range(0, len(apiPath)):
@@ -189,8 +189,11 @@ def setLexers(self):

self.api = QsciAPIs(self.lexer)
chekBoxAPI = self.settings.value("pythonConsole/preloadAPI", True, type=bool)
chekBoxPreparedAPI = self.settings.value("pythonConsole/usePreparedAPIFile", False, type=bool)
if chekBoxAPI:
self.api.loadPrepared( QgsApplication.pkgDataPath() + "/python/qsci_apis/pyqgis_master.pap" )
self.api.loadPrepared(QgsApplication.pkgDataPath() + "/python/qsci_apis/pyqgis_master.pap")
elif chekBoxPreparedAPI:
self.api.loadPrepared(self.settings.value("pythonConsole/preparedAPIFile"))
else:
apiPath = self.settings.value("pythonConsole/userAPI", [])
for i in range(0, len(apiPath)):
@@ -21,6 +21,7 @@

from PyQt4.QtCore import *
from PyQt4.QtGui import *
from console_compile_apis import PrepareAPIDialog

from ui_console_settings import Ui_SettingsDialogPythonConsole

@@ -32,6 +33,7 @@ def __init__(self, parent):
self.setupUi(self)

self.listPath = []
self.lineEdit.setReadOnly(True)

self.restoreSettings()
self.initialCheck()
@@ -47,6 +49,7 @@ def __init__(self, parent):
SIGNAL("clicked()"), self.loadAPIFile)
self.connect(self.removeAPIpath,
SIGNAL("clicked()"), self.removeAPI)
self.compileAPIs.clicked.connect(self._prepareAPI)

def initialCheck(self):
if self.preloadAPI.isChecked():
@@ -58,6 +61,7 @@ def enableDisable(self, value):
self.tableWidget.setEnabled(value)
self.addAPIpath.setEnabled(value)
self.removeAPIpath.setEnabled(value)
self.groupBoxPreparedAPI.setEnabled(value)

def loadAPIFile(self):
settings = QSettings()
@@ -70,12 +74,41 @@ def loadAPIFile(self):
lastDirPath = QFileInfo(fileAPI).path()
settings.setValue("pythonConsole/lastDirAPIPath", fileAPI)

def _prepareAPI(self):
if self.tableWidget.rowCount() != 0:
pap_file = QFileDialog().getSaveFileName(self,
"",
'*.pap',
"Prepared APIs file (*.pap)")
else:
QMessageBox.information(self, self.tr("Warning!"),
self.tr('You need to add some APIs file in order to compile'))
return
if pap_file:
api_lexer = 'QsciLexerPython'
api_files = []
count = self.tableWidget.rowCount()
for i in range(0, count):
api_files.append(self.tableWidget.item(i, 1).text())
api_dlg = PrepareAPIDialog(api_lexer, api_files, pap_file, self)
api_dlg.show()
api_dlg.activateWindow()
api_dlg.raise_()
api_dlg.prepareAPI()
self.lineEdit.setText(pap_file)

def accept(self):
if not self.preloadAPI.isChecked():
if not self.preloadAPI.isChecked() and \
not self.groupBoxPreparedAPI.isChecked():
if self.tableWidget.rowCount() == 0:
QMessageBox.information(self, self.tr("Warning!"),
self.tr('Please specify API file or check "Use preloaded API files"'))
self.tr('Please specify API file or check "Use preloaded API files"'))
return
if self.groupBoxPreparedAPI.isChecked() and \
not self.lineEdit.text():
QMessageBox.information(self, self.tr("Warning!"),
self.tr('The APIs file was not compiled, click on "Compile APIs..."'))
return
self.saveSettings()
self.listPath = []
QDialog.accept( self )
@@ -122,6 +155,9 @@ def saveSettings(self):
settings.setValue("pythonConsole/autoCompleteEnabledEditor", self.groupBoxAutoCompletionEditor.isChecked())
settings.setValue("pythonConsole/autoCompleteEnabled", self.groupBoxAutoCompletion.isChecked())

settings.setValue("pythonConsole/usePreparedAPIFile", self.groupBoxPreparedAPI.isChecked())
settings.setValue("pythonConsole/preparedAPIFile", self.lineEdit.text())

if self.autoCompFromAPIEditor.isChecked():
settings.setValue("pythonConsole/autoCompleteSourceEditor", 'fromAPI')
elif self.autoCompFromDocEditor.isChecked():
@@ -147,8 +183,9 @@ def restoreSettings(self):
self.fontComboBox.setCurrentFont(QFont(settings.value("pythonConsole/fontfamilytext",
"Monospace")))
self.fontComboBoxEditor.setCurrentFont(QFont(settings.value("pythonConsole/fontfamilytextEditor",
"Monospace")))
"Monospace")))
self.preloadAPI.setChecked(settings.value("pythonConsole/preloadAPI", True, type=bool))
self.lineEdit.setText(settings.value("pythonConsole/preparedAPIFile"))
itemTable = settings.value("pythonConsole/userAPI", [])
if itemTable:
for i in range(len(itemTable)):

0 comments on commit 57cb575

Please sign in to comment.
You can’t perform that action at this time.