From 2b18c1d19584346d17ed24f9b03e4f5a82b45e69 Mon Sep 17 00:00:00 2001 From: Qiuyu Chen <34405372+qchen59@users.noreply.github.com> Date: Tue, 26 Oct 2021 19:02:43 -0400 Subject: [PATCH 01/22] Update the Zenodo --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index a955a71..8859b3e 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ ![Python v3.9](https://img.shields.io/badge/python-v3.9-blue) [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT) -[![DOI](https://zenodo.org/badge/403393616.svg)](https://zenodo.org/badge/latestdoi/403393616) +[![DOI](https://zenodo.org/badge/419116957.svg)](https://zenodo.org/badge/latestdoi/419116957) ![GitHub code size in bytes](https://img.shields.io/github/languages/code-size/lyonva/ScheduleBot) [![GitHub issues](https://img.shields.io/github/issues/lyonva/ScheduleBot)](https://github.com/lyonva/ScheduleBot/issues) [![GitHub release (latest by date including pre-releases)](https://img.shields.io/github/v/release/lyonva/ScheduleBot?include_prereleases)](https://github.com/lyonva/ScheduleBot/releases) From e3f63807a6eab2851adfac72c059b6cb41b21982 Mon Sep 17 00:00:00 2001 From: Qiuyu Chen <34405372+qchen59@users.noreply.github.com> Date: Tue, 26 Oct 2021 22:01:41 -0400 Subject: [PATCH 02/22] Create travis_CI --- .travis.yml | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 .travis.yml diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..f15cc94 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,16 @@ +language: python +python: + - "3.9" +# command to install dependencies +before_install: + - "pip install -U pip" + - "export PYTHONPATH=$PYTHONPATH:$(pwd)" + - "pip install coverage" + - "pip install coveralls" +install: + - pip install -r requirements.txt +# command to run tests +script: + - "coverage run --source 'code' -m pytest" +after_success: + coveralls From b44ab155526e0ab73663ac304f77eeaef3b6fb4d Mon Sep 17 00:00:00 2001 From: Qiuyu Chen <34405372+qchen59@users.noreply.github.com> Date: Tue, 26 Oct 2021 22:20:11 -0400 Subject: [PATCH 03/22] Update the travis_CI badge --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 8859b3e..38aba1b 100644 --- a/README.md +++ b/README.md @@ -3,6 +3,7 @@ ![Python v3.9](https://img.shields.io/badge/python-v3.9-blue) [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT) [![DOI](https://zenodo.org/badge/419116957.svg)](https://zenodo.org/badge/latestdoi/419116957) +[![Build Status](https://app.travis-ci.com/qchen59/ScheduleBot.svg?branch=main)](https://app.travis-ci.com/github/qchen59/ScheduleBot) ![GitHub code size in bytes](https://img.shields.io/github/languages/code-size/lyonva/ScheduleBot) [![GitHub issues](https://img.shields.io/github/issues/lyonva/ScheduleBot)](https://github.com/lyonva/ScheduleBot/issues) [![GitHub release (latest by date including pre-releases)](https://img.shields.io/github/v/release/lyonva/ScheduleBot?include_prereleases)](https://github.com/lyonva/ScheduleBot/releases) From 641009ff3781b5151b8b9cfd036b8c3a5e6eb38e Mon Sep 17 00:00:00 2001 From: Qiuyu Chen <34405372+qchen59@users.noreply.github.com> Date: Tue, 26 Oct 2021 22:26:33 -0400 Subject: [PATCH 04/22] Update the source for coverage --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index f15cc94..3d15b60 100644 --- a/.travis.yml +++ b/.travis.yml @@ -11,6 +11,6 @@ install: - pip install -r requirements.txt # command to run tests script: - - "coverage run --source 'code' -m pytest" + - "coverage run --source 'src' -m pytest" after_success: coveralls From 9e5346915ff89931d05334d92ea4e5cca34412be Mon Sep 17 00:00:00 2001 From: Qiuyu Chen <34405372+qchen59@users.noreply.github.com> Date: Tue, 26 Oct 2021 22:41:59 -0400 Subject: [PATCH 05/22] Add omit files --- .coveragerc | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 .coveragerc diff --git a/.coveragerc b/.coveragerc new file mode 100644 index 0000000..1ac516b --- /dev/null +++ b/.coveragerc @@ -0,0 +1,2 @@ +[run] +omit = ./src/schedulebot.py, ./src/__init__.py.py, From 9db9f9d3483de8ab7270a3e5ca8a0ec4109a5bd9 Mon Sep 17 00:00:00 2001 From: Qiuyu Chen <34405372+qchen59@users.noreply.github.com> Date: Tue, 26 Oct 2021 22:50:58 -0400 Subject: [PATCH 06/22] Update the coverage and other badges --- README.md | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 38aba1b..86250c3 100644 --- a/README.md +++ b/README.md @@ -4,13 +4,12 @@ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT) [![DOI](https://zenodo.org/badge/419116957.svg)](https://zenodo.org/badge/latestdoi/419116957) [![Build Status](https://app.travis-ci.com/qchen59/ScheduleBot.svg?branch=main)](https://app.travis-ci.com/github/qchen59/ScheduleBot) -![GitHub code size in bytes](https://img.shields.io/github/languages/code-size/lyonva/ScheduleBot) -[![GitHub issues](https://img.shields.io/github/issues/lyonva/ScheduleBot)](https://github.com/lyonva/ScheduleBot/issues) -[![GitHub release (latest by date including pre-releases)](https://img.shields.io/github/v/release/lyonva/ScheduleBot?include_prereleases)](https://github.com/lyonva/ScheduleBot/releases) -[![GitHub all releases](https://img.shields.io/github/downloads/lyonva/ScheduleBot/total)](https://github.com/lyonva/ScheduleBot/releases) +![GitHub code size in bytes](https://img.shields.io/github/languages/code-size/qchen59/ScheduleBot) +[![GitHub issues](https://img.shields.io/github/issues/qchen59/ScheduleBot)](https://github.com/qchen59/ScheduleBot/issues) +[![GitHub release (latest by date including pre-releases)](https://img.shields.io/github/v/release/qchen59/ScheduleBot?include_prereleases)](https://github.com/qchen59/ScheduleBot/releases) +[![GitHub all releases](https://img.shields.io/github/downloads/qchen59/ScheduleBot/total)](https://github.com/qchen59/ScheduleBot/releases) [![Platform](https://img.shields.io/badge/platform-discord-blue)](https://discord.com/) -[![Test cases](https://github.com/lyonva/ScheduleBot/actions/workflows/python-app.yml/badge.svg)](https://github.com/lyonva/ScheduleBot/actions/workflows/python-app.yml) -[![Code coverage](https://raw.githubusercontent.com/lyonva/ScheduleBot/main/docs/img/coverage.svg)](https://github.com/lyonva/ScheduleBot/actions/workflows/python-app.yml) +[![Coverage Status](https://coveralls.io/repos/github/qchen59/ScheduleBot/badge.svg?branch=main)](https://coveralls.io/github/qchen59/ScheduleBot?branch=main) # ScheduleBot @@ -71,7 +70,8 @@ The bot will ask you for the name of the type and your preferred times. ## Releases -- [All releases](https://github.com/lyonva/ScheduleBot/releases) +- [All releases](https://github.com/ +/ScheduleBot/releases) - Latest: [v0](https://github.com/lyonva/ScheduleBot/releases/tag/v0) ## Documentation From cfc55406404ddc5e58c07398d20f342376eb362a Mon Sep 17 00:00:00 2001 From: Qiuyu Chen Date: Wed, 27 Oct 2021 02:36:16 -0400 Subject: [PATCH 07/22] Fix typo, fix write event file bugs --- src/functionality/create_event_type.py | 2 +- src/functionality/shared_functions.py | 36 ++++++++++---------------- 2 files changed, 14 insertions(+), 24 deletions(-) diff --git a/src/functionality/create_event_type.py b/src/functionality/create_event_type.py index fa8d308..30a49f3 100644 --- a/src/functionality/create_event_type.py +++ b/src/functionality/create_event_type.py @@ -162,7 +162,7 @@ def check(m): rows.append(line) continue else: - await channel.send("Inalid input, Time range is not changed.") + await channel.send("Invalid input, Time range is not changed.") rows.append(line) continue else: diff --git a/src/functionality/shared_functions.py b/src/functionality/shared_functions.py index e446a95..00870ee 100644 --- a/src/functionality/shared_functions.py +++ b/src/functionality/shared_functions.py @@ -1,7 +1,7 @@ import os import csv from pathlib import Path -from Event import Event +from src.Event import Event from datetime import datetime @@ -147,7 +147,6 @@ def read_event_file(user_id): with open(os.path.expanduser("~/Documents") + "/ScheduleBot/Event/" + user_id + ".csv", "r") as calendar_lines: calendar_lines = csv.reader(calendar_lines, delimiter=",") rows = [] - # Stores the current row in an array of rows if the row is not a new-line character # This check prevents an accidental empty lines from being kept in the updated file for row in calendar_lines: @@ -168,9 +167,8 @@ def add_event_to_file(user_id, current): line_number = 0 rows = read_event_file(user_id) # If the file already has events - if len(rows) > 0: + if len(rows) > 1: for i in rows: - # Skips check with empty lines if len(i) > 0 and line_number != 0: @@ -182,7 +180,6 @@ def add_event_to_file(user_id, current): "", "", ) - # If the current Event occurs before the temp Event, insert the current at that position if current < temp_event: rows.insert(line_number, [""] + current.to_list()) @@ -193,22 +190,15 @@ def add_event_to_file(user_id, current): rows.insert(len(rows), [""] + current.to_list()) break line_number += 1 - - # Open current user's calendar file for writing - with open( - os.path.expanduser("~/Documents") + "/ScheduleBot/Event/" + user_id + ".csv", - "w", - newline="", - ) as calendar_file: - # Write to column headers and array of rows back to the calendar file - csvwriter = csv.writer(calendar_file) - csvwriter.writerows(rows) - # If the file has no events, add the current Event to the file else: - with open( - os.path.expanduser("~/Documents") + "/ScheduleBot/Event/" + user_id + ".csv", - "w", - newline="", - ) as calendar_file: - csvwriter = csv.writer(calendar_file) - csvwriter.writerow([""] + current.to_list()) + rows.insert(len(rows), [""] + current.to_list()) + # Open current user's calendar file for writing + with open( + os.path.expanduser("~/Documents") + "/ScheduleBot/Event/" + user_id + ".csv", + "w", + newline="", + ) as calendar_file: + # Write to column headers and array of rows back to the calendar file + csvwriter = csv.writer(calendar_file) + csvwriter.writerows(rows) + From 51f04b027f19e889995807e14361dcf76aa6a889 Mon Sep 17 00:00:00 2001 From: hughman98 <30803969+hughman98@users.noreply.github.com> Date: Wed, 27 Oct 2021 14:35:33 -0400 Subject: [PATCH 08/22] Add exportfile to help command --- src/schedulebot.py | 1 + 1 file changed, 1 insertion(+) diff --git a/src/schedulebot.py b/src/schedulebot.py index 082df4d..52d2ef1 100644 --- a/src/schedulebot.py +++ b/src/schedulebot.py @@ -38,6 +38,7 @@ async def help(ctx): em.add_field(name="day", value="Shows everything on your schedule for today", inline=False) em.add_field(name="typecreate", value="Creates a new event type", inline=True) em.add_field(name="typedelete", value="Deletes an event type", inline=True) + em.add_field(name="exportfile", value="Exports a CSV file of your events", inline=True) await ctx.send(embed=em) From cbdbdff428d7b2e38b5cd741f6068175111b2baf Mon Sep 17 00:00:00 2001 From: hughman98 <30803969+hughman98@users.noreply.github.com> Date: Wed, 27 Oct 2021 14:35:37 -0400 Subject: [PATCH 09/22] Create export_file.py --- src/functionality/export_file.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 src/functionality/export_file.py diff --git a/src/functionality/export_file.py b/src/functionality/export_file.py new file mode 100644 index 0000000..f773c79 --- /dev/null +++ b/src/functionality/export_file.py @@ -0,0 +1,19 @@ +import os +import csv +from shared_functions import create_event_file + + +async def export_file(ctx, client): + channel = await ctx.author.create_dm() + print(ctx.author.id) + + def check(m): + return m.content is not None and m.channel == channel and m.author == ctx.author + + user = str(ctx.author.id) + + # Checks if the calendar csv file exists, and creates it if it does not + if not os.path.exists(os.path.expanduser("~/Documents") + "/ScheduleBot/Type/" + str(user) + ".csv"): + create_event_file(user) + + channel.send("test") From e58b58a781fb991cf411850911b049249d54af3a Mon Sep 17 00:00:00 2001 From: hughman98 <30803969+hughman98@users.noreply.github.com> Date: Wed, 27 Oct 2021 14:45:23 -0400 Subject: [PATCH 10/22] Add export_file function --- src/schedulebot.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/schedulebot.py b/src/schedulebot.py index 52d2ef1..29d0216 100644 --- a/src/schedulebot.py +++ b/src/schedulebot.py @@ -11,6 +11,7 @@ from functionality.FindAvailableTime import find_avaialbleTime from functionality.delete_event_type import delete_event_type from functionality.DisplayFreeTime import get_free_time +from functionality.export_file import export_file bot = commands.Bot(command_prefix="!") # Creates the bot with a command prefix of '!' bot.remove_command("help") # Removes the help command, so it can be created using Discord embed pages later @@ -139,6 +140,20 @@ async def day(ctx): """ await get_highlight(ctx) +@bot.event +async def exportfile(ctx): + """ + Function: + exportfile + Description: + Sends the user a CSV file containing their scheduled events. + Input: + ctx - Discord context window + Output: + - A CSV file sent to the context that contains a user's scheduled events. + """ + + await export_file(ctx) # creating new event type @bot.command() From b8f3d945b5e3a1697551fe735e3b9ee59f3a4473 Mon Sep 17 00:00:00 2001 From: hughman98 <30803969+hughman98@users.noreply.github.com> Date: Wed, 27 Oct 2021 14:50:24 -0400 Subject: [PATCH 11/22] Bugfix --- src/functionality/export_file.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/functionality/export_file.py b/src/functionality/export_file.py index f773c79..4490a8f 100644 --- a/src/functionality/export_file.py +++ b/src/functionality/export_file.py @@ -1,6 +1,6 @@ import os import csv -from shared_functions import create_event_file +from functionality.shared_functions import create_event_file async def export_file(ctx, client): From ec0c49ba6b28365aacc42a82c0dfd99e38f6b0e8 Mon Sep 17 00:00:00 2001 From: hughman98 <30803969+hughman98@users.noreply.github.com> Date: Wed, 27 Oct 2021 14:50:55 -0400 Subject: [PATCH 12/22] bugfix --- src/schedulebot.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/schedulebot.py b/src/schedulebot.py index 29d0216..b7b9600 100644 --- a/src/schedulebot.py +++ b/src/schedulebot.py @@ -39,7 +39,7 @@ async def help(ctx): em.add_field(name="day", value="Shows everything on your schedule for today", inline=False) em.add_field(name="typecreate", value="Creates a new event type", inline=True) em.add_field(name="typedelete", value="Deletes an event type", inline=True) - em.add_field(name="exportfile", value="Exports a CSV file of your events", inline=True) + em.add_field(name="exportfile", value="Exports a CSV file of your events", inline=False) await ctx.send(embed=em) From f16ed45264eee38145a512e549511d7a82921e7e Mon Sep 17 00:00:00 2001 From: hughman98 <30803969+hughman98@users.noreply.github.com> Date: Wed, 27 Oct 2021 14:55:24 -0400 Subject: [PATCH 13/22] bugfix --- src/schedulebot.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/schedulebot.py b/src/schedulebot.py index b7b9600..c86ce24 100644 --- a/src/schedulebot.py +++ b/src/schedulebot.py @@ -140,7 +140,7 @@ async def day(ctx): """ await get_highlight(ctx) -@bot.event +@bot.command() async def exportfile(ctx): """ Function: From dfe29f9c01bc22fa3995aecc2e4981b0e8d2d453 Mon Sep 17 00:00:00 2001 From: hughman98 <30803969+hughman98@users.noreply.github.com> Date: Wed, 27 Oct 2021 15:17:21 -0400 Subject: [PATCH 14/22] Added exportfile functionality --- src/functionality/export_file.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/functionality/export_file.py b/src/functionality/export_file.py index 4490a8f..12d174b 100644 --- a/src/functionality/export_file.py +++ b/src/functionality/export_file.py @@ -1,19 +1,20 @@ import os import csv +import discord from functionality.shared_functions import create_event_file -async def export_file(ctx, client): +async def export_file(ctx): channel = await ctx.author.create_dm() print(ctx.author.id) def check(m): return m.content is not None and m.channel == channel and m.author == ctx.author - user = str(ctx.author.id) + user_id = str(ctx.author.id) # Checks if the calendar csv file exists, and creates it if it does not - if not os.path.exists(os.path.expanduser("~/Documents") + "/ScheduleBot/Type/" + str(user) + ".csv"): - create_event_file(user) + if not os.path.exists(os.path.expanduser("~/Documents") + "/ScheduleBot/Event/" + user_id + ".csv"): + create_event_file(user_id) - channel.send("test") + await channel.send(file=discord.File(os.path.expanduser("~/Documents") + "/ScheduleBot/Event/" + user_id + ".csv")) From c55f49113a8266f3e4f504232ae87694e9b770af Mon Sep 17 00:00:00 2001 From: hughman98 <30803969+hughman98@users.noreply.github.com> Date: Wed, 27 Oct 2021 16:31:08 -0400 Subject: [PATCH 15/22] Add importfile to help menu --- src/schedulebot.py | 1 + 1 file changed, 1 insertion(+) diff --git a/src/schedulebot.py b/src/schedulebot.py index c86ce24..186ff78 100644 --- a/src/schedulebot.py +++ b/src/schedulebot.py @@ -40,6 +40,7 @@ async def help(ctx): em.add_field(name="typecreate", value="Creates a new event type", inline=True) em.add_field(name="typedelete", value="Deletes an event type", inline=True) em.add_field(name="exportfile", value="Exports a CSV file of your events", inline=False) + em.add_field(name="importfile", value="Import events from a CSV file", inline=False) await ctx.send(embed=em) From 41c71e5d96676791ae65b15a7774ca5b0e1a2c62 Mon Sep 17 00:00:00 2001 From: hughman98 <30803969+hughman98@users.noreply.github.com> Date: Wed, 27 Oct 2021 16:36:07 -0400 Subject: [PATCH 16/22] Create import_file.py --- src/functionality/import_file.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 src/functionality/import_file.py diff --git a/src/functionality/import_file.py b/src/functionality/import_file.py new file mode 100644 index 0000000..12d174b --- /dev/null +++ b/src/functionality/import_file.py @@ -0,0 +1,20 @@ +import os +import csv +import discord +from functionality.shared_functions import create_event_file + + +async def export_file(ctx): + channel = await ctx.author.create_dm() + print(ctx.author.id) + + def check(m): + return m.content is not None and m.channel == channel and m.author == ctx.author + + user_id = str(ctx.author.id) + + # Checks if the calendar csv file exists, and creates it if it does not + if not os.path.exists(os.path.expanduser("~/Documents") + "/ScheduleBot/Event/" + user_id + ".csv"): + create_event_file(user_id) + + await channel.send(file=discord.File(os.path.expanduser("~/Documents") + "/ScheduleBot/Event/" + user_id + ".csv")) From 46031cfa483f91da091d673ed604a959ec6283ad Mon Sep 17 00:00:00 2001 From: hughman98 <30803969+hughman98@users.noreply.github.com> Date: Wed, 27 Oct 2021 17:07:11 -0400 Subject: [PATCH 17/22] Add importfile function --- src/schedulebot.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/schedulebot.py b/src/schedulebot.py index 186ff78..4c988b1 100644 --- a/src/schedulebot.py +++ b/src/schedulebot.py @@ -12,6 +12,7 @@ from functionality.delete_event_type import delete_event_type from functionality.DisplayFreeTime import get_free_time from functionality.export_file import export_file +from functionality.import_file import import_file bot = commands.Bot(command_prefix="!") # Creates the bot with a command prefix of '!' bot.remove_command("help") # Removes the help command, so it can be created using Discord embed pages later @@ -156,6 +157,21 @@ async def exportfile(ctx): await export_file(ctx) +@bot.command() +async def importfile(ctx): + """ + Function: + importfile + Description: + Reads a CSV file containing events submitted by the user, and adds those events + Input: + ctx - Discord context window + Output: + - Events are added to a users profile. + """ + + await import_file(ctx, bot) + # creating new event type @bot.command() async def typecreate(ctx): From e7b3435d0ca995ccd806df5e0824bf6810fc8ccf Mon Sep 17 00:00:00 2001 From: hughman98 <30803969+hughman98@users.noreply.github.com> Date: Wed, 27 Oct 2021 17:26:15 -0400 Subject: [PATCH 18/22] Added basic function skeleton --- src/functionality/import_file.py | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/functionality/import_file.py b/src/functionality/import_file.py index 12d174b..9990a17 100644 --- a/src/functionality/import_file.py +++ b/src/functionality/import_file.py @@ -1,20 +1,22 @@ import os import csv import discord -from functionality.shared_functions import create_event_file +import pandas as pd +from functionality.shared_functions import create_event_tree, create_type_tree, add_event_to_file, turn_types_to_string +from Event import Event -async def export_file(ctx): +async def import_file(ctx, client): channel = await ctx.author.create_dm() - print(ctx.author.id) def check(m): - return m.content is not None and m.channel == channel and m.author == ctx.author + return len(m.attachments) == 1 and m.channel == channel and m.author == ctx.author user_id = str(ctx.author.id) # Checks if the calendar csv file exists, and creates it if it does not - if not os.path.exists(os.path.expanduser("~/Documents") + "/ScheduleBot/Event/" + user_id + ".csv"): - create_event_file(user_id) + await channel.send("Please provide your file below:") + event_msg = await client.wait_for("message", check=check) + + print(event_msg.attachments[0]) - await channel.send(file=discord.File(os.path.expanduser("~/Documents") + "/ScheduleBot/Event/" + user_id + ".csv")) From 1e26fcdb087f63ab2fe72a3a5d52152da8480633 Mon Sep 17 00:00:00 2001 From: hughman98 <30803969+hughman98@users.noreply.github.com> Date: Wed, 27 Oct 2021 17:26:31 -0400 Subject: [PATCH 19/22] Add pandas to requirements --- requirements.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/requirements.txt b/requirements.txt index 2758169..5dc5a3c 100644 --- a/requirements.txt +++ b/requirements.txt @@ -6,3 +6,4 @@ coverage>=5.0.1 coverage-badge lark pdoc3 +pandas \ No newline at end of file From d4d2dd4d410224f969527b3aea38075d72a20aca Mon Sep 17 00:00:00 2001 From: hughman98 <30803969+hughman98@users.noreply.github.com> Date: Wed, 27 Oct 2021 18:49:03 -0400 Subject: [PATCH 20/22] Added verify_csv function --- src/functionality/import_file.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/functionality/import_file.py b/src/functionality/import_file.py index 9990a17..5c8361a 100644 --- a/src/functionality/import_file.py +++ b/src/functionality/import_file.py @@ -5,6 +5,32 @@ from functionality.shared_functions import create_event_tree, create_type_tree, add_event_to_file, turn_types_to_string from Event import Event +def verify_csv(data): + """ + Function: + verify_csv + Description: + Verifies that CSV data retrieved through pandas matches the expected format + Input: + data - A Pandas Dataframe of data pulled from a CSV + Output: + - True if the data matches the expectation, false otherwise + """ + + if data.columns[0] != "ID": + return False + if data.columns[1] != "Name": + return False + if data.columns[2] != "Start Date": + return False + if data.columns[3] != "End Date": + return False + if data.columns[4] != "Type": + return False + if data.columns[5] != "Notes": + return False + + return True async def import_file(ctx, client): channel = await ctx.author.create_dm() From 85685e6f39af7eaaff5c679a2872c21faa1ad5d6 Mon Sep 17 00:00:00 2001 From: hughman98 <30803969+hughman98@users.noreply.github.com> Date: Wed, 27 Oct 2021 18:49:21 -0400 Subject: [PATCH 21/22] Added convert_time function --- src/functionality/import_file.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/src/functionality/import_file.py b/src/functionality/import_file.py index 5c8361a..6cafb58 100644 --- a/src/functionality/import_file.py +++ b/src/functionality/import_file.py @@ -32,6 +32,35 @@ def verify_csv(data): return True +def convert_time(old_str): + """ + Function: + convert_time + Description: + Converts a time string from YYYY-MM-DD HH:MM:SS format to mm/dd/yy hh:mm am/pm format + Input: + old_str - The string to be converted + Output: + - the converted string + """ + + new_str = old_str[5:7] + '/' + old_str[8:10] + '/' + old_str[2:4] + ' ' + + hour_int = int(old_str[11:13]) + if (hour_int >= 12): + ap = "pm" + hour_int = hour_int - 12 + else: + ap = "am" + + hour = str(hour_int) + if len(hour) == 1: + hour = '0' + hour + + new_str = new_str + hour + ':' + old_str[14:16] + ap + + return new_str + async def import_file(ctx, client): channel = await ctx.author.create_dm() From 6344d1aff0c499d44b01592e959bc9506692b2d9 Mon Sep 17 00:00:00 2001 From: hughman98 <30803969+hughman98@users.noreply.github.com> Date: Wed, 27 Oct 2021 18:49:38 -0400 Subject: [PATCH 22/22] Added import file functionality --- src/functionality/import_file.py | 36 ++++++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 4 deletions(-) diff --git a/src/functionality/import_file.py b/src/functionality/import_file.py index 6cafb58..5b2a7b4 100644 --- a/src/functionality/import_file.py +++ b/src/functionality/import_file.py @@ -2,8 +2,11 @@ import csv import discord import pandas as pd +import tempfile +from discord import Attachment from functionality.shared_functions import create_event_tree, create_type_tree, add_event_to_file, turn_types_to_string from Event import Event +from parse.match import parse_period def verify_csv(data): """ @@ -65,13 +68,38 @@ async def import_file(ctx, client): channel = await ctx.author.create_dm() def check(m): - return len(m.attachments) == 1 and m.channel == channel and m.author == ctx.author + return m.content is not None and m.channel == channel and m.author == ctx.author user_id = str(ctx.author.id) # Checks if the calendar csv file exists, and creates it if it does not - await channel.send("Please provide your file below:") - event_msg = await client.wait_for("message", check=check) + await channel.send("Please upload your file below.") - print(event_msg.attachments[0]) + # Loops until we receive a file. + while True: + event_msg = await client.wait_for("message", check=check) + + if len(event_msg.attachments) != 1: + await channel.send("No file detected. Please upload your your file below.\nYou can do this by dropping " + "the file directly into Discord. Do not write out the file contents in the message.") + else: + break + + temp_file = tempfile.TemporaryFile() + await event_msg.attachments[0].save(fp=temp_file.file, seek_begin=True, use_cached=False) + data = pd.read_csv(temp_file) + + if not verify_csv(data): + await channel.send("Unexpected CSV Format. Import has failed.") + return + + # creates an event tree if one doesn't exist yet. + create_event_tree(str(ctx.author.id)) + + for index, row in data.iterrows(): + time_period = parse_period(convert_time(row['Start Date']) + ' ' + convert_time(row['End Date'])) + current = Event(row['Name'], time_period[0], time_period[1], row['Type'], row['Notes']) + add_event_to_file(str(ctx.author.id), current) + + await channel.send("Your events were successfully added!")