2

Right now, I’m extracting messages using

pybabel extract -F babel.cfg -o messages.pot .

This walks through all my Python files and extracts messages correctly. However, I call this through subprocess.call(), which is pretty ugly, given PyBbel is also written in Python.

I took a look into PyBabel, and it uses setuptools comands to do its work. I could copy the extract_messages.run() method to my Python script, but it doesn’t feel too elegant. Is there a better way to do it? There are tons of articles on how to create new setuptools commands, but no one writes about invoking them…

desertnaut
  • 57,590
  • 26
  • 140
  • 166
GergelyPolonkai
  • 6,230
  • 6
  • 35
  • 69

2 Answers2

4

Maybe this is what you are looking for: How do i run the python 'sdist' command from within a python automated script without using subprocess?

I'm going to show a few alternatives to run the Babel Python code, without creating a new subprocess, from higher to lower level.

This is kind of a hack, taken from the answers linked above:

from setuptools.dist import Distribution
from babel.messages.frontend import extract_messages

dist = Distribution({'name': 'my-project', 'version': '1.0.0'}) # etc.
dist.script_name = 'setup.py'
cmd = extract_messages(dist)
cmd.ensure_finalized()
cmd.run()  # TODO: error handling

pylabel script actually does something like this:

from babel.messages.frontend import main

sys.argv[0] = re.sub(r'(-script\.pyw?|\.exe)?$', '', sys.argv[0])
sys.exit(main())

But you can avoid sending the commands over the sys.argv and actually call the CommandInterface python code from babel.

This is my favorite way to call it:

from babel.messages.frontend import CommandLineInterface

CommandLineInterface().run(['pybabel','extract','-F','babel.cfg','-k','lazy_gettext','-o','messages.pot','sample_project'])
CommandLineInterface().run(['pybabel','init','-i','messages.pot','-d','translations','-l','en'])
CommandLineInterface().run(['pybabel','compile','-d','translations'])
CommandLineInterface().run(['pybabel','update','-d','translations'])

That's the closest you can get to the low level code, unless you want to start copy/pasting and customizing the python code. Again, this is a 100% python solution, and it doesn't call a new process.

Good luck

Emilio
  • 2,526
  • 22
  • 26
0

I use os to do it right now with this script:

#!venv/bin/python
import os

pybabel = 'venv/bin/pybabel'
os.system(pybabel + ' extract -F babel.cfg -k lazy_gettext -o messages.pot app')
os.system(pybabel + ' update -i messages.pot -d app/translations')
os.unlink('messages.pot')

Hope it give you an idea

Jonathan Arias
  • 471
  • 7
  • 18