3
|
1 |
#
|
|
2 |
# Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
3 |
# All rights reserved.
|
|
4 |
# This component and the accompanying materials are made available
|
|
5 |
# under the terms of "Eclipse Public License v1.0"
|
|
6 |
# which accompanies this distribution, and is available
|
|
7 |
# at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
|
8 |
#
|
|
9 |
# Initial Contributors:
|
|
10 |
# Nokia Corporation - initial contribution.
|
|
11 |
#
|
|
12 |
# Contributors:
|
|
13 |
#
|
|
14 |
# Description:
|
|
15 |
#
|
|
16 |
|
|
17 |
import sys
|
|
18 |
import logging
|
|
19 |
import inspect
|
|
20 |
|
|
21 |
def load_plugin_classes(entry_point_group, base_class):
|
|
22 |
"""
|
|
23 |
Load plugin classes from plug-in entry points.
|
|
24 |
|
|
25 |
@param entry_point_group: Entry point group from which to load
|
|
26 |
classes. Each entry point is expected to be an iterable of
|
|
27 |
plugin class instances.
|
|
28 |
@param base_class: The base class that every loaded class must inherit.
|
|
29 |
@return: List of loaded plugin classes.
|
|
30 |
"""
|
|
31 |
log = logging.getLogger('cone')
|
|
32 |
log.setLevel(logging.DEBUG)
|
|
33 |
validator_classes = []
|
|
34 |
|
|
35 |
import pkg_resources
|
|
36 |
working_set = pkg_resources.WorkingSet(sys.path)
|
|
37 |
for entry_point in working_set.iter_entry_points(entry_point_group):
|
|
38 |
class_list = entry_point.load()
|
|
39 |
|
|
40 |
# Make sure that the class list is a list
|
|
41 |
try:
|
|
42 |
class_list = [c for c in class_list]
|
|
43 |
except:
|
|
44 |
log.warn("Entry point %s:%s is not iterable (%r)" % (entry_point_group, entry_point.name, class_list))
|
|
45 |
continue
|
|
46 |
|
|
47 |
for i, cls in enumerate(class_list):
|
|
48 |
if not inspect.isclass(cls):
|
|
49 |
log.warn("Object %d from entry point %s:%s is not a class (%r)" % (i, entry_point_group, entry_point.name, cls))
|
|
50 |
elif not issubclass(cls, base_class):
|
|
51 |
log.warn("Object %d from entry point %s:%s is not a sub-class of %s.%s (%r)" \
|
|
52 |
% (i, entry_point, entry_point.name,
|
|
53 |
base_class.__module__,
|
|
54 |
base_class.__name__,
|
|
55 |
cls))
|
|
56 |
else:
|
|
57 |
msg = "Validator class %r loaded from egg entry point %s:%s, item %d" % (cls, entry_point_group, entry_point.name, i)
|
|
58 |
log.debug(msg)
|
|
59 |
#print msg
|
|
60 |
validator_classes.append(cls)
|
|
61 |
|
|
62 |
return validator_classes
|
|
63 |
|
|
64 |
def filter_classes(classes, filter):
|
|
65 |
"""
|
|
66 |
Filter the given list of validator by the given ProblemTypeFilter object.
|
|
67 |
|
|
68 |
@param classes: The class list for filter. Each
|
|
69 |
class is assumed to have a PROBLEM_TYPES attribute that defines
|
|
70 |
an iterable of the types of problems that the problem yields.
|
|
71 |
@param filter: The filter object to use. Can be None, in which case
|
|
72 |
the class list is simply returned back.
|
|
73 |
@return: The filtered list.
|
|
74 |
"""
|
|
75 |
if filter == None:
|
|
76 |
return classes
|
|
77 |
else:
|
|
78 |
result = []
|
|
79 |
for klass in classes:
|
|
80 |
for problem_type in klass.PROBLEM_TYPES:
|
|
81 |
if filter.match(problem_type):
|
|
82 |
result.append(klass)
|
|
83 |
break
|
|
84 |
return result
|