summaryrefslogtreecommitdiff
path: root/Redim/config.py
blob: 5d604f030f6d2eb48f3e2e282a463b79f73ba45c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
"""Contient la gestion de l'enregistrement des choix de l'utilisateur
ainsi que la gestion de la supression des fichiers anciennement décompressés
par pyinstaller"""

from os import mkdir, listdir, environ, getenv, system
from os.path import isdir, join, getmtime, isfile
from time import time
from shutil import rmtree
import sys
import json
import logging
import traceback


class Config():
    """Gestion de l'enregistrement des choix de l'utilisateur
    """
    def __init__(self):
        self.base_configuration = {
            "version": 1.0,
            "dimensions": [[500, 350], [800, 800]],
            "background": [255, 255, 255],
            "transparence": False,
            "formats_acceptes": (
                "webp",
                "png",
                "jpg",
                "jpeg",
                "bmp",
                "gif"
            ),
            "formats_possibles": (
                "webp",
                "png",
                "jpg"
            ),
            "format_choisi": ""
        }
        self.user_profile = getenv("USERPROFILE")
        if sys.platform != "linux":
            self.json_path = join(
                self.user_profile,
                "AppData",
                "Local",
                "Redim"
            )
        else:
            self.json_path = "."
        if sys.platform != "linux":
            self.nettoyage_pyinstaller()

    def sauvegarde(self, configuration):
        """Création du dossier de sauvegarde pour le fichier de configuration
        et enregistrement des préférence de l'utilisateur
        """
        if not isdir(self.json_path):
            mkdir(self.json_path)
        with open(join(self.json_path, "config"), "w") as file_config:
            json.dump(configuration, file_config)
        print("\n[-] Modification enregistree.")

    def lecture(self):
        """Return le contenu du fichier de configuration, si aucun fichier
        n'éxiste, création d'un avec les valeurs par défaut
        """
        if not isfile(join(self.json_path, "config")):
            self.sauvegarde(self.base_configuration)
            return self.base_configuration
        with open(join(self.json_path, "config"), "r") as file_config:
            configuration = json.load(file_config)
        return configuration

    def reset(self):
        """Suppression et réenregistrement de la configuration par défaut si
        le dossier de configuration existe
        """
        if isdir(self.json_path):
            rmtree(self.json_path)
            self.sauvegarde(self.base_configuration)

    @staticmethod
    def excepthook(type, value, error_traceback):
        """Système de journalisation en cas de plantage.
        """
        logging.error(
            "\nType: " + type.__name__ + "\n"
            + "Info: " + str(value) + "\n\n"
            + "".join(traceback.format_tb(error_traceback))
            + "\n==============================="
        )
        print(
            "\nTraceback:\n\n"
            + "Type: " + type.__name__ + "\n"
            + "Info: " + str(value) + "\n\n"
            + "".join(traceback.format_tb(error_traceback)) + "\n"
        )
        sys.exit(1)

    @staticmethod
    def nettoyage_pyinstaller():
        """Supression des fichiers anciennement décompressés par pyinstaller
        """
        for i in listdir(environ["TMP"]):
            if i.startswith("_MEI")\
                and isdir(i)\
                    and (
                            int(getmtime(join(environ["TMP"], i)))
                            < (time() - 86400)):
                rmtree(join(environ["TMP"], i))

    @staticmethod
    def redimensionnement_fenetre():
        """Redimensionne la fenetre du CMD sous windows
        """
        if sys.platform != "linux":
            system("mode con: cols=70 lines=35")