Move manager to its subdirectory
offlate/core/__init__.py unknown status 1
1 | + | # Copyright (c) 2020 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 | + | #### |
offlate/core/manager.py unknown status 1
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 .systems.gitlab import GitlabProject | |
21 | + | from .systems.github import GithubProject | |
22 | + | from .formats.exception import UnsupportedFormatException | |
23 | + | from .systems.list import * | |
24 | + | ||
25 | + | import json | |
26 | + | import os | |
27 | + | ||
28 | + | def rmdir(dir): | |
29 | + | dir = Path(dir) | |
30 | + | for item in dir.iterdir(): | |
31 | + | if item.is_dir(): | |
32 | + | rmdir(item) | |
33 | + | else: | |
34 | + | item.unlink() | |
35 | + | dir.rmdir() | |
36 | + | ||
37 | + | class ProjectSettings: | |
38 | + | def __init__(self, confdir): | |
39 | + | self.confdir = confdir | |
40 | + | self.reload() | |
41 | + | ||
42 | + | def write(self): | |
43 | + | with open(self.confdir + '/conf.json', 'w') as f: | |
44 | + | f.write(json.dumps(self.conf)) | |
45 | + | ||
46 | + | def reload(self): | |
47 | + | try: | |
48 | + | with open(self.confdir + '/conf.json') as f: | |
49 | + | self.conf = json.load(f) | |
50 | + | except Exception: | |
51 | + | with open(self.confdir + '/conf.json', 'w') as f: | |
52 | + | f.write(json.dumps({})) | |
53 | + | self.conf = {} | |
54 | + | ||
55 | + | class ProjectManager: | |
56 | + | def __init__(self): | |
57 | + | self.projects = [] | |
58 | + | self.project_list = dict() | |
59 | + | home = str(Path.home()) | |
60 | + | self.basedir = home + '/.local/share/offlate' | |
61 | + | self.confdir = home + '/.config/offlate' | |
62 | + | Path(self.basedir).mkdir(parents=True, exist_ok=True) | |
63 | + | Path(self.confdir).mkdir(parents=True, exist_ok=True) | |
64 | + | self.settings = ProjectSettings(self.confdir) | |
65 | + | try: | |
66 | + | with open(self.basedir + '/projects.json') as f: | |
67 | + | self.projects = json.load(f) | |
68 | + | for p in self.projects: | |
69 | + | proj = self.loadProject(p['name'], p['lang'], p['system'], | |
70 | + | p['info']) | |
71 | + | proj.open(self.basedir+'/'+p['name']) | |
72 | + | except Exception as e: | |
73 | + | print(e) | |
74 | + | with open(self.basedir + '/projects.json', 'w') as f: | |
75 | + | f.write(json.dumps([])) | |
76 | + | ||
77 | + | def createProject(self, name, lang, system, data, callback): | |
78 | + | callback.progress(0) | |
79 | + | ||
80 | + | projectpath = self.basedir + '/' + name | |
81 | + | path = Path(projectpath) | |
82 | + | if not path.exists(): | |
83 | + | path.mkdir(parents=True) | |
84 | + | else: | |
85 | + | if len([x for x in self.projects if x['name'] == name]) > 0: | |
86 | + | callback.project_exists() | |
87 | + | else: | |
88 | + | callback.project_present(projectpath) | |
89 | + | return False | |
90 | + | ||
91 | + | try: | |
92 | + | proj = self.loadProject(name, lang, system, data) | |
93 | + | proj.initialize(projectpath, callback) | |
94 | + | self.projects.append({"name": name, "lang": lang, "system": system, | |
95 | + | "info": data}) | |
96 | + | except Exception as e: | |
97 | + | callback.project_error(e) | |
98 | + | rmdir(projectpath) | |
99 | + | return False | |
100 | + | ||
101 | + | callback.progress(100) | |
102 | + | ||
103 | + | self.writeProjects() | |
104 | + | return True | |
105 | + | ||
106 | + | def loadProject(self, name, lang, system, data): | |
107 | + | if not "Generic" in self.settings.conf: | |
108 | + | self.settings.conf["Generic"] = {} | |
109 | + | ||
110 | + | version_file = open(os.path.join(os.path.dirname(os.path.realpath(__file__)), | |
111 | + | 'data/VERSION')) | |
112 | + | self.settings.conf["Generic"]["offlate_version"] = version_file.read().strip() | |
113 | + | ||
114 | + | if system == TRANSLATION_PROJECT: | |
115 | + | if not "TP" in self.settings.conf: | |
116 | + | self.settings.conf["TP"] = {} | |
117 | + | settings = self.settings.conf["TP"] | |
118 | + | for s in self.settings.conf["Generic"].keys(): | |
119 | + | settings[s] = self.settings.conf["Generic"][s] | |
120 | + | proj = TPProject(settings, name, lang, data) | |
121 | + | if system == TRANSIFEX: | |
122 | + | if not 'Transifex' in self.settings.conf: | |
123 | + | self.settings.conf['Transifex'] = {} | |
124 | + | settings = self.settings.conf["Transifex"] | |
125 | + | for s in self.settings.conf["Generic"].keys(): | |
126 | + | settings[s] = self.settings.conf["Generic"][s] | |
127 | + | proj = TransifexProject(settings, name, lang, data) | |
128 | + | if system == GITLAB: | |
129 | + | if not 'Gitlab' in self.settings.conf: | |
130 | + | self.settings.conf['Gitlab'] = {} | |
131 | + | settings = self.settings.conf["Gitlab"] | |
132 | + | for s in self.settings.conf["Generic"].keys(): | |
133 | + | settings[s] = self.settings.conf["Generic"][s] | |
134 | + | proj = GitlabProject(settings, name, lang, data) | |
135 | + | if system == GITHUB: | |
136 | + | if not 'Github' in self.settings.conf: | |
137 | + | self.settings.conf['Github'] = {} | |
138 | + | settings = self.settings.conf['Github'] | |
139 | + | for s in self.settings.conf["Generic"].keys(): | |
140 | + | settings[s] = self.settings.conf["Generic"][s] | |
141 | + | proj = GithubProject(settings, name, lang, data) | |
142 | + | self.project_list[name] = proj | |
143 | + | return proj | |
144 | + | ||
145 | + | def update(self): | |
146 | + | for p in self.projects: | |
147 | + | proj = self.project_list[p['name']] | |
148 | + | p['info'] = proj.data | |
149 | + | ||
150 | + | def writeProjects(self): | |
151 | + | with open(self.basedir + '/projects.json', 'w') as f: | |
152 | + | f.write(json.dumps(self.projects)) | |
153 | + | ||
154 | + | def listProjects(self): | |
155 | + | return self.projects | |
156 | + | ||
157 | + | def getProject(self, name): | |
158 | + | return self.project_list[name] | |
159 | + | ||
160 | + | def updateSettings(self, data=None): | |
161 | + | if data == None: | |
162 | + | self.settings.conf = data | |
163 | + | self.settings.update() | |
164 | + | else: | |
165 | + | self.settings.write() | |
166 | + | ||
167 | + | def getConf(self): | |
168 | + | return self.settings.conf | |
169 | + | ||
170 | + | def remove(self, name): | |
171 | + | rmdir(self.basedir + '/' + name) | |
172 | + | self.projects = [x for x in self.projects if x['name'] != name] | |
173 | + | self.writeProjects() | |
174 | + | ||
175 | + | def updateProject(self, name, lang, system, info, callback): | |
176 | + | try: | |
177 | + | rmdir(self.basedir + '/' + name) | |
178 | + | except: | |
179 | + | pass | |
180 | + | self.projects = [x for x in self.projects if x['name'] != name] | |
181 | + | return self.createProject(name, lang, system, info, callback) | |
182 | + | ||
183 | + | def isConfigured(self, system): | |
184 | + | if not "Generic" in self.settings.conf: | |
185 | + | self.settings.conf["Generic"] = {} | |
186 | + | if system == TRANSLATION_PROJECT: | |
187 | + | if not "TP" in self.settings.conf: | |
188 | + | self.settings.conf["TP"] = {} | |
189 | + | settings = self.settings.conf["TP"] | |
190 | + | for s in self.settings.conf["Generic"].keys(): | |
191 | + | if not s in self.settings.conf["Generic"]: | |
192 | + | self.settings.conf["Generic"][s] = None | |
193 | + | settings[s] = self.settings.conf["Generic"][s] | |
194 | + | return TPProject.isConfigured(settings) | |
195 | + | elif system == TRANSIFEX: | |
196 | + | if not "Transifex" in self.settings.conf: | |
197 | + | self.settings.conf["Transifex"] = {} | |
198 | + | settings = self.settings.conf["Transifex"] | |
199 | + | for s in self.settings.conf["Generic"].keys(): | |
200 | + | if not s in self.settings.conf["Generic"]: | |
201 | + | self.settings.conf["Generic"][s] = None | |
202 | + | settings[s] = self.settings.conf["Generic"][s] | |
203 | + | return TransifexProject.isConfigured(settings) | |
204 | + | elif system == GITLAB: | |
205 | + | if not "Gitlab" in self.settings.conf: | |
206 | + | self.settings.conf["Gitlab"] = {} | |
207 | + | settings = self.settings.conf["Gitlab"] | |
208 | + | for s in self.settings.conf["Generic"].keys(): | |
209 | + | if not s in self.settings.conf["Generic"]: | |
210 | + | self.settings.conf["Generic"][s] = None | |
211 | + | settings[s] = self.settings.conf["Generic"][s] | |
212 | + | return GitlabProject.isConfigured(settings) | |
213 | + | elif system == GITHUB: | |
214 | + | if not "Github" in self.settings.conf: | |
215 | + | self.settings.conf["Github"] = {} | |
216 | + | settings = self.settings.conf["Github"] | |
217 | + | for s in self.settings.conf["Generic"].keys(): | |
218 | + | if not s in self.settings.conf["Generic"]: | |
219 | + | self.settings.conf["Generic"][s] = None | |
220 | + | settings[s] = self.settings.conf["Generic"][s] | |
221 | + | return GithubProject.isConfigured(settings) | |
222 | + | else: | |
223 | + | return False |
offlate/manager.py unknown status 2
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 .systems.gitlab import GitlabProject | |
21 | - | from .systems.github import GithubProject | |
22 | - | from .formats.exception import UnsupportedFormatException | |
23 | - | from .systems.list import * | |
24 | - | ||
25 | - | import json | |
26 | - | import os | |
27 | - | ||
28 | - | def rmdir(dir): | |
29 | - | dir = Path(dir) | |
30 | - | for item in dir.iterdir(): | |
31 | - | if item.is_dir(): | |
32 | - | rmdir(item) | |
33 | - | else: | |
34 | - | item.unlink() | |
35 | - | dir.rmdir() | |
36 | - | ||
37 | - | class ProjectSettings: | |
38 | - | def __init__(self, confdir): | |
39 | - | self.confdir = confdir | |
40 | - | self.reload() | |
41 | - | ||
42 | - | def write(self): | |
43 | - | with open(self.confdir + '/conf.json', 'w') as f: | |
44 | - | f.write(json.dumps(self.conf)) | |
45 | - | ||
46 | - | def reload(self): | |
47 | - | try: | |
48 | - | with open(self.confdir + '/conf.json') as f: | |
49 | - | self.conf = json.load(f) | |
50 | - | except Exception: | |
51 | - | with open(self.confdir + '/conf.json', 'w') as f: | |
52 | - | f.write(json.dumps({})) | |
53 | - | self.conf = {} | |
54 | - | ||
55 | - | class ProjectManager: | |
56 | - | def __init__(self): | |
57 | - | self.projects = [] | |
58 | - | self.project_list = dict() | |
59 | - | home = str(Path.home()) | |
60 | - | self.basedir = home + '/.local/share/offlate' | |
61 | - | self.confdir = home + '/.config/offlate' | |
62 | - | Path(self.basedir).mkdir(parents=True, exist_ok=True) | |
63 | - | Path(self.confdir).mkdir(parents=True, exist_ok=True) | |
64 | - | self.settings = ProjectSettings(self.confdir) | |
65 | - | try: | |
66 | - | with open(self.basedir + '/projects.json') as f: | |
67 | - | self.projects = json.load(f) | |
68 | - | for p in self.projects: | |
69 | - | proj = self.loadProject(p['name'], p['lang'], p['system'], | |
70 | - | p['info']) | |
71 | - | proj.open(self.basedir+'/'+p['name']) | |
72 | - | except Exception as e: | |
73 | - | print(e) | |
74 | - | with open(self.basedir + '/projects.json', 'w') as f: | |
75 | - | f.write(json.dumps([])) | |
76 | - | ||
77 | - | def createProject(self, name, lang, system, data, callback): | |
78 | - | callback.progress(0) | |
79 | - | ||
80 | - | projectpath = self.basedir + '/' + name | |
81 | - | path = Path(projectpath) | |
82 | - | if not path.exists(): | |
83 | - | path.mkdir(parents=True) | |
84 | - | else: | |
85 | - | if len([x for x in self.projects if x['name'] == name]) > 0: | |
86 | - | callback.project_exists() | |
87 | - | else: | |
88 | - | callback.project_present(projectpath) | |
89 | - | return False | |
90 | - | ||
91 | - | try: | |
92 | - | proj = self.loadProject(name, lang, system, data) | |
93 | - | proj.initialize(projectpath, callback) | |
94 | - | self.projects.append({"name": name, "lang": lang, "system": system, | |
95 | - | "info": data}) | |
96 | - | except Exception as e: | |
97 | - | callback.project_error(e) | |
98 | - | rmdir(projectpath) | |
99 | - | return False | |
100 | - | ||
101 | - | callback.progress(100) | |
102 | - | ||
103 | - | self.writeProjects() | |
104 | - | return True | |
105 | - | ||
106 | - | def loadProject(self, name, lang, system, data): | |
107 | - | if not "Generic" in self.settings.conf: | |
108 | - | self.settings.conf["Generic"] = {} | |
109 | - | ||
110 | - | version_file = open(os.path.join(os.path.dirname(os.path.realpath(__file__)), | |
111 | - | 'data/VERSION')) | |
112 | - | self.settings.conf["Generic"]["offlate_version"] = version_file.read().strip() | |
113 | - | ||
114 | - | if system == TRANSLATION_PROJECT: | |
115 | - | if not "TP" in self.settings.conf: | |
116 | - | self.settings.conf["TP"] = {} | |
117 | - | settings = self.settings.conf["TP"] | |
118 | - | for s in self.settings.conf["Generic"].keys(): | |
119 | - | settings[s] = self.settings.conf["Generic"][s] | |
120 | - | proj = TPProject(settings, name, lang, data) | |
121 | - | if system == TRANSIFEX: | |
122 | - | if not 'Transifex' in self.settings.conf: | |
123 | - | self.settings.conf['Transifex'] = {} | |
124 | - | settings = self.settings.conf["Transifex"] | |
125 | - | for s in self.settings.conf["Generic"].keys(): | |
126 | - | settings[s] = self.settings.conf["Generic"][s] | |
127 | - | proj = TransifexProject(settings, name, lang, data) | |
128 | - | if system == GITLAB: | |
129 | - | if not 'Gitlab' in self.settings.conf: | |
130 | - | self.settings.conf['Gitlab'] = {} | |
131 | - | settings = self.settings.conf["Gitlab"] | |
132 | - | for s in self.settings.conf["Generic"].keys(): | |
133 | - | settings[s] = self.settings.conf["Generic"][s] | |
134 | - | proj = GitlabProject(settings, name, lang, data) | |
135 | - | if system == GITHUB: | |
136 | - | if not 'Github' in self.settings.conf: | |
137 | - | self.settings.conf['Github'] = {} | |
138 | - | settings = self.settings.conf['Github'] | |
139 | - | for s in self.settings.conf["Generic"].keys(): | |
140 | - | settings[s] = self.settings.conf["Generic"][s] | |
141 | - | proj = GithubProject(settings, name, lang, data) | |
142 | - | self.project_list[name] = proj | |
143 | - | return proj | |
144 | - | ||
145 | - | def update(self): | |
146 | - | for p in self.projects: | |
147 | - | proj = self.project_list[p['name']] | |
148 | - | p['info'] = proj.data | |
149 | - | ||
150 | - | def writeProjects(self): | |
151 | - | with open(self.basedir + '/projects.json', 'w') as f: | |
152 | - | f.write(json.dumps(self.projects)) | |
153 | - | ||
154 | - | def listProjects(self): | |
155 | - | return self.projects | |
156 | - | ||
157 | - | def getProject(self, name): | |
158 | - | return self.project_list[name] | |
159 | - | ||
160 | - | def updateSettings(self, data=None): | |
161 | - | if data == None: | |
162 | - | self.settings.conf = data | |
163 | - | self.settings.update() | |
164 | - | else: | |
165 | - | self.settings.write() | |
166 | - | ||
167 | - | def getConf(self): | |
168 | - | return self.settings.conf | |
169 | - | ||
170 | - | def remove(self, name): | |
171 | - | rmdir(self.basedir + '/' + name) | |
172 | - | self.projects = [x for x in self.projects if x['name'] != name] | |
173 | - | self.writeProjects() | |
174 | - | ||
175 | - | def updateProject(self, name, lang, system, info, callback): | |
176 | - | try: | |
177 | - | rmdir(self.basedir + '/' + name) | |
178 | - | except: | |
179 | - | pass | |
180 | - | self.projects = [x for x in self.projects if x['name'] != name] | |
181 | - | return self.createProject(name, lang, system, info, callback) | |
182 | - | ||
183 | - | def isConfigured(self, system): | |
184 | - | if not "Generic" in self.settings.conf: | |
185 | - | self.settings.conf["Generic"] = {} | |
186 | - | if system == TRANSLATION_PROJECT: | |
187 | - | if not "TP" in self.settings.conf: | |
188 | - | self.settings.conf["TP"] = {} | |
189 | - | settings = self.settings.conf["TP"] | |
190 | - | for s in self.settings.conf["Generic"].keys(): | |
191 | - | if not s in self.settings.conf["Generic"]: | |
192 | - | self.settings.conf["Generic"][s] = None | |
193 | - | settings[s] = self.settings.conf["Generic"][s] | |
194 | - | return TPProject.isConfigured(settings) | |
195 | - | elif system == TRANSIFEX: | |
196 | - | if not "Transifex" in self.settings.conf: | |
197 | - | self.settings.conf["Transifex"] = {} | |
198 | - | settings = self.settings.conf["Transifex"] | |
199 | - | for s in self.settings.conf["Generic"].keys(): | |
200 | - | if not s in self.settings.conf["Generic"]: | |
201 | - | self.settings.conf["Generic"][s] = None | |
202 | - | settings[s] = self.settings.conf["Generic"][s] | |
203 | - | return TransifexProject.isConfigured(settings) | |
204 | - | elif system == GITLAB: | |
205 | - | if not "Gitlab" in self.settings.conf: | |
206 | - | self.settings.conf["Gitlab"] = {} | |
207 | - | settings = self.settings.conf["Gitlab"] | |
208 | - | for s in self.settings.conf["Generic"].keys(): | |
209 | - | if not s in self.settings.conf["Generic"]: | |
210 | - | self.settings.conf["Generic"][s] = None | |
211 | - | settings[s] = self.settings.conf["Generic"][s] | |
212 | - | return GitlabProject.isConfigured(settings) | |
213 | - | elif system == GITHUB: | |
214 | - | if not "Github" in self.settings.conf: | |
215 | - | self.settings.conf["Github"] = {} | |
216 | - | settings = self.settings.conf["Github"] | |
217 | - | for s in self.settings.conf["Generic"].keys(): | |
218 | - | if not s in self.settings.conf["Generic"]: | |
219 | - | self.settings.conf["Generic"][s] = None | |
220 | - | settings[s] = self.settings.conf["Generic"][s] | |
221 | - | return GithubProject.isConfigured(settings) | |
222 | - | else: | |
223 | - | return False |
offlate/ui/manager.py
28 | 28 | from .editor import EditorWindow | |
29 | 29 | from .parallel import RunnableCallback, RunnableSignals | |
30 | 30 | ||
31 | - | from ..manager import ProjectManager | |
31 | + | from ..core.manager import ProjectManager | |
32 | 32 | ||
33 | 33 | from ..formats.exception import UnsupportedFormatException | |
34 | 34 | from ..systems.systemException import ProjectNotFoundSystemException |