Use custom exception type for unsupported format

Julien LepillerSat Oct 13 10:44:58+0200 2018

2594d5e

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

11
from pathlib import Path
22
from .systems.tp import TPProject
33
from .systems.transifex import TransifexProject
4+
from .formats.formatException import UnsupportedFormatException
45
56
import json
67
import os
78
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+
818
class ProjectSettings:
919
    def __init__(self, confdir):
1020
        self.confdir = confdir

5666
    def createProject(self, name, lang, system, data):
5767
        projectpath = self.basedir + '/' + name
5868
        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)
7588
        self.writeProjects()
7689
7790
    def update(self):

offlate/systems/transifex.py

55
import requests
66
from requests.auth import HTTPBasicAuth
77
from ..formats.yaml import YamlFormat
8+
from ..formats.formatException import UnsupportedFormatException
89
910
class TransifexProject:
1011
    def __init__(self, conf, name, lang, data={}):

5455
                oldformat = YamlFormat({'dest': fname+'.old', 'source': sname+'.old'})
5556
                currentformat = YamlFormat({'dest': fname, 'source': sname})
5657
            else:
57-
                raise Exception("Unsupported format: " + ff['i18n_type'])
58+
                raise UnsupportedFormatException(ff['i18n_type'])
5859
            currentformat.merge(oldformat, callback)
5960
6061
    def filename(self, slug, is_source):

6667
        if f['i18n_type'] == 'YML':
6768
            ext = 'yml'
6869
        else:
69-
            raise Exception("Unsupported format: " + f['i18n_type'])
70+
            raise UnsupportedFormatException(ff['i18n_type'])
7071
        return self.basedir + '/' + slug + ('.source' if is_source else '') + '.' + ext
7172
7273
    def getFiles(self, slug):

112113
                        {'dest':   self.filename(slug['slug'], False),
113114
                         'source': self.filename(slug['slug'], True)})
114115
            else:
115-
                raise Exception("Unsupported format: " + slug['i18n_type'])
116+
                raise UnsupportedFormatException(ff['i18n_type'])
116117
            self.slugs.append(myslug)
117118
            content[slug['slug']] = myslug.content()
118119
        return content

offlate/window.py

497497
            menu.addAction(act)
498498
499499
    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
501507
        tab = ProjectView(project,
502508
            showTranslated = self.showTranslatedAct.isChecked(),
503509
            showUntranslated = self.showUntranslatedAct.isChecked(),