add logging, @register future deprecation warning
This commit is contained in:
		
							parent
							
								
									5b6ec72d0b
								
							
						
					
					
						commit
						0bb6eb6b6a
					
				@ -1,11 +1,12 @@
 | 
				
			|||||||
from enum import Enum
 | 
					from enum import Enum
 | 
				
			||||||
from functools import wraps
 | 
					from functools import wraps
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					from .log import logger
 | 
				
			||||||
 | 
					
 | 
				
			||||||
plugins = {}
 | 
					plugins = {}
 | 
				
			||||||
cmds = {}
 | 
					cmds = {}
 | 
				
			||||||
lstnrs = {}
 | 
					lstnrs = {}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					 | 
				
			||||||
class OutputType(Enum):
 | 
					class OutputType(Enum):
 | 
				
			||||||
    Message = 'message'
 | 
					    Message = 'message'
 | 
				
			||||||
    Action = 'action'
 | 
					    Action = 'action'
 | 
				
			||||||
@ -24,6 +25,7 @@ class Output:
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
class _BasePlugin:
 | 
					class _BasePlugin:
 | 
				
			||||||
    enabled = True
 | 
					    enabled = True
 | 
				
			||||||
 | 
					    logger = logger
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def enable(self):
 | 
					    def enable(self):
 | 
				
			||||||
        self.enabled = True
 | 
					        self.enabled = True
 | 
				
			||||||
@ -79,41 +81,42 @@ class Command(_BasePlugin):
 | 
				
			|||||||
def action(msg):
 | 
					def action(msg):
 | 
				
			||||||
    return Output(OutputType.Action, msg)
 | 
					    return Output(OutputType.Action, msg)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					 | 
				
			||||||
def message(msg):
 | 
					def message(msg):
 | 
				
			||||||
    return Output(OutputType.Message, msg)
 | 
					    return Output(OutputType.Message, msg)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					 | 
				
			||||||
def _add_command(command, help_text, func):
 | 
					def _add_command(command, help_text, func):
 | 
				
			||||||
    if command not in cmds:
 | 
					    if command not in cmds:
 | 
				
			||||||
        Command(command, help_text=help_text, run=func).add_command()
 | 
					        Command(command, help_text=help_text, run=func).add_command()
 | 
				
			||||||
    else:
 | 
					    else:
 | 
				
			||||||
        cmds[command].update_plugin(help_text=help_text, run=func)
 | 
					        cmds[command].update_plugin(help_text=help_text, run=func)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					 | 
				
			||||||
def _ops_plugin(command, ops_msg, func):
 | 
					def _ops_plugin(command, ops_msg, func):
 | 
				
			||||||
    if command not in cmds:
 | 
					    if command not in cmds:
 | 
				
			||||||
        Command(command, ops=True, ops_msg=ops_msg).add_command()
 | 
					        Command(command, ops=True, ops_msg=ops_msg).add_command()
 | 
				
			||||||
    else:
 | 
					    else:
 | 
				
			||||||
        cmds[command].enable_ops(ops_msg)
 | 
					        cmds[command].enable_ops(ops_msg)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					 | 
				
			||||||
def _add_listener(name, func):
 | 
					def _add_listener(name, func):
 | 
				
			||||||
    Listener(name, run=func).add_listener()
 | 
					    Listener(name, run=func).add_listener()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					 | 
				
			||||||
def clear_plugins():
 | 
					def clear_plugins():
 | 
				
			||||||
    cmds.clear()
 | 
					    cmds.clear()
 | 
				
			||||||
    lstnrs.clear()
 | 
					    lstnrs.clear()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def command(command, help_text='N/A'):
 | 
				
			||||||
def register(command, help_text='N/A'):
 | 
					 | 
				
			||||||
    @wraps(command)
 | 
					    @wraps(command)
 | 
				
			||||||
    def register_for_command(func):
 | 
					    def register_for_command(func):
 | 
				
			||||||
        _add_command(command, help_text, func)
 | 
					        _add_command(command, help_text, func)
 | 
				
			||||||
        return func
 | 
					        return func
 | 
				
			||||||
    return register_for_command
 | 
					    return register_for_command
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def register(command, help_text='N/A'):
 | 
				
			||||||
 | 
					    logger.warn('@register decorator has been deprecated in favor of @command. This will cause errors in future versions.')
 | 
				
			||||||
 | 
					    @wraps(command)
 | 
				
			||||||
 | 
					    def register_for_command(func):
 | 
				
			||||||
 | 
					        _add_command(command, help_text, func)
 | 
				
			||||||
 | 
					        return func
 | 
				
			||||||
 | 
					    return register_for_command
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def listener(name):
 | 
					def listener(name):
 | 
				
			||||||
    def register_as_listener(func):
 | 
					    def register_as_listener(func):
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user