1 | # Officeshots.org - Test your office documents in different applications |
---|
2 | # Copyright (C) 2009 Stichting Lone Wolves |
---|
3 | # Written by Sander Marechal <s.marechal@jejik.com> |
---|
4 | # |
---|
5 | # This program is free software: you can redistribute it and/or modify |
---|
6 | # it under the terms of the GNU Affero General Public License as |
---|
7 | # published by the Free Software Foundation, either version 3 of the |
---|
8 | # License, or (at your option) any later version. |
---|
9 | # |
---|
10 | # This program is distributed in the hope that it will be useful, |
---|
11 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
12 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
13 | # GNU Affero General Public License for more details. |
---|
14 | # |
---|
15 | # You should have received a copy of the GNU Affero General Public License |
---|
16 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
---|
17 | |
---|
18 | """ |
---|
19 | The core backend class |
---|
20 | """ |
---|
21 | |
---|
22 | class BackendException: |
---|
23 | """ |
---|
24 | A base Backend exception class. Backends should derive their own |
---|
25 | exceptions from this. |
---|
26 | |
---|
27 | When the Factory catches this exception and recoverable is False, then |
---|
28 | the backend will be unloaded. |
---|
29 | """ |
---|
30 | def __init__(self, message, recoverable = False): |
---|
31 | self.message = message |
---|
32 | self.recoverable = recoverable |
---|
33 | |
---|
34 | def __str__(self): |
---|
35 | if self.recoverable: |
---|
36 | return self.message + ' (Recoverable)' |
---|
37 | return self.message + ' (Not recoverable)' |
---|
38 | |
---|
39 | class Backend: |
---|
40 | """ |
---|
41 | The base backend class. Backends shoudl derive from this |
---|
42 | """ |
---|
43 | def __init__(self, options, config, section): |
---|
44 | self.options = options |
---|
45 | self.config = config |
---|
46 | self.section = section |
---|
47 | |
---|
48 | self.application = config.get(section, 'application') |
---|
49 | self.major = config.get(section, 'major') |
---|
50 | self.minor = config.get(section, 'minor') |
---|
51 | self.doctype = config.get(section, 'doctype') |
---|
52 | self.formats = [s.strip() for s in config.get(section, 'formats').split(',')] |
---|
53 | |
---|
54 | def initialize(self): |
---|
55 | """ |
---|
56 | This is called right after instanciating the backend. |
---|
57 | Returning False will cause the backend not to be loaded |
---|
58 | """ |
---|
59 | return True |
---|
60 | |
---|
61 | def can_process(self, job): |
---|
62 | """ |
---|
63 | Return True if this backend is eligable to process this Job, False otherwise |
---|
64 | """ |
---|
65 | eligable = ( |
---|
66 | job['application'] == self.application and |
---|
67 | job['major'] == self.major and |
---|
68 | job['minor'] == self.minor and |
---|
69 | job['doctype'] == self.doctype and |
---|
70 | job['format'] in self.formats |
---|
71 | ) |
---|
72 | return eligable |
---|
73 | |
---|
74 | def process(self, job): |
---|
75 | """ |
---|
76 | Process a job. Backends must override this method. |
---|
77 | """ |
---|
78 | raise NotImplementedError |
---|