Skip to content

Commit 5a781d5

Browse files
author
Simon Berger
committed
env config and happy Codacy, hopefully
1 parent 4e239aa commit 5a781d5

22 files changed

+115
-83
lines changed

.docker/supervisord.conf

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ nodaemon=true
55
priority=10
66
directory=/gisi
77
command=python run.py
8+
startsecs=3
89
user=root
910
autorestart=true
1011
stdout_logfile=/dev/stdout

.gitignore

+2-6
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,6 @@
33
.pytest_cache/
44
venv/
55
gh-pages/
6-
logs/*.log
6+
logs/
77

8-
data/config.json
9-
10-
geckodriver.exe
11-
inklecate.exe
12-
ink-engine-runtime.dll
8+
data/config.json

.travis.yml

+9-8
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,18 @@ services:
1010
addons:
1111
apt:
1212
packages:
13-
- firefox
14-
- time
13+
- google-chrome-stable
14+
- curl
1515
- unzip
1616

1717
before_install:
18-
- >
19-
wget https://github.com/mozilla/geckodriver/releases/download/v0.19.1/geckodriver-v0.19.1-linux64.tar.gz &&
20-
tar -xvzf geckodriver* &&
21-
chmod +x geckodriver &&
22-
sudo mv geckodriver /usr/local/bin/
18+
- CHROMEDRIVER_VERSION=$(curl -sS chromedriver.storage.googleapis.com/LATEST_RELEASE) && \
19+
mkdir -p /opt/chromedriver-"$CHROMEDRIVER_VERSION" && \
20+
curl -sS -o /tmp/chromedriver_linux64.zip http://chromedriver.storage.googleapis.com/"$CHROMEDRIVER_VERSION"/chromedriver_linux64.zip && \
21+
unzip -qq /tmp/chromedriver_linux64.zip -d /opt/chromedriver-"$CHROMEDRIVER_VERSION" && \
22+
rm /tmp/chromedriver_linux64.zip && \
23+
chmod +x /opt/chromedriver-"$CHROMEDRIVER_VERSION"/chromedriver && \
24+
ln -fs /opt/chromedriver-"$CHROMEDRIVER_VERSION"/chromedriver /usr/local/bin/chromedriver
2325

2426
- sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 3FA7E0328081BFF6A14DA29AA6A19B38D3D831EF
2527
- echo "deb http://download.mono-project.com/repo/ubuntu wheezy/snapshots/4.8.1 main"
@@ -37,7 +39,6 @@ install:
3739
- pip install pycparser
3840
- pip install codecov
3941
- pip install pytest pytest-cov pytest-asyncio
40-
- pip install -e .
4142
- pip install -r requirements.txt
4243

4344
script:

Dockerfile

+9-9
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,27 @@ LABEL maintainer=Simon
44

55
# Install required packages
66
RUN apt-get -yqq update && \
7-
apt-get -yqq install curl unzip && \
7+
apt-get -yqq --no-install-recommends install curl unzip && \
88
rm -rf /var/lib/apt/lists/*
99

1010
# Install Chrome WebDriver
11-
RUN CHROMEDRIVER_VERSION=`curl -sS chromedriver.storage.googleapis.com/LATEST_RELEASE` && \
12-
mkdir -p /opt/chromedriver-$CHROMEDRIVER_VERSION && \
13-
curl -sS -o /tmp/chromedriver_linux64.zip http://chromedriver.storage.googleapis.com/$CHROMEDRIVER_VERSION/chromedriver_linux64.zip && \
14-
unzip -qq /tmp/chromedriver_linux64.zip -d /opt/chromedriver-$CHROMEDRIVER_VERSION && \
11+
RUN CHROMEDRIVER_VERSION=$(curl -sS chromedriver.storage.googleapis.com/LATEST_RELEASE) && \
12+
mkdir -p /opt/chromedriver-"$CHROMEDRIVER_VERSION" && \
13+
curl -sS -o /tmp/chromedriver_linux64.zip http://chromedriver.storage.googleapis.com/"$CHROMEDRIVER_VERSION"/chromedriver_linux64.zip && \
14+
unzip -qq /tmp/chromedriver_linux64.zip -d /opt/chromedriver-"$CHROMEDRIVER_VERSION" && \
1515
rm /tmp/chromedriver_linux64.zip && \
16-
chmod +x /opt/chromedriver-$CHROMEDRIVER_VERSION/chromedriver && \
17-
ln -fs /opt/chromedriver-$CHROMEDRIVER_VERSION/chromedriver /usr/local/bin/chromedriver
16+
chmod +x /opt/chromedriver-"$CHROMEDRIVER_VERSION"/chromedriver && \
17+
ln -fs /opt/chromedriver-"$CHROMEDRIVER_VERSION"/chromedriver /usr/local/bin/chromedriver
1818

1919
# Install Google Chrome
2020
RUN curl -sS -o - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add - && \
2121
echo "deb http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google-chrome.list && \
2222
apt-get -yqq update && \
23-
apt-get -yqq install google-chrome-stable && \
23+
apt-get -yqq --no-install-recommends install google-chrome-stable && \
2424
rm -rf /var/lib/apt/lists/*
2525

2626
# Install Supervisord
27-
RUN apt-get update && apt-get install -y supervisor && rm -rf /var/lib/apt/lists/*
27+
RUN apt-get update && apt-get install -y --no-install-recommends supervisor && rm -rf /var/lib/apt/lists/*
2828
COPY .docker/supervisord.conf /etc/supervisor/conf.d/supervisord.conf
2929

3030
# Install Python requirements

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
# Gisi
2+
[![Codacy Badge](https://api.codacy.com/project/badge/Grade/65592becb1a1474ca4fa9301b4b63cf6)](https://www.codacy.com/app/siku2/Gisi?utm_source=github.com&utm_medium=referral&utm_content=GieselaDev/Gisi&utm_campaign=Badge_Grade)
13
[![Build Status](https://travis-ci.org/GieselaDev/Gisi.svg?branch=master)](https://travis-ci.org/GieselaDev/Gisi)
24
[![codecov](https://codecov.io/gh/GieselaDev/Gisi/branch/master/graph/badge.svg)](https://codecov.io/gh/GieselaDev/Gisi)
35

4-
# Gisi
56
Something like a self bot?

data/config.json.sample

-3
This file was deleted.

gisi/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@
66
log = logging.getLogger(__name__)
77
log.debug("logging setup")
88

9-
from .config import SetDefaults
9+
from .config import set_defaults
1010
from .gisi import Gisi
1111
from .signals import GisiSignal

gisi/cogs/draw.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
import matplotlib.cm as colour_map
88
import numpy
99
from PIL import Image, ImageDraw, ImageEnhance, ImageFilter, ImageFont
10-
from bs4 import BeautifulSoup, Comment
10+
from bs4 import BeautifulSoup
11+
from bs4.element import Comment
1112
from discord import File, User
1213
from discord.ext.commands import ColourConverter, group
1314
from wordcloud import ImageColorGenerator, WordCloud
@@ -72,7 +73,6 @@ async def add_font(self, ctx, *flags):
7273
-n | name for the new font
7374
"""
7475
flags = FlagConverter.from_spec(flags)
75-
font_io = io.BytesIO()
7676
font_name = flags.get("n", None)
7777

7878
if ctx.message.attachments:

gisi/cogs/eventory.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@
88

99

1010
def setup(bot: Bot):
11-
bot.add_cog(EventoryCog(bot, directory=FileLocations.EVENTORY))
11+
# bot.add_cog(EventoryCog(bot, directory=FileLocations.EVENTORY))
12+
pass

gisi/cogs/google.py

+8-8
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from discord import Embed, File
1010
from discord.ext.commands import group
1111

12-
from gisi import SetDefaults
12+
from gisi import set_defaults
1313
from gisi.constants import Colours
1414
from gisi.utils import EmbedPaginator, FlagConverter, add_embed, copy_embed, extract_keys, maybe_extract_keys, \
1515
text_utils
@@ -29,7 +29,7 @@ class Google:
2929
def __init__(self, bot):
3030
self.bot = bot
3131
self.aiosession = bot.aiosession
32-
self.cse = CSE(self.bot.config.google_api_key, search_engine=self.bot.config.search_engine_id,
32+
self.cse = CSE(self.bot.config.GOOGLE_API_KEY, search_engine=self.bot.config.SEARCH_ENGINE_ID,
3333
aiosession=self.aiosession)
3434

3535
@group(invoke_without_command=True)
@@ -91,12 +91,12 @@ async def image(self, ctx, *flags):
9191

9292

9393
def setup(bot):
94-
SetDefaults({
95-
"google_api_key": None,
96-
"search_engine_id": "002017775112634544492:izg2ejvnmiq"
94+
set_defaults({
95+
"GOOGLE_API_KEY": None,
96+
"SEARCH_ENGINE_ID": "002017775112634544492:izg2ejvnmiq"
9797
})
98-
if not bot.config.google_api_key:
99-
log.error("No google api key found in config (key: \"google_api_key\"). Can't initialise cog!")
98+
if not bot.config.GOOGLE_API_KEY:
99+
log.error("No google api key found in config (key: \"GOOGLE_API_KEY\"). Can't initialise cog!")
100100
return
101101
bot.add_cog(Google(bot))
102102

@@ -203,7 +203,7 @@ async def create_image(self, session):
203203
im = Image.new("RGB", (2 * horizontal_padding + line_width, im_height), (35, 39, 42))
204204

205205
current_y = vertical_padding
206-
for spaced_width, width, images in lines:
206+
for _, width, images in lines:
207207
x = horizontal_padding
208208
spacing = (line_width - width) // (len(images) - 1) if len(images) > 1 else 0
209209
for img in images:

gisi/cogs/grammar.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from discord.ext.commands import group
44

5-
from gisi import SetDefaults
5+
from gisi import set_defaults
66

77
log = logging.getLogger(__name__)
88

@@ -78,7 +78,7 @@ async def on_message(self, message):
7878

7979

8080
def setup(bot):
81-
SetDefaults({
81+
set_defaults({
8282
"grammar_check_enabled": False
8383
})
8484
bot.add_cog(Grammar(bot))

gisi/cogs/programming.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ async def eval(self, ctx):
9292
if out:
9393
result = "\n".join(out)
9494
if len(result) > 1024:
95-
haste_url = await hastebin.post(self.aiosession, str(result))
95+
haste_url = await hastebin.post(self.aiosession, str(result))
9696
result = f"The Output is too big. [Here's a link to a Hastebin]({haste_url})"
9797
em.add_field(
9898
name="Output",
@@ -122,7 +122,7 @@ class BeautyFormatter(json.JSONEncoder):
122122
"access_token",
123123
"token",
124124
"webhook_url",
125-
"mongodb_uri"
125+
"mongo_uri"
126126
]
127127

128128
def stringify(self, key: str, value: str, *, shallow: bool = False):

gisi/cogs/text.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from discord import Embed
1212
from discord.ext.commands import group
1313

14-
from gisi import SetDefaults
14+
from gisi import set_defaults
1515
from gisi.constants import Colours
1616
from gisi.utils import text_utils
1717

@@ -292,7 +292,7 @@ async def on_message_edit(self, before, after):
292292

293293

294294
def setup(bot):
295-
SetDefaults({
295+
set_defaults({
296296
"replacer_enabled": True
297297
})
298298
bot.add_cog(Text(bot))

gisi/cogs/wolfram.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from discord import Embed, File
99
from discord.ext.commands import command
1010

11-
from gisi import Gisi, SetDefaults
11+
from gisi import Gisi, set_defaults
1212
from gisi.constants import Colours
1313
from gisi.utils import chunks, extract_keys
1414

@@ -24,7 +24,7 @@ class WolframAlpha:
2424
def __init__(self, bot: Gisi):
2525
self.bot = bot
2626
self.aiosession = bot.aiosession
27-
self.wolfram_client = Client(self.bot.config.wolfram_app_id, aiosession=self.aiosession)
27+
self.wolfram_client = Client(self.bot.config.WOLFRAM_APP_ID, aiosession=self.aiosession)
2828

2929
@command()
3030
async def ask(self, ctx, *, query):
@@ -61,8 +61,8 @@ async def ask(self, ctx, *, query):
6161

6262

6363
def setup(bot):
64-
SetDefaults({
65-
"wolfram_app_id": "EH8PUT-35T4RK4AVL"
64+
set_defaults({
65+
"WOLFRAM_APP_ID": "EH8PUT-35T4RK4AVL"
6666
})
6767
bot.add_cog(WolframAlpha(bot))
6868

gisi/config.py

+43-17
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,86 @@
11
import json
22
import logging
3+
import os
4+
from ast import literal_eval
5+
from typing import Any, Dict
36

47
from .constants import FileLocations
58

69
log = logging.getLogger(__name__)
710

11+
MUST_SET = object()
12+
813

914
class Defaults:
10-
token = None
11-
command_prefix = ">"
12-
mongodb_uri = None
15+
TOKEN = MUST_SET
16+
COMMAND_PREFIX = ">"
17+
MONGO_URI = MUST_SET
18+
MONGO_DATABASE = "Gisi"
1319

14-
webhook_url = None
20+
WEBHOOK_URL = None
1521

16-
default_font = "arial"
22+
DEFAULT_FONT = "arial"
1723

1824

19-
# noinspection PyPep8Naming
20-
def SetDefaults(defaults: dict):
25+
def set_defaults(defaults: dict):
2126
for key, value in defaults.items():
2227
setattr(Defaults, key, value)
2328
log.debug(f"added {len(defaults)} setting(s) to default config")
2429

2530

2631
class Config:
27-
def __init__(self, config):
32+
def __init__(self, config: Dict[str, Any]):
2833
self.config = config
2934

30-
def __str__(self):
35+
def __str__(self) -> str:
3136
return "<Config>"
3237

33-
def __getattr__(self, item):
38+
def __getattr__(self, item: str) -> Any:
3439
return self.get(item)
3540

36-
def __getitem__(self, item):
41+
def __getitem__(self, item: str) -> Any:
3742
return self.get(item)
3843

39-
def __setitem__(self, key, value):
44+
def __setitem__(self, key: str, value: Any):
4045
return self.set(key, value)
4146

4247
@classmethod
4348
def load(cls):
44-
with open(FileLocations.CONFIG, "r") as f:
45-
data = json.load(f)
49+
try:
50+
with open(FileLocations.CONFIG, "r") as f:
51+
data = json.load(f)
52+
except FileNotFoundError:
53+
data = {}
4654
return cls(data)
4755

4856
def save(self):
4957
with open(FileLocations.CONFIG, "w+") as f:
5058
json.dump(self.config, f)
5159
log.info("saved config")
5260

53-
def get(self, key):
54-
return self.config.get(key, getattr(Defaults, key))
61+
def get(self, key: str) -> Any:
62+
try:
63+
return self.config[key]
64+
except KeyError:
65+
try:
66+
return literal_eval(os.environ[key])
67+
except (KeyError, SyntaxError):
68+
return getattr(Defaults, key)
5569

56-
def set(self, key, value):
70+
def set(self, key: str, value: Any):
5771
if not hasattr(Defaults, key):
5872
raise KeyError(f"Key not in Defaults! ({key})")
5973
self.config[key] = value
6074
self.save()
75+
76+
77+
def extract_env(config: Config):
78+
env = os.environ
79+
for key, default_value in vars(Defaults).items():
80+
value = env.get(key)
81+
if value:
82+
value = literal_eval(value)
83+
config.config[key] = value
84+
elif default_value == MUST_SET:
85+
if key not in config.config:
86+
raise KeyError(f"Key {key} missing in environment variables!")

0 commit comments

Comments
 (0)