-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Feature][Python Console] Add Reformat code action #51733
Conversation
Great addition @YoannQDQ ! Since it's a new feature it'll need to wait till after 3.30 is released before it's eligible for merge. |
Nice addition indeed ! Are we sure that |
Hum... I'm not sure. Besides, I just added black as well, and I know for sure it is NOT installed (neither on Linux nor on Windows...). Do you happen to know how we can ensure black and autopep8 are installed? |
Yes, for try:
import autopep8
AUTO_PEP8_INSTALLED=True
except ImportError:
AUTO_PEP8_INSTALLED=False And then enable buttons carefully according to this variable ? |
Thanks, but I meant "How to force black and autopep8 to be packaged with QGIS?". |
dadee7f
to
382c6d7
Compare
Nice! |
You're probably right. I'll change it. And yes, they are. |
Once #51783 is merged, you'll be able to use the new structure for the settings in Python.
|
5ffd6d7
to
0ec0da6
Compare
IMO, it definitely needs a try/except on import as proposed in #51733 (comment)
Python error on startup |
Yes my bad, I should have explicitly converted this PR to a draft before. I'm well aware that it does not work as it is right now, but I'm reluctant to just wrap the imports in So, in my earlier comment, I asked how to properly ensure the relevant python modules are installed along QGIS, for every supported platform (at least Windows, MacOS, Ubuntu, Debian). Another possibility would be to use the following function to install modules as needed. import importlib
import subprocess
def install(module):
try:
return importlib.import_module(module)
except ModuleNotFoundError:
pass
try:
import pip
except ModuleNotFoundError:
print(f"Pip not found. Try installing python3-pip with your package manager")
return None
result = subprocess.run(f"python -m pip install {module}", shell=True, capture_output=True)
if result.returncode != 0:
print(f"Pip error: {result.stderr.decode('utf8')}")
return None
try:
return importlib.import_module(module)
except ModuleNotFoundError:
print("Module not found")
return None When the user triggers the reformat action, he would be prompted to confirm the installation of the required python modules (which could fail depending on the platform, and in offline situations). def check_install(required_modules):
missing = set()
for module in set(required_modules):
try:
importlib.import_module(module)
except ModuleNotFoundError:
missing.add(module)
if not missing:
return True
modules = "\n - ".join(sorted(missing))
res = QMessageBox.question(
iface.mainWindow(),
"Info",
"Some required python modules are not installed:\n - {0}\n Do you want to try and install them now?".format(modules)
)
if res != QMessageBox.Yes:
return False
for module in sorted():
if install(missing):
missing.remove(module)
if not missing:
QMessageBox.information(
iface.mainWindow(),
"Info",
"Modules were successfuly installed!"
)
return True
QMessageBox.warning(
iface.mainWindow(),
"Warning",
"Some modules could not be installed"
)
return False |
39ea4ca
to
f526ab4
Compare
I wouldn't worry about that. This feature is quite advanced even for many people who write Python scripts in QGIS. I would bet that those who care about code formatting are very likely to also be capable of installing packages to their QGIS environment. PS: This is a super cool feature, thank you! I'll use it a lot :) |
+1 with @kannes I guess it also depends how visible are these buttons if the underneath library is not installed.
I guess the second one is better to "invite" people installing them. But from a UX point of view, the first one is better. I would go for 2 I think. |
65cad6a
to
9b1e2dc
Compare
f1c1e61
to
db9d0fc
Compare
c58cf16
to
5669f16
Compare
@YoannQDQ would you mind splitting out the package installation parts of this PR for submission as a follow up pr? Those are slightly controversial and I'd like to merge just the actual reformatting part of this work before we tackle the harder questions... |
5669f16
to
0cc4398
Compare
Done. |
Thanks @YoannQDQ ! |
@YoannQDQ A documentation ticket will be opened at https://github.com/qgis/QGIS-Documentation when this PR is merged. Please update the description (not the comments) with helpful description and screenshot to help the work from documentors. Thank you! |
@YoannQDQ |
This PR introduced settings without correctly declaring them as stated here #51733 (comment) |
Description
Add a format code action in the Python Console Editor:
Settings
Sorting example
isort will sort import statement in three different groups: