Initial version of the Python scripts to manage l10n using the XLIFF format
Add the following files: python3-flightgear/README-l10n.txt python3-flightgear/fg-convert-translation-files python3-flightgear/fg-new-translations python3-flightgear/fg-update-translation-files python3-flightgear/flightgear/__init__.py python3-flightgear/flightgear/meta/__init__.py python3-flightgear/flightgear/meta/exceptions.py python3-flightgear/flightgear/meta/i18n.py python3-flightgear/flightgear/meta/logging.py python3-flightgear/flightgear/meta/misc.py They should work on Python 3.4 and later (tested with 3.5.3). The folder structure is chosen so that other FG support modules can insert themselves here, and possibly be used together. I put all of these inside 'flightgear.meta', because I don't expect them to be needed at FG runtime (neither now nor in the future), probably not even by the CMake build system. To declare that a string has plural forms, simply set the attribute 'with-plural' to 'true' on the corresponding element of the default translation (and as in Qt, use %n as a placeholder for the number that determines which singular or plural form to use).
This commit is contained in:
0
python3-flightgear/flightgear/__init__.py
Normal file
0
python3-flightgear/flightgear/__init__.py
Normal file
0
python3-flightgear/flightgear/meta/__init__.py
Normal file
0
python3-flightgear/flightgear/meta/__init__.py
Normal file
58
python3-flightgear/flightgear/meta/exceptions.py
Normal file
58
python3-flightgear/flightgear/meta/exceptions.py
Normal file
@@ -0,0 +1,58 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
# exceptions.py --- Simple, general-purpose subclass of Exception
|
||||
#
|
||||
# Copyright (C) 2015, 2017 Florent Rougon
|
||||
#
|
||||
# This program is free software; you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation; either version 2 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License along
|
||||
# with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
|
||||
"""Simple, general-purpose Exception subclass."""
|
||||
|
||||
|
||||
class FGPyException(Exception):
|
||||
def __init__(self, message=None, *, mayCapitalizeMsg=True):
|
||||
"""Initialize an FGPyException instance.
|
||||
|
||||
Except in cases where 'message' starts with a proper noun or
|
||||
something like that, its first character should be given in
|
||||
lower case. Automated treatments of this exception may print the
|
||||
message with its first character changed to upper case, unless
|
||||
'mayCapitalizeMsg' is False. In other words, if the case of the
|
||||
first character of 'message' must not be changed under any
|
||||
circumstances, set 'mayCapitalizeMsg' to False.
|
||||
|
||||
"""
|
||||
self.message = message
|
||||
self.mayCapitalizeMsg = mayCapitalizeMsg
|
||||
|
||||
def __str__(self):
|
||||
return self.completeMessage()
|
||||
|
||||
def __repr__(self):
|
||||
return "{}.{}({!r})".format(__name__, type(self).__name__, self.message)
|
||||
|
||||
# Typically overridden by subclasses with a custom constructor
|
||||
def detail(self):
|
||||
return self.message
|
||||
|
||||
def completeMessage(self):
|
||||
if self.message:
|
||||
return "{shortDesc}: {detail}".format(
|
||||
shortDesc=self.ExceptionShortDescription,
|
||||
detail=self.detail())
|
||||
else:
|
||||
return self.ExceptionShortDescription
|
||||
|
||||
ExceptionShortDescription = "FlightGear Python generic exception"
|
||||
1822
python3-flightgear/flightgear/meta/i18n.py
Normal file
1822
python3-flightgear/flightgear/meta/i18n.py
Normal file
File diff suppressed because it is too large
Load Diff
95
python3-flightgear/flightgear/meta/logging.py
Normal file
95
python3-flightgear/flightgear/meta/logging.py
Normal file
@@ -0,0 +1,95 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
# logging.py --- Simple logging infrastructure (mostly taken from FFGo)
|
||||
# Copyright (C) 2015, 2017 Florent Rougon
|
||||
#
|
||||
# This program is free software; you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation; either version 2 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License along
|
||||
# with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
|
||||
import sys
|
||||
|
||||
from . import misc
|
||||
|
||||
|
||||
class LogLevel(misc.OrderedEnum):
|
||||
debug, info, notice, warning, error, critical = range(6)
|
||||
|
||||
# List containing the above log levels as strings in increasing priority order
|
||||
allLogLevels = [member.name for member in LogLevel]
|
||||
allLogLevels.sort(key=lambda n: LogLevel[n].value)
|
||||
|
||||
|
||||
def _logFuncFactory(level):
|
||||
def logFunc(self, *args, **kwargs):
|
||||
self.log(LogLevel[level], True, *args, **kwargs)
|
||||
|
||||
def logFunc_noPrefix(self, *args, **kwargs):
|
||||
self.log(LogLevel[level], False, *args, **kwargs)
|
||||
|
||||
return (logFunc, logFunc_noPrefix)
|
||||
|
||||
|
||||
class Logger:
|
||||
def __init__(self, progname=None, logLevel=LogLevel.notice,
|
||||
defaultOutputStream=sys.stdout, logFile=None):
|
||||
self.progname = progname
|
||||
self.logLevel = logLevel
|
||||
self.defaultOutputStream = defaultOutputStream
|
||||
self.logFile = logFile
|
||||
|
||||
def setLogFile(self, *args, **kwargs):
|
||||
self.logFile = open(*args, **kwargs)
|
||||
|
||||
def log(self, level, printLogLevel, *args, **kwargs):
|
||||
if printLogLevel and level >= LogLevel.warning and args:
|
||||
args = [level.name.upper() + ": " + args[0]] + list(args[1:])
|
||||
|
||||
if level >= self.logLevel:
|
||||
if (self.progname is not None) and args:
|
||||
tArgs = [self.progname + ": " + args[0]] + list(args[1:])
|
||||
else:
|
||||
tArgs = args
|
||||
|
||||
kwargs["file"] = self.defaultOutputStream
|
||||
print(*tArgs, **kwargs)
|
||||
|
||||
if self.logFile is not None:
|
||||
kwargs["file"] = self.logFile
|
||||
print(*args, **kwargs)
|
||||
|
||||
# Don't overload log() with too many tests or too much indirection for
|
||||
# little use
|
||||
def logToFile(self, *args, **kwargs):
|
||||
kwargs["file"] = self.logFile
|
||||
print(*args, **kwargs)
|
||||
|
||||
# NP functions are “no prefix” variants which never prepend the log level
|
||||
# (otherwise, it is only prepended for warning and higher levels).
|
||||
debug, debugNP = _logFuncFactory("debug")
|
||||
info, infoNP = _logFuncFactory("info")
|
||||
notice, noticeNP = _logFuncFactory("notice")
|
||||
warning, warningNP = _logFuncFactory("warning")
|
||||
error, errorNP = _logFuncFactory("error")
|
||||
critical, criticalNP = _logFuncFactory("critical")
|
||||
|
||||
|
||||
class DummyLogger(Logger):
|
||||
def setLogFile(self, *args, **kwargs):
|
||||
pass
|
||||
|
||||
def log(self, *args, **kwargs):
|
||||
pass
|
||||
|
||||
def logToFile(self, *args, **kwargs):
|
||||
pass
|
||||
81
python3-flightgear/flightgear/meta/misc.py
Normal file
81
python3-flightgear/flightgear/meta/misc.py
Normal file
@@ -0,0 +1,81 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
# misc.py --- Miscellaneous classes and/or functions
|
||||
# Copyright (C) 2015-2017 Florent Rougon
|
||||
#
|
||||
# This program is free software; you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation; either version 2 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License along
|
||||
# with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
|
||||
import enum
|
||||
|
||||
# Based on an example from the 'enum' documentation
|
||||
class OrderedEnum(enum.Enum):
|
||||
"""Base class for enumerations whose members can be ordered.
|
||||
|
||||
Contrary to enum.IntEnum, this class maintains normal enum.Enum
|
||||
invariants, such as members not being comparable to members of other
|
||||
enumerations (nor of any other class, actually).
|
||||
|
||||
"""
|
||||
def __ge__(self, other):
|
||||
if self.__class__ is other.__class__:
|
||||
return self.value >= other.value
|
||||
return NotImplemented
|
||||
|
||||
def __gt__(self, other):
|
||||
if self.__class__ is other.__class__:
|
||||
return self.value > other.value
|
||||
return NotImplemented
|
||||
|
||||
def __le__(self, other):
|
||||
if self.__class__ is other.__class__:
|
||||
return self.value <= other.value
|
||||
return NotImplemented
|
||||
|
||||
def __lt__(self, other):
|
||||
if self.__class__ is other.__class__:
|
||||
return self.value < other.value
|
||||
return NotImplemented
|
||||
|
||||
def __eq__(self, other):
|
||||
if self.__class__ is other.__class__:
|
||||
return self.value == other.value
|
||||
return NotImplemented
|
||||
|
||||
def __ne__(self, other):
|
||||
if self.__class__ is other.__class__:
|
||||
return self.value != other.value
|
||||
return NotImplemented
|
||||
|
||||
|
||||
# Taken from <http://effbot.org/zone/element-lib.htm#prettyprint> and modified
|
||||
# by Florent Rougon
|
||||
def indentXmlTree(elem, level=0, basicOffset=2, lastChild=False):
|
||||
def indentation(level):
|
||||
return "\n" + level*basicOffset*" "
|
||||
|
||||
if len(elem):
|
||||
if not elem.text or not elem.text.strip():
|
||||
elem.text = indentation(level+1)
|
||||
|
||||
for e in elem[:-1]:
|
||||
indentXmlTree(e, level+1, basicOffset, False)
|
||||
if len(elem):
|
||||
indentXmlTree(elem[-1], level+1, basicOffset, True)
|
||||
|
||||
if level and (not elem.tail or not elem.tail.strip()):
|
||||
if lastChild:
|
||||
elem.tail = indentation(level-1)
|
||||
else:
|
||||
elem.tail = indentation(level)
|
||||
Reference in New Issue
Block a user