Untitled Diff

Creado El diff nunca expira
0 eliminaciones
327 líneas
15 adiciones
340 líneas
# ../translations/strings.py
# ../translations/strings.py


"""Provides translation functionality."""
"""Provides translation functionality."""


# =============================================================================
# =============================================================================
# >> IMPORTS
# >> IMPORTS
# =============================================================================
# =============================================================================
# Python Imports
# Python Imports
# Binascii
# Binascii
from binascii import unhexlify
from binascii import unhexlify
# Codecs
# Codecs
from codecs import unicode_escape_decode
from codecs import unicode_escape_decode
# Re
# Re
from re import compile as re_compile
from re import compile as re_compile
from re import VERBOSE
from re import VERBOSE


# Site-Package Imports
# Site-Package Imports
# Configobj
# Configobj
from configobj import Section
from configobj import Section


# Source.Python Imports
# Source.Python Imports
# Core
# Core
from core import GameConfigObj
from core import GameConfigObj
# Paths
# Paths
from paths import TRANSLATION_PATH
from paths import TRANSLATION_PATH
from paths import GAME_PATH
from paths import GAME_PATH
# Translations
# Translations
from translations.manager import language_manager
from translations.manager import language_manager




# =============================================================================
# =============================================================================
# >> ALL DECLARATION
# >> ALL DECLARATION
# =============================================================================
# =============================================================================
__all__ = ('LangStrings',
__all__ = ('LangStrings',
'TranslationStrings',
'TranslationStrings',
)
)




# =============================================================================
# =============================================================================
# >> GLOBAL VARIABLES
# >> GLOBAL VARIABLES
# =============================================================================
# =============================================================================
# Get an re.compile instance to correct all double escaped strings
# Get an re.compile instance to correct all double escaped strings
_double_escaped_pattern = re_compile(
_double_escaped_pattern = re_compile(
r"""(\\(?:(?P<octal>[0-7]{1,3})|x(?P<hexadecimal>[0-9|a-f|A-F]{2})|
r"""(\\(?:(?P<octal>[0-7]{1,3})|x(?P<hexadecimal>[0-9|a-f|A-F]{2})|
(?P<notation>a|b|e|f|n|r|s|t|v)))""", VERBOSE)
(?P<notation>a|b|e|f|n|r|s|t|v)))""", VERBOSE)




# =============================================================================
# =============================================================================
# >> CLASSES
# >> CLASSES
# =============================================================================
# =============================================================================
class LangStrings(dict):
class LangStrings(dict):
"""Dictionary class used to store all strings for a plugin."""
"""Dictionary class used to store all strings for a plugin."""


def __init__(self, infile, encoding='utf_8'):
def __init__(self, infile, encoding='utf_8'):
"""Add all strings and fix double escaped strings."""
"""Add all strings and fix double escaped strings."""
# Initialize the dictionary
# Initialize the dictionary
super().__init__()
super().__init__()


# Get the path to the given file
# Get the path to the given file
self._mainfile = TRANSLATION_PATH / infile + '.ini'
self._mainfile = TRANSLATION_PATH / infile + '.ini'
self._encoding = encoding
self._encoding = encoding


# Does the file exist?
# Does the file exist?
if not self._mainfile.isfile():
if not self._mainfile.isfile():


# Raise an error
# Raise an error
raise FileNotFoundError(
raise FileNotFoundError(
'No file found at {0}'.format(self._mainfile))
'No file found at {0}'.format(self._mainfile))


# Get the path to the server specific file
# Get the path to the server specific file
self._serverfile = self._mainfile.parent / '{0}_server.ini'.format(
self._serverfile = self._mainfile.parent / '{0}_server.ini'.format(
self._mainfile.namebase)
self._mainfile.namebase)


# Get the strings from the main file
# Get the strings from the main file
main_strings = GameConfigObj(self._mainfile, encoding=encoding)
main_strings = GameConfigObj(self._mainfile, encoding=encoding)


# Does the server specific file exist?
# Does the server specific file exist?
if not self._serverfile.isfile() and not infile.startswith('_core/'):
if not self._serverfile.isfile() and not infile.startswith('_core/'):


# Create the server specific file
# Create the server specific file
self._create_server_file()
self._create_server_file()


# Otherwise
# Otherwise
else:
else:


# Get any strings from the server specific file
# Get any strings from the server specific file
server_strings = GameConfigObj(self._serverfile, encoding=encoding)
server_strings = GameConfigObj(self._serverfile, encoding=encoding)


# Merge the two ConfigObj instances together
# Merge the two ConfigObj instances together
main_strings.merge(server_strings)
main_strings.merge(server_strings)


# Loop through all strings
# Loop through all strings
for key in main_strings:
for key in main_strings:


# Is the current string not a Section?
# Is the current string not a Section?
if not isinstance(main_strings[key], Section):
if not isinstance(main_strings[key], Section):


# No need to go further
# No need to go further
continue
continue