Skip to content

Commit

Permalink
[pyqgis-console] using gist to share snippets instead of codepad
Browse files Browse the repository at this point in the history
  • Loading branch information
slarosa authored and nyalldawson committed Sep 26, 2020
1 parent df564fd commit c92e873
Show file tree
Hide file tree
Showing 3 changed files with 175 additions and 152 deletions.
72 changes: 38 additions & 34 deletions python/console/console_editor.py
Expand Up @@ -36,6 +36,7 @@
import codecs import codecs
import re import re
import importlib import importlib
from functools import partial




class KeyFilter(QObject): class KeyFilter(QObject):
Expand Down Expand Up @@ -270,9 +271,13 @@ def contextMenuEvent(self, e):
QCoreApplication.translate("PythonConsole", "Uncomment"), QCoreApplication.translate("PythonConsole", "Uncomment"),
self.parent.pc.uncommentCode, 'Shift+Ctrl+3') self.parent.pc.uncommentCode, 'Shift+Ctrl+3')
menu.addSeparator() menu.addSeparator()
codePadAction = menu.addAction(self.iconCodePad, gist_menu = QMenu(self)
QCoreApplication.translate("PythonConsole", "Share on Codepad"), gist_menu.setTitle(QCoreApplication.translate("PythonConsole", "Share on GitHub"))
self.codepad) 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, showCodeInspection = menu.addAction(self.iconObjInsp,
QCoreApplication.translate("PythonConsole", "Hide/Show Object Inspector"), QCoreApplication.translate("PythonConsole", "Hide/Show Object Inspector"),
self.objectListEditor) self.objectListEditor)
Expand All @@ -283,7 +288,7 @@ def contextMenuEvent(self, e):
syntaxCheck.setEnabled(False) syntaxCheck.setEnabled(False)
pasteAction.setEnabled(False) pasteAction.setEnabled(False)
pyQGISHelpAction.setEnabled(False) pyQGISHelpAction.setEnabled(False)
codePadAction.setEnabled(False) gist_menu.setEnabled(False)
cutAction.setEnabled(False) cutAction.setEnabled(False)
runSelected.setEnabled(False) # spellok runSelected.setEnabled(False) # spellok
copyAction.setEnabled(False) copyAction.setEnabled(False)
Expand All @@ -295,7 +300,8 @@ def contextMenuEvent(self, e):
runSelected.setEnabled(True) # spellok runSelected.setEnabled(True) # spellok
copyAction.setEnabled(True) copyAction.setEnabled(True)
cutAction.setEnabled(True) cutAction.setEnabled(True)
codePadAction.setEnabled(True) if self.settings.value("pythonConsole/accessTokenGithub", ''):
gist_menu.setEnabled(True)
pyQGISHelpAction.setEnabled(True) pyQGISHelpAction.setEnabled(True)
if not self.text() == '': if not self.text() == '':
selectAllAction.setEnabled(True) selectAllAction.setEnabled(True)
Expand Down Expand Up @@ -358,36 +364,34 @@ def objectListEditor(self):
listObj.show() listObj.show()
self.parent.pc.objectListButton.setChecked(True) self.parent.pc.objectListButton.setChecked(True)


def codepad(self): def shareOnGist(self, is_public):
import urllib.request import requests
import urllib.parse import json
import urllib.error
listText = self.selectedText().split('\n') ACCESS_TOKEN = self.settings.value("pythonConsole/accessTokenGithub", '')
getCmd = [] URL = "https://api.github.com/gists"
for strLine in listText:
getCmd.append(strLine) headers = {'Authorization': 'token %s' % ACCESS_TOKEN}
pasteText = "\n".join(getCmd) params = {'scope': 'gist'}
url = 'http://codepad.org'
values = {'lang': 'Python', path = self.parent.tw.currentWidget().path
'code': pasteText, filename = os.path.basename(path) if path else None
'submit': 'Submit'} 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: try:
response = urllib.request.urlopen(url, urllib.parse.urlencode(values)) res = requests.post(URL, headers=headers, params=params, data=json.dumps(data)).json()
url = response.read() print(res)
for href in url.split("</a>"): if res['html_url']:
if "Link:" in href: QApplication.clipboard().setText(res['html_url'])
ind = href.index('Link:') msg = QCoreApplication.translate('PythonConsole', 'URL copied to clipboard.')
found = href[ind + 5:] self.parent.pc.callWidgetMessageBarEditor(msg, 0, True)
for i in found.split('">'): except requests.ConnectionError as e:
if '<a href=' in i: msg = QCoreApplication.translate('PythonConsole', 'Connection error: ')
link = i.replace('<a href="', "").strip() self.parent.pc.callWidgetMessageBarEditor(msg + repr(e.args), 0, True)
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)


def hideEditor(self): def hideEditor(self):
self.parent.pc.splitterObj.hide() self.parent.pc.splitterObj.hide()
Expand Down
3 changes: 3 additions & 0 deletions python/console/console_settings.py
Expand Up @@ -145,6 +145,8 @@ def saveSettings(self):
settings.setValue("pythonConsole/preloadAPI", self.preloadAPI.isChecked()) settings.setValue("pythonConsole/preloadAPI", self.preloadAPI.isChecked())
settings.setValue("pythonConsole/autoSaveScript", self.autoSaveScript.isChecked()) settings.setValue("pythonConsole/autoSaveScript", self.autoSaveScript.isChecked())


settings.setValue("pythonConsole/accessTokenGithub", self.tokenGhLineEdit.text())

fontFamilyText = self.fontComboBox.currentText() fontFamilyText = self.fontComboBox.currentText()
settings.setValue("pythonConsole/fontfamilytext", fontFamilyText) settings.setValue("pythonConsole/fontfamilytext", fontFamilyText)


Expand Down Expand Up @@ -207,6 +209,7 @@ def restoreSettings(self):
font.family()))) font.family())))
self.preloadAPI.setChecked(settings.value("pythonConsole/preloadAPI", True, type=bool)) self.preloadAPI.setChecked(settings.value("pythonConsole/preloadAPI", True, type=bool))
self.lineEdit.setText(settings.value("pythonConsole/preparedAPIFile", "", type=str)) self.lineEdit.setText(settings.value("pythonConsole/preparedAPIFile", "", type=str))
self.tokenGhLineEdit.setText(settings.value("pythonConsole/accessTokenGithub", "", type=str))
itemTable = settings.value("pythonConsole/userAPI", []) itemTable = settings.value("pythonConsole/userAPI", [])
if itemTable: if itemTable:
self.tableWidget.setRowCount(0) self.tableWidget.setRowCount(0)
Expand Down

0 comments on commit c92e873

Please sign in to comment.