1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
|
"""
This module contains the hooks load and our builtin hooks.
"""
import re
import pkg_resources
from .utils import write_message
#: Name of the entrypoint to use in setup.py
ENTRYPOINT = 'pyqt_distutils_hooks'
def load_hooks():
"""
Load the exposed hooks.
Returns a dict of hooks where the keys are the name of the hook and the
values are the actual hook functions.
"""
hooks = {}
for entrypoint in pkg_resources.iter_entry_points(ENTRYPOINT):
name = str(entrypoint).split('=')[0].strip()
try:
hook = entrypoint.load()
except Exception as e:
write_message('failed to load entry-point %r (error="%s")' % (name, e), 'yellow')
else:
hooks[name] = hook
return hooks
def hook(ui_file_path):
"""
This is the prototype of a hook function.
"""
pass
def gettext(ui_file_path):
"""
Let you use gettext instead of the Qt tools for l18n
"""
with open(ui_file_path, 'r') as fin:
content = fin.read()
# replace ``_translate("context", `` by ``_(``
content = re.sub(r'_translate\(".*",\s', '_(', content)
content = content.replace(
' _translate = QtCore.QCoreApplication.translate', '')
with open(ui_file_path, 'w') as fout:
fout.write(content)
|