Skip to content

Commit

Permalink
Some more typing hints for Command
Browse files Browse the repository at this point in the history
  • Loading branch information
oklahomer committed Jul 18, 2015
1 parent 4688ece commit ab76f60
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 12 deletions.
41 changes: 29 additions & 12 deletions sarah/bot_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,48 @@
import threading
from apscheduler.schedulers.background import BackgroundScheduler
import sys
from typing import Callable, Optional, Dict, Any, Sequence, Iterable
from typing import Callable, Optional, Sequence
from sarah.thread import ThreadExecutor
from sarah.types import CommandFunction, PluginConfig
from sarah.types import CommandFunction, PluginConfig, AnyFunction, \
CommandConfig


class Command(object):
def __init__(self,
name: str,
function: CommandFunction,
module_name: str,
config: Dict[str, Any]=None) -> None:
config: CommandConfig=None) -> None:
if not config:
config = {}
self.name = name
self.function = function
self.module_name = module_name
self.config = config
self.__name = name
self.__function = function
self.__module_name = module_name
self.__config = config

@property
def name(self):
return self.__name

@property
def function(self):
return self.__function

@property
def module_name(self):
return self.__module_name

@property
def config(self):
return self.__config

def execute(self, *args) -> str:
args = list(args)
args.append(self.config)
return self.function(*args)
args.append(self.__config)
return self.__function(*args)

def set_config(self, config: Dict) -> None:
self.config = config
def set_config(self, config: CommandConfig) -> None:
self.__config = config


class BotBase(object, metaclass=abc.ABCMeta):
Expand Down Expand Up @@ -73,7 +90,7 @@ def stop(self) -> None:
self.worker.shutdown(wait=False)

@classmethod
def concurrent(cls, callback_function: Callable[[Iterable[Any]], Any]):
def concurrent(cls, callback_function: AnyFunction):
@wraps(callback_function)
def wrapper(self, *args, **kwargs):
if self.worker:
Expand Down
4 changes: 4 additions & 0 deletions sarah/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,8 @@

CommandFunction = Callable[[Iterable[Any]], str]

CommandConfig = Dict[str, Any]

Path = AnyStr

AnyFunction = Callable[[Iterable[Any]], Any]

0 comments on commit ab76f60

Please sign in to comment.