[pyqgis-console] using gist to share snippets instead of codepad

This commit is contained in:
Salvatore Larosa 2020-09-15 14:33:19 +02:00 committed by Nyall Dawson
parent df564fd1af
commit c92e873815
3 changed files with 175 additions and 152 deletions

View File

@ -36,6 +36,7 @@ import traceback
import codecs
import re
import importlib
from functools import partial
class KeyFilter(QObject):
@ -270,9 +271,13 @@ class Editor(QgsPythonConsoleBase):
QCoreApplication.translate("PythonConsole", "Uncomment"),
self.parent.pc.uncommentCode, 'Shift+Ctrl+3')
menu.addSeparator()
codePadAction = menu.addAction(self.iconCodePad,
QCoreApplication.translate("PythonConsole", "Share on Codepad"),
self.codepad)
gist_menu = QMenu(self)
gist_menu.setTitle(QCoreApplication.translate("PythonConsole", "Share on GitHub"))
gist_menu.setIcon(self.iconCodePad)
gist_menu.addAction(QCoreApplication.translate("PythonConsole", "Secret Gist"),
partial(self.shareOnGist, False))
gist_menu.addAction(QCoreApplication.translate("PythonConsole", "Public Gist"),
partial(self.shareOnGist, True))
showCodeInspection = menu.addAction(self.iconObjInsp,
QCoreApplication.translate("PythonConsole", "Hide/Show Object Inspector"),
self.objectListEditor)
@ -283,7 +288,7 @@ class Editor(QgsPythonConsoleBase):
syntaxCheck.setEnabled(False)
pasteAction.setEnabled(False)
pyQGISHelpAction.setEnabled(False)
codePadAction.setEnabled(False)
gist_menu.setEnabled(False)
cutAction.setEnabled(False)
runSelected.setEnabled(False) # spellok
copyAction.setEnabled(False)
@ -295,7 +300,8 @@ class Editor(QgsPythonConsoleBase):
runSelected.setEnabled(True) # spellok
copyAction.setEnabled(True)
cutAction.setEnabled(True)
codePadAction.setEnabled(True)
if self.settings.value("pythonConsole/accessTokenGithub", ''):
gist_menu.setEnabled(True)
pyQGISHelpAction.setEnabled(True)
if not self.text() == '':
selectAllAction.setEnabled(True)
@ -358,36 +364,34 @@ class Editor(QgsPythonConsoleBase):
listObj.show()
self.parent.pc.objectListButton.setChecked(True)
def codepad(self):
import urllib.request
import urllib.parse
import urllib.error
listText = self.selectedText().split('\n')
getCmd = []
for strLine in listText:
getCmd.append(strLine)
pasteText = "\n".join(getCmd)
url = 'http://codepad.org'
values = {'lang': 'Python',
'code': pasteText,
'submit': 'Submit'}
def shareOnGist(self, is_public):
import requests
import json
ACCESS_TOKEN = self.settings.value("pythonConsole/accessTokenGithub", '')
URL = "https://api.github.com/gists"
headers = {'Authorization': 'token %s' % ACCESS_TOKEN}
params = {'scope': 'gist'}
path = self.parent.tw.currentWidget().path
filename = os.path.basename(path) if path else None
filename = filename if filename else "pyqgis_snippet.py"
selected_text = self.selectedText()
data = {"description": "Gist created by PyQGIS Console",
"public": is_public,
"files": {filename: {"content": selected_text}}}
try:
response = urllib.request.urlopen(url, urllib.parse.urlencode(values))
url = response.read()
for href in url.split("</a>"):
if "Link:" in href:
ind = href.index('Link:')
found = href[ind + 5:]
for i in found.split('">'):
if '<a href=' in i:
link = i.replace('<a href="', "").strip()
if link:
QApplication.clipboard().setText(link)
msgText = QCoreApplication.translate('PythonConsole', 'URL copied to clipboard.')
self.parent.pc.callWidgetMessageBarEditor(msgText, 0, True)
except urllib.error.URLError as e:
msgText = QCoreApplication.translate('PythonConsole', 'Connection error: ')
self.parent.pc.callWidgetMessageBarEditor(msgText + repr(e.args), 0, True)
res = requests.post(URL, headers=headers, params=params, data=json.dumps(data)).json()
print(res)
if res['html_url']:
QApplication.clipboard().setText(res['html_url'])
msg = QCoreApplication.translate('PythonConsole', 'URL copied to clipboard.')
self.parent.pc.callWidgetMessageBarEditor(msg, 0, True)
except requests.ConnectionError as e:
msg = QCoreApplication.translate('PythonConsole', 'Connection error: ')
self.parent.pc.callWidgetMessageBarEditor(msg + repr(e.args), 0, True)
def hideEditor(self):
self.parent.pc.splitterObj.hide()

View File

@ -145,6 +145,8 @@ class optionsDialog(QDialog, Ui_SettingsDialogPythonConsole):
settings.setValue("pythonConsole/preloadAPI", self.preloadAPI.isChecked())
settings.setValue("pythonConsole/autoSaveScript", self.autoSaveScript.isChecked())
settings.setValue("pythonConsole/accessTokenGithub", self.tokenGhLineEdit.text())
fontFamilyText = self.fontComboBox.currentText()
settings.setValue("pythonConsole/fontfamilytext", fontFamilyText)
@ -207,6 +209,7 @@ class optionsDialog(QDialog, Ui_SettingsDialogPythonConsole):
font.family())))
self.preloadAPI.setChecked(settings.value("pythonConsole/preloadAPI", True, type=bool))
self.lineEdit.setText(settings.value("pythonConsole/preparedAPIFile", "", type=str))
self.tokenGhLineEdit.setText(settings.value("pythonConsole/accessTokenGithub", "", type=str))
itemTable = settings.value("pythonConsole/userAPI", [])
if itemTable:
self.tableWidget.setRowCount(0)

View File

@ -70,12 +70,135 @@
<rect>
<x>0</x>
<y>0</y>
<width>887</width>
<height>658</height>
<width>881</width>
<height>722</height>
</rect>
</property>
<layout class="QGridLayout" name="gridLayout_9">
<item row="0" column="0">
<layout class="QGridLayout" name="gridLayout_4">
<item row="3" column="0">
<widget class="QgsCollapsibleGroupBox" name="groupBoxRunDebugEditor">
<property name="sizePolicy">
<sizepolicy hsizetype="MinimumExpanding" vsizetype="MinimumExpanding">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="title">
<string>Run and Debug</string>
</property>
<property name="collapsed" stdset="0">
<bool>false</bool>
</property>
<property name="saveCollapsedState" stdset="0">
<bool>true</bool>
</property>
<layout class="QGridLayout" name="gridLayout_21">
<item row="0" column="0">
<widget class="QCheckBox" name="enableObjectInspector">
<property name="text">
<string>Enable Object Inspector (switching between tabs may be slow)</string>
</property>
<property name="checked">
<bool>false</bool>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QCheckBox" name="autoSaveScript">
<property name="text">
<string>Auto-save script before running</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item row="1" column="0">
<widget class="QgsCollapsibleGroupBox" name="groupBoxAutoCompletion">
<property name="title">
<string>Autocompletion</string>
</property>
<property name="checkable">
<bool>true</bool>
</property>
<property name="checked">
<bool>true</bool>
</property>
<property name="collapsed" stdset="0">
<bool>false</bool>
</property>
<property name="saveCheckedState" stdset="0">
<bool>true</bool>
</property>
<property name="saveCollapsedState" stdset="0">
<bool>true</bool>
</property>
<layout class="QGridLayout" name="gridLayout_5">
<item row="1" column="0" colspan="3">
<layout class="QGridLayout" name="layoutRadioButton">
<item row="0" column="2">
<widget class="QRadioButton" name="autoCompFromDocAPI">
<property name="toolTip">
<string>Get autocompletion from current document and installed APIs</string>
</property>
<property name="text">
<string>From doc and APIs</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QRadioButton" name="autoCompFromAPI">
<property name="toolTip">
<string>Get autocompletion from installed APIs</string>
</property>
<property name="text">
<string>From API files</string>
</property>
<property name="checked">
<bool>true</bool>
</property>
</widget>
</item>
<item row="0" column="0">
<widget class="QRadioButton" name="autoCompFromDoc">
<property name="toolTip">
<string>Get autocompletion from current document</string>
</property>
<property name="text">
<string>From document</string>
</property>
</widget>
</item>
</layout>
</item>
<item row="0" column="2">
<widget class="QLabel" name="label_36">
<property name="text">
<string>characters</string>
</property>
</widget>
</item>
<item row="0" column="0">
<widget class="QLabel" name="label_6">
<property name="text">
<string>Autocompletion threshold</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QSpinBox" name="autoCompThreshold">
<property name="maximum">
<number>10</number>
</property>
<property name="value">
<number>2</number>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item row="0" column="0" colspan="2">
<widget class="QgsCollapsibleGroupBox" name="groupBoxFontColor">
<property name="sizePolicy">
<sizepolicy hsizetype="MinimumExpanding" vsizetype="MinimumExpanding">
@ -522,91 +645,6 @@
</layout>
</widget>
</item>
<item row="1" column="0">
<widget class="QgsCollapsibleGroupBox" name="groupBoxAutoCompletion">
<property name="title">
<string>Autocompletion</string>
</property>
<property name="checkable">
<bool>true</bool>
</property>
<property name="checked">
<bool>true</bool>
</property>
<property name="collapsed" stdset="0">
<bool>false</bool>
</property>
<property name="saveCheckedState" stdset="0">
<bool>true</bool>
</property>
<property name="saveCollapsedState" stdset="0">
<bool>true</bool>
</property>
<layout class="QGridLayout" name="gridLayout_5">
<item row="0" column="0">
<widget class="QLabel" name="label_6">
<property name="text">
<string>Autocompletion threshold</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QSpinBox" name="autoCompThreshold">
<property name="maximum">
<number>10</number>
</property>
<property name="value">
<number>2</number>
</property>
</widget>
</item>
<item row="0" column="2">
<widget class="QLabel" name="label_36">
<property name="text">
<string>characters</string>
</property>
</widget>
</item>
<item row="1" column="0" colspan="3">
<layout class="QGridLayout" name="layoutRadioButton">
<item row="0" column="2">
<widget class="QRadioButton" name="autoCompFromDocAPI">
<property name="toolTip">
<string>Get autocompletion from current document and installed APIs</string>
</property>
<property name="text">
<string>From doc and APIs</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QRadioButton" name="autoCompFromAPI">
<property name="toolTip">
<string>Get autocompletion from installed APIs</string>
</property>
<property name="text">
<string>From API files</string>
</property>
<property name="checked">
<bool>true</bool>
</property>
</widget>
</item>
<item row="0" column="0">
<widget class="QRadioButton" name="autoCompFromDoc">
<property name="toolTip">
<string>Get autocompletion from current document</string>
</property>
<property name="text">
<string>From document</string>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
</item>
<item row="2" column="0">
<widget class="QgsCollapsibleGroupBox" name="groupBox_4">
<property name="title">
@ -640,38 +678,16 @@
</layout>
</widget>
</item>
<item row="3" column="0">
<widget class="QgsCollapsibleGroupBox" name="groupBoxRunDebugEditor">
<property name="sizePolicy">
<sizepolicy hsizetype="MinimumExpanding" vsizetype="MinimumExpanding">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<item row="4" column="0">
<widget class="QGroupBox" name="groupBox">
<property name="title">
<string>Run and Debug</string>
<string>GitHub Access Token</string>
</property>
<property name="collapsed" stdset="0">
<bool>false</bool>
</property>
<property name="saveCollapsedState" stdset="0">
<bool>true</bool>
</property>
<layout class="QGridLayout" name="gridLayout_21">
<layout class="QGridLayout" name="gridLayout_6">
<item row="0" column="0">
<widget class="QCheckBox" name="enableObjectInspector">
<property name="text">
<string>Enable Object Inspector (switching between tabs may be slow)</string>
</property>
<property name="checked">
<bool>false</bool>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QCheckBox" name="autoSaveScript">
<property name="text">
<string>Auto-save script before running</string>
<widget class="QLineEdit" name="tokenGhLineEdit">
<property name="placeholderText">
<string>&lt;ACCESS_TOKEN&gt;</string>
</property>
</widget>
</item>