-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugin.py
159 lines (129 loc) · 5.15 KB
/
plugin.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# -*- coding: utf-8 -*-
"""
************************************************************************
Name : plugin.py
Description : QGIS plugin to run external Python files.
copyright : (C) 2023 by Gabriel De Luca
email : caprieldeluca@gmail.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 3 of the License, or
(at your option) any later version.
************************************************************************
"""
import io
from pathlib import Path
import runpy
import sys, traceback
from qgis.core import QgsApplication
from qgis.PyQt.QtGui import QIcon
from qgis.PyQt.QtCore import QSettings
from qgis.PyQt.QtWidgets import (
QAction,
QMenu,
QFileDialog
)
class Puentes:
"""Main plugin class."""
def __init__(self, iface):
"""Init the plugin."""
self.iface = iface
self.welcome_path = str(Path(__file__).parent / 'welcome.py')
self.file_path = QSettings().value('plugins/puentes/file_path', self.welcome_path)
def initGui(self):
"""Init actions, menu entries and toolbar."""
# Init actions
self.run_action = QAction(
icon=QIcon(str(Path(__file__).parent / 'run.png')),
text='&Run',
parent=self.iface.mainWindow())
self.configure_action = QAction(
icon=QIcon(str(Path(__file__).parent / 'configure.png')),
text='&Configure',
parent=self.iface.mainWindow())
# Connect actions to run methods
self.run_action.triggered.connect(
self.run_command)
self.configure_action.triggered.connect(
self.configure_command)
# Init menu
self.menu = QMenu('&Puentes')
self.menu.addActions([
self.run_action,
self.configure_action])
self.iface.pluginMenu().addMenu(self.menu)
# Init toolbar
self.toolbar = self.iface.addToolBar('Puentes Toolbar')
self.toolbar.setObjectName('Puentes Toolbar')
self.toolbar.addAction(self.run_action)
def unload(self):
"""Remove menu entry and toolbar."""
self.iface.removePluginMenu(
'&Puentes',
self.run_action)
self.iface.removePluginMenu(
'&Puentes',
self.configure_action)
del self.toolbar
#####
# Run command
#####
def run_command(self):
"""Load (run) the bridged module"""
plog("----------")
plog("Run:", self.file_path)
try:
runpy.run_path(self.file_path, init_globals={'plog': plog})
except Exception:
# From Python 3.10 only exc_value (the Exception instance) is needed,
# exc_type and exc_traceback are preserved for backwards compatibility
exc_type, exc_value, exc_traceback = sys.exc_info()
# Create a StackSummary object to get its length
stack_length = len(traceback.extract_tb(exc_traceback))
# Define limit as negative index to remove first frame
# (this file exception) from the stacktrace
limit = 1 - stack_length
plog(*traceback.format_exception(exc_type,
exc_value,
exc_traceback,
limit=limit))
#####
# Configure command
#####
def configure_command(self):
"""Set a new Python file to be loaded"""
(filename, filter) = QFileDialog.getOpenFileName(
parent=self.iface.mainWindow(),
caption='Open Python File',
directory=str(Path(self.file_path).parent),
filter="*.py")
if filename:
self.file_path = str(Path(filename))
# Save the path to settings right here, so it does not
# depend on run.
QSettings().setValue('plugins/puentes/file_path', self.file_path)
plog("----------")
plog("Configured to run:", self.file_path)
#####
# plog (global)
#####
def plog(*objects, level=0):
"""Send *objects as log to Puentes tab of Log Messages Panel
Use plog() instead of print() in pyqgis files.
The plog name is assigned as a global name to the executed file.
'from puentes.plugin import plog can be used in any other code from QGIS.
level: 0 INFO (default)
1 WARNING
2 CRITICAL
"""
messagelog = QgsApplication.messageLog()
stringio = io.StringIO()
# End without newline, so force to flush the io buffer
print(*objects, end='', file=stringio, flush=True)
msg = stringio.getvalue()
formatted_msg = msg.replace("<","~::").replace(">","::~")
messagelog.logMessage(
message=formatted_msg,
tag="Puentes",
level=level)