|
1 #============================================================================ |
|
2 #Name : api.py |
|
3 #Part of : Helium |
|
4 |
|
5 #Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
6 #All rights reserved. |
|
7 #This component and the accompanying materials are made available |
|
8 #under the terms of the License "Eclipse Public License v1.0" |
|
9 #which accompanies this distribution, and is available |
|
10 #at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
11 # |
|
12 #Initial Contributors: |
|
13 #Nokia Corporation - initial contribution. |
|
14 # |
|
15 #Contributors: |
|
16 # |
|
17 #Description: |
|
18 #=============================================================================== |
|
19 |
|
20 import logging |
|
21 logger = logging.getLogger("datasources.api") |
|
22 |
|
23 class MissingProperty(Exception): |
|
24 """ An exception to indicate about a missing property """ |
|
25 pass |
|
26 |
|
27 class DataSource(object): |
|
28 """ This abstract class defines a DataSource for the packager application. """ |
|
29 def __init__(self, epocroot, data=None): |
|
30 self.epocroot = epocroot |
|
31 self._data = data |
|
32 if data is None: |
|
33 self._data = {} |
|
34 |
|
35 def getComponents(self): |
|
36 """ The getComponents method must return a list of BuildData object (one per component). |
|
37 In case of error (e.g incomplete configuration) the method will raise an Exception. |
|
38 """ |
|
39 raise NotImplementedError |
|
40 |
|
41 def getHelp(self): |
|
42 return None |
|
43 |
|
44 help = property(lambda self: self.getHelp()) |
|
45 |
|
46 |
|
47 DATASOURCES = {} |
|
48 |
|
49 def getDataSource(name, epocroot, data): |
|
50 if name in DATASOURCES: |
|
51 logger.debug("Creating datasource for %s." % name) |
|
52 return DATASOURCES[name](epocroot, data) |
|
53 else: |
|
54 logger.info("Loading %s." % name) |
|
55 def class_import(name): |
|
56 try: |
|
57 components = name.split('.') |
|
58 klassname = components.pop() |
|
59 mod = __import__('.'.join(components), globals(), locals(), [klassname]) |
|
60 return getattr(mod, klassname) |
|
61 except: |
|
62 raise Exception("Could not load %s" % name) |
|
63 return class_import(name)(epocroot, data) |
|
64 |
|
65 |
|
66 def getDataSourceHelp(): |
|
67 doc = "" |
|
68 for name in DATASOURCES: |
|
69 dshelp = DATASOURCES[name](None, None).help |
|
70 if dshelp is not None: |
|
71 doc = doc + "--- %s -----------------------------------\n" % name + dshelp +\ |
|
72 "\n------------------------------------------\n" |
|
73 return doc |