Compare commits

...
Author SHA1 Message Date
Mikaël Capelle ca2ca66de8 Fix version in config. 2022-04-21 21:16:20 +02:00
Mikaël Capelle 0ec04f4e9e Fix setup.py 2022-04-21 21:10:14 +02:00
Mikaël Capelle d43d067c84 Update for Qt6. 2022-04-21 21:03:48 +02:00
Mikaël Capelle 06e0fe1cca Update documentation. 2021-02-26 19:25:23 +01:00
Mikaël Capelle c57280578d Update PyQt5-stubs version. 2021-02-26 19:25:15 +01:00
Mikaël Capelle 178b1d111d Fix some argument names in the configuration file. 2021-02-07 20:20:02 +01:00
Mikaël Capelle a5b374f3f3 Bump stubs version to 2.4.0. 2021-02-06 20:05:13 +01:00
Mikaël Capelle 14fa117b2d Update documentation. 2021-02-06 19:57:20 +01:00
Mikaël Capelle 6cab2a555b Add highlighting for VS code settings. 2021-01-18 22:24:36 +01:00
Mikaël Capelle b555debad7 Fix documentation. 2021-01-18 22:22:49 +01:00
Mikaël Capelle 4f9e212b93 Update Github actions for 2.4. 2021-01-18 22:08:11 +01:00
Mikaël Capelle 1388c1113a Add documentation on how to automatically reload plugins. 2021-01-18 22:05:24 +01:00
Mikaël Capelle 15dae1d2bc Update following changes to code base. 2021-01-18 21:57:04 +01:00
Mikaël Capelle 0aa1e2d24f Ignore the widgets submodule (for now). 2021-01-01 19:38:07 +01:00
Mikaël Capelle df29edf5a5 Update configuration files. 2021-01-01 19:37:54 +01:00
Mikaël Capelle 15d43de37e Fix typos. 2020-11-30 12:50:39 +01:00
Mikaël Capelle 7b9c6ab32c Add refresh parameter in waitForApplication. 2020-11-30 12:40:25 +01:00
Mikaël Capelle 318a4acfb1 Update configuration for 2.4.0a4. 2020-11-21 14:18:26 +01:00
Mikaël Capelle 820bc3e0fe Fix duplicate functions detection for static methods. 2020-11-21 14:18:09 +01:00
Mikaël Capelle c980dd024b Discard void arguments when parsing functions. 2020-11-21 14:17:56 +01:00
Mikaël Capelle c7d8442398 Add fix for std::optional parsing. 2020-11-21 14:17:42 +01:00
Mikaël Capelle ae09efbab3 Fix Github actions. 2020-11-21 13:39:26 +01:00
Mikaël Capelle a203ee8215 Add mypy ignore for CI. 2020-11-14 13:49:50 +01:00
Mikaël Capelle 9fa09d1b88 Load module following PEP 489 method. 2020-11-14 13:44:44 +01:00
Mikaël Capelle 4d40644aa5 Update README. 2020-11-14 13:42:18 +01:00
Mikaël Capelle fa0facabe8 Remove unused import. 2020-11-14 13:36:43 +01:00
Mikaël Capelle 02be57db1d Clean code and add CI. 2020-11-14 13:34:51 +01:00
Mikaël Capelle 233c8f51e8 Update GH action. 2020-11-13 20:30:40 +01:00
Mikaël Capelle 6f390a4748 Update version. 2020-11-13 20:24:16 +01:00
Mikaël Capelle afd3751266 Update stubs for 2.4.3. 2020-11-13 20:19:44 +01:00
Mikaël Capelle c813d6e86a Fix description of dataDirectory. 2020-11-13 19:52:21 +01:00
Mikaël Capelle 822dfc161e Add examples in the documentation. 2020-10-07 20:47:37 +02:00
Mikaël Capelle c61c13e469 Switch to official pypi. 2020-09-24 18:53:58 +02:00
Mikaël Capelle 5d99ba470b Update config for 2.4.0a2. 2020-09-24 18:52:30 +02:00
Mikaël Capelle 6d48f0504b Add gh-actions to publish to pypi. 2020-09-24 18:47:46 +02:00
Mikaël Capelle c60d1ac92f Add favicon. 2020-09-24 16:15:27 +02:00
Mikaël Capelle 3f0da6f9a9 Add .nojekyll after documentation generation. 2020-09-24 16:05:31 +02:00
Mikaël Capelle 6e9912d1ef Fix github action. 2020-09-24 15:45:45 +02:00
Mikaël Capelle 2a06eab0a6 Fix github actions. 2020-09-24 15:37:01 +02:00
Mikaël Capelle d477f0849c Switch to separate repository for documentation. 2020-09-24 15:34:24 +02:00
Mikaël Capelle 9ca265736f Switch repository to ModOrganizer2 organization. 2020-09-24 14:40:39 +02:00
Mikaël Capelle 3a5afaf690 Update 2.4 configuration to match MO2 code. 2020-09-23 19:48:24 +02:00
25 changed files with 13734 additions and 601 deletions
+16 -22
View File
@@ -1,4 +1,4 @@
name: CI
name: Build Documentation
on:
push:
@@ -11,32 +11,26 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- uses: actions/checkout@v2
with:
persist-credentials: false
# Standard drop-in approach that should work for most people.
- uses: ammaraskar/sphinx-action@master
env:
PYTHONPATH: .
with:
pre-build-command: "apt-get update -y && apt-get install -y libgl1-mesa-glx && cp stubs/2.3.0/mobase.pyi docs/mobase.py"
pre-build-command: "apt-get update -y && apt-get install -y libgl1-mesa-glx && cp stubs/2.4.0/mobase.pyi docs/mobase.py"
docs-folder: "docs/"
# Publish built docs to gh-pages branch.
# ===============================
- name: Commit documentation changes
run: |
git clone https://github.com/Holt59/mo2-pystubs-generation.git --branch gh-pages --single-branch gh-pages
cp -r docs/build/html/* gh-pages/
cd gh-pages
git config --local user.email "action@github.com"
git config --local user.name "GitHub Action"
git add .
git commit -m "Update documentation." -a || true
# The above command will fail if no changes were present, so we ignore
# that.
- name: Push changes
uses: ad-m/github-push-action@master
- name: Install SSH Client 🔑
uses: webfactory/ssh-agent@v0.4.1
with:
branch: gh-pages
directory: gh-pages
github_token: ${{ secrets.GITHUB_TOKEN }}
ssh-private-key: ${{ secrets.DEPLOY_KEY }}
- name: Deploy 🚀
uses: JamesIves/github-pages-deploy-action@3.7.1
with:
SSH: true
REPOSITORY_NAME: ModOrganizer2/python-plugins-doc
BRANCH: master
FOLDER: docs/build/html
+24
View File
@@ -0,0 +1,24 @@
name: Check Linting
on: [push, pull_request]
jobs:
checks:
runs-on: ubuntu-latest
strategy:
max-parallel: 4
matrix:
python-version: [3.8]
steps:
- uses: actions/checkout@v2
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install tox
- name: Test with tox
run: tox -e py38-lint
+3 -3
View File
@@ -1,4 +1,4 @@
name: CI
name: Check Documentation
on: [pull_request]
@@ -14,5 +14,5 @@ jobs:
env:
PYTHONPATH: .
with:
pre-build-command: "apt-get update -y && apt-get install -y libgl1-mesa-glx && cp stubs/2.3.0/mobase.pyi docs/mobase.py"
docs-folder: "docs/"
pre-build-command: "apt-get update -y && apt-get install -y libgl1-mesa-glx && cp stubs/2.4.0/mobase.pyi docs/mobase.py"
docs-folder: "docs/"
+40
View File
@@ -0,0 +1,40 @@
# This workflows will upload a Python Package using Twine when a release is created
# For more information see: https://help.github.com/en/actions/language-and-framework-guides/using-python-with-github-actions#publishing-to-package-registries
name: Upload Python Package
on:
release:
types: [published]
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Replace string
uses: frabert/replace-string-action@v1.1
id: version
with:
string: ${{ github.event.release.tag_name }}
pattern: "v?([0-9][.][0-9][.][0-9]).*"
replace-with: "$1"
- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: '3.10'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install setuptools wheel twine
- name: Build and publish
env:
TWINE_USERNAME: ${{ secrets.PYPI_USERNAME }}
TWINE_PASSWORD: ${{ secrets.PYPI_PASSWORD }}
run: |
cd stubs/setup
cp ../${{ steps.version.outputs.replaced }}/mobase.pyi mobase-stubs/__init__.pyi
python setup.py sdist bdist_wheel
twine upload dist/*
+31 -44
View File
@@ -1,6 +1,6 @@
# Mod Organizer 2 - Python stubs generation
This little project can be used to generate python stubs (`.pyi` file) for the MO2 python
This project can be used to generate python stubs (`.pyi` file) for the MO2 python
interface `mobase`.
## Using the stubs
@@ -31,35 +31,35 @@ pip install .
Some words of warning:
- The stubs are as correct as possible, but some errors are expected.
- If you see a `InterfaceNotImplemented` class anywhere in the stubs, it means that a proper interface is
currently not available.
- Some classes are said (in the stubs) to inherit `QWidget` or `QObject`. This is true on the C++ side but NOT
on the python side. The inheritance is only added to help with auto-completion since these classes also
override `__getattr__` to dispatch to the underlying `QWidget` or `QObject`. Some things might not work as
expected with these class (e.g., `isintance(myObject, QObject)` will return `False`), which is why a
`_object()` and `_widget()` method is also provided.
- If you see a `InterfaceNotImplemented` class anywhere in the stubs, it means that
a proper interface is currently not available.
- Some classes are said (in the stubs) to inherit `QWidget` or `QObject`. This is true
on the C++ side but NOT on the python side. The inheritance is only added to help with
auto-completion since these classes also override `__getattr__` to dispatch to the
underlying `QWidget` or `QObject`. Some things might not work as expected with these
class (e.g., `isinstance(myObject, QObject)` will return `False`), which is why a
`_object()` and `_widget()` method is also provided.
## Generating the stubs
The stubs are generated using python by parsing the `mobase` module.
You need the version of python that matches your current MO2 installation: e.g., if you have a `python38.dll` in
your MO2 installation path, then you need **python 3.8**.
You need the version of python that matches your current MO2 installation: e.g., if you
have a `python38.dll` in your MO2 installation path, then you need **Python 3.8**.
To generate the stubs, you can run:
```
# Change the output folder to whatever you want:
python main.py -c configs\config-2.3.yml -o stubs\setup\mobase-stubs\__init__.pyi ${MO2_INSTALL_PATH}
python main.py -c configs\config-2.4.yml ${MO2_INSTALL_PATH}
```
Where `${MO2_INSTALL_PATH}` is the path to your MO2 installation (the one containing `ModOrganizer.exe`).
The latest stubs are kept under `stubs/setup/mobase-stubs/__init__.pyi`, and when a new version is released,
the stubs are backed-up under `stubs/x.y.z/mobase.pyi`.
If you do not specify a `-o` option, output will go to `stdout`, so you should redirect.
Warning and "critical" messages are printed to `stderr`.
The stubs are generated under `stubs/setup/mobase-stubs/__init__.pyi`, you
can change the output file by using the `-o` option
The latest stubs are kept under `stubs/setup/mobase-stubs/__init__.pyi`,
and when a new version is released, the stubs are backed-up under
`stubs/x.y.z/mobase.pyi`.
A few options are available for `main.py`:
@@ -72,16 +72,18 @@ positional arguments:
optional arguments:
-h, --help show this help message and exit
-o OUTPUT, --output OUTPUT
output file (output to stdout if not specified)
output file (default stubs/setup/mobase-stubs/__init__.pyi)
-v, --verbose verbose mode (all logs go to stderr)
-c CONFIG, --config CONFIG
configuration file
```
The stubs generator will try hard to find a valid stubs for all classes and methods of `mobase`.
A lot of information is available through the `-v` options. Without it, only conversions or fixes
considered "strange" will be outputed.
For instance, here is the output with the current `config-2.3.yml` file:
The stubs generator will try hard to find a valid stubs for all classes
and methods of `mobase`.
A lot of information is available through the `-v` options. Without it,
only conversions or fixes
considered "strange" will be shown.
For instance, here is the output with the current `config-2.4.yml` file:
```
WARNING: Replacing IOrganizer::FileInfo with FileInfo.
@@ -90,33 +92,18 @@ WARNING: Replacing IPluginInstaller::EInstallResult with InstallResult.
WARNING: Replacing IPluginInstaller::EInstallResult with InstallResult.
```
As you can see, only a few types were manually fixed (specified in `config-2.3.yml`).
As you can see, only a few types were manually fixed (specified in
`config-2.4.yml`).
## Configuration file
The configuration file contains information for the stubs that cannot be deduced by `main` (or are too
complex to deduce), and the documentation for everything.
The configuration file contains information for the stubs that cannot be
deduced by `main` (or are too complex to deduce), and the documentation for everything.
## Uploading the stubs to pypi
The upload of the stubs to https://pypi.org/project/mobase-stubs/ has to be done manually. Here
are the steps:
1. Check the version of the stubs:
- The version is specified in the configuration file. If you need to modify the stubs of the
current version, you need to add a `.postX` after the version since PyPi does not allow
re-upload of the same release.
2. Generate the stubs using the procedure above. You should generate the stubs under `stubs/setup/mobase-stubs/__init__.pyi`.
3. If necessary, update the dependencies in `setup.py` (`PyQt5-stubs` version and python version).
4. Go to `stubs/setup` and run:
```bash
python setup.py sdist bdist_wheel
twine upload dist/*
```
You need to set the environment variables `TWINE_USERNAME` and `TWINE_PASSWORD` to appropriate values
before running `twine upload`.
The upload of the stubs to https://pypi.org/project/mobase-stubs/ should be
done automatically when a new Github release is made.
## Extras — Starts a python interpreter with `mobase`
@@ -138,4 +125,4 @@ Permission is hereby granted, free of charge, to any person obtaining a copy of
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
See [LICENSE](LICENSE).
See [LICENSE](LICENSE).
+600 -158
View File
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
View File
+4 -1
View File
@@ -13,7 +13,7 @@
import os
import sys
sys.path.insert(0, os.path.abspath("../../stubs/2.3rc1/"))
sys.path.insert(0, os.path.abspath("../../stubs/2.4.0/"))
# -- Project information -----------------------------------------------------
@@ -64,7 +64,10 @@ html_sidebars = {
"**": ["globaltoc.html", "relations.html", "sourcelink.html", "searchbox.html"]
}
html_favicon = "mo2.ico"
# Add any paths that contain custom static files (such as style sheets) here,
# relative to this directory. They are copied after the builtin static files,
# so a file named "default.css" will overwrite the builtin "default.css".
html_static_path = ["_static"]
html_extra_path = [".nojekyll"]
Binary file not shown.

After

Width:  |  Height:  |  Size: 4.2 KiB

+82 -25
View File
@@ -15,7 +15,7 @@ This guide assumes that:
`Python extension <https://marketplace.visualstudio.com/items?itemName=ms-python.python>`_.
- You have Python installed: https://www.python.org/downloads/.
- It is recommended but not mandatory to use the Python version that is used by MO2.
- You must use the Python version that used by MO2.
You can check the ``pythonXX.dll`` in the MO2 installation folder to find the Python version used by MO2 (``python38.dll`` means Python 3.8).
- You obviously need a valid MO2 installation: https://github.com/modorganizer2/modorganizer/releases
@@ -28,8 +28,8 @@ Preparation
-----------
**Note:** This part is optional but highly recommended if you want a proper environment to work with.
Everything here is written to be as simple as possible but you can of course adapt it to your preferences: use a python virtual
environment, use workspace settings instead of global ones, etc.
Everything here is written to be as simple as possible but you can of course adapt it to your preferences:
use a python virtual environment, use workspace settings instead of global ones, etc.
1. Get the ``mobase`` stubs
...........................
@@ -39,11 +39,14 @@ as ``flake8`` or ``mypy``.
Instead, we provide `stubs <https://stackoverflow.com/questions/59051631/what-is-the-use-of-stub-files-pyi-in-python>`_
which can be used for auto-completion or type-checking.
The stubs for ``mobase`` are available at https://github.com/ModOrganizer2/pystubs-generation/tree/master/stubs.
You want to download the ``mobase.pyi`` file in the folder corresponding to your MO2 version and put it under ``$MO2DIR/plugins/data``.
You can install the stubs for ``mobase`` using ``pip``:
.. code::
pip install mobase-stubs
This will install the stubs for ``mobase`` but also for PyQt5, which is heavily used by MO2.
**Note:** It is possible to put the stubs in a different location, but we are going to use ``$MO2DIR/plugins/data`` for PyQt5,
so we might as well use it for the stubs.
2. Configure Visual Studio Code for ``mobase``
..............................................
@@ -57,32 +60,86 @@ Open ``settings.json`` (Ctrl+Shift+P, then "Open Settings (JSON)"), and add the
"python.linting.enabled": true,
"python.linting.mypyEnabled": true,
"python.linting.flake8Enabled": true,
"python.autoComplete.extraPaths": [
"$MO2DIR\\plugins\\data",
]
3. Configure ``mypy`` to find the ``mobase`` stubs
..................................................
3. [Optional] Configure ``black`` to auto-format your source files
..................................................................
There are multiply way to configure ``mypy``:
This step is optional for your own plugin but recent MO2 plugins use ``black``
to get consistent formatting.
1. You can create a ``mypy.ini`` file somewhere containing:
You can install ``black`` with ``pip``:
.. code-block:: ini
.. code::
[mypy]
mypy_path = $MO2DIR\plugins\data
pip install black flake8-black
And then add the following to ``settings.json`` (with the correct path):
To configure Visual Studio Code to auto-format your code with ``black`` when saving, open ``settings.json``
(Ctrl+Shift+P, then "Open Settings (JSON)"), and add the following entries:
.. code-block:: json-object
"python.linting.mypyArgs": [
"--config-file=path-to-mypy.ini",
]
"editor.formatOnSave": true,
"editor.formatOnPaste": true,
"python.formatting.provider": "black",
2. You can set the ``MYPYPATH`` environment variable to ``$MO2DIR\plugins\data`` (this requires
restarting VS code).
4. [Optional] Automatically reload plugins during development
.............................................................
This section is optional and requires you to already have written a "working"
plugin (a plugin that MO2 can load).
Since MO2 2.4 alpha 6, a new command has been added to ``ModOrganizer.exe`` to
reload plugins during execution.
If your plugin is named "My Plugin", you can use the following command to reload
it while MO2 is running:
.. code::
$MO2DIR\ModOrganizer.exe reload-plugin "My Plugin"
If you are using Visual Studio Code, you can send this command to MO2 automatically
after saving files from your project.
1. Create a "reload plugin" task in Visual Studio Code (Ctrl+Shift+P then
``Tasks: Configure task`` or open ``.vscode/tasks.json``) using the following
snippet (replace the name and directory as needed):
.. code:: javascript
// .vscode/tasks.json
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "reload plugin",
"type": "shell",
"command": "$MO2DIR/ModOrganizer.exe",
"args": [
"reload-plugin", "My Plugin"
]
}
]
}
2. Install the `Trigger Task on Save <https://marketplace.visualstudio.com/items?itemName=Gruntfuggly.triggertaskonsave>`_
extension from Visual Studio Code marketplace.
3. Add the following to your Visual Studio Code settings (``.vscode/settings.json``)
.. code:: javascript
// .vscode/settings.json
{
"triggerTaskOnSave.on": true,
"triggerTaskOnSave.tasks": {
"reload plugin": [
"*.py"
]
}
}
Testing the setup
-----------------
@@ -128,8 +185,8 @@ If your setup is valid, here is what you should have.
.. image:: images/check-setup-3.png
If everything is as above, you can delete the test file and move on to writting
If everything is as above, you can delete the test file and move on to writing
your own plugin!
.. |error-window| image:: images/error-window.png
.. |error-window| image:: images/error-window.png
+123 -2
View File
@@ -185,7 +185,7 @@ Most of these are pretty simple to understand:
enable the plugin. You can also use a plugin setting to allow users to disable
your plugins.
- ``settings``: Returns the list of settings (that user can modify) for this plugin.
Settings can be ``int```, ``bool``, ``str`` or list of ``str``. Here we indicate
Settings can be ``int``, ``bool``, ``str`` or list of ``str``. Here we indicate
that we have a "enabled" setting that user could use to disable the plugin (and
we use it in ``isActive``).
@@ -198,4 +198,125 @@ a "bug" in ``boost.python``).
The ``init`` method is called by MO2 to initialize the plugin. The given argument, ``organizer``,
is an instance of :class:`IOrganizer<mobase.IOrganizer>` which is the class used to interface with MO2.
Here, we use it in the ``isActive()`` method to retrieve the "enabled" setting for our plugin.
See :class:`IOrganizer<mobase.IOrganizer>` for more details.
See :class:`IOrganizer<mobase.IOrganizer>` for more details.
Examples
--------
This section contains (links to) examples of MO2 Python plugins.
Some of these plugins have been created for educational purpose and are thus very detailed and
easy to understand or get started from.
Tutorial Plugins
................
`This repository <https://github.com/Holt59/modorganizer-python_plugins>`_ contains examples of Python
plugins that were written only to help users write their own plugins.
If you want to start somewhere, this is the place to go.
Official Plugins
................
These plugins are (or will be) included in MO2 releases and are usually maintain by some members of
the MO2 development teams.
These plugins are not as well documented as the ones in the repository above.
- `Basic Games <https://github.com/ModOrganizer2/modorganizer-basic_games>`_ [``IPluginGame``]
This is the meta-plugin for "basic" games. It is a complex
plugins and should mostly be investigated if you want to add a game to it.
- `FNIS Tool <https://github.com/ModOrganizer2/modorganizer-fnistool>`_ [``IPluginTool``]:
Plugin to integrate FNIS into MO2.
- `Installer Wizard <https://github.com/ModOrganizer2/modorganizer-installer_wizard>`_ [``IPluginInstaller``]:
Installer for BAIN archives containing wizard scripts.
- `Preview DDS <https://github.com/ModOrganizer2/modorganizer-preview_dds>`_ [``IPluginPreview``]:
Plugin to preview DDS files. Quite complex due to the use
of OpenGL for display.
- `Form 43 Checker <https://github.com/ModOrganizer2/modorganizer-form43_checker>`_ [``IPluginDiagnose``]:
Plugin that warn users if there are form 43 ESPs (Skyrim ESPs)
enabled when managing a Skyrim SE instance.
- `Tool Configurator <https://github.com/ModOrganizer2/modorganizer-tool_configurator>`_ [``IPluginTool``]:
Plugin that allows easier modifications of game settings.
Mostly contains a complex GUI for managing INI files.
- `Script Extender Plugin Checker <https://github.com/ModOrganizer2/modorganizer-script_extender_plugin_checker>`_ [``IPluginDiagnose``]:
Plugin that checks Script Extender logs to see
if some plugins have failed to load and display information to the user if possible.
Unofficial Plugins
..................
These plugins have been created by developers for MO2 and are usually distributed on Nexus.
- `Merge Plugins Hide <https://github.com/deorder/mo2-plugins>`_ [``IPluginTool``]:
Hide / unhide plugins that were merged using ``Merge Plugins`` or ``zMerge``.
- `OpenMW Exporter <https://github.com/AnyOldName3/ModOrganizer-to-OpenMW>`_ [``IPluginTool``]:
A Mod Organizer plugin to export your VFS, plugin selection and load order to OpenMW.
- `Orphaned Script Extender Save Deleter <https://github.com/AnyOldName3/modorganizer-orphaned_script_extender_save_deleter>`_ [``IPluginTool``]:
Mod Organizer plugin to delete orphaned script extender co-saves.
- `Sync Mod Order <https://github.com/deorder/mo2-plugins>`_ [``IPluginTool``]:
Synchronize mod order from current profile to another while keeping the (enabled/disabled) state intact.
*Feel free to open an issue or a pull-request if you want to add your own plugin to the list.*
Internationalization
--------------------
If you plan to distribute your plugin, it is often a good idea to provide translations for it.
Adding translation code
.......................
Mod Organizer uses Qt translation system, so you need to adapt your plugin code to provide
translation strings.
To do this, you need two things:
1. In every class containing strings you need to translate, you must add a ``__tr`` function
that takes a ``str`` input and call `QApplication.translate` on it (see example below).
2. You need to wrap all translatable strings in a call to ``self.__str("My String")`` (see
example below).
.. code:: python
from PyQt5.QtWidgets import QApplication
class MyPlugin(...):
def localizedName(self) -> str:
# Use self.__tr to wrap string you want translatable.
return self.__tr("My Plugin Name")
def __tr(self, txt: str) -> str:
# The first argument must EXACTLY match the class name:
return QApplication.translate("MyPlugin", txt)
Generating Qt translation files
...............................
Once your code is updated, you need to generate the Qt translation file ``.ts``.
You can use ``PyQt5.lupdate_main`` for this:
.. code:: bash
PyQt5.lupdate_main mysourcefile.py -ts mysourcefile.ts
You should generate a single translation file for your whole plugin even if it contains
multiple files by passing all Python file and Qt UI (``.ui``) file to the command above.
Translating
...........
Now that you have the original ``.ts`` file, you need to translate it in order to obtain
translation files for other languages.
To do so, you can use online services such as `Transifex <https://www.transifex.com>`_ or
simply Qt Linguistic tools.
Distributing translations
.........................
Once you have obtained translation files for another language, e.g. French, you need to
compile it into a ``.qm`` file and then ship it.
- If you are using a single Python file plugin ``myplugin.py``, the name of the compiled
translation must be ``myplugin_fr.qm``.
- If you are shipping a module ``mymoduleplugin``, the name of the compiled translation
must be ``mymoduleplugin``.
-1
View File
@@ -3,7 +3,6 @@
import logging
import sys
logging.basicConfig(stream=sys.stderr, format="%(levelname)s: %(message)s")
logger = logging.getLogger(__name__)
logger.setLevel(logging.WARNING)
+32 -13
View File
@@ -1,22 +1,43 @@
# -*- encoding: utf-8 -*-
import importlib.machinery
import importlib.util
import os
import sys
from pathlib import Path
def load_module(name: str, path: Path):
# Create the loader:
loader = importlib.machinery.ExtensionFileLoader( # type: ignore
name, path.as_posix()
)
# Extract the spec:
spec = importlib.util.spec_from_loader(name, loader)
# Create the module and execute it?
module = importlib.util.module_from_spec(spec)
if module is None:
raise ImportError(f"Failed to import module {name} from {path}.")
loader.exec_module(module)
return module
def load_mobase(path: Path, moprivate: bool = False):
""" Load the mobase from the given MO2 installation path and
"""
Load the mobase from the given MO2 installation path and
returns it.
Args:
path: Path to the MO2 installation (folder containg the ModOrganizer.exe).
path: Path to the MO2 installation (folder containing the ModOrganizer.exe).
moprivate: If True, the moprivate module will also be loaded and returned
alongisde mobase.
alongside mobase.
Returns: The mobase module. """
Returns: The mobase module.
"""
# We need absolute path for loading DLL and modules:
path = path.resolve()
@@ -29,22 +50,20 @@ def load_mobase(path: Path, moprivate: bool = False):
[str(path), str(path.joinpath("dlls")), os.environ.get("PATH", "")]
)
else:
os.add_dll_directory(str(path))
os.add_dll_directory(str(path.joinpath("dlls")))
os.add_dll_directory(str(path)) # type: ignore[attr-defined]
os.add_dll_directory(str(path.joinpath("dlls"))) # type: ignore[attr-defined]
# We need to add plugins/data to sys.path, mainly for PyQt5
sys.path.insert(1, path.joinpath("plugins", "data").as_posix())
mobase = importlib.machinery.ExtensionFileLoader(
"mobase", path.joinpath("plugins", "data", "pythonrunner.dll").as_posix()
).load_module()
mobase = load_module("mobase", path.joinpath("plugins", "data", "pythonrunner.dll"))
if not moprivate:
return mobase
moprivate = importlib.machinery.ExtensionFileLoader(
"moprivate", path.joinpath("plugins", "data", "pythonrunner.dll").as_posix()
).load_module()
moprivate = load_module(
"moprivate", path.joinpath("plugins", "data", "pythonrunner.dll")
)
return mobase, moprivate
+132 -46
View File
@@ -1,13 +1,14 @@
# -*- encoding: utf-8 -*-
from typing import Optional, List, Any, Dict, Union
from typing import Any, Dict, List, Optional, Union
from . import logger
from . import utils
from . import logger, utils
class Type:
""" Class representing a python type. """
"""
Class representing a python type.
"""
# The `MoVariant` actual type - This should be List["MoVariant"] and
# Dict[str, "MoVariant"], but mypy (and other type checkers) do not
@@ -18,7 +19,7 @@ class Type:
def __init__(self, name: Union[str, type]):
# Import only here since we change the path to find them:
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt6 import QtCore, QtGui, QtWidgets
if isinstance(name, type):
name = name.__name__
@@ -35,7 +36,10 @@ class Type:
self.name = "{}.{}".format(m.__name__, self.name)
def typing(self, settings: utils.Settings) -> str:
""" Returns a valid typing representation for this type. """
"""
Returns:
A valid typing representation for this type.
"""
from .register import MOBASE_REGISTER
# Check if this is a mobase object, in which case we escape:
@@ -51,15 +55,30 @@ class Type:
return self.name
def is_none(self) -> bool:
""" Check if this type represent None. """
"""
Check if this type represent None.
Returns:
True if this type represents None.
"""
return self.name.lower() in ("none", "nonetype")
def is_object(self) -> bool:
""" Check if this type represent the generic "object" type. """
"""
Check if this type represent the generic "object" type.
Returns:
True if this type represent the generic object type.
"""
return self.name.lower() == "object"
def is_any(self) -> bool:
""" Check if this type repesent the typing "Any". """
"""
Check if this type represent the typing "Any".
Returns:
True if this type represent the typing "Any".
"""
return self.name == "Any"
def __str__(self):
@@ -78,7 +97,9 @@ class Type:
class CType(Type):
""" Class representing a C++ type from boost::python. """
"""
Class representing a C++ type from boost::python.
"""
# List of smart pointer types - Not including pointer that should not
# be exposed (unique_ptr, weak_ptr):
@@ -119,7 +140,7 @@ class CType(Type):
def _try_fix(self, name, settings: utils.Settings):
from .parser import parse_ctype, magic_split, parse_csig
from .parser import magic_split, parse_csig, parse_ctype
from .register import MOBASE_REGISTER
pname = name
@@ -221,7 +242,12 @@ class CType(Type):
args = [a for a in args if a != "boost::tuples::null_type"]
name = "Tuple[{}]".format(", ".join(args))
# Fix for function...
# Fix for optional:
if name.startswith("std::optional"):
name = name[13:].strip()[1:-1].strip()
name = "Optional[{}]".format(parse_ctype(name).typing(settings))
# Fix for function:
if name.startswith("std::function"):
name = name[13:].strip()[1:-1].strip()
rtype, vargs = parse_csig(name, "")
@@ -254,14 +280,30 @@ class CType(Type):
return name
def _is_builtin_python_type(self, t: Type):
"""Check if the given type is a 'raw' python type, i.e., a type that cannot
def _is_builtin_python_type(self, t: Type) -> bool:
"""
Check if the given type is a 'raw' python type, i.e., a type that cannot
be a C++ reference. This is mainly used to report errors when pointers to such
type are present in the interface."""
type are present in the interface.
Args:
t: The type to check.
Returns:
True if the given type is a raw python type, False otherwise.
"""
return t.name in ["bool", "int", "float", "str", "list", "std", "dict", "bytes"]
def typing(self, settings: utils.Settings) -> str:
""" Returns a valid typing representation for this type. """
"""
Create a valid typing representation for this type.
args:
settings: The settings to use.
Returns:
A valid typing representing for this type.
"""
from .register import MOBASE_REGISTER
name = self.name
@@ -292,11 +334,21 @@ class CType(Type):
return name
def is_raw_pointer(self) -> bool:
""" Check if this type is a raw pointer type. """
"""
Check if this type is a raw pointer type.
Returns:
True if this type is a raw point type, False otherwise.
"""
return self.name.endswith("*")
def is_smart_pointer(self) -> bool:
""" Check if this type is a smart pointer type. """
"""
Check if this type is a smart pointer type.
Returns:
True if this type is a smart pointer type, False otherwise.
"""
for ptr in self.SMART_POINTERS:
if self.name.startswith(ptr):
@@ -305,19 +357,28 @@ class CType(Type):
return False
def is_pointer(self) -> bool:
""" Check if this type corresponds to a pointer type. """
"""
Check if this type corresponds to a pointer type.
Returns:
True if this type represents a pointer type (raw or smart), False
otherwise.
"""
return self.is_raw_pointer() or self.is_smart_pointer()
def is_optional(self) -> bool:
""" Check if this type is optional (i.e., can be None in python). """
"""
Check if this type is optional (i.e., can be None in python).
Returns:
True if this type can be optional, False otherwise.
"""
return self._optional
def is_none(self) -> bool:
""" Check if this type represent None. """
return self.name.lower() == "void"
def is_object(self) -> bool:
""" Check if this type represent the generic "object" type """
return self.name.lower() == "_object *"
def __str__(self) -> str:
@@ -328,7 +389,9 @@ class CType(Type):
class Ret:
""" Class representing the return value of a function (type and documentation). """
"""
Class representing the return value of a function (type and documentation).
"""
type: Type
doc: str
@@ -339,7 +402,9 @@ class Ret:
class Arg:
""" Class representing a function argument (type and eventual default value). """
"""
Class representing a function argument (type and eventual default value).
"""
# Constant representing None since None indicates no default value:
DEFAULT_NONE = "None"
@@ -369,17 +434,6 @@ class Arg:
if value is not None and value.startswith("mobase."):
value = value[7:]
# I have issues with inner classes, so we need to prepend manually...
# if value.find(".") != -1:
# print(value)
# parts = value.split(".")
# tvalue = ".".join(parts[:-1])
# print(" " + tvalue)
# for k in MOBASE_REGISTER.objects:
# if k != tvalue and k.endswith(tvalue) and k[-len(tvalue) - 1] == ".":
# value = "{}.{}".format(k, parts[-1])
# print(" " + value)
return value
def has_default_value(self) -> bool:
@@ -404,7 +458,9 @@ class Arg:
class Exc:
""" Small class representing exception that can be raised from functions. """
"""
Small class representing exception that can be raised from functions.
"""
type: Type
doc: str
@@ -415,7 +471,9 @@ class Exc:
class Function:
""" Class representing a function. """
"""
Class representing a function.
"""
name: str
ret: Ret
@@ -449,7 +507,9 @@ class Function:
class Method(Function):
""" Class representing a method. """
"""
Class representing a method.
"""
cls: "Class"
abstract: Union[str, bool]
@@ -486,7 +546,9 @@ class Method(Function):
class Constant:
""" Class representing a constant. """
"""
Class representing a constant.
"""
name: str
type: Optional[Type]
@@ -505,7 +567,9 @@ class Constant:
class Property:
""" Class representing a property. """
"""
Class representing a property.
"""
name: str
type: Type
@@ -523,7 +587,9 @@ class Property:
class Class:
""" Class representing a class. """
"""
Class representing a class.
"""
name: str
bases: List["Class"]
@@ -573,7 +639,10 @@ class Class:
@property
def canonical_name(self):
""" Return the canonical name of this class. """
"""
Returns:
The canonical name of this class.
"""
name = self.name
oc = self.outer_class
while oc is not None:
@@ -582,6 +651,17 @@ class Class:
return name
@property
def all_bases(self):
"""
Returns:
All the bases of this class, including bases of bases and so on.
"""
bases = set(self.bases)
for b in self.bases:
bases = bases.union(b.all_bases)
return bases
def is_deprecated(self):
return self.deprecated
@@ -591,18 +671,24 @@ class Class:
class PyClass(Class):
"""Class use to wrap Python class to be used as parent class for some classes
in mobase."""
"""
Class use to wrap Python class to be used as parent class for some classes
in mobase.
"""
def __init__(
self, name: str,
self,
name: str,
):
super().__init__(name, [], [])
self.abstract = False
class Enum(Class):
""" Class representing an enum. """
"""
Class representing an enum.
"""
def __init__(self, name: str, values: Dict[str, int]):
# Note: Boost.Python.enum inherits int() not enum.Enum() but for the sake
+14 -23
View File
@@ -2,26 +2,13 @@
import inspect
import re
from collections import OrderedDict, defaultdict
from typing import Dict, List, Optional, Tuple, Union
from collections import defaultdict, OrderedDict
from typing import List, Tuple, Optional, Dict, Union
from .register import MobaseRegister
from .mtypes import (
Type,
CType,
Class,
PyClass,
Enum,
Arg,
Ret,
Method,
Constant,
Property,
Function,
)
from . import logger
from .mtypes import (Arg, Class, Constant, CType, Enum, Function, Method,
Property, PyClass, Ret, Type)
from .register import MobaseRegister
def magic_split(value: str, sep=",", open="(<", close=")>"):
@@ -167,7 +154,7 @@ def parse_csig(s, name) -> Tuple[CType, List[Arg]]:
args_ss = magic_split(args_s, ",", "(<", ")>")
args = [parse_carg(v, i > len(args_ss) - c - 1) for i, v in enumerate(args_ss)]
return rtype, args
return rtype, [arg for arg in args if not arg.type.is_none()]
def parse_psig(s: str, name: str) -> Tuple[Type, List[Arg]]:
@@ -313,13 +300,14 @@ def make_enum(fullname: str, e: type) -> Enum:
values = e.values # type: ignore
return Enum(
e.__name__, OrderedDict((values[k].name, k) for k in sorted(values.keys())),
e.__name__,
OrderedDict((values[k].name, k) for k in sorted(values.keys())),
)
class Overload:
""" Small class to avoid mypy issues... """
"""Small class to avoid mypy issues..."""
rtype: Type
args: List[Arg]
@@ -387,7 +375,10 @@ def make_functions(name: str, e) -> List[Function]:
return [
Function(
e.__name__, Ret(ovld.rtype), ovld.args, has_overloads=len(overloads) > 1,
e.__name__,
Ret(ovld.rtype),
ovld.args,
has_overloads=len(overloads) > 1,
)
for ovld in overloads
]
@@ -475,7 +466,7 @@ def make_class(fullname: str, e: type, register: MobaseRegister) -> Class:
# Find the methods:
methods = [m[1] for m in all_attrs if callable(m[1])]
methods = sorted(methods, key=lambda m: m.__name__)
methods = sorted(methods, key=lambda m: str(m.__name__))
# Filter out methods not provided or implemented:
methods = [
+10 -7
View File
@@ -1,19 +1,20 @@
# -*- encoding: utf-8 -*-
from collections import OrderedDict
from typing import Optional, Dict, Union, List
from typing import Dict, List, Optional, Union
from . import logger
from .mtypes import Class, Type, CType, Function
from .mtypes import Class, CType, Function, Type
class MobaseRegister:
""" Class that register class. """
"""
Class that register classes.
"""
objects: Dict[str, Union[Class, List[Function]]]
def __init__(self):
""" Create a new register with the list of objects. """
self.raw_objects: Dict[str, Union[type]] = OrderedDict()
self.objects = {}
@@ -26,15 +27,17 @@ class MobaseRegister:
def make_object(
self, name: str, e: Optional[type] = None
) -> Union["Class", List["Function"]]:
"""Construct a Function, Class or Enum for the given object.
"""
Construct a Function, Class or Enum for the given object.
Args:
name: The name of the object to inspect.
e: The object to inspect, or None to fetch it from the underlying list.
Returns: A Class object for the given type, or a list of function overloads.
Returns:
A Class object for the given type, or a list of function overloads.
"""
from .parser import make_enum, make_class, is_enum, make_functions
from .parser import is_enum, make_class, make_enum, make_functions
if e is None:
e = self.raw_objects[name]
+56 -22
View File
@@ -1,14 +1,16 @@
# -*- encoding: utf-8 -*-
from collections import OrderedDict, defaultdict
from typing import List, Dict, Any, Tuple, TextIO, Optional, Union, NamedTuple
from . import logger
from . import register
from . import mtypes
from typing import (TYPE_CHECKING, Any, Dict, List, NamedTuple, Optional, Set,
TextIO, Tuple, Union)
import yaml
from . import logger, mtypes
if TYPE_CHECKING:
from .register import MobaseRegister
class Settings:
class FunctionSettings(NamedTuple):
@@ -21,7 +23,7 @@ class Settings:
abstract: Optional[bool] = None
deprecated: bool = False
register: "register.MobaseRegister"
register: "MobaseRegister"
# Name to ignore:
_ignore_names: List[str]
@@ -32,9 +34,7 @@ class Settings:
# Content of mobase:
_mobase: Dict[str, Dict[str, Any]]
def __init__(
self, register: "register.MobaseRegister", fp: Optional[TextIO] = None
):
def __init__(self, register: "MobaseRegister", fp: Optional[TextIO] = None):
self.register = register
@@ -63,7 +63,8 @@ class Settings:
return self._mobase
def _get_class_settings(self, canonical_name: str) -> Optional[Dict[str, Any]]:
"""Retrieve the settings for the given class.
"""
Retrieve the settings for the given class.
Args:
canonical_name: Canonical name of the class.
@@ -84,7 +85,8 @@ class Settings:
def _parse_function_settings(
self, settings: Union[str, Dict[str, Any]]
) -> "FunctionSettings":
"""Parse settings for a function or method.
"""
Parse settings for a function or method.
Args:
settings: Settings corresponding to a function.
@@ -168,8 +170,7 @@ class Settings:
if fsettings.args is not None:
if len(fsettings.args) != len(fn.args):
logger.warn(
"Mismatch number of arguments for function mobase.{}."
.format(sname) # noqa
f"Mismatch number of arguments for function mobase.{sname}."
)
for sarg, marg in zip(fsettings.args, fn.args):
@@ -197,7 +198,8 @@ class Settings:
logger.warn("Missing settings for function mobase.{}.".format(sname))
def patch_class(self, cls: "mtypes.Class"):
"""Patch the given class using the given overwrites.
"""
Patch the given class using the given overwrites.
See config.json for some examples of valid overwrites.
@@ -289,6 +291,8 @@ class Settings:
else:
sname = m.name
missing_settings: Set[str] = set()
# If the name is in the settings:
if sname in csettings:
keys[sname] = True
@@ -324,6 +328,20 @@ class Settings:
for sarg, marg in zip(fsettings.args, margs):
marg.doc = sarg.doc
if (
not marg.name.startswith("arg")
and marg.name != sarg.name
):
logger.warn(
"Mismatch argument name for method {}.{}: {} {}, using {}.".format( # noqa: E501
cls.canonical_name,
sname,
marg.name,
sarg.name,
sarg.name,
)
)
marg.name = sarg.name
if not sarg.type.is_none():
marg.type = sarg.type
@@ -346,16 +364,22 @@ class Settings:
# Only warn for "normal" methods:
elif not m.name.startswith("__"):
logger.warn(
"Missing settings for method {}.{}.".format(
cls.canonical_name, sname
)
)
missing_settings.add(sname)
# Remove the deprecated methods:
noverloads = 0
for m in ms:
if m.is_deprecated():
overriden: bool = False
for bc in cls.all_bases:
for bm in bc.methods:
if bm.name == "__init__" or bm.name != m.name:
continue
if len(bm.args) == len(m.args) and all(
ba.type == ma.type
for ba, ma in zip(bm.args[1:], m.args[1:])
):
overriden = True
if m.is_deprecated() or overriden:
cls.methods.remove(m)
else:
noverloads += 1
@@ -363,6 +387,14 @@ class Settings:
for m in ms:
m.overloads = noverloads > 1
if noverloads > 0 and missing_settings:
for sname in missing_settings:
logger.warn(
"Missing settings for method {}.{}.".format(
cls.canonical_name, sname
)
)
# Patch inner classes:
for ic in cls.inner_classes:
keys[ic.name] = True
@@ -382,12 +414,14 @@ class Settings:
def clean_class(cls: "mtypes.Class", settings: Settings):
"""Clean the given class object.
"""
Clean the given class object.
Args:
cls: The class object to clean.
settings: The settings.
"""
from .register import MOBASE_REGISTER
# Remove duplicate methods (based on name and argument types):
@@ -396,7 +430,7 @@ def clean_class(cls: "mtypes.Class", settings: Settings):
] = OrderedDict()
methods_by_name = defaultdict(list)
for m in cls.methods:
k = (m.name, tuple(m.args[1:]))
k = (m.name, tuple(m.args if m.is_static() else m.args[1:]))
if k not in methods:
methods[k] = []
methods[k].append(m)
+21 -8
View File
@@ -1,9 +1,9 @@
# -*- encoding: utf-8 -*-
from typing import TextIO, List, Union, Tuple
from typing import List, TextIO, Tuple, Union
from . import logger
from .mtypes import Function, Class, Method, Property, Enum
from .mtypes import Class, Enum, Function, Method, Property
from .utils import Settings
@@ -21,7 +21,8 @@ class Writer:
print(*args, **kwargs)
def _print_doc(self, doc: str, indent: str):
"""Print the given documentation at the given indentaiton level.
"""
Print the given documentation at the given indentation level.
Args:
doc: Documentation to print.
@@ -36,7 +37,9 @@ class Writer:
self._print()
def print_imports(self, imports: List[Union[str, Tuple[str, List[str]]]]):
""" Print the given imports. """
"""
Print the given imports.
"""
for imp in imports:
if isinstance(imp, str):
self._print("import {}".format(imp))
@@ -45,7 +48,9 @@ class Writer:
self._print()
def print_function(self, fn: Function, indent: str = ""):
""" Print the given Function object at the given indentation level. """
"""
Print the given Function object at the given indentation level.
"""
if fn.has_overloads():
self._print("{}@overload".format(indent))
@@ -117,7 +122,9 @@ class Writer:
self._print()
def print_property(self, cls: Class, prop: Property, indent: str):
""" Print the given Property object at the given indentation level. """
"""
Print the given Property object at the given indentation level.
"""
if prop.type.is_object() or prop.type.is_any():
logger.warning(
@@ -142,7 +149,9 @@ class Writer:
self._print()
def print_class(self, cls: Class, indent: str = ""):
""" Print the given Class object at the given indentation level. """
"""
Print the given Class object at the given indentation level.
"""
bc = ""
if cls.bases or cls.is_abstract():
@@ -187,7 +196,11 @@ class Writer:
# Note: We do not print the value, we use ...
self._print(
"{}{}{} = {}{}".format(
indent + " ", constant.name, typing, "...", comment,
indent + " ",
constant.name,
typing,
"...",
comment,
)
)
+9 -7
View File
@@ -2,21 +2,18 @@
import argparse
import logging
import sys
from pathlib import Path
import black
from generator import logger
from generator.loader import load_mobase
from generator.register import MOBASE_REGISTER
from generator.mtypes import Class, Function, Type
from generator.parser import is_enum
from generator.mtypes import Type, Class, Function
from generator.register import MOBASE_REGISTER
from generator.utils import Settings, clean_class
from generator.writer import Writer
parser = argparse.ArgumentParser("Stubs generator for the MO2 python interface")
parser.add_argument(
"install_dir",
@@ -29,8 +26,8 @@ parser.add_argument(
"-o",
"--output",
type=Path,
default=sys.stdout,
help="output file (output to stdout if not specified)",
default="stubs/setup/mobase-stubs/__init__.pyi",
help="output file (default stubs/setup/mobase-stubs/__init__.pyi)",
)
parser.add_argument(
"-v", "--verbose", action="store_true", help="verbose mode (all logs go to stderr)"
@@ -72,6 +69,11 @@ for name in dir(mobase):
if name == "MoVariant":
continue
# For now, ignore this since it is a submodule and we
# not handle them.
if name == "widgets":
continue
objects.append((name, getattr(mobase, name)))
# Enum first, and then alphabetical. Might cause issue with base classes, so

Some files were not shown because too many files have changed in this diff Show More