Files

126 lines
4.4 KiB
Markdown
Raw Permalink Normal View History

2020-05-10 19:32:47 +02:00
# Mod Organizer 2 - Python stubs generation
2020-11-14 13:42:18 +01:00
This project can be used to generate python stubs (`.pyi` file) for the MO2 python
2020-05-10 19:32:47 +02:00
interface `mobase`.
## Using the stubs
2020-08-07 10:47:41 +02:00
You can install the `stubs` with `pip`:
```bash
pip install mobase-stubs
```
2020-05-10 19:32:47 +02:00
2020-08-07 11:07:58 +02:00
This will install the latest stubs on PyPi which usually corresponds to the latest release of
MO2.
You can install stubs for a specific version of MO2:
```bash
2023-09-19 21:30:03 +02:00
pip install mobase-stubs==2.5.*
```
2020-05-10 19:32:47 +02:00
Some words of warning:
2022-05-05 13:51:39 +02:00
2020-05-10 19:32:47 +02:00
- The stubs are as correct as possible, but some errors are expected.
2020-11-14 13:42:18 +01:00
- 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.
2020-05-10 19:32:47 +02:00
## Generating the stubs
2020-05-10 20:22:17 +02:00
The stubs are generated using python by parsing the `mobase` module.
2020-11-14 13:42:18 +01:00
You need the version of python that matches your current MO2 installation: e.g., if you
2022-05-05 13:51:39 +02:00
have a `python310.dll` in your MO2 installation path, then you need **Python 3.10**.
2020-05-10 20:22:17 +02:00
To generate the stubs, you can run:
2020-05-10 19:32:47 +02:00
2022-05-05 13:51:39 +02:00
```bash
2023-09-19 21:30:03 +02:00
# install the package
poetry install
2022-05-05 13:51:39 +02:00
# change the output folder to whatever you want
mo2-stubs-generator -c configs/config-2.4.yml -o mobase-stubs ${MO2_INSTALL_PATH}
2020-05-10 19:32:47 +02:00
```
2022-05-05 13:51:39 +02:00
Where `${MO2_INSTALL_PATH}` is the path to your MO2 installation (the one
containing `ModOrganizer.exe`).
2020-05-10 19:32:47 +02:00
2022-05-05 13:51:39 +02:00
The stubs are generated under `stubs/setup/mobase-stubs` by default, you
can change the output file by using the `-o` option.
The stubs under `stubs/setup/mobase-stubs` should not be committed as these are
generated from the version stubs under `stubs/${VERSION}/mobase-stubs`.
2020-05-10 19:32:47 +02:00
2022-05-05 13:51:39 +02:00
A few options are available for `mo2-stubs-generator`:
2020-05-10 19:32:47 +02:00
2022-05-05 13:51:39 +02:00
```bash
$ mo2-stubs-generator --help
usage: stubs generator for the MO2 python interface [-h] [-o OUTPUT] [-v] [-c CONFIG] INSTALL_DIR
2020-05-10 19:32:47 +02:00
positional arguments:
INSTALL_DIR installation directory of Mod Organizer 2
2020-05-10 19:32:47 +02:00
2022-05-05 13:51:39 +02:00
options:
2020-05-10 19:32:47 +02:00
-h, --help show this help message and exit
-o OUTPUT, --output OUTPUT
2022-05-05 13:51:39 +02:00
output folder (default stubs/setup/mobase-stubs)
2020-05-10 19:32:47 +02:00
-v, --verbose verbose mode (all logs go to stderr)
-c CONFIG, --config CONFIG
configuration file
```
2020-11-14 13:42:18 +01:00
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,
2022-05-05 13:51:39 +02:00
only conversions or fixes considered "strange" will be shown.
2020-05-10 19:32:47 +02:00
## Configuration file
2020-11-14 13:42:18 +01:00
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.
2020-05-10 19:32:47 +02:00
2020-08-07 10:47:41 +02:00
## Uploading the stubs to pypi
2022-05-05 13:51:39 +02:00
The upload of the stubs to [https://pypi.org/project/mobase-stubs/](https://pypi.org/project/mobase-stubs/)
should be done automatically when a new Github tag is pushed.
2020-05-10 19:32:47 +02:00
2022-05-05 13:51:39 +02:00
## Extras — Using `mobase` in a Python interpreter
2020-05-15 18:36:48 +02:00
2022-05-05 17:52:32 +02:00
It is possible to start a (i)python interpreter with `mobase` imported by running
2022-05-05 13:51:39 +02:00
```bash
python -i -m mo2.stubs.generator.loader ${MO2_INSTALL_PATH}
2020-05-15 18:36:48 +02:00
```
2022-05-05 19:13:12 +02:00
You can also import `mobase` in your code using the following (after installing
this package):
```python
from mo2.stubs.generator import load_mobase
mobase = load_mobase(MO2_INSTALL_PATH)
# the above will probably not give you type-completion in your IDE or typing, so
# you can use the following (if the stubs are installed)
load_mobase(MO2_INSTALL_PATH)
import mobase
import mobase.widgets
```
2022-05-05 13:51:39 +02:00
**Note:** Most classes in `mobase` cannot be instantiated, so this is mostly intended
for MO2 developers.
2020-05-15 18:36:48 +02:00
2022-05-05 13:51:39 +02:00
## License
2020-05-15 18:36:27 +02:00
The MIT License (MIT)
2020-08-07 10:47:41 +02:00
Copyright (c) 2020, Holt59.
2020-05-15 18:36:27 +02:00
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
2020-11-14 13:42:18 +01:00
See [LICENSE](LICENSE).