setup: Refactor babel compile_catalog integration

This commit is contained in:
Markus Frosch 2020-05-17 23:24:14 +02:00
parent a298022563
commit 25ce1b16e0
1 changed files with 62 additions and 32 deletions

View File

@ -16,8 +16,9 @@ import platform
from terminatorlib.version import APP_NAME, APP_VERSION from terminatorlib.version import APP_NAME, APP_VERSION
PO_DIR = 'po' GETTEXT_SOURCE = 'po'
MO_DIR = os.path.join('build', 'mo') GETTEXT_DOMAIN = 'terminator'
GETTEXT_TARGET = os.path.join('share', 'locale')
CSS_DIR = os.path.join('terminatorlib', 'themes') CSS_DIR = os.path.join('terminatorlib', 'themes')
if sys.version_info < (3, 0): if sys.version_info < (3, 0):
@ -32,39 +33,45 @@ class TerminatorDist(Distribution):
global_options = Distribution.global_options + [ global_options = Distribution.global_options + [
("build-documentation", None, "Build the documentation"), ("build-documentation", None, "Build the documentation"),
("install-documentation", None, "Install the documentation"), ("install-documentation", None, "Install the documentation"),
("without-gettext", None, "Don't build/install gettext .mo files"),
("without-icon-cache", None, "Don't attempt to run gtk-update-icon-cache")] ("without-icon-cache", None, "Don't attempt to run gtk-update-icon-cache")]
def __init__ (self, *args): def __init__ (self, *args):
self.without_gettext = False
self.without_icon_cache = False self.without_icon_cache = False
Distribution.__init__(self, *args) Distribution.__init__(self, *args)
class BuildData(build): class CustomBuild(build):
"""
Custom build extensions to build
"""
def run(self): def run(self):
build.run(self) build.run(self)
self.build_i18n()
if not self.distribution.without_gettext: def build_i18n(self):
# Build the translations """
for po in glob.glob (os.path.join (PO_DIR, '*.po')): Compiling files for gettext from *.po to *.mo with the proper target path
"""
info('compiling i18n files')
from babel.messages.frontend import compile_catalog
compiler = compile_catalog(self.distribution)
compiler.domain = [GETTEXT_DOMAIN]
for po in glob.glob(os.path.join(GETTEXT_SOURCE, '*.po')):
lang = os.path.basename(po[:-3]) lang = os.path.basename(po[:-3])
mo = os.path.join(MO_DIR, lang, 'terminator.mo') mo = os.path.join(self.build_base, GETTEXT_TARGET, lang, 'LC_MESSAGES', 'terminator.mo')
directory = os.path.dirname(mo) directory = os.path.dirname(mo)
if not os.path.exists(directory): if not os.path.exists(directory):
info('creating %s' % directory)
os.makedirs(directory) os.makedirs(directory)
if newer(po, mo): if newer(po, mo):
info('compiling %s -> %s' % (po, mo))
from babel.messages.frontend import compile_catalog
compiler = compile_catalog(self.distribution)
compiler.domain = ['terminator']
compiler.input_file = po compiler.input_file = po
compiler.output_file = mo compiler.output_file = mo
compiler.run() compiler.run()
class Uninstall(Command): class Uninstall(Command):
description = "Attempt an uninstall from an install --record file" description = "Attempt an uninstall from an install --record file"
@ -140,12 +147,14 @@ class InstallData(install_data):
warn("updating the GTK icon cache failed: %s" % str(e)) warn("updating the GTK icon cache failed: %s" % str(e))
def _find_mo_files(self): def _find_mo_files(self):
"""
search for gettext files built during build step
"""
data_files = [] data_files = []
if not self.distribution.without_gettext: build_base = self.distribution.command_obj['build'].build_base
for mo in glob.glob (os.path.join (MO_DIR, '*', 'terminator.mo')): for mo in glob.glob(os.path.join(build_base, GETTEXT_TARGET, '*', 'LC_MESSAGES', '*.mo')):
lang = os.path.basename(os.path.dirname(mo)) dest = mo.lstrip(build_base + os.sep)
dest = os.path.join('share', 'locale', lang, 'LC_MESSAGES')
data_files.append((dest, [mo])) data_files.append((dest, [mo]))
return data_files return data_files
@ -161,6 +170,27 @@ class InstallData(install_data):
return data_files return data_files
class UpdateCatalogs(Command):
"""Update all gettext catalogs """
description = __doc__
user_options = []
def initialize_options(self):
pass
def finalize_options(self):
pass
def run(self):
from babel.messages.frontend import update_catalog
updater = update_catalog(self.distribution)
updater.input_file = os.path.join(GETTEXT_SOURCE, 'terminator.pot')
for po in glob.glob(os.path.join(GETTEXT_SOURCE, '*.po')):
updater.output_file = po
updater.run()
if platform.system() in ['FreeBSD', 'OpenBSD']: if platform.system() in ['FreeBSD', 'OpenBSD']:
man_dir = 'man' man_dir = 'man'
else: else:
@ -220,6 +250,6 @@ setup(name=APP_NAME,
tests_require=test_deps, tests_require=test_deps,
extras_require={'test': test_deps}, extras_require={'test': test_deps},
package_data={'terminatorlib': ['preferences.glade', 'layoutlauncher.glade']}, package_data={'terminatorlib': ['preferences.glade', 'layoutlauncher.glade']},
cmdclass={'build': BuildData, 'install_data': InstallData, 'uninstall': Uninstall}, cmdclass={'build': CustomBuild, 'install_data': InstallData, 'uninstall': Uninstall},
distclass=TerminatorDist) distclass=TerminatorDist)