Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
chatbot/handlers/service.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
39 lines (30 sloc)
986 Bytes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# pylint: disable=C0413,C0103 | |
""" Service base clase and global_services. """ | |
import sys | |
import os | |
sys.path.insert(0, os.path.abspath("..")) | |
from config import ConfigDict, global_config | |
global_services = {} | |
def register_service(name): | |
""" Registers a service globally. """ | |
def wrap(c): | |
if name in global_config.services: | |
# create service using global_config | |
s = c(global_config.services[name]) | |
else: | |
# create service without config | |
try: | |
s = c() | |
except TypeError: | |
return c | |
global_services[name] = s | |
return c | |
return wrap | |
class Service(object): | |
""" Service base class. """ | |
def __init__(self, config=ConfigDict()): | |
if isinstance(config, dict): | |
config = ConfigDict(**config) | |
elif not isinstance(config, ConfigDict): | |
raise TypeError("config must be dict or ConfigDict") | |
self.config = config |