@@ -563,6 +563,7 @@ def load_plugins(options: Options, errors: Errors) -> Plugin:
563
563
Return a plugin that encapsulates all plugins chained together. Always
564
564
at least include the default plugin (it's last in the chain).
565
565
"""
566
+ import importlib
566
567
567
568
default_plugin = DefaultPlugin (options ) # type: Plugin
568
569
if not options .config_file :
@@ -579,34 +580,44 @@ def plugin_error(message: str) -> None:
579
580
custom_plugins = [] # type: List[Plugin]
580
581
errors .set_file (options .config_file , None )
581
582
for plugin_path in options .plugins :
582
- # Plugin paths are relative to the config file location.
583
- plugin_path = os .path .join (os .path .dirname (options .config_file ), plugin_path )
584
-
585
- if not os .path .isfile (plugin_path ):
586
- plugin_error ("Can't find plugin '{}'" .format (plugin_path ))
587
- plugin_dir = os .path .dirname (plugin_path )
588
- fnam = os .path .basename (plugin_path )
589
- if not fnam .endswith ('.py' ):
583
+ func_name = 'plugin'
584
+ plugin_dir = None # type: Optional[str]
585
+ if ':' in os .path .basename (plugin_path ):
586
+ plugin_path , func_name = plugin_path .rsplit (':' , 1 )
587
+ if plugin_path .endswith ('.py' ):
588
+ # Plugin paths can be relative to the config file location.
589
+ plugin_path = os .path .join (os .path .dirname (options .config_file ), plugin_path )
590
+ if not os .path .isfile (plugin_path ):
591
+ plugin_error ("Can't find plugin '{}'" .format (plugin_path ))
592
+ plugin_dir = os .path .dirname (plugin_path )
593
+ fnam = os .path .basename (plugin_path )
594
+ module_name = fnam [:- 3 ]
595
+ sys .path .insert (0 , plugin_dir )
596
+ elif re .search (r'[\\/]' , plugin_path ):
597
+ fnam = os .path .basename (plugin_path )
590
598
plugin_error ("Plugin '{}' does not have a .py extension" .format (fnam ))
591
- module_name = fnam [: - 3 ]
592
- import importlib
593
- sys . path . insert ( 0 , plugin_dir )
599
+ else :
600
+ module_name = plugin_path
601
+
594
602
try :
595
- m = importlib .import_module (module_name )
603
+ module = importlib .import_module (module_name )
596
604
except Exception :
597
- print ('Error importing plugin {}\n ' .format (plugin_path ))
598
- raise # Propagate to display traceback
605
+ plugin_error ("Error importing plugin '{}'" .format (plugin_path ))
599
606
finally :
600
- assert sys .path [0 ] == plugin_dir
601
- del sys .path [0 ]
602
- if not hasattr (m , 'plugin' ):
603
- plugin_error ('Plugin \' {}\' does not define entry point function "plugin"' .format (
604
- plugin_path ))
607
+ if plugin_dir is not None :
608
+ assert sys .path [0 ] == plugin_dir
609
+ del sys .path [0 ]
610
+
611
+ if not hasattr (module , func_name ):
612
+ plugin_error ('Plugin \' {}\' does not define entry point function "{}"' .format (
613
+ plugin_path , func_name ))
614
+
605
615
try :
606
- plugin_type = getattr (m , 'plugin' )(__version__ )
616
+ plugin_type = getattr (module , func_name )(__version__ )
607
617
except Exception :
608
618
print ('Error calling the plugin(version) entry point of {}\n ' .format (plugin_path ))
609
619
raise # Propagate to display traceback
620
+
610
621
if not isinstance (plugin_type , type ):
611
622
plugin_error (
612
623
'Type object expected as the return value of "plugin"; got {!r} (in {})' .format (
0 commit comments