Skip to content

Commit

Permalink
Change how paths are handled, now they are more explicit and easier t…
Browse files Browse the repository at this point in the history
…o use
  • Loading branch information
MaT1g3R committed Jul 8, 2017
1 parent 2d72ae1 commit 2f788fe
Show file tree
Hide file tree
Showing 14 changed files with 48 additions and 26 deletions.
2 changes: 1 addition & 1 deletion bot/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@

from bot.error_handler import command_error_handler, format_command_error, \
format_traceback
from bot.logger import command_formatter
from bot.session_manager import SessionManager
from core.helpers import detailed_help, general_help_embed, \
get_command_collections, get_valid_commands
from data_controller.mongo import DatabaseController
from bot.logger import command_formatter


class HahaNoUR(Bot):
Expand Down
6 changes: 4 additions & 2 deletions bot/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@
from datetime import datetime
from pathlib import Path
from sys import stdout
from discord import Message

from colorlog import ColoredFormatter
from discord import Message
from pytz import timezone

CONSOLE_FORMAT = ('%(asctime)s %(log_color)s%(levelname)s %(name)s: '
'%(message)s')
FILE_FORMAT = '%(asctime)s %(levelname)s %(name)s: %(message)s'


def command_formatter(message: Message, command_name=None) -> str:
"""
Format a command into a message to be logged.
Expand All @@ -25,9 +26,10 @@ def command_formatter(message: Message, command_name=None) -> str:

content += 'from ' + message.author.name + '(' + message.author.id + ') '
if message.server:
content += 'in ' + message.server.name + ' #' + message.channel.name
content += 'in ' + message.server.name + ' #' + message.channel.name
return content


def timestamp(*args):
"""
Gets the current timestamp
Expand Down
5 changes: 5 additions & 0 deletions config/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from pathlib import Path

config_path = Path(Path(__file__).parent)

__all__ = ['config_path']
5 changes: 3 additions & 2 deletions core/get_names.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from json import dump, load
from pathlib import Path

from requests import get

from data import data_path

API_URL = 'http://schoolido.lu/api/'


Expand All @@ -12,7 +13,7 @@ def get_idol_names() -> list:
:return: List of names
"""
path = Path('data/names.json')
path = data_path.joinpath('names.json')
try:
size = (get(API_URL + "idols").json())["count"]
result_list = get(
Expand Down
21 changes: 10 additions & 11 deletions core/image_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@
from collections import deque
from logging import INFO
from pathlib import Path
import os
from typing import List, Sequence, Tuple

from PIL import Image

from bot import SessionManager
from idol_images import idol_img_path
from scout_output import scout_output_path

IDOL_IMAGES_PATH = Path('idol_images')
OUTPUT_PATH = Path('scout_output')
CIRCLE_DISTANCE = 10


Expand All @@ -24,7 +23,7 @@ async def create_image(session_manager: SessionManager,
:param idol_circle_urls: urls of idol circle images to be stitched together.
:param num_rows: Number of rows to use in the image
:param output_filename: name of output image file
:param align: to align middle the image or not.
:return: path pointing to created image
"""
if num_rows > len(idol_circle_urls):
Expand All @@ -34,13 +33,13 @@ async def create_image(session_manager: SessionManager,
# Save images that do not exists
for image_url in idol_circle_urls:
url_path = urllib.parse.urlsplit(image_url).path
file_path = IDOL_IMAGES_PATH.joinpath(Path(url_path).name)
file_path = idol_img_path.joinpath(Path(url_path).name)
image_filepaths.append(file_path)
await download_image_from_url(image_url, file_path, session_manager)
# Load images
circle_images = [Image.open(str(i)) for i in image_filepaths]
image = _build_image(circle_images, num_rows, 10, 10, align)
output_path = OUTPUT_PATH.joinpath(output_filename)
output_path = scout_output_path.joinpath(output_filename)
image.save(str(output_path), 'PNG')
return str(output_path)

Expand All @@ -57,10 +56,10 @@ async def download_image_from_url(
:return: path of saved image
"""
# Create directories for storing images if they do not exist
if not IDOL_IMAGES_PATH.is_dir():
IDOL_IMAGES_PATH.mkdir(parents=True, exist_ok=True)
if not OUTPUT_PATH.is_dir():
OUTPUT_PATH.mkdir(parents=True, exist_ok=True)
if not idol_img_path.is_dir():
idol_img_path.mkdir(parents=True, exist_ok=True)
if not scout_output_path.is_dir():
scout_output_path.mkdir(parents=True, exist_ok=True)
response = await session_manager.get(url)
async with response:
# Checking if the image already exists in two different ways because
Expand Down Expand Up @@ -106,7 +105,7 @@ def compute_pos(
:param num_rows: the number of rows.
:param x_padding: x spacing between each image
:param y_padding: y spacing between each row
:param align: to align middle the image or not.
:return: Positions for all images, the total x size, the total y size
"""
total_x, total_y = 0, 0
Expand Down
4 changes: 2 additions & 2 deletions core/scout.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from bot import SessionManager
from core.argument_parser import parse_arguments
from core.get_names import get_idol_names
from core.image_generator import IDOL_IMAGES_PATH, create_image, \
from core.image_generator import idol_img_path, create_image, \
download_image_from_url

API_URL = 'http://schoolido.lu/api/'
Expand Down Expand Up @@ -108,7 +108,7 @@ async def _handle_solo_scout(self):
else:
url = "http:" + card["card_image"]

self.image_path = IDOL_IMAGES_PATH.joinpath(
self.image_path = idol_img_path.joinpath(
basename(urllib.parse.urlsplit(url).path))

await download_image_from_url(url, Path(self.image_path),
Expand Down
5 changes: 5 additions & 0 deletions data/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from pathlib import Path

data_path = Path(Path(__file__).parent)

__all__ = ['data_path']
Empty file removed idol_images/.gitkeep
Empty file.
4 changes: 4 additions & 0 deletions idol_images/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from pathlib import Path

idol_img_path = Path(Path(__file__).parent)
__all__ = ['idol_img_path']
Empty file removed logs/.gitkeep
Empty file.
5 changes: 5 additions & 0 deletions logs/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from pathlib import Path

log_path = Path(Path(__file__).parent)

__all__ = ['log_path']
12 changes: 4 additions & 8 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,23 @@
from asyncio import get_event_loop, set_event_loop_policy
from asyncio import get_event_loop
from json import load
from pathlib import Path
from time import time

#from uvloop import EventLoopPolicy

from bot import HahaNoUR, get_session_manager
from bot.logger import setup_logging
from commands import AlbumCommands, InfoCommands, ScoutCommands
from config import config_path
from data_controller.mongo import DatabaseController
from logs import log_path


def main():
start_time = int(time())
log_path = Path(Path(__file__).parent.joinpath('logs'))
logger = setup_logging(start_time, log_path)
#set_event_loop_policy(EventLoopPolicy())
loop = get_event_loop()
session_manager = loop.run_until_complete(get_session_manager(logger))
db = DatabaseController()
with Path('config/config.json').open() as f:
with config_path.joinpath('config.json').open() as f:
config = load(f)
f.close()
bot = HahaNoUR(config['default_prefix'], start_time, logger,
session_manager, db, config['error_log'])
bot.remove_command('help')
Expand Down
Empty file removed scout_output/.gitkeep
Empty file.
5 changes: 5 additions & 0 deletions scout_output/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from pathlib import Path

scout_output_path = Path(Path(__file__).parent)

__all__ = ['scout_output_path']

0 comments on commit 2f788fe

Please sign in to comment.