Skip to content

Commit

Permalink
trim trailing whitespace
Browse files Browse the repository at this point in the history
  • Loading branch information
haydenfree committed Jul 10, 2024
1 parent 3891f12 commit 67e8405
Showing 1 changed file with 43 additions and 43 deletions.
86 changes: 43 additions & 43 deletions src/DataHandler.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ include("CSVUtilities.jl")
read_csv(file_path::AbstractString)
Read (i.e., deserialize) a CSV file containing either a curriculum or a degree plan, and returns a corresponding
`Curriculum` or `DegreePlan` data object. The required format for curriculum or degree plan CSV files is
`Curriculum` or `DegreePlan` data object. The required format for curriculum or degree plan CSV files is
described in [File Format](@ref).
# Arguments
Expand Down Expand Up @@ -45,15 +45,15 @@ function read_csv(file_path::AbstractString)
curric_learning_outcomes_count=0
part_missing_term=false
output = ""
# Open the CSV file and read in the basic information such as the type (curric or degreeplan), institution, degree type, etc
# Open the CSV file and read in the basic information such as the type (curric or degreeplan), institution, degree type, etc
open(file_path) do csv_file
read_line = csv_line_reader(readline(csv_file), ',')
courses_header += 1
if strip(read_line[1]) == "Curriculum"
curric_name = read_line[2]
read_line = csv_line_reader(readline(csv_file), ',')
is_dp = read_line[1] == "Degree Plan"
if is_dp
if is_dp
dp_name = read_line[2]
read_line = csv_line_reader(readline(csv_file), ',')
courses_header += 1
Expand All @@ -79,23 +79,23 @@ function read_csv(file_path::AbstractString)
courses_header += 1
end
if read_line[1] == "Courses"
courses_header += 1
courses_header += 1
else
println("Could not find Courses")
return false
end
end

# File isn't formatted correctly, couldn't find the curriculum field in Col A Row 1
else
else
println("Could not find a Curriculum")
return false
end

# This is the row containing Course ID, Course Name, Prefix, etc
read_line = csv_line_reader(readline(csv_file), ',')

# Checks that all courses have an ID, and counts the total number of courses
while length(read_line) > 0 && read_line[1] != "Additional Courses" && read_line[1] != "Course Learning Outcomes" &&
while length(read_line) > 0 && read_line[1] != "Additional Courses" && read_line[1] != "Course Learning Outcomes" &&
read_line[1] != "Curriculum Learning Outcomes" && !startswith(read_line[1], "#")

# Enforce that each course has an ID
Expand All @@ -112,11 +112,11 @@ function read_csv(file_path::AbstractString)
# Enforce that each course has an associated term if the file is a degree plan
if (is_dp)
if (length(read_line) == 10)
error("Each Course in a Degree Plan must have an associated term." *
error("Each Course in a Degree Plan must have an associated term." *
"\nCourse with ID \'$(read_line[1])\' ($(read_line[2])) has no term.")
return false
elseif (read_line[11] == 0)
error("Each Course in a Degree Plan must have an associated term." *
error("Each Course in a Degree Plan must have an associated term." *
"\nCourse with ID \'$(read_line[1])\' ($(read_line[2])) has no term.")
return false
end
Expand All @@ -138,58 +138,58 @@ function read_csv(file_path::AbstractString)
df_all_courses = DataFrame()
df_additional_courses = DataFrame()
if length(read_line) > 0 && read_line[1] == "Additional Courses"
if !is_dp
println("Only Degree Plan can have additional courses")
if !is_dp
println("Only Degree Plan can have additional courses")
return false
end
additional_course_start = courses_header+course_count+1
read_line = csv_line_reader(readline(csv_file), ',')
while length(read_line) > 0 && read_line[1] != "Course Learning Outcomes" &&
while length(read_line) > 0 && read_line[1] != "Course Learning Outcomes" &&
read_line[1] != "Curriculum Learning Outcomes" && !startswith(read_line[1], "#")
additional_course_count += 1
read_line = csv_line_reader(readline(csv_file), ',')
end
end
end
if additional_course_count > 1
df_additional_courses = CSV.File(file_path, header = additional_course_start, limit = additional_course_count - 1, delim = ',', silencewarnings = true) |> DataFrame
df_all_courses = vcat(df_courses,df_additional_courses)
else
df_all_courses = df_courses
end

df_course_learning_outcomes=""
if length(read_line)>0 && read_line[1] == "Course Learning Outcomes"
learning_outcomes_start = additional_course_start+additional_course_count+1
read_line = csv_line_reader(readline(csv_file), ',')
while length(read_line)>0 && !startswith(read_line[1],"#") && read_line[1] != "Curriculum Learning Outcomes"
learning_outcomes_count +=1
read_line = csv_line_reader(readline(csv_file), ',')
end
end
if learning_outcomes_count > 1
df_course_learning_outcomes = CSV.File(file_path, header = learning_outcomes_start, limit = learning_outcomes_count - 1, delim = ',', silencewarnings = true) |> DataFrame
end
end
end
course_learning_outcomes = Dict{Int, Array{LearningOutcome}}()
if df_course_learning_outcomes != ""
if df_course_learning_outcomes != ""
course_learning_outcomes = generate_course_lo(df_course_learning_outcomes)
if typeof(course_learning_outcomes) == Bool && !course_learning_outcomes
return false
end
end

df_curric_learning_outcomes = ""
if length(read_line)>0 && read_line[1] == "Curriculum Learning Outcomes"
curric_learning_outcomes_start = learning_outcomes_start+learning_outcomes_count+1
read_line = csv_line_reader(readline(csv_file), ',')
while length(read_line)>0 && !startswith(read_line[1],"#")
curric_learning_outcomes_count +=1
read_line = csv_line_reader(readline(csv_file), ',')
end
end
if learning_outcomes_count > 1
df_curric_learning_outcomes = CSV.File(file_path, header = curric_learning_outcomes_start, limit = curric_learning_outcomes_count - 1, delim = ',', silencewarnings = true) |> DataFrame
end
end
end

curric_learning_outcomes = if df_curric_learning_outcomes != "" generate_curric_lo(df_curric_learning_outcomes) else LearningOutcome[] end

if is_dp
Expand All @@ -198,7 +198,7 @@ function read_csv(file_path::AbstractString)
return false
end
all_courses_arr = [course[2] for course in all_courses]
additional_courses = read_courses(df_additional_courses, all_courses)
additional_courses = read_courses(df_additional_courses, all_courses)
ac_arr = Course[]
for course in additional_courses
push!(ac_arr, course[2])
Expand All @@ -219,10 +219,10 @@ function read_csv(file_path::AbstractString)
if typeof(curric_courses) == Bool && !curric_courses
return false
end
curric_courses_arr = [course[2] for course in curric_courses]
curric_courses_arr = [course[2] for course in curric_courses]
curric = Curriculum(curric_name, curric_courses_arr, learning_outcomes = curric_learning_outcomes, degree_type = curric_dtype,
system_type=curric_stype, institution=curric_inst, CIP=curric_CIP)
output = curric
output = curric
end
end
# Current file is the temp file created by remove_empty_lines(), remove the file.
Expand All @@ -237,7 +237,7 @@ end
"""
write_csv(c::Curriculum, file_path::AbstractString)
Write (i.e., serialize) a `Curriculum` data object to disk as a CSV file. To read
Write (i.e., serialize) a `Curriculum` data object to disk as a CSV file. To read
(i.e., deserialize) a curriculum CSV file, use the corresponding `read_csv` function.
The file format used to store curricula is described in [File Format](@ref).
Expand Down Expand Up @@ -267,13 +267,13 @@ end
"""
write_csv(dp::DegreePlan, file_path::AbstractString)
Write (i.e., serialize) a `DegreePlan` data object to disk as a CSV file. To read
Write (i.e., serialize) a `DegreePlan` data object to disk as a CSV file. To read
(i.e., deserialize) a degree plan CSV file, use the corresponding `read_csv` function.
The file format used to store degree plans is described in [File Format](@ref).
# Arguments
- `dp::DegreePlan` : the `DegreePlan` data object to be serialized.
- `file_path::AbstractString` : the absolute or relative path where the CSV file will be stored.
- `file_path::AbstractString` : the absolute or relative path where the CSV file will be stored.
# Examples:
```julia-repl
Expand All @@ -284,7 +284,7 @@ function write_csv(original_plan::DegreePlan, file_path::AbstractString; iostrea
if iostream
csv_file = IOBuffer()
write_csv_content(csv_file, original_plan, true, metrics=metrics)
else
else
open(file_path, "w") do csv_file
write_csv_content(csv_file, original_plan, true, metrics=metrics)
end
Expand All @@ -306,7 +306,7 @@ function write_csv_content(csv_file, program, is_degree_plan; metrics=false)
curric_name = "Curriculum," * string(curric.name) * ",,,,,,,,,"
end
write(csv_file, curric_name)

# Write Degree Plan Name
if is_degree_plan
dp_name = "\nDegree Plan," * "\""*string(program.name) *"\""* ",,,,,,,,,"
Expand All @@ -315,20 +315,20 @@ function write_csv_content(csv_file, program, is_degree_plan; metrics=false)

# Write Institution Name
curric_ins = "\nInstitution," * "\""*string(curric.institution) *"\""* ",,,,,,,,,"
write(csv_file, curric_ins)
write(csv_file, curric_ins)

# Write Degree Type
curric_dtype = "\nDegree Type," *"\""* string(curric.degree_type) * "\""*",,,,,,,,,"
write(csv_file,curric_dtype)
write(csv_file,curric_dtype)

# Write System Type (Semester or Quarter)
curric_stype = "\nSystem Type," * "\""*string(dict_curric_system[curric.system_type]) * "\""*",,,,,,,,,"
write(csv_file, curric_stype)
write(csv_file, curric_stype)

# Write CIP Code
curric_CIP = "\nCIP," * "\""*string(curric.CIP) * "\""*",,,,,,,,,"
write(csv_file, curric_CIP)

# Define course header
if is_degree_plan
if metrics
Expand All @@ -345,12 +345,12 @@ function write_csv_content(csv_file, program, is_degree_plan; metrics=false)
course_header="\nCourse ID,Course Name,Prefix,Number,Prerequisites,Corequisites,Strict-Corequisites,Credit Hours,Institution,Canonical Name"
end
end
write(csv_file, "\nCourses,,,,,,,,,,")
write(csv_file, course_header)
write(csv_file, "\nCourses,,,,,,,,,,")
write(csv_file, course_header)

# Define dict to store all course learning outcomes
all_course_lo = Dict{Int, Array{LearningOutcome, 1}}()

# if metrics is true ensure that all values are present before writing courses
if metrics
complexity(curric)
Expand All @@ -365,22 +365,22 @@ function write_csv_content(csv_file, program, is_degree_plan; metrics=false)
for (term_id, term) in enumerate(program.terms)
for course in term.courses
if !isdefined(program, :additional_courses) || !find_courses(program.additional_courses, course.id)
write(csv_file, course_line(course, term_id, metrics = metrics))
write(csv_file, course_line(course, term_id, metrics = metrics))
end
end
end
# Check if the original plan has additional courses defined
if isdefined(program, :additional_courses)
# Write the additional courses section of the CSV
write(csv_file, "\nAdditional Courses,,,,,,,,,,")
write(csv_file, course_header)
write(csv_file, course_header)
# Iterate through each term
for (term_id, term) in enumerate(program.terms)
# Iterate through each course in the current term
for course in term.courses
# Check if the current course is an additional course, if so, write it here
if find_courses(program.additional_courses, course.id)
write(csv_file, course_line(course, term_id, metrics = metrics))
write(csv_file, course_line(course, term_id, metrics = metrics))
end
# Check if the current course has learning outcomes, if so store them
if length(course.learning_outcomes) > 0
Expand All @@ -402,5 +402,5 @@ function write_csv_content(csv_file, program, is_degree_plan; metrics=false)
end

# Write course and curriculum learning outcomes, if any
write_learning_outcomes(curric, csv_file, all_course_lo)
write_learning_outcomes(curric, csv_file, all_course_lo)
end

0 comments on commit 67e8405

Please sign in to comment.