# Path: cardinal/decorators.py
# def command(triggers):
#     if isinstance(triggers, str):
#         triggers = [triggers]
# 
#     if not isinstance(triggers, list):
#         raise TypeError("Command must be a trigger string or list of triggers")
# 
#     def wrap(f):
#         f.commands = triggers
#         return f
# 
#     return wrap
# 
# def help(lines):
#     # For backwards compatibility
#     if isinstance(lines, str):
#         lines = [lines]
# 
#     if not isinstance(lines, list):
#         raise TypeError("Help must be a help string or list of help strings")
# 
#     def wrap(f):
#         # Create help list or prepend to it
#         if not hasattr(f, 'help'):
#             f.help = lines
#         else:
#             f.help = lines + f.help
# 
#         return f
# 
#     return wrap


import logging
import requests
from twisted.internet import defer
from twisted.internet.threads import deferToThread
from cardinal.decorators import command, help



API_URL = "https://api.wolframalpha.com/v1/result"


class WolframAlphaPlugin:
    def __init__(self, config):
        self.app_id = config.get('app_id', None)
        if not self.app_id:
            raise Exception("Please set app_id in plugin config")
        self.logger = logging.getLogger(__name__)

    @defer.inlineCallbacks
    def make_query(self, query):
        self.logger.debug("Making query to Wolfram Alpha: %s", query)

        r = yield deferToThread(requests.get, API_URL, params={
            'appid': self.app_id,
            'i': query,
        })
        r.raise_for_status()
        answer = r.text

        self.logger.debug("Got answer for query '%s': %s", query, answer)

        return answer

    @command(['wolfram', 'calc'])
    @help('Make a query with Wolfram Alpha')

# Path: cardinal/decorators.py
# def command(triggers):
#     if isinstance(triggers, str):
#         triggers = [triggers]
# 
#     if not isinstance(triggers, list):
#         raise TypeError("Command must be a trigger string or list of triggers")
# 
#     def wrap(f):
#         f.commands = triggers
#         return f
# 
#     return wrap
# 
# def help(lines):
#     # For backwards compatibility
#     if isinstance(lines, str):
#         lines = [lines]
# 
#     if not isinstance(lines, list):
#         raise TypeError("Help must be a help string or list of help strings")
# 
#     def wrap(f):
#         # Create help list or prepend to it
#         if not hasattr(f, 'help'):
#             f.help = lines
#         else:
#             f.help = lines + f.help
# 
#         return f
# 
#     return wrap


# Path: plugins/wolframalpha/plugin.py
import logging
import requests
from twisted.internet import defer
from twisted.internet.threads import deferToThread
from cardinal.decorators import command, help




API_URL = "https://api.wolframalpha.com/v1/result"


class WolframAlphaPlugin:
    def __init__(self, config):
        self.app_id = config.get('app_id', None)
        if not self.app_id:
            raise Exception("Please set app_id in plugin config")
        self.logger = logging.getLogger(__name__)

    @defer.inlineCallbacks
    def make_query(self, query):
        self.logger.debug("Making query to Wolfram Alpha: %s", query)

        r = yield deferToThread(requests.get, API_URL, params={
            'appid': self.app_id,
            'i': query,
        })
        r.raise_for_status()
        answer = r.text

        self.logger.debug("Got answer for query '%s': %s", query, answer)

        return answer

    @command(['wolfram', 'calc'])
    @help('Make a query with Wolfram Alpha')

    @help('Syntax: .wolfram <query>')