Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 0 additions & 8 deletions example/example_ngrok.py

This file was deleted.

9 changes: 3 additions & 6 deletions example/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
pyramid==1.10.4
Flask
python-dotenv
blinker
glados[servelocal]

# Uncomment this line if you want to use ngrok
# pyngrok
# Or if you want ngrok support:
# glados[servelocal,ngrok]
8 changes: 8 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ install_requires =
pyyaml
requests

[options.entry_points]
console_scripts =
glados-servelocal = glados.servelocal:run

[options.packages.find]
where=src

Expand All @@ -46,6 +50,10 @@ docs =
Sphinx
sphinx-rtd-theme
sphinx-autodoc-typehints
servelocal =
Flask
ngrok =
pyngrok

[bdist_wheel]
universal=0
Expand Down
22 changes: 9 additions & 13 deletions src/glados/__init__.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,19 @@
import logging
from .utils import PyJSON, get_var, get_enc_var

from .route_type import RouteType, EventRoutes, BOT_ROUTES, VERIFY_ROUTES
from .request import GladosRequest, SlackVerification
from .bot import BotImporter, GladosBot
from .configs import GladosConfig, read_config
from .core import Glados
from .errors import (
GladosPathExistsError,
GladosRouteNotFoundError,
GladosBotNotFoundError,
GladosError,
GladosPathExistsError,
GladosRouteNotFoundError,
)

from .bot import GladosBot, BotImporter
from .router import GladosRouter, GladosRoute
from .plugin import GladosPlugin, PluginImporter

from .configs import GladosConfig
from .utils import read_config

from .core import Glados
from .request import GladosRequest, SlackVerification
from .route_type import BOT_ROUTES, VERIFY_ROUTES, EventRoutes, RouteType
from .router import GladosRoute, GladosRouter
from .utils import PyJSON, get_enc_var, get_var

# LOGGING_FORMAT = "%(asctime)s :: %(levelname)-8s :: [%(filename)s:%(lineno)s :: %(funcName)s() ] %(message)s"
LOGGING_FORMAT = (
Expand Down
23 changes: 16 additions & 7 deletions src/glados/bot.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
from slack import WebClient
from slack.web.classes.messages import Message
from slack.web.slack_response import SlackResponse
from slack.errors import SlackRequestError
import yaml
import glob
import logging
from typing import Dict, Union

import logging
import yaml
from slack import WebClient
from slack.errors import SlackRequestError
from slack.web.classes.messages import Message
from slack.web.slack_response import SlackResponse

from glados import GladosRequest, get_var, get_enc_var
from .request import GladosRequest
from .utils import get_enc_var, get_var


class BotImporter:
Expand All @@ -27,6 +28,7 @@ def import_bots(self):
"""
files = glob.glob(f"{self._dir}/*.yaml")
logging.debug(f"bot config files found: {files}")

for f in files:
with open(f) as file:
self._bots_yaml.update(yaml.load(file, Loader=yaml.FullLoader))
Expand Down Expand Up @@ -92,25 +94,29 @@ def check_for_env_vars(self, value):
Any:
Returns the value of the var from either the passed in value, or the env var value.
"""

if type(value) is dict and "env_var" in value:
var_name = value["env_var"]
try:
return get_var(var_name)
except KeyError:
logging.critical(f"missing env var: {value['env_var']}")

if type(value) is dict and "enc_env_var" in value:
var_name = value["enc_env_var"]
try:
return get_enc_var(var_name)
except KeyError:
logging.critical(f"missing enc env var: {value['enc_env_var']}")

return value

def validate_slack_signature(self, request: GladosRequest):
valid = self.client.validate_slack_signature(
signing_secret=self.signing_secret, **request.slack_verify.json
)
logging.info(f"valid payload signature from slack: {valid}")

if not valid:
raise SlackRequestError("Signature of request is not valid")

Expand All @@ -128,6 +134,7 @@ def send_message(self, channel: str, message: Message) -> SlackResponse:
-------

"""

return self.client.chat_postMessage(
channel=channel, as_user=True, **message.to_dict()
).data
Expand All @@ -145,6 +152,7 @@ def update_message(self, channel: str, ts: str, message: Message) -> SlackRespon
-------

"""

return self.client.chat_update(channel=channel, ts=ts, **message.to_dict()).data

def delete_message(self, channel: str, ts: str) -> SlackResponse:
Expand All @@ -159,4 +167,5 @@ def delete_message(self, channel: str, ts: str) -> SlackResponse:
-------

"""

return self.client.chat_delete(channel=channel, ts=ts).data
22 changes: 17 additions & 5 deletions src/glados/configs.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
import json
import yaml
from pathlib import Path
from glados import PyJSON
import logging
from typing import Union, List
from pathlib import Path
from typing import List, Union

import yaml

from .utils import PyJSON


def read_config(config_file: str):
logging.debug(f"Reading GLaDOS config from {config_file}")
config = GladosConfig(config_file)
config.read_config()

return config


class GladosConfig:
Expand Down Expand Up @@ -35,12 +45,14 @@ def read_config(self):
@property
def sections(self) -> List[str]:
"""what sections are there in the config file

Returns
-------
List[str]:
sorted list of sections in the yaml file
"""

if not self.config:
return list()

return sorted(list(self.config.to_dict().keys()))
25 changes: 12 additions & 13 deletions src/glados/core.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,13 @@
from typing import List, Dict, TYPE_CHECKING
import yaml
import logging
from typing import Dict, List

from glados import (
GladosPlugin,
GladosRequest,
GladosRouter,
GladosBot,
BotImporter,
PluginImporter,
read_config,
)
import yaml

if TYPE_CHECKING:
from glados import GladosConfig
from .bot import BotImporter, GladosBot
from .configs import GladosConfig, read_config
from .plugin import GladosPlugin, PluginImporter
from .request import GladosRequest
from .router import GladosRouter


class Glados:
Expand All @@ -40,6 +34,7 @@ def __init__(

def read_config(self):
# TODO: Fix logging setup

if not self.config_file:
logging.info("glados config file not set.")

Expand All @@ -63,11 +58,13 @@ def read_config(self):
self.bots_config_dir = config.get("bots_config_folder")

import_bots = config.get("import_bots")

if import_bots:
logging.info("auto-importing bots as set in glados config file")
self.import_bots()

import_plugins = config.get("import_plugins", True)

if import_plugins:
self.import_plugins()

Expand All @@ -86,6 +83,7 @@ def import_plugins(self):
importer.discover_plugins()
importer.load_discovered_plugins_config(False)
importer.import_discovered_plugins(self.bots)

for plugin in importer.plugins.values():
print(type(plugin))
self.add_plugin(plugin)
Expand Down Expand Up @@ -133,4 +131,5 @@ def request(self, request: GladosRequest):
-------

"""

return self.router.exec_route(request)
Loading