csv-library-website/library/csvparser/csvparser.py

143 lines
3.8 KiB
Python

"""This parses the varlib.csv but only in a way
that is actually useful for the site"""
import csv
import os
script_dir = os.path.dirname(__file__)
image_dir = os.path.abspath(os.path.join(script_dir, "../static/images"))
def parsecsv():
libcsv = open(os.path.join(script_dir, "varlib.csv"), "r")
with libcsv:
csv_as_dict = csv.DictReader(libcsv)
return csv_as_dict
def getpublications():
libcsv = open(os.path.join(script_dir, "varlib.csv"), "r")
with libcsv:
csv_as_dict = csv.DictReader(libcsv)
publications = {}
for row in csv_as_dict:
year = row["Year"]
if not year:
year = "Unknown"
pubinfo = {
"Title": row["Publication"],
"Author": row["Author"],
"Type": row["Type"].lower().title(),
"Year": year,
"License": row["LicenseShort"].lower().title(),
"Image": hasimage(row["Id"])
}
publications[row["Id"]] = pubinfo
return publications
def hasimage(id):
image_file = os.path.join(image_dir, "image-{0}.jpg".format(id))
if os.path.exists(image_file):
return True
else:
return False
def gettypes():
libcsv = open(os.path.join(script_dir, "varlib.csv"), "r")
with libcsv:
csv_as_dict = csv.DictReader(libcsv)
listoftypes = []
for row in csv_as_dict:
lowertype = row["Type"].lower().title()
if lowertype not in listoftypes:
listoftypes.append(lowertype)
return listoftypes
def getyears():
libcsv = open(os.path.join(script_dir, "varlib.csv"), "r")
with libcsv:
csv_as_dict = csv.DictReader(libcsv)
listofyears = []
for row in csv_as_dict:
uniqueyear = row["Year"]
if not uniqueyear:
uniqueyear = "Unknown"
if uniqueyear not in listofyears:
listofyears.append(uniqueyear)
listofyears.sort()
return listofyears
def getlicenses():
libcsv = open(os.path.join(script_dir, "varlib.csv"), "r")
with libcsv:
csv_as_dict = csv.DictReader(libcsv)
listoflicenses = []
for row in csv_as_dict:
license = row["LicenseShort"].lower().title()
if not license:
license = "No License Mentioned"
if license not in listoflicenses:
listoflicenses.append(license)
return listoflicenses
def getfieldsofinterest():
libcsv = open(os.path.join(script_dir, "varlib.csv"), "r")
with libcsv:
csv_as_dict = csv.DictReader(libcsv)
fieldsofinterest = {}
for row in csv_as_dict:
fields = row["Fields"].split(',')
fieldsofinterest[row["Id"]] = fields
return fieldsofinterest
def getpublicationfromcsvrow(row):
year = row["Year"]
if not year:
year = "Unknown"
license = row["License"]
if not license:
license = "No license mentioned"
borrowed = row["Currently borrowed by"]
if not borrowed:
borrowed = "No one"
pubinfo = {
"Title": row["Publication"],
"Author": row["Author"],
"Year": year,
"Custodian": row["Custodian"],
"Fields": row["Fields"],
"Type": row["Type"],
"Publishers": row["Publishers"],
"License": license,
"Highlights": row["Highlights"],
"Comments": row["Comments"],
"Borrowed": borrowed,
}
return pubinfo
def getfullpublication(pubid):
libcsv = open(os.path.join(script_dir, "varlib.csv"), "r")
pubinfo = {}
with libcsv:
csv_as_dict = csv.DictReader(libcsv)
for row in csv_as_dict:
if pubid == row["Id"]:
pubinfo = getpublicationfromcsvrow(row)
# print(pubinfo)
return pubinfo
# print(getlicenses())