Skip to content

Commit 52e3c07

Browse files
authored
Merge pull request #207 from blurstudio/site_config
Add site customization of _common_members
2 parents ff11ce4 + 67710d4 commit 52e3c07

File tree

7 files changed

+214
-6
lines changed

7 files changed

+214
-6
lines changed

Qt.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
import shutil
4444
import importlib
4545

46-
__version__ = "1.0.0.b5"
46+
__version__ = "1.0.0.b6"
4747

4848
# Enable support for `from Qt import *`
4949
__all__ = []
@@ -431,6 +431,7 @@
431431
"QMetaMethod",
432432
"QMetaObject",
433433
"QMetaProperty",
434+
"QMetaType",
434435
"QMimeData",
435436
"QModelIndex",
436437
"QMutex",
@@ -598,6 +599,18 @@
598599
}
599600

600601

602+
def _apply_site_config():
603+
try:
604+
import QtSiteConfig
605+
except ImportError:
606+
# If no QtSiteConfig module found, no modifications
607+
# to _common_members are needed.
608+
pass
609+
else:
610+
# Update _common_members with any changes made by QtSiteConfig
611+
QtSiteConfig.update_members(_common_members)
612+
613+
601614
def _new_module(name):
602615
return types.ModuleType(__name__ + "." + name)
603616

@@ -1041,6 +1054,9 @@ def _install():
10411054

10421055
_log("Order: '%s'" % "', '".join(order))
10431056

1057+
# Allow site-level customization of the available modules.
1058+
_apply_site_config()
1059+
10441060
found_binding = False
10451061
for name in order:
10461062
_log("Trying %s" % name)

README.md

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,14 @@ Qt.py enables you to write software that runs on any of the 4 supported bindings
3535
- [Install](#install)
3636
- [Usage](#usage)
3737
- [Documentation](#documentation)
38+
- [Environment Variables](#environment-variables)
39+
- [Subset](#subset)
40+
- [Branch binding-specific code](#branch-binding-specific-code)
41+
- [Override preferred choice](#override-preferred-choice)
42+
- [QtSiteConfig.py](#qtsiteconfigpy)
43+
- [Compile Qt Designer files](#compile-qt-designer-files)
44+
- [Loading Qt Designer files](#loading-qt-designer-files)
45+
- [sip API v2](#sip-api-v2)
3846
- [Rules](#rules)
3947
- [How it works](#how-it-works)
4048
- [Known problems](#known-problems)
@@ -193,6 +201,45 @@ Using the OS path separator (`os.pathsep`) which is `:` on Unix systems and `;`
193201

194202
<br>
195203

204+
##### QtSiteConfig.py
205+
206+
Add or remove members from Qt.py at run-time.
207+
208+
- [Examples](/examples/QtSiteConfig)
209+
210+
<br>
211+
212+
If you need to expose a module that isn't included in Qt.py by default or wish to remove something from being exposed in Qt.py you can do so by creating a `QtSiteConfig.py` module and making it available to Python.
213+
214+
1. Create a new file `QtSiteConfig.py`
215+
2. Implement `update_members`
216+
3. Expose to Python
217+
218+
```python
219+
# QtSiteConfig.py
220+
def update_members(members):
221+
"""Called by Qt.py at run-time to modify the modules it makes available.
222+
223+
Arguments:
224+
members (dict): The members considered by Qt.py
225+
226+
"""
227+
228+
members.pop("QtCore")
229+
```
230+
231+
Finally, expose the module to Python.
232+
233+
```bash
234+
$ set PYTHONPATH=/path/to
235+
$ python -c "import Qt.QtCore"
236+
ImportError: No module named Qt.QtCore
237+
```
238+
239+
> Linux and MacOS users, replace `set` with `export`
240+
241+
<br>
242+
196243
##### Compile Qt Designer files
197244

198245
> WARNING - ALPHA FUNCTIONALITY<br>
@@ -406,15 +453,15 @@ if binding("PyQt4"):
406453
Below are some of the conventions that used throughout the Qt.py module and tests.
407454

408455
- **Etiquette: PEP8**
409-
- All code is written in PEP8. It is recommended you use a linter as you work, flake8 and pylinter are both good options. Anaconda if using Sublime is another good option.
456+
- All code is written in PEP8. It is recommended you use a linter as you work, flake8 and pylinter are both good options. Anaconda if using Sublime is another good option.
410457
- **Etiquette: Double quotes**
411458
- " = yes, ' = no.
412459
- **Etiquette: Napoleon docstrings**
413-
- Any docstrings are made in Google Napoleon format. See [Napoleon](https://sphinxcontrib-napoleon.readthedocs.io/en/latest/example_google.html) for details.
460+
- Any docstrings are made in Google Napoleon format. See [Napoleon](https://sphinxcontrib-napoleon.readthedocs.io/en/latest/example_google.html) for details.
414461
- **Etiquette: Semantic Versioning**
415-
- This project follows [semantic versioning](http://semver.org).
462+
- This project follows [semantic versioning](http://semver.org).
416463
- **Etiquette: Underscore means private**
417-
- Anything prefixed with an underscore means that it is internal to Qt.py and not for public consumption.
464+
- Anything prefixed with an underscore means that it is internal to Qt.py and not for public consumption.
418465

419466
**Running tests**
420467

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Contrived example used for unit testing. This makes the QtCore module
2+
# not accessible. I chose this so it can be tested everywhere, for a more
3+
# realistic example see the README
4+
5+
6+
def update_members(members):
7+
"""This function is called by Qt.py to modify the modules it exposes.
8+
9+
Arguments:
10+
members (dict): The members considered by Qt.py
11+
12+
"""
13+
14+
members.pop("QtCore")

examples/QtSiteConfig/README.md

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
## `QtSiteConfig` example
2+
3+
This example illustrates how to make a QtSiteConfig module and how it affects Qt.py at run-time.
4+
5+
<br>
6+
7+
**Usage**
8+
9+
```bash
10+
$ cd to/this/directory
11+
$ python main.py
12+
# Qt.QtCore was successfully removed by QSideConfig.py
13+
```
14+
15+
Because `QtSiteConfig.py` is in the current working directory, it is available to import by Python. If running from a different directory, then you can append this directory to your `PYTHONPATH`
16+
17+
```bash
18+
$ set PYTHONPATH=path/to/QtSiteConfig/
19+
$ python main.py
20+
# Qt.QtCore was successfully removed by QSideConfig.py
21+
```
22+
23+
> Linux and MacOS users: Replace `set` with `export`
24+
25+
<br>
26+
27+
**Advanced example**
28+
29+
If you need to you can also add modules that are not in the standard Qt.py.
30+
31+
```python
32+
def update_members_example(members):
33+
"""An example of adding QJsonDocument to QtCore and the Qsci.
34+
35+
Remove "_example" from the function name to use this example.
36+
37+
Arguments:
38+
members (dict): The default list of members in Qt.py.
39+
Update this dict with any modifications needed.
40+
41+
"""
42+
43+
# Include QJsonDocument module
44+
members["QtCore"].append("QJsonDocument")
45+
46+
# Include Qsci module for scintilla lexer support.
47+
members["Qsci"] = [
48+
"QsciAPIs",
49+
"QsciAbstractAPIs",
50+
"QsciCommand",
51+
"QsciCommandSet",
52+
"QsciDocument",
53+
"QsciLexer",
54+
"QsciLexerAVS",
55+
"QsciLexerBash",
56+
"QsciLexerBatch",
57+
"QsciLexerCMake",
58+
"QsciLexerCPP",
59+
"QsciLexerCSS",
60+
"QsciLexerCSharp",
61+
"QsciLexerCoffeeScript",
62+
"QsciLexerCustom",
63+
"QsciLexerD",
64+
"QsciLexerDiff",
65+
"QsciLexerFortran",
66+
"QsciLexerFortran77",
67+
"QsciLexerHTML",
68+
"QsciLexerIDL",
69+
"QsciLexerJSON",
70+
"QsciLexerJava",
71+
"QsciLexerJavaScript",
72+
"QsciLexerLua",
73+
"QsciLexerMakefile",
74+
"QsciLexerMarkdown",
75+
"QsciLexerMatlab",
76+
"QsciLexerOctave",
77+
"QsciLexerPO",
78+
"QsciLexerPOV",
79+
"QsciLexerPascal",
80+
"QsciLexerPerl",
81+
"QsciLexerPostScript",
82+
"QsciLexerProperties",
83+
"QsciLexerPython",
84+
"QsciLexerRuby",
85+
"QsciLexerSQL",
86+
"QsciLexerSpice",
87+
"QsciLexerTCL",
88+
"QsciLexerTeX",
89+
"QsciLexerVHDL",
90+
"QsciLexerVerilog",
91+
"QsciLexerXML",
92+
"QsciLexerYAML",
93+
"QsciMacro",
94+
"QsciPrinter",
95+
"QsciScintilla",
96+
"QsciScintillaBase",
97+
"QsciStyle",
98+
"QsciStyledText",
99+
]
100+
```

examples/QtSiteConfig/__init__.py

Whitespace-only changes.

examples/QtSiteConfig/main.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
"""Example of QtSideConfig module used to modify exposed members of Qt.py"""
2+
3+
import os
4+
import sys
5+
6+
7+
def test():
8+
"""QtCore is taken out of Qt.py via QSiteConfig.py"""
9+
10+
# Expose this directory, and therefore QtSiteConfig, to Python
11+
sys.path.insert(0, os.path.dirname(__file__))
12+
13+
try:
14+
from Qt import QtCore
15+
16+
except ImportError:
17+
print("Qt.QtCore was successfully removed by QSideConfig.py")
18+
19+
else:
20+
raise ImportError(
21+
"Qt.QtCore was importable, update_members was not "
22+
"applied correctly."
23+
)
24+
25+
# Suppress 'Qt.QtCore' imported but unused warning
26+
QtCore
27+
28+
29+
if __name__ == '__main__':
30+
test()

examples/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@ If you wish to contribute, make a pull request. Please put your example files in
1212

1313
#### List of examples
1414

15-
* [`loadUi`](https://github.com/mottosso/Qt.py/blob/master/examples/loadUi/README.md) - add base instance argument
15+
* [`loadUi`](/examples/loadUi/README.md) - add base instance argument
16+
* [`QtSiteConfig`](/examples/QtSiteConfig/README.md) - expose additional Qt modules and classes

0 commit comments

Comments
 (0)