offlate/offlate/manager.py

manager.py

1
#   Copyright (c) 2018 Julien Lepiller <julien@lepiller.eu>
2
#
3
#   This program is free software: you can redistribute it and/or modify
4
#   it under the terms of the GNU Affero General Public License as
5
#   published by the Free Software Foundation, either version 3 of the
6
#   License, or (at your option) any later version.
7
#
8
#   This program is distributed in the hope that it will be useful,
9
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
10
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
#   GNU Affero General Public License for more details.
12
#
13
#   You should have received a copy of the GNU Affero General Public License
14
#   along with this program.  If not, see <https://www.gnu.org/licenses/>.
15
####
16
17
from pathlib import Path
18
from .systems.tp import TPProject
19
from .systems.transifex import TransifexProject
20
from .formats.formatException import UnsupportedFormatException
21
22
import json
23
import os
24
25
def rmdir(dir):
26
    dir = Path(dir)
27
    for item in dir.iterdir():
28
        if item.is_dir():
29
            rmdir(item)
30
        else:
31
            item.unlink()
32
    dir.rmdir()
33
34
class ProjectSettings:
35
    def __init__(self, confdir):
36
        self.confdir = confdir
37
        self.reload()
38
39
    def write(self):
40
        with open(self.confdir + '/conf.json', 'w') as f:
41
            f.write(json.dumps(self.conf))
42
43
    def reload(self):
44
        try:
45
            with open(self.confdir + '/conf.json') as f:
46
                self.conf = json.load(f)
47
        except Exception:
48
            with open(self.confdir + '/conf.json', 'w') as f:
49
                f.write(json.dumps({}))
50
51
class ProjectManager:
52
    def __init__(self):
53
        self.projects = []
54
        self.project_list = dict()
55
        home = str(Path.home())
56
        self.basedir = home + '/.local/share/offlate'
57
        self.confdir = home + '/.config/offlate'
58
        Path(self.basedir).mkdir(parents=True, exist_ok=True)
59
        Path(self.confdir).mkdir(parents=True, exist_ok=True)
60
        self.settings = ProjectSettings(self.confdir)
61
        try:
62
            with open(self.basedir + '/projects.json') as f:
63
                self.projects = json.load(f)
64
                for p in self.projects:
65
                    if p['system'] == 0:
66
                        if not "TP" in self.settings.conf:
67
                            self.settings.conf["TP"] = {}
68
                        self.project_list[p['name']] = \
69
                            TPProject(self.settings.conf["TP"], p['name'], p['lang'], p['info'])
70
                        self.project_list[p['name']].open(self.basedir+'/'+p['name'])
71
                    if p['system'] == 1:
72
                        if not "Transifex" in self.settings.conf:
73
                            self.settings.conf['Transifex'] = {}
74
                        self.project_list[p['name']] = \
75
                            TransifexProject(self.settings.conf['Transifex'], p['name'], p['lang'], p['info'])
76
                        self.project_list[p['name']].open(self.basedir+'/'+p['name'])
77
        except Exception as e:
78
            print(e)
79
            with open(self.basedir + '/projects.json', 'w') as f:
80
                f.write(json.dumps([]))
81
82
    def createProject(self, name, lang, system, data):
83
        projectpath = self.basedir + '/' + name
84
        Path(projectpath).mkdir(parents=True)
85
        try:
86
            if system == 0: #TP
87
                if not "TP" in self.settings.conf:
88
                    self.settings.conf["TP"] = {}
89
                proj = TPProject(self.settings.conf["TP"], name, lang)
90
                proj.initialize(projectpath)
91
                self.project_list[name] = proj
92
                self.projects.append({"name": name, "lang": lang, "system": system,
93
                        "info": {"version": proj.version}})
94
            if system == 1: #Transifex
95
                if not 'Transifex' in self.settings.conf:
96
                    self.settings.conf['Transifex'] = {}
97
                proj = TransifexProject(self.settings.conf['Transifex'], name, lang, data)
98
                proj.initialize(projectpath)
99
                self.project_list[name] = proj
100
                self.projects.append({"name": name, "lang": lang, "system": system,
101
                        "info": data})
102
        except UnsupportedFormatException:
103
            rmdir(projectpath)
104
        self.writeProjects()
105
106
    def update(self):
107
        for p in self.projects:
108
            proj = self.project_list[p['name']]
109
            p['info'] = proj.data
110
111
    def writeProjects(self):
112
        with open(self.basedir + '/projects.json', 'w') as f:
113
            f.write(json.dumps(self.projects))
114
115
    def listProjects(self):
116
        return self.projects
117
118
    def getProject(self, name):
119
        return self.project_list[name]
120
121
    def updateSettings(self, data=None):
122
        if data == None:
123
            self.settings.conf = data
124
            self.settings.update()
125
        else:
126
            self.settings.write()
127
128
    def getConf(self):
129
        return self.settings.conf
130