[translations] Use pathlib

This commit is contained in:
lovetox
2020-06-28 21:15:36 +02:00
parent 08e390abc0
commit ddc128ce8b

View File

@@ -1,5 +1,8 @@
import logging import logging
import os import os
import shutil
from glob import glob
from pathlib import Path
from gajim.common import configpaths from gajim.common import configpaths
@@ -16,39 +19,33 @@ class PluginsTranslationsPlugin(GajimPlugin):
'enabling this plugin.') 'enabling this plugin.')
self.config_dialog = None self.config_dialog = None
self.config_default_values = {'last_version': '0'} self.config_default_values = {'last_version': '0'}
self.locale_dir = os.path.join( self.locale_dir = Path(configpaths.get('PLUGINS_USER')) / 'locale'
configpaths.get('PLUGINS_USER'), 'locale')
def activate(self): def activate(self):
if self.config['last_version'] == self.version: if self.config['last_version'] == self.version:
return return
from glob import glob
import shutil
files = glob(self.__path__ + '/*.mo') files = glob(self.__path__ + '/*.mo')
# remove old data
self._remove_translations() self._remove_translations()
# create dirs and copy files self.locale_dir.mkdir()
os.mkdir(self.locale_dir)
locales = [ locales = [
os.path.splitext(os.path.basename(name))[0] for name in files os.path.splitext(os.path.basename(name))[0] for name in files
] ]
log.info('Installing new translations...') log.info('Installing new translations...')
for locale in locales: for locale in locales:
dst = os.path.join(os.path.join(self.locale_dir, locale), dst = self.locale_dir / locale / 'LC_MESSAGES' / 'gajim_plugins.mo'
'LC_MESSAGES/gajim_plugins.mo') dst.mkdir(parents=True)
os.makedirs(os.path.split(dst)[0]) shutil.copy2(os.path.join(self.__path__, '%s.mo' % locale),
shutil.copy2(os.path.join(self.__path__, '%s.mo' % locale), dst) str(dst))
self.config['last_version'] = self.version self.config['last_version'] = self.version
def _remove_translations(self): def _remove_translations(self):
log.info('Removing old translations...') log.info('Removing old translations...')
if os.path.isdir(self.locale_dir): if self.locale_dir.exists():
import shutil shutil.rmtree(str(self.locale_dir))
shutil.rmtree(self.locale_dir)
def deactivate(self): def deactivate(self):
self._remove_translations() self._remove_translations()