Use custom exception type for unsupported format
offlate/formats/formatException.py unknown status 1
1 | + | class UnsupportedFormatException(Exception): | |
2 | + | def __init__(self, message): | |
3 | + | super().__init__('Unsupported format: '+message) |
offlate/manager.py
1 | 1 | from pathlib import Path | |
2 | 2 | from .systems.tp import TPProject | |
3 | 3 | from .systems.transifex import TransifexProject | |
4 | + | from .formats.formatException import UnsupportedFormatException | |
4 | 5 | ||
5 | 6 | import json | |
6 | 7 | import os | |
7 | 8 | ||
9 | + | def rmdir(dir): | |
10 | + | dir = Path(dir) | |
11 | + | for item in dir.iterdir(): | |
12 | + | if item.is_dir(): | |
13 | + | rmdir(item) | |
14 | + | else: | |
15 | + | item.unlink() | |
16 | + | dir.rmdir() | |
17 | + | ||
8 | 18 | class ProjectSettings: | |
9 | 19 | def __init__(self, confdir): | |
10 | 20 | self.confdir = confdir | |
… | |||
56 | 66 | def createProject(self, name, lang, system, data): | |
57 | 67 | projectpath = self.basedir + '/' + name | |
58 | 68 | Path(projectpath).mkdir(parents=True) | |
59 | - | if system == 0: #TP | |
60 | - | if not "TP" in self.settings.conf: | |
61 | - | self.settings.conf["TP"] = {} | |
62 | - | proj = TPProject(self.settings.conf["TP"], name, lang) | |
63 | - | proj.initialize(projectpath) | |
64 | - | self.project_list[name] = proj | |
65 | - | self.projects.append({"name": name, "lang": lang, "system": system, | |
66 | - | "info": {"version": proj.version}}) | |
67 | - | if system == 1: #Transifex | |
68 | - | if not 'Transifex' in self.settings.conf: | |
69 | - | self.settings.conf['Transifex'] = {} | |
70 | - | proj = TransifexProject(self.settings.conf['Transifex'], name, lang, data) | |
71 | - | proj.initialize(projectpath) | |
72 | - | self.project_list[name] = proj | |
73 | - | self.projects.append({"name": name, "lang": lang, "system": system, | |
74 | - | "info": data}) | |
69 | + | try: | |
70 | + | if system == 0: #TP | |
71 | + | if not "TP" in self.settings.conf: | |
72 | + | self.settings.conf["TP"] = {} | |
73 | + | proj = TPProject(self.settings.conf["TP"], name, lang) | |
74 | + | proj.initialize(projectpath) | |
75 | + | self.project_list[name] = proj | |
76 | + | self.projects.append({"name": name, "lang": lang, "system": system, | |
77 | + | "info": {"version": proj.version}}) | |
78 | + | if system == 1: #Transifex | |
79 | + | if not 'Transifex' in self.settings.conf: | |
80 | + | self.settings.conf['Transifex'] = {} | |
81 | + | proj = TransifexProject(self.settings.conf['Transifex'], name, lang, data) | |
82 | + | proj.initialize(projectpath) | |
83 | + | self.project_list[name] = proj | |
84 | + | self.projects.append({"name": name, "lang": lang, "system": system, | |
85 | + | "info": data}) | |
86 | + | except UnsupportedFormatException: | |
87 | + | rmdir(projectpath) | |
75 | 88 | self.writeProjects() | |
76 | 89 | ||
77 | 90 | def update(self): |
offlate/systems/transifex.py
5 | 5 | import requests | |
6 | 6 | from requests.auth import HTTPBasicAuth | |
7 | 7 | from ..formats.yaml import YamlFormat | |
8 | + | from ..formats.formatException import UnsupportedFormatException | |
8 | 9 | ||
9 | 10 | class TransifexProject: | |
10 | 11 | def __init__(self, conf, name, lang, data={}): | |
… | |||
54 | 55 | oldformat = YamlFormat({'dest': fname+'.old', 'source': sname+'.old'}) | |
55 | 56 | currentformat = YamlFormat({'dest': fname, 'source': sname}) | |
56 | 57 | else: | |
57 | - | raise Exception("Unsupported format: " + ff['i18n_type']) | |
58 | + | raise UnsupportedFormatException(ff['i18n_type']) | |
58 | 59 | currentformat.merge(oldformat, callback) | |
59 | 60 | ||
60 | 61 | def filename(self, slug, is_source): | |
… | |||
66 | 67 | if f['i18n_type'] == 'YML': | |
67 | 68 | ext = 'yml' | |
68 | 69 | else: | |
69 | - | raise Exception("Unsupported format: " + f['i18n_type']) | |
70 | + | raise UnsupportedFormatException(ff['i18n_type']) | |
70 | 71 | return self.basedir + '/' + slug + ('.source' if is_source else '') + '.' + ext | |
71 | 72 | ||
72 | 73 | def getFiles(self, slug): | |
… | |||
112 | 113 | {'dest': self.filename(slug['slug'], False), | |
113 | 114 | 'source': self.filename(slug['slug'], True)}) | |
114 | 115 | else: | |
115 | - | raise Exception("Unsupported format: " + slug['i18n_type']) | |
116 | + | raise UnsupportedFormatException(ff['i18n_type']) | |
116 | 117 | self.slugs.append(myslug) | |
117 | 118 | content[slug['slug']] = myslug.content() | |
118 | 119 | return content |
offlate/window.py
497 | 497 | menu.addAction(act) | |
498 | 498 | ||
499 | 499 | def open(self, name): | |
500 | - | project = self.manager.getProject(name) | |
500 | + | try: | |
501 | + | project = self.manager.getProject(name) | |
502 | + | except Exception: | |
503 | + | self.qd = QErrorMessage() | |
504 | + | self.qd.showMessage(self.tr("Unsupported / Unknown project")) | |
505 | + | self.qd.exec_() | |
506 | + | return | |
501 | 507 | tab = ProjectView(project, | |
502 | 508 | showTranslated = self.showTranslatedAct.isChecked(), | |
503 | 509 | showUntranslated = self.showUntranslatedAct.isChecked(), |