Skip to content

Commit

Permalink
Making some functions complete async
Browse files Browse the repository at this point in the history
  • Loading branch information
geirawsm committed Aug 26, 2024
1 parent 1da8dbb commit f666c54
Show file tree
Hide file tree
Showing 16 changed files with 124 additions and 73 deletions.
15 changes: 7 additions & 8 deletions sausage_bot/cogs/autoevent.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ async def event_remove(
Use if you want to remove all events
'''
await interaction.response.defer(ephemeral=True)
event_dict = discord_commands.get_scheduled_events()
event_dict = await discord_commands.get_scheduled_events()
log.debug(f'Got `event_dict`: {event_dict}')
# Delete all events
_guild = discord_commands.get_guild()
Expand Down Expand Up @@ -211,7 +211,7 @@ async def list_events(self, interaction: discord.Interaction):
Lists all the planned events: `!autoevent list`
'''
await interaction.response.defer(ephemeral=True)
events = discord_commands.get_sorted_scheduled_events()
events = await discord_commands.get_sorted_scheduled_events()
if events is None:
msg_out = envs.AUTOEVENT_NO_EVENTS_LISTED
else:
Expand Down Expand Up @@ -254,7 +254,7 @@ async def event_sync(
# Check that `start_time` is a decent time
re_check = re.match(r'^(\d{1,2})[-:.,;_]+(\d{1,2})', str(start_time))
if re_check:
timer_epoch = datetime_handling.get_dt() + int(countdown)
timer_epoch = await datetime_handling.get_dt() + int(countdown)
rel_start = f'<t:{timer_epoch}:R>'
timer_msg = await interaction.followup.send(
f'Sync til {re_check.group(1)}:{re_check.group(2)} {rel_start}'
Expand Down Expand Up @@ -294,12 +294,11 @@ async def event_announce(
# Get event
_guild = discord_commands.get_guild()
_event = _guild.get_scheduled_event(int(event))
rel_start = '<t:{}:R>'.format(
datetime_handling.get_dt(
format='epoch',
dt=_event.start_time.astimezone()
)
epoch_time = await datetime_handling.get_dt(
format='epoch',
dt=_event.start_time.astimezone()
)
rel_start = f'<t:{epoch_time}:R>'
announce_text = 'Minner om eventen som '\
'begynner {}, 30 min før kampstart'.format(
rel_start
Expand Down
2 changes: 1 addition & 1 deletion sausage_bot/cogs/dilemmas.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ async def setup(bot):
# Populate the inserts if json file exist
if file_io.file_exist(envs.dilemmas_file):
log.verbose('Found old json file')
dilemmas_inserts = db_helper.json_to_db_inserts(cog_name)
dilemmas_inserts = await db_helper.json_to_db_inserts(cog_name)
log.debug(f'`dilemmas_inserts` is {dilemmas_inserts}')

# Prep of DBs should only be done if the db files does not exist
Expand Down
4 changes: 2 additions & 2 deletions sausage_bot/cogs/poll.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ async def poll(
post_time, 'HHmm', 'local'
)
log.verbose(f'dt_post: {dt_post} ({type(dt_post)})')
dt_now = datetime_handling.get_dt()
dt_post_secs = datetime_handling.get_dt(dt=dt_post)
dt_now = await datetime_handling.get_dt()
dt_post_secs = await datetime_handling.get_dt(dt=dt_post)
if dt_post_secs < dt_now:
await interaction.followup.send(
'Posting time is in the past'
Expand Down
15 changes: 8 additions & 7 deletions sausage_bot/cogs/quote.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ async def get_random_quote():
# Post quote
quote_number = random_quote[0][0]
quote_text = random_quote[0][2]
quote_date = get_dt(
quote_date = await get_dt(
format='datetextfull',
dt=random_quote[0][3]
)
Expand All @@ -364,7 +364,7 @@ async def get_random_quote():
log.verbose(f'quote_out: {quote_out}')
if quote_out:
quote_text = quote_out[0][2]
quote_date = get_dt(
quote_date = await get_dt(
format='datetextfull',
dt=quote_out[0][3]
)
Expand Down Expand Up @@ -409,9 +409,9 @@ async def quote_add(
# Datetime will be saved as ISO8601:
# YYYY-MM-DD HH:MM:SS.SSS
if not quote_out['datetime']:
iso_date = str(get_dt(format='ISO8601'))
iso_date = str(await get_dt(format='ISO8601'))
else:
iso_date = get_dt(format='ISO8601', dt=quote_out['datetime'])
iso_date = await get_dt(format='ISO8601', dt=quote_out['datetime'])
log.verbose(f'iso_date: {iso_date}')
# Add the quote
await db_helper.insert_many_all(
Expand Down Expand Up @@ -456,7 +456,8 @@ async def quote_edit(
await interaction.followup.send(_msg, ephemeral=True)
return
if update_triggered:
log.verbose('Discovered changes in quote:', pretty=modal_in.quote_out)
log.verbose('Discovered changes in quote:',
pretty=modal_in.quote_out)
# Update quote
await db_helper.update_fields(
template_info=envs.quote_db_schema,
Expand All @@ -465,7 +466,7 @@ async def quote_edit(
],
updates=[
('quote_text', modal_in.quote_out['quote_text']),
('datetime', get_dt(
('datetime', await get_dt(
format='ISO8601',
dt=modal_in.quote_out['datetime']
))
Expand Down Expand Up @@ -556,7 +557,7 @@ async def setup(bot):
# Populate the inserts if json file exist
if file_io.file_exist(envs.quote_file):
log.verbose('Found old json file')
quote_inserts = db_helper.json_to_db_inserts(cog_name)
quote_inserts = await db_helper.json_to_db_inserts(cog_name)

# Prep of DB should only be done if the db files does not exist
quote_prep_is_ok = False
Expand Down
2 changes: 1 addition & 1 deletion sausage_bot/cogs/roles.py
Original file line number Diff line number Diff line change
Expand Up @@ -1658,7 +1658,7 @@ async def setup(bot):
if not file_io.file_exist(envs.roles_db_roles_schema['db_file']):
if file_io.file_exist(envs.roles_settings_file):
log.verbose('Found old json file')
roles_inserts = db_helper.json_to_db_inserts(cog_name)
roles_inserts = await db_helper.json_to_db_inserts(cog_name)
roles_inserts_msg = roles_inserts['msg_inserts']
roles_inserts_reactions = roles_inserts['reactions_inserts']
roles_inserts_settings = roles_inserts['settings_inserts']
Expand Down
3 changes: 1 addition & 2 deletions sausage_bot/cogs/rss.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ async def feed_name_autocomplete(
length_counter = 90
length_counter -= len(str(feed[1]))
length_counter -= len(str(feed[3]))

return [
discord.app_commands.Choice(
name='{feed_name}: #{channel} ({url})'.format(
Expand Down Expand Up @@ -523,7 +522,7 @@ async def setup(bot):
if file_io.file_exist(envs.rss_feeds_file) or\
file_io.file_exist(envs.rss_feeds_logs_file):
log.verbose('Found old json files')
rss_inserts = db_helper.json_to_db_inserts(cog_name)
rss_inserts = await db_helper.json_to_db_inserts(cog_name)
log.debug(f'Got these inserts:\n{rss_inserts}')

# Prep of DBs should only be done if the db files does not exist
Expand Down
14 changes: 7 additions & 7 deletions sausage_bot/cogs/stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -508,11 +508,11 @@ async def tabify(
)
log_stats = False
if date_exist:
if datetime_handling.get_dt(
format='date'
) > datetime_handling.get_dt(
date_now = await datetime_handling.get_dt(format='date')
date_exist = await datetime_handling.get_dt(
format='date', dt=date_exist
):
)
if date_now > date_exist:
log_stats = True
else:
log.verbose('Today has already been logged, skipping...')
Expand All @@ -521,7 +521,7 @@ async def tabify(
if log_stats:
stats_log_inserts.append(
(
str(datetime_handling.get_dt('ISO8601')),
str(await datetime_handling.get_dt('ISO8601')),
files_in_codebase, lines_in_codebase,
members['member_count']
)
Expand All @@ -537,7 +537,7 @@ async def tabify(
roles_members = await tabify(
dict_in=members['roles'], headers=['Rolle', 'Brukere']
)
dt_log = datetime_handling.get_dt('datetimefull')
dt_log = await datetime_handling.get_dt('datetimefull')
stats_msg = ''
log.debug('`show_role_stats` is {}'.format(
stats_settings['show_role_stats']
Expand Down Expand Up @@ -588,7 +588,7 @@ async def setup(bot):
if file_io.file_exist(envs.stats_file) or\
file_io.file_exist(envs.stats_logs_file):
log.verbose('Found old json files')
stats_file_inserts = db_helper.json_to_db_inserts(cog_name)
stats_file_inserts = await db_helper.json_to_db_inserts(cog_name)
stats_settings_inserts = stats_file_inserts['stats_inserts']
stats_log_inserts = stats_file_inserts['stats_logs_inserts']
log.debug(f'`stats_file_inserts` is \n{stats_file_inserts}')
Expand Down
2 changes: 1 addition & 1 deletion sausage_bot/cogs/youtube.py
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@ async def setup(bot):
if file_io.file_exist(envs.youtube_feeds_file) and \
file_io.file_exist(envs.youtube_feeds_logs_file):
log.verbose('Found old json file - feeds')
youtube_inserts = db_helper.json_to_db_inserts(cog_name)
youtube_inserts = await db_helper.json_to_db_inserts(cog_name)
log.debug(f'Got these inserts:\n{youtube_inserts}')

# Prep of DBs should only be done if the db files does not exist
Expand Down
6 changes: 3 additions & 3 deletions sausage_bot/docs/autodoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,17 @@
# import re


def dump(item):
async def dump(item):
'Prettydump the content of item and exit'
_dump = astpretty.pformat(item, indent=4, show_offsets=True)
if doc_args.file_out:
file_io.write_file(doc_envs.DOCS_DIR / doc_args.file_out, _dump)
else:
filename = 'dump-{}_{}.md'.format(
datetime_handling.get_dt(
await datetime_handling.get_dt(
format='revdate', sep='-'
),
datetime_handling.get_dt(
awaiat datetime_handling.get_dt(
format='timefull', sep='-'
)
)
Expand Down
53 changes: 53 additions & 0 deletions sausage_bot/test/locale_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
import pytest
import yaml
import ast
import astpretty
import glob
import sys

from sausage_bot.util import envs
from sausage_bot.docs.autodoc import get_funcs

'''
Get all python files
Look for I18N.t()
Get first link
Get that locale file
Compare links in python file with locale file
'''

filelist_py = glob.glob('**/*.py', recursive=True, root_dir=envs.ROOT_DIR)

#for file in filelist:

#_file = '{}/{}'.format(envs.ROOT_DIR, filelist[2])
_file = '/home/geir/coding/git/discord/sausage_bot/sausage_bot/testing/random_test.py'

i18n_check = {}

with open(_file) as fd:
file_contents = fd.read()
parsed_file = ast.parse(file_contents)
for node in ast.walk(parsed_file):
if isinstance(node, ast.Call):
try:
if node.func.value.id == 'I18N' and node.func.attr == 't':
i18n_line = str(node.args[0].value)
locale_splits = i18n_line.split('.')
locale_name = locale_splits[0]
locale_files = glob.glob(
'**/{}*.yml'.format(locale_name),
recursive=True, root_dir=envs.LOCALE_DIR
)
for locale_file in locale_files:
locale_file_full = '{}/{}'.format(
envs.LOCALE_DIR,
locale_file
)
with open(locale_file_full, 'r') as file_in:
locale_file_in = yaml.safe_load(file_in)
print(locale_file_in)
except:
pass
6 changes: 3 additions & 3 deletions sausage_bot/test/net_io_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
from sausage_bot.util import net_io


def test_make_event_start_stop():
async def test_make_event_start_stop():
date_yes, time_yes = ('17.05.2022', '21:00')
date_yes, time_no = ('17.05.2022', '671:00')

assert type(net_io.make_event_start_stop(date_yes, time_yes)) is dict
assert net_io.make_event_start_stop(date_yes, time_no) is None
assert type(await net_io.make_event_start_stop(date_yes, time_yes)) is dict
assert await net_io.make_event_start_stop(date_yes, time_no) is None
10 changes: 5 additions & 5 deletions sausage_bot/util/datetime_handling.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
pendulum.week_ends_at(pendulum.SUNDAY)


def make_dt(date_in):
async def make_dt(date_in):
'''
Make a datetime-object from string input
Expand Down Expand Up @@ -116,7 +116,7 @@ def make_dt(date_in):
if all(len(timeunit) == 2 for timeunit in d_split):
d = d_split
date_in = f'{d[0]} {d[1]} {d[2]}{d[3]} {d[4]} {d[5]}'
return pendulum.from_format(
return await pendulum.from_format(
date_in, 'DD MM YYYY HH mm'
)
pass
Expand All @@ -139,7 +139,7 @@ def make_dt(date_in):
return None


def get_dt(format='epoch', sep='.', dt=False):
async def get_dt(format='epoch', sep='.', dt=False):
'''
Get a datetime object in preferred dateformat.
Expand Down Expand Up @@ -180,10 +180,10 @@ def get_dt(format='epoch', sep='.', dt=False):
'''
if isinstance(dt, datetime.datetime):
log.debug('Input is a datetime object')
dt = make_dt(str(dt))
dt = await make_dt(str(dt))
if isinstance(dt, str):
log.debug('Input is a string')
dt = make_dt(dt)
dt = await make_dt(dt)
if dt is None:
print('Can\'t process date `{}`. Aborting.'.format(dt))
return None
Expand Down
8 changes: 4 additions & 4 deletions sausage_bot/util/db_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ async def prep_table(
return delete_json_ok


def json_to_db_inserts(cog_name):
async def json_to_db_inserts(cog_name):
'''
This is a cleanup function to be used for converting from old json
files to sqlite files
Expand Down Expand Up @@ -155,7 +155,7 @@ def json_to_db_inserts(cog_name):
quotes_inserts.append(
(
str(uuid4()), quote_file[quote]['quote'],
get_dt(
await get_dt(
format="ISO8601", dt=re.sub(
r'[\(\)]+', '',
quote_file[quote]['datetime']
Expand Down Expand Up @@ -277,7 +277,7 @@ def json_to_db_inserts(cog_name):
rss_logs_inserts.append(
(
rss_logs_index[feed], link,
str(get_dt(format='ISO8601'))
str(await get_dt(format='ISO8601'))
)
)
return {
Expand Down Expand Up @@ -330,7 +330,7 @@ def json_to_db_inserts(cog_name):
yt_logs_inserts.append(
(
yt_logs_index[feed], link,
str(get_dt(format='ISO8601'))
str(await get_dt(format='ISO8601'))
)
)
return {
Expand Down
Loading

0 comments on commit f666c54

Please sign in to comment.