Skip to content

Commit

Permalink
Added support to save history in python console
Browse files Browse the repository at this point in the history
- edited console.py to save history on closeEvent
- edited console_sci.py to add read, clear and write history methods
- edited help file
- update to Italian translate file
  • Loading branch information
slarosa committed Sep 25, 2012
1 parent 69d0624 commit 59ff69e
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 16 deletions.
12 changes: 10 additions & 2 deletions i18n/qgis_it.ts
Expand Up @@ -6281,13 +6281,21 @@ Cambiare questa situazione prima, perché il plugin OSM non quale layer è la de
<source>Run command</source>
<translation>Esegui comando</translation>
</message>
<message>
<source>## History saved successfully ##</source>
<translation>## Storia comandi salvata con successo ##</translation>
</message>
<message>
<source>## History cleared successfully ##</source>
<translation>## Storia comandi cancellata con successo ##</translation>
</message>
<message>
<source>## To access Quantum GIS environment from this console
## use qgis.utils.iface object (instance of QgisInterface class).
## use qgis.utils.iface object (instance of QgisInterface class). Read help for more info.

</source>
<translation>## Per accedere all&apos;ambiente Quantum GIS da questa console
## usa l&apos;oggetto qgis.utils.iface (istanza della classe QgisInterface).
## usa l&apos;oggetto qgis.utils.iface (istanza della classe QgisInterface). Consulta l'aiuto per ottenere più informazioni.

</translation>
</message>
Expand Down
1 change: 1 addition & 0 deletions python/console.py
Expand Up @@ -260,6 +260,7 @@ def openHelp(self):
dlg.exec_()

def closeEvent(self, event):
self.edit.writeHistoryFile()
QWidget.closeEvent(self, event)


Expand Down
64 changes: 51 additions & 13 deletions python/console_sci.py
Expand Up @@ -32,6 +32,7 @@
import code

_init_commands = ["from qgis.core import *", "import qgis.utils"]
_historyFile = os.path.join(str(QDir.homePath()),".qgis","console_history.txt")

class PythonEdit(QsciScintilla, code.InteractiveInterpreter):
def __init__(self, parent=None):
Expand Down Expand Up @@ -59,6 +60,8 @@ def __init__(self, parent=None):

self.history = QStringList()
self.historyIndex = 0
# Read history command file
self.readHistoryFile()

# Brace matching: enable for a brace immediately before or after
# the current position
Expand Down Expand Up @@ -206,7 +209,7 @@ def setLexers(self, lexer):
def insertInitText(self):
#self.setLexers(False)
txtInit = QCoreApplication.translate("PythonConsole","## To access Quantum GIS environment from this console\n"
"## use qgis.utils.iface object (instance of QgisInterface class).\n\n")
"## use qgis.utils.iface object (instance of QgisInterface class). Read help for more info.\n\n")
initText = self.setText(txtInit)

def getCurrentPos(self):
Expand Down Expand Up @@ -311,6 +314,29 @@ def updateHistory(self, command):
self.history.append(command)
self.historyIndex = len(self.history)

def writeHistoryFile(self):
#hystoryFile = os.path.join(str(QDir.homePath()),".qgis","console_history.txt")
wH = open(_historyFile, 'w')
for s in self.history:
wH.write(s + '\n')
wH.close()

def readHistoryFile(self):
#hystoryFile = os.path.join(str(QDir.homePath()),".qgis","console_history.txt")
fileExist = QFile.exists(_historyFile)
if fileExist:
rH = open(_historyFile, 'r')
for line in rH:
if line != "\n":
l = line.rstrip('\n')
self.updateHistory(l)
else:
return

def clearHistoryFile(self):
cH = open(_historyFile, 'w')
cH.close()

def showPrevious(self):
if self.historyIndex < len(self.history) and not self.history.isEmpty():
line, pos = self.getCurLine()
Expand Down Expand Up @@ -475,18 +501,30 @@ def currentCommand(self):
def runCommand(self, cmd):
self.updateHistory(cmd)
self.SendScintilla(QsciScintilla.SCI_NEWLINE)
self.buffer.append(cmd)
src = "\n".join(self.buffer)
more = self.runsource(src, "<input>")
if not more:
self.buffer = []

output = sys.stdout.get_and_clean_data()
if output:
self.append(output)

self.move_cursor_to_end()
self.displayPrompt(more)
if cmd in ('_save', '_clear'):
if cmd == '_save':
self.writeHistoryFile()
print QCoreApplication.translate("PythonConsole", "## History saved successfully ##")
#del self.buffer[-1]
elif cmd == '_clear':
self.clearHistoryFile()
print QCoreApplication.translate("PythonConsole", "## History cleared successfully ##")
#del self.buffer[-1]
output = sys.stdout.get_and_clean_data()
if output:
self.append(output)
self.displayPrompt(False)
else:
self.buffer.append(cmd)
src = "\n".join(self.buffer)
more = self.runsource(src, "<input>")
if not more:
self.buffer = []
output = sys.stdout.get_and_clean_data()
if output:
self.append(output)
self.move_cursor_to_end()
self.displayPrompt(more)

def write(self, txt):
self.SendScintilla(QsciScintilla.SCI_SETSTYLING, len(txt), 1)
Expand Down
6 changes: 5 additions & 1 deletion python/helpConsole/help.htm
Expand Up @@ -22,7 +22,7 @@ <h2>Python Console for QGIS</h2>
</tr>
</table>
<p align="justify">
Now you can use auto-completion for syntax in console!!!
Now you can use auto-completion and highlighting for syntax in console!!!
<br>
(Thanks to Larry Shaffer who provided the API files)
<br><br>
Expand All @@ -31,6 +31,10 @@ <h2>Python Console for QGIS</h2>
To import the class QgisInterface can also use the dedicated
button on the toolbar on the left.
<br><br>
To save history commands type '<b>_save</b>' in console or close this widget.
<br><br>
To clear history commands type '<b>_clear</b>' in console.
<br><br>
The following is a description of the tools in the toolbar:
</p>
<table width="100%" bordercolor="#000" border="1">
Expand Down

0 comments on commit 59ff69e

Please sign in to comment.