Python-i18n: implement gettext functions in libcalamares.utils
- remove Job.gettextPath - add libcalamares.utils.gettext_path() - add libcalamares.utils.gettext_lang() - modify examples in main.py - add some gettext debug-output from dummypython - correct namespace mis-labeling - provide two forms of GlobalStorage - regular use, has a JobQueue with storage - testing use, creates GlobalStorage separately, provide independent access to that for Python.
This commit is contained in:
parent
5326e9ee06
commit
74be2fd098
@ -101,14 +101,22 @@ GlobalStorage::debugDump() const
|
|||||||
namespace CalamaresPython
|
namespace CalamaresPython
|
||||||
{
|
{
|
||||||
|
|
||||||
|
Calamares::GlobalStorage* GlobalStoragePythonWrapper::s_gs_instance = nullptr;
|
||||||
|
|
||||||
// The special handling for nullptr is only for the testing
|
// The special handling for nullptr is only for the testing
|
||||||
// script for the python bindings, which passes in None;
|
// script for the python bindings, which passes in None;
|
||||||
// normal use will have a GlobalStorage from JobQueue::instance()
|
// normal use will have a GlobalStorage from JobQueue::instance()
|
||||||
// passed in. Testing use will leak the allocated GlobalStorage
|
// passed in. Testing use will leak the allocated GlobalStorage
|
||||||
// object, but that's OK for testing.
|
// object, but that's OK for testing.
|
||||||
GlobalStoragePythonWrapper::GlobalStoragePythonWrapper( Calamares::GlobalStorage* gs )
|
GlobalStoragePythonWrapper::GlobalStoragePythonWrapper( Calamares::GlobalStorage* gs )
|
||||||
: m_gs( gs ? gs : new Calamares::GlobalStorage )
|
: m_gs( gs ? gs : s_gs_instance )
|
||||||
{}
|
{
|
||||||
|
if (!m_gs)
|
||||||
|
{
|
||||||
|
s_gs_instance = new Calamares::GlobalStorage;
|
||||||
|
m_gs = s_gs_instance;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
bool
|
bool
|
||||||
GlobalStoragePythonWrapper::contains( const std::string& key ) const
|
GlobalStoragePythonWrapper::contains( const std::string& key ) const
|
||||||
|
@ -87,8 +87,14 @@ public:
|
|||||||
int remove( const std::string& key );
|
int remove( const std::string& key );
|
||||||
boost::python::api::object value( const std::string& key ) const;
|
boost::python::api::object value( const std::string& key ) const;
|
||||||
|
|
||||||
|
// This is a helper for scripts that do not go through
|
||||||
|
// the JobQueue (i.e. the module testpython script),
|
||||||
|
// which allocate their own (singleton) GlobalStorage.
|
||||||
|
static Calamares::GlobalStorage* globalStorageInstance() { return s_gs_instance; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Calamares::GlobalStorage* m_gs;
|
Calamares::GlobalStorage* m_gs;
|
||||||
|
static Calamares::GlobalStorage* s_gs_instance; // See globalStorageInstance()
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace CalamaresPython
|
} // namespace CalamaresPython
|
||||||
|
@ -316,4 +316,4 @@ Helper::handleLastError()
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
} // namespace Calamares
|
} // namespace CalamaresPython
|
||||||
|
@ -70,7 +70,6 @@ BOOST_PYTHON_MODULE( libcalamares )
|
|||||||
.def_readonly( "module_name", &CalamaresPython::PythonJobInterface::moduleName )
|
.def_readonly( "module_name", &CalamaresPython::PythonJobInterface::moduleName )
|
||||||
.def_readonly( "pretty_name", &CalamaresPython::PythonJobInterface::prettyName )
|
.def_readonly( "pretty_name", &CalamaresPython::PythonJobInterface::prettyName )
|
||||||
.def_readonly( "working_path", &CalamaresPython::PythonJobInterface::workingPath )
|
.def_readonly( "working_path", &CalamaresPython::PythonJobInterface::workingPath )
|
||||||
.def_readonly( "gettext_path", &CalamaresPython::PythonJobInterface::gettextPath )
|
|
||||||
.def_readonly( "configuration", &CalamaresPython::PythonJobInterface::configuration )
|
.def_readonly( "configuration", &CalamaresPython::PythonJobInterface::configuration )
|
||||||
.def(
|
.def(
|
||||||
"setprogress",
|
"setprogress",
|
||||||
@ -223,6 +222,12 @@ BOOST_PYTHON_MODULE( libcalamares )
|
|||||||
&CalamaresPython::gettext_languages,
|
&CalamaresPython::gettext_languages,
|
||||||
"Returns list of languages (most to least-specific) for gettext."
|
"Returns list of languages (most to least-specific) for gettext."
|
||||||
);
|
);
|
||||||
|
|
||||||
|
bp::def(
|
||||||
|
"gettext_path",
|
||||||
|
&CalamaresPython::gettext_path,
|
||||||
|
"Returns path for gettext search."
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -186,14 +186,6 @@ PythonJobInterface::PythonJobInterface( Calamares::PythonJob* parent )
|
|||||||
prettyName = m_parent->prettyName().toStdString();
|
prettyName = m_parent->prettyName().toStdString();
|
||||||
workingPath = m_parent->m_workingPath.toStdString();
|
workingPath = m_parent->m_workingPath.toStdString();
|
||||||
configuration = CalamaresPython::variantMapToPyDict( m_parent->m_configurationMap );
|
configuration = CalamaresPython::variantMapToPyDict( m_parent->m_configurationMap );
|
||||||
|
|
||||||
if (moduleDir.cd("../_lang/python"))
|
|
||||||
gettextPath = moduleDir.absolutePath().toStdString();
|
|
||||||
else
|
|
||||||
{
|
|
||||||
debug( "No _lang/ directory for translations." );
|
|
||||||
gettextPath = std::string();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -211,31 +203,50 @@ obscure( const std::string& string )
|
|||||||
return CalamaresUtils::obscure( QString::fromStdString( string ) ).toStdString();
|
return CalamaresUtils::obscure( QString::fromStdString( string ) ).toStdString();
|
||||||
}
|
}
|
||||||
|
|
||||||
std::list< std::string >
|
bp::list
|
||||||
gettext_languages()
|
gettext_languages()
|
||||||
{
|
{
|
||||||
std::list< std::string > pyList;
|
bp::list pyList;
|
||||||
QVariant localeConf_ = Calamares::JobQueue::instance()->globalStorage()->value( "localeConf" );
|
|
||||||
|
// There are two ways that Python jobs can be initialised:
|
||||||
|
// - through JobQueue, in which case that has an instance which holds
|
||||||
|
// a GlobalStorage object, or
|
||||||
|
// - through the Python test-script, which initialises its
|
||||||
|
// own GlobalStoragePythonWrapper, which then holds a
|
||||||
|
// GlobalStorage object for all of Python.
|
||||||
|
Calamares::JobQueue* jq = Calamares::JobQueue::instance();
|
||||||
|
Calamares::GlobalStorage* gs = jq ? jq->globalStorage() : CalamaresPython::GlobalStoragePythonWrapper::globalStorageInstance();
|
||||||
|
|
||||||
|
QVariant localeConf_ = gs->value( "localeConf" );
|
||||||
if ( localeConf_.canConvert< QVariantMap >() )
|
if ( localeConf_.canConvert< QVariantMap >() )
|
||||||
{
|
{
|
||||||
QVariant lang_ = localeConf_.value< QVariantMap >()[ "LANG" ];
|
QVariant lang_ = localeConf_.value< QVariantMap >()[ "LANG" ];
|
||||||
if ( lang_.canConvert< QString >() )
|
if ( lang_.canConvert< QString >() )
|
||||||
{
|
{
|
||||||
QString lang = lang_.value< QString >();
|
QString lang = lang_.value< QString >();
|
||||||
pyList.push_back( lang.toStdString() );
|
pyList.append( lang.toStdString() );
|
||||||
if ( lang.indexOf( '.' ) > 0)
|
if ( lang.indexOf( '.' ) > 0)
|
||||||
{
|
{
|
||||||
lang.truncate( lang.indexOf( '.' ) );
|
lang.truncate( lang.indexOf( '.' ) );
|
||||||
pyList.push_back( lang.toStdString() );
|
pyList.append( lang.toStdString() );
|
||||||
}
|
}
|
||||||
if ( lang.indexOf( '_' ) > 0)
|
if ( lang.indexOf( '_' ) > 0)
|
||||||
{
|
{
|
||||||
lang.truncate( lang.indexOf( '_' ) );
|
lang.truncate( lang.indexOf( '_' ) );
|
||||||
pyList.push_back( lang.toStdString() );
|
pyList.append( lang.toStdString() );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return pyList;
|
return pyList;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::string
|
||||||
|
gettext_path()
|
||||||
|
{
|
||||||
|
// TODO: distinguish between -d runs and normal runs
|
||||||
|
// TODO: can we detect DESTDIR-installs?
|
||||||
|
return std::string();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -63,7 +63,7 @@ std::string obscure( const std::string& string );
|
|||||||
|
|
||||||
std::string gettext_path();
|
std::string gettext_path();
|
||||||
|
|
||||||
std::list< std::string > gettext_languages();
|
boost::python::list gettext_languages();
|
||||||
|
|
||||||
void debug( const std::string& s );
|
void debug( const std::string& s );
|
||||||
|
|
||||||
@ -77,7 +77,6 @@ public:
|
|||||||
std::string moduleName;
|
std::string moduleName;
|
||||||
std::string prettyName;
|
std::string prettyName;
|
||||||
std::string workingPath;
|
std::string workingPath;
|
||||||
std::string gettextPath;
|
|
||||||
|
|
||||||
boost::python::dict configuration;
|
boost::python::dict configuration;
|
||||||
|
|
||||||
|
@ -35,7 +35,7 @@ from time import gmtime, strftime, sleep
|
|||||||
|
|
||||||
import gettext
|
import gettext
|
||||||
_ = gettext.translation("calamares-python",
|
_ = gettext.translation("calamares-python",
|
||||||
localedir=libcalamares.utils.gettext_path,
|
localedir=libcalamares.utils.gettext_path(),
|
||||||
languages=libcalamares.utils.gettext_languages(),
|
languages=libcalamares.utils.gettext_languages(),
|
||||||
fallback=True).gettext
|
fallback=True).gettext
|
||||||
|
|
||||||
@ -46,6 +46,11 @@ def pretty_name():
|
|||||||
|
|
||||||
def run():
|
def run():
|
||||||
"""Dummy python job."""
|
"""Dummy python job."""
|
||||||
|
libcalamares.utils.debug("LocaleDir=" +
|
||||||
|
str(libcalamares.utils.gettext_path()))
|
||||||
|
libcalamares.utils.debug("Languages=" +
|
||||||
|
str(libcalamares.utils.gettext_languages()))
|
||||||
|
|
||||||
os.system("/bin/sh -c \"touch ~/calamares-dummypython\"")
|
os.system("/bin/sh -c \"touch ~/calamares-dummypython\"")
|
||||||
accumulator = strftime("%Y-%m-%d %H:%M:%S", gmtime()) + "\n"
|
accumulator = strftime("%Y-%m-%d %H:%M:%S", gmtime()) + "\n"
|
||||||
accumulator += "Calamares version: " + libcalamares.VERSION_SHORT + "\n"
|
accumulator += "Calamares version: " + libcalamares.VERSION_SHORT + "\n"
|
||||||
|
Loading…
Reference in New Issue
Block a user