Implement add-run.py

This commit is contained in:
Zachary Talis 2021-06-25 13:35:35 -04:00
parent a8405dcba7
commit 397f3ecf8c
6 changed files with 139 additions and 20 deletions

35
a Normal file
View file

@ -0,0 +1,35 @@
diff --git a/scripts/add-run.py b/scripts/add-run.py
index e69de29..c2ab3a8 100644
--- a/scripts/add-run.py
+++ b/scripts/add-run.py
@@ -0,0 +1,30 @@
+### Script for adding a new category. Handles adding data to config.csv.
+
+from utils import csv as util_csv
+
+divider = "----------"
+
+print(
+ f"\nWe'll ask for the name, description, and rules of the new category.\n\n{divider}\n"
+)
+
+# tk_category_name, tk_category_dashname, tk_category_description, tk_category_rules
+
+tk_category_name = input("Name: ")
+tk_category_description = input("Description: ")
+tk_category_rules = input("Rules: ")
+
+tk_category_dashname = tk_category_name.replace(" ", "_")
+
+categoryDict = {
+ "tk_category_name": tk_category_name,
+ "tk_category_dashname": tk_category_dashname,
+ "tk_category_description": tk_category_description,
+ "tk_category_rules": tk_category_rules,
+}
+
+util_csv.dictWriter("../csv/config.csv", categoryDict, "a")
+
+print(
+ f"\n{divider}\n\nAdded category! If you made a mistake, you can manually edit csv/categories.csv (and csv/runs.csv if you've added any runs to the borked category). \n"
+)

View file

@ -1 +1 @@
tk_run_id,tk_run_date,tk_run_game,tk_run_category,tk_run_runner,tk_run_verifier,tk_run_link,tk_run_description tk_run_id,tk_run_runner,tk_run_verifier,tk_run_category_dashname,tk_run_date,tk_run_description,tk_run_link

1 tk_run_id tk_run_runner tk_run_game tk_run_verifier tk_run_category tk_run_category_dashname tk_run_date tk_run_description tk_run_link

View file

@ -8,8 +8,6 @@ print(
f"\nWe'll ask for the name, description, and rules of the new category.\n\n{divider}\n" f"\nWe'll ask for the name, description, and rules of the new category.\n\n{divider}\n"
) )
# tk_category_name, tk_category_dashname, tk_category_description, tk_category_rules
tk_category_name = input("Name: ") tk_category_name = input("Name: ")
tk_category_description = input("Description: ") tk_category_description = input("Description: ")
tk_category_rules = input("Rules: ") tk_category_rules = input("Rules: ")
@ -17,13 +15,13 @@ tk_category_rules = input("Rules: ")
tk_category_dashname = tk_category_name.replace(" ", "_") tk_category_dashname = tk_category_name.replace(" ", "_")
categoryDict = { categoryDict = {
"tk_category_name": tk_category_name,
"tk_category_dashname": tk_category_dashname, "tk_category_dashname": tk_category_dashname,
"tk_category_name": tk_category_name,
"tk_category_description": tk_category_description, "tk_category_description": tk_category_description,
"tk_category_rules": tk_category_rules, "tk_category_rules": tk_category_rules,
} }
util_csv.dictWriter("../csv/config.csv", categoryDict, "a") util_csv.dictWriter("../csv/categories.csv", categoryDict, "a")
print( print(
f"\n{divider}\n\nAdded category! If you made a mistake, you can manually edit csv/categories.csv (and csv/runs.csv if you've added any runs to the borked category). \n" f"\n{divider}\n\nAdded category! If you made a mistake, you can manually edit csv/categories.csv (and csv/runs.csv if you've added any runs to the borked category). \n"

View file

@ -0,0 +1,86 @@
### Script for adding a new category. Handles adding data to config.csv.
from utils import csv as util_csv
import os
## Check to see if there's at least one category ##
categoriesOld = util_csv.dictReaderMultiRow(
"../csv/categories.csv", "tk_category_dashname"
)
if len(categoriesOld.keys()) == 0:
print("Add a category first (using scripts/add-category.py)")
os._exit(1)
divider = "----------"
print(
f"\nWe'll ask for the runner, verifier, category, run date, category, description of, and recording link of the new run.\n\n{divider}\n"
)
tk_run_id = 1
existingRuns = util_csv.dictReaderMultiRow("../csv/runs.csv", "tk_run_id")
for id in existingRuns:
tk_run_id = int(id) + 1
tk_run_runner = input("Runner: ")
tk_run_verifier = input("Verifier: ")
## End of: Check to see if there's at least one category ##
## Handle input of tk_run_category_dashname ##
doneCategoryInput = False
displayName = "tk_category_name"
while not doneCategoryInput:
print("\nCategory (input a number)")
index = 0
categoriesNew = {}
for category in categoriesOld:
categoriesNew[index] = categoriesOld[category]
print(f"{index} - {categoriesNew[index][displayName]}")
index += 1
try:
try:
rawCategoryInput = input("Your pick: ")
except KeyboardInterrupt:
os._exit(1)
categoryInput = int(rawCategoryInput)
if categoryInput <= index:
try:
tk_run_category_dashname = categoriesNew[categoryInput][
"tk_category_dashname"
]
doneCategoryInput = True
except:
print("Not a valid choice!")
else:
print("Not a valid choice!")
except:
print("Not a number!")
print(f"You picked - {categoriesNew[categoryInput][displayName]}\n")
## End of: Handle input of tk_run_category_dashname ##
tk_run_date = input("Date (format - MM/DD/YYYY): ")
tk_run_description = input("Description: ")
tk_run_link = input("Recording link (format - https://foo.bar): ")
runDict = {
"tk_run_id": tk_run_id,
"tk_run_runner": tk_run_runner,
"tk_run_verifier": tk_run_verifier,
"tk_run_category_dashname": tk_run_category_dashname,
"tk_run_date": tk_run_date,
"tk_run_description": tk_run_description,
"tk_run_link": tk_run_link,
}
util_csv.dictWriter("../csv/runs.csv", runDict, "a")
print(
f"\n{divider}\n\nAdded run! If you made a mistake, you can manually edit csv/runs.csv\n"
)

View file

@ -10,7 +10,7 @@ print(
tk_game_name = input("Your game's name: ") tk_game_name = input("Your game's name: ")
tk_game_description = input("Description for your game: ") tk_game_description = input("Description for your game: ")
tk_url = input("URL of your site (format example - foobar.com): ") tk_url = input("URL of your site (format - foobar.com): ")
tk_logo_alt = input("Description of your game's logo (this is used for alt-text): ") tk_logo_alt = input("Description of your game's logo (this is used for alt-text): ")
configDict = { configDict = {

View file

@ -1,15 +1,14 @@
### Util library for interacting with csv files ### Util library for interacting with csv files
import csv import csv
import os
def readerWithFunction(filepath, function, arg=None): def readerWithFunction(filepath, function, arg=None):
"""Read from a csv and execute a function using each row. Return a sequential list of all values returned by the function. """Read from a csv and execute a function using each row. Return a sequential list of all values returned by the function.
filepath -- the path of the csv we're reading filepath -- the path of the csv we're reading\n
function -- the function we're executing once per row function -- the function we're executing once per row\n
arg -- if the function takes an additional argument, we pass arg (default None) arg -- if the function takes an additional argument, we pass arg (default None)\n
""" """
with open(filepath, newline="") as file: with open(filepath, newline="") as file:
reader = csv.reader(file, delimiter=",") reader = csv.reader(file, delimiter=",")
@ -26,10 +25,10 @@ def readerWithFunction(filepath, function, arg=None):
def dictWriter(filepath, dict, flag="w"): def dictWriter(filepath, dict, flag="w"):
"""Completely rewrite a csv using the values in a single dictionary. """Completely rewrite a csv using the values in a single dictionary.
filepath -- the path of the csv, whether or not it exists filepath -- the path of the csv, whether or not it exists\n
dict -- the dictionary to pull all values from dict -- the dictionary to pull all values from\n
flag -- flag for open() (default "w") flag -- flag for open() (default "w")\n
"w" will also activate writeheader() "w" will also activate writeheader()\n
""" """
with open(filepath, flag, newline="") as file: with open(filepath, flag, newline="") as file:
@ -43,7 +42,7 @@ def dictWriter(filepath, dict, flag="w"):
def dictReaderFirstRow(filepath): def dictReaderFirstRow(filepath):
"""Get a dictionary of the first (non-header) row of values in a csv """Get a dictionary of the first (non-header) row of values in a csv
filepath -- the path of the csv, whether or not it exists filepath -- the path of the csv, whether or not it exists\n
""" """
with open(filepath, newline="") as file: with open(filepath, newline="") as file:
dict = {} dict = {}
@ -60,7 +59,8 @@ def dictReaderFirstRow(filepath):
def dictReaderMultiRow(filepath, idName): def dictReaderMultiRow(filepath, idName):
"""Get a dictionary of dictionaries of the (non-header) row of values in a csv """Get a dictionary of dictionaries of the (non-header) row of values in a csv
filepath -- the path of the csv, whether or not it exists filepath -- the path of the csv, whether or not it exists\n
idName -- the property in the header of the csv to use as each key of the resulting dictionary\n
""" """
with open(filepath, newline="") as file: with open(filepath, newline="") as file:
dict = {} dict = {}
@ -74,8 +74,8 @@ def dictReaderMultiRow(filepath, idName):
def getIndexOfField(filepath, field): def getIndexOfField(filepath, field):
"""Get the index (column number) of a field in a csv. """Get the index (column number) of a field in a csv.
filepath -- the path of the csv we're searching filepath -- the path of the csv we're searching\n
field -- the field string we're looking for field -- the field string we're looking for\n
""" """
return readerWithFunction(filepath, sub_getIndexOfField, field)[0] return readerWithFunction(filepath, sub_getIndexOfField, field)[0]
@ -83,7 +83,7 @@ def getIndexOfField(filepath, field):
def sub_getIndexOfField(row, field): def sub_getIndexOfField(row, field):
"""Helper function for getIndexOfField() that can be passed to readerWithFunction(). """Helper function for getIndexOfField() that can be passed to readerWithFunction().
row - the array representation of a csv row (presumably the fields row) row - the array representation of a csv row (presumably the fields row)\n
field - the field string we're looking for field - the field string we're looking for\n
""" """
return row.index(field) return row.index(field)