33import importlib
44import os
55
6+ # TODO: this should be removed
67OPSPlugin = collections .namedtuple (
78 "OPSPlugin" , ['name' , 'location' , 'func' , 'section' , 'plugin_type' ]
89)
910
11+ class PluginRegistrationError (RuntimeError ):
12+ pass
1013
14+ # TODO: make more generic than OPS (requires_ops => requires_lib)
1115class Plugin (object ):
1216 """Generic OPS plugin object"""
1317 def __init__ (self , requires_ops , requires_cli ):
1418 self .requires_ops = requires_ops
1519 self .requires_cli = requires_cli
20+ self .location = None
21+ self .plugin_type = None
22+
23+ def attach_metadata (self , location , plugin_type ):
24+ # error is already registered and data doesn't match
25+ error_condition = (
26+ (self .location is not None or self .plugin_type is not None )
27+ and (self .location != location
28+ or self .plugin_type != plugin_type )
29+ )
30+ if error_condition : # -no-cov-
31+ raise PluginRegistrationError (
32+ "The plugin " + repr (self ) + "has been previously "
33+ "registered with different metadata."
34+ )
35+ self .location = location
36+ self .plugin_type = plugin_type
1637
1738
18- class CommandPlugin (Plugin ):
39+ class OPSCommandPlugin (Plugin ):
1940 """Plugin for subcommands to the OPS CLI"""
2041 def __init__ (self , command , section , requires_ops = (1 , 0 ),
21- requires_cli = (0 , 1 )):
42+ requires_cli = (0 , 4 )):
2243 self .command = command
2344 self .section = section
2445 super ().__init__ (requires_ops , requires_cli )
@@ -27,20 +48,29 @@ def __init__(self, command, section, requires_ops=(1, 0),
2748 def name (self ):
2849 return self .command .name
2950
51+ @property
52+ def func (self ):
53+ # TODO: this is temporary to minimally change the API
54+ # (this is what calling functions ask for
55+ return self .command
56+
57+ def __repr__ (self ):
58+ return "OPSCommandPlugin(" + self .name + ")"
59+
3060
31- class ParserPlugin (Plugin ):
61+ class OPSParserPlugin (Plugin ):
3262 """Plugin to add a new Parser (top-level stage in YAML parsing"""
33- def __init__ (self , parser , requires_ops = (1 ,0 ), requires_cli = (0 ,3 )):
63+ def __init__ (self , parser , requires_ops = (1 , 0 ), requires_cli = (0 , 4 )):
3464 self .parser = parser
3565 super ().__init__ (requires_ops , requires_cli )
3666
3767
38- class InstanceBuilderPlugin (Plugin ):
68+ class OPSInstanceBuilderPlugin (Plugin ):
3969 """
4070 Plugin to add a new object type (InstanceBuilder) to YAML parsing.
4171 """
42- def __init__ (self , yaml_name , instance_builder , requires_ops = (1 ,0 ),
43- requires_cli = (0 ,3 )):
72+ def __init__ (self , yaml_name , instance_builder , requires_ops = (1 , 0 ),
73+ requires_cli = (0 , 3 )):
4474 self .yaml_name = yaml_name
4575 self .instance_builder = instance_builder
4676 super ().__init__ (requires_ops , requires_cli )
@@ -60,17 +90,20 @@ class CLIPluginLoader(object):
6090 Details on steps 1, 2, and 4 differ based on whether this is a
6191 filesystem-based plugin or a namespace-based plugin.
6292 """
63- def __init__ (self , plugin_type , search_path ):
93+ def __init__ (self , plugin_type , search_path , plugin_class = Plugin ):
6494 self .plugin_type = plugin_type
6595 self .search_path = search_path
96+ self .plugin_class = plugin_class
6697
98+ # TODO: this should be _find_candidate_modules
6799 def _find_candidates (self ):
68100 raise NotImplementedError ()
69101
70102 @staticmethod
71103 def _make_nsdict (candidate ):
72104 raise NotImplementedError ()
73105
106+ # TODO: this should validate with an isinstance of the plugin class
74107 @staticmethod
75108 def _validate (nsdict ):
76109 for attr in ['CLI' , 'SECTION' ]:
@@ -81,24 +114,43 @@ def _validate(nsdict):
81114 def _get_command_name (self , candidate ):
82115 raise NotImplementedError ()
83116
117+ # TODO: this should return the actual plugin objects
84118 def _find_valid (self ):
85119 candidates = self ._find_candidates ()
86120 namespaces = {cand : self ._make_nsdict (cand ) for cand in candidates }
87121 valid = {cand : ns for cand , ns in namespaces .items ()
88122 if self ._validate (ns )}
89123 return valid
90124
125+ def _find_candidate_namespaces (self ):
126+ candidates = self ._find_candidates ()
127+ namespaces = {cand : self ._make_nsdict (cand ) for cand in candidates }
128+ return namespaces
129+
130+ def _is_my_plugin (self , obj ):
131+ return isinstance (obj , self .plugin_class )
132+
133+ def _find_plugins (self , namespaces ):
134+ for loc , ns in namespaces .items ():
135+ for obj in ns .values ():
136+ if self ._is_my_plugin (obj ):
137+ obj .attach_metadata (loc , self .plugin_type )
138+ yield obj
139+
91140 def __call__ (self ):
92- valid = self ._find_valid ()
93- plugins = [
94- OPSPlugin (name = self ._get_command_name (cand ),
95- location = cand ,
96- func = ns ['CLI' ],
97- section = ns ['SECTION' ],
98- plugin_type = self .plugin_type )
99- for cand , ns in valid .items ()
100- ]
141+ namespaces = self ._find_candidate_namespaces ()
142+ plugins = list (self ._find_plugins (namespaces ))
101143 return plugins
144+ # valid = self._find_valid()
145+ # plugins = [
146+ # OPSPlugin(name=self._get_command_name(cand),
147+ # location=cand,
148+ # func=ns['CLI'],
149+ # section=ns['SECTION'],
150+ # plugin_type=self.plugin_type)
151+ # for cand, ns in valid.items()
152+ # ]
153+ # return plugins
102154
103155
104156class FilePluginLoader (CLIPluginLoader ):
@@ -175,8 +227,8 @@ def _make_nsdict(candidate):
175227 return vars (candidate )
176228
177229 def _get_command_name (self , candidate ):
178- # +1 for the dot
179230 command_name = candidate .__name__
231+ # +1 for the dot
180232 command_name = command_name [len (self .search_path ) + 1 :]
181233 command_name = command_name .replace ('_' , '-' ) # commands use -
182234 return command_name
0 commit comments