Working setup script writes csv
This commit is contained in:
parent
650ac1afbf
commit
b95fb99898
3 changed files with 64 additions and 10 deletions
|
@ -1 +1 @@
|
|||
tk_game,tk_description,tk_url,tk_logo_alt
|
||||
tk_game,tk_description,tk_url,tk_logo_alt
|
||||
|
|
|
|
@ -5,9 +5,7 @@ from utils import csv as util_csv
|
|||
divider = "----------"
|
||||
|
||||
print(
|
||||
"\nWelcome to paceboard! Let's set up your leaderboard site :)\nWe'll save this info in csv/config.csv\n\n"
|
||||
+ divider
|
||||
+ "\n"
|
||||
f"\nWelcome to paceboard! Let's set up your leaderboard site :)\nWe'll save this info in csv/config.csv\n\n{divider}\n"
|
||||
)
|
||||
|
||||
tk_game = input("Your game's name: ")
|
||||
|
@ -16,9 +14,14 @@ tk_url = input("URL of your site (format example - foobar.com): ")
|
|||
tk_logo_alt = input("Description of your game's logo (this is used for alt-text): ")
|
||||
|
||||
print(
|
||||
"\n"
|
||||
+ divider
|
||||
+ "\n\nPerfect! You can always overwrite the info above by running this script again.\nHere's an overview of how to use paceboard:\n\n- Add a category using scripts/add-category.py\n- Add a run using scripts/add-run.py\n- Use scripts/generate.py to update the site with the info you've added.\n- All categories, runs, and configuration details are stored in csv/\n- Replace the logo saved as assets/img/logo.png\n- For the adventurous, you can restyle your site's pages by editing files within templates/ and css/\n\nRemember to run scripts/generate.py to initialize your site.\nThat's all for now! <3\n"
|
||||
f"\n{divider}\n\nPerfect! You can always overwrite the info above by running this script again.\nHere's an overview of how to use paceboard:\n\n- Add a category using scripts/add-category.py\n- Add a run using scripts/add-run.py\n- Use scripts/generate.py to update the site with the info you've added.\n- All categories, runs, and configuration details are stored in csv/\n- Replace the logo saved as assets/img/logo.png\n- For the adventurous, you can restyle your site's pages by editing files within templates/ and css/\n\nRemember to run scripts/generate.py to initialize your site.\nThat's all for now! <3\n"
|
||||
)
|
||||
|
||||
util_csv.findFile()
|
||||
configDict = {
|
||||
"tk_game": tk_game,
|
||||
"tk_description": tk_description,
|
||||
"tk_url": tk_url,
|
||||
"tk_logo_alt": tk_logo_alt,
|
||||
}
|
||||
|
||||
util_csv.dictWriterSingleDict("../csv/config.csv", configDict)
|
||||
|
|
|
@ -1,2 +1,53 @@
|
|||
def findFile():
|
||||
print("cool")
|
||||
### Util library for interacting with csv files
|
||||
|
||||
import csv
|
||||
|
||||
|
||||
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.
|
||||
|
||||
filepath -- the path of the csv we're reading
|
||||
function -- the function we're executing once per row
|
||||
arg -- if the function takes an additional argument, we pass arg (default None)
|
||||
"""
|
||||
with open(filepath, newline="") as file:
|
||||
reader = csv.reader(file, delimiter=",")
|
||||
value = []
|
||||
for row in reader:
|
||||
if arg == None:
|
||||
value.append(function(row))
|
||||
else:
|
||||
value.append(function(row, arg))
|
||||
file.close()
|
||||
return value
|
||||
|
||||
|
||||
def dictWriterSingleDict(filepath, dict):
|
||||
"""Completely rewrite a csv using the values in a single dictionary.
|
||||
|
||||
filepath -- the path of the csv, whether or not it exists
|
||||
dict -- the dictionary to pull all values from
|
||||
"""
|
||||
with open(filepath, "w", newline="") as file:
|
||||
dictWriter = csv.DictWriter(file, fieldnames=dict.keys(), delimiter=",")
|
||||
dictWriter.writeheader()
|
||||
dictWriter.writerow(dict)
|
||||
file.close()
|
||||
|
||||
|
||||
def getIndexOfField(filepath, field):
|
||||
"""Get the index (column number) of a field in a csv.
|
||||
|
||||
filepath -- the path of the csv we're searching
|
||||
field -- the field string we're looking for
|
||||
"""
|
||||
return readerWithFunction(filepath, sub_getIndexOfField, field)[0]
|
||||
|
||||
|
||||
def sub_getIndexOfField(row, field):
|
||||
"""Helper function for getIndexOfField() that can be passed to readerWithFunction().
|
||||
|
||||
row - the array representation of a csv row (presumably the fields row)
|
||||
field - the field string we're looking for
|
||||
"""
|
||||
return row.index(field)
|
||||
|
|
Loading…
Reference in a new issue