#!/usr/bin/env python3 import os from codecs import open import argparse from configparser import ConfigParser class FixLanguage: def __init__(self,home): self.config = ConfigParser() self.config.optionxform = str self.path_language='{home}/.config/plasma-localerc'.format(home=home) self.language_to_fix = ['es','ca@valencia'] def parse_file(self): if os.path.exists(self.path_language): with open(self.path_language, 'r', encoding='utf-8') as fd: self.config.read_file(fd) if 'Translations' in self.config and 'LANGUAGE' in self.config['Translations']: self.languages = self.config['Translations']['LANGUAGE'].split(':') return self.languages = list() def generate_lang_and_language(self): if self.languages[0] == 'es': self.lang = 'es_ES.UTF-8' elif self.languages[0] == 'ca@valencia': self.lang = 'ca_ES.utf8@valencia' def is_need_fix(self): return self.languages[0] in self.language_to_fix def write_file(self): self.config['Translations']['LANGUAGE'] = ':'.join(self.languages) self.config['Formats']['LANG'] = self.lang with open(self.path_language, 'w', encoding='utf-8') as fd: self.config.write(fd) def run(self): if os.path.exists(self.path_language): self.parse_file() if len(self.languages) > 0 and self.is_need_fix(): self.generate_lang_and_language() self.write_file() if __name__ == "__main__": parser = argparse.ArgumentParser() parser.add_argument('-f') arguments = vars(parser.parse_args()) if arguments['f'] != None: app = FixLanguage(arguments['f']) else: app = FixLanguage(os.environ['HOME']) app.run()