Skip to content

Commit

Permalink
Auto build *_ui.py for *_ui imports from *.ui files
Browse files Browse the repository at this point in the history
  • Loading branch information
altendky committed Aug 2, 2018
1 parent e08e75d commit d981617
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 41 deletions.
88 changes: 64 additions & 24 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# Local
# DO NOT EDIT THIS GENERATED FILE
# Edit .gitignore.io and .gitignore.local
# Rebuild this file by running `gitignoreio`

# Local

*_ui.py


# Created by https://www.gitignore.io/api/git,vim,python,pycharm
Expand All @@ -9,40 +13,48 @@
*.orig

### PyCharm ###
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and WebStorm
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839

# User-specific stuff:
# User-specific stuff
.idea/**/workspace.xml
.idea/**/tasks.xml
.idea/dictionaries
.idea/**/usage.statistics.xml
.idea/**/dictionaries
.idea/**/shelf

# Sensitive or high-churn files:
# Sensitive or high-churn files
.idea/**/dataSources/
.idea/**/dataSources.ids
.idea/**/dataSources.xml
.idea/**/dataSources.local.xml
.idea/**/sqlDataSources.xml
.idea/**/dynamic.xml
.idea/**/uiDesigner.xml
.idea/**/dbnavigator.xml

# Gradle:
# Gradle
.idea/**/gradle.xml
.idea/**/libraries

# Gradle and Maven with auto-import
# When using Gradle or Maven with auto-import, you should exclude module files,
# since they will be recreated, and may cause churn. Uncomment if using
# auto-import.
# .idea/modules.xml
# .idea/*.iml
# .idea/modules

# CMake
cmake-build-debug/
cmake-build-*/

# Mongo Explorer plugin:
# Mongo Explorer plugin
.idea/**/mongoSettings.xml

## File-based project format:
# File-based project format
*.iws

## Plugin-specific files:

# IntelliJ
/out/
out/

# mpeltonen/sbt-idea plugin
.idea_modules/
Expand All @@ -53,15 +65,15 @@ atlassian-ide-plugin.xml
# Cursive Clojure plugin
.idea/replstate.xml

# Ruby plugin and RubyMine
/.rakeTasks

# Crashlytics plugin (for Android Studio and IntelliJ)
com_crashlytics_export_strings.xml
crashlytics.properties
crashlytics-build.properties
fabric.properties

# Editor-based Rest Client
.idea/httpRequests

### PyCharm Patch ###
# Comment Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-215987721

Expand Down Expand Up @@ -99,6 +111,7 @@ wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
Expand All @@ -116,16 +129,21 @@ htmlcov/
.coverage
.coverage.*
.cache
.pytest_cache/
nosetests.xml
coverage.xml
*.cover
.hypothesis/
.pytest_cache/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3

# Flask stuff:
instance/
.webassets-cache
Expand All @@ -146,7 +164,7 @@ target/
.python-version

# celery beat schedule file
celerybeat-schedule.*
celerybeat-schedule

# SageMath parsed files
*.sage.py
Expand All @@ -173,17 +191,39 @@ venv.bak/
# mypy
.mypy_cache/

### Python Patch ###
.venv/

### Python.VirtualEnv Stack ###
# Virtualenv
# http://iamzed.com/2009/05/07/a-primer-on-virtualenv/
[Bb]in
[Ii]nclude
[Ll]ib
[Ll]ib64
[Ll]ocal
[Ss]cripts
pyvenv.cfg
pip-selfcheck.json

### Vim ###
# swap
.sw[a-p]
.*.sw[a-p]
# session
# Swap
[._]*.s[a-v][a-z]
[._]*.sw[a-p]
[._]s[a-rt-v][a-z]
[._]ss[a-gi-z]
[._]sw[a-p]

# Session
Session.vim
# temporary

# Temporary
.netrwhist
*~
# auto-generated tag files
# Auto-generated tag files
tags
# Persistent undo
[._]*.un~


# End of https://www.gitignore.io/api/git,vim,python,pycharm
1 change: 1 addition & 0 deletions .gitignore.local
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*_ui.py
2 changes: 2 additions & 0 deletions src/basicpyqt5example/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ def excepthook(excType=None, excValue=None, tracebackobj=None):

import PyQt5.QtWidgets

import basicpyqt5example.uiimporthook
basicpyqt5example.uiimporthook.install_ui_finder()
import basicpyqt5example.mainwindow


Expand Down
12 changes: 3 additions & 9 deletions src/basicpyqt5example/mainwindow.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,9 @@
# <http://www.gnu.org/licenses/>.


import pathlib

import PyQt5.uic

import basicpyqt5example.mainwindow_ui
import basicpyqt5example.systeminfo


Expand All @@ -34,16 +33,11 @@ def f(*_, **kw):
return f


Ui, UiBase = PyQt5.uic.loadUiType(
pathlib.Path(__file__).parents[0] / 'mainwindow.ui',
)


class MainWindow(UiBase):
class MainWindow(PyQt5.QtWidgets.QMainWindow):
def __init__(self, parent=None):
super().__init__(parent)

self.ui = Ui()
self.ui = basicpyqt5example.mainwindow_ui.Ui_MainWindow()
self.ui.setupUi(self)

self.ui.actionSystem_Info.triggered.connect(
Expand Down
12 changes: 4 additions & 8 deletions src/basicpyqt5example/systeminfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@
# <http://www.gnu.org/licenses/>.


import pathlib
import sys

import PyQt5.QtCore
import PyQt5.uic
import sip

import basicpyqt5example.systeminfo_ui


system_info = f'''\
sys.version: {sys.version}
Expand All @@ -35,16 +36,11 @@
SIP_VERSION_STR: {sip.SIP_VERSION_STR}'''


Ui, UiBase = PyQt5.uic.loadUiType(
pathlib.Path(__file__).parents[0] / 'systeminfo.ui',
)


class SystemInfoDialog(UiBase):
class SystemInfoDialog(PyQt5.QtWidgets.QDialog):
def __init__(self, parent=None):
super().__init__(parent)

self.ui = Ui()
self.ui = basicpyqt5example.systeminfo_ui.Ui_Dialog()
self.ui.setupUi(self)

self.ui.label.setText(system_info)
27 changes: 27 additions & 0 deletions src/basicpyqt5example/uiimporthook.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import pathlib
import sys

import PyQt5.uic


class UiFinder:
def find_spec(self, fullname, path, target=None):
suffix = '_ui'

if target is not None or not fullname.endswith(suffix):
return

_, _, base = fullname.rpartition('.')
base = base[:-len(suffix)]

path, = path
path = pathlib.Path(path)
py_path = path / (base + suffix + '.py')
ui_path = path / (base + '.ui')

with open(py_path, 'w') as py:
PyQt5.uic.compileUi(ui_path, py, indent=4)


def install_ui_finder():
sys.meta_path.insert(0, UiFinder())

0 comments on commit d981617

Please sign in to comment.