Skip to content

Commit

Permalink
Added option to return spreadsheet headers when parsing
Browse files Browse the repository at this point in the history
Convert EOC script now finds its columns using the headers
  • Loading branch information
Dantemss committed Dec 18, 2024
1 parent b2c8d2f commit 6821a30
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 25 deletions.
9 changes: 6 additions & 3 deletions app/routines/process_spreadsheet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ class ProcessSpreadsheet

protected

def exec(filename:, offset:, pad_cells: true, &block)
def exec(filename:, offset: 1, pad_cells: true, return_headers: false, &block)
raise ArgumentError, 'A block must be provided' if block.nil?

if File.extname(filename) == 'csv'
Expand All @@ -14,14 +14,17 @@ def exec(filename:, offset:, pad_cells: true, &block)
method = :each_row_streaming
end

args = []
pad_to_size = 0 if pad_cells
klass.new(filename).public_send(method).each_with_index do |row, row_index|
if pad_cells
if return_headers && row_index == 0
args << row.map(&:to_s)

Check warning on line 21 in app/routines/process_spreadsheet.rb

View check run for this annotation

Codecov / codecov/patch

app/routines/process_spreadsheet.rb#L21

Added line #L21 was not covered by tests
elsif pad_cells
row += [nil] * (pad_to_size - row.length) if pad_to_size > row.length
pad_to_size = row.length
end

block.call(row, row_index) if row_index >= offset
block.call(*args.concat([row, row_index])) if row_index >= offset
end
end
end
42 changes: 20 additions & 22 deletions lib/tasks/exercises/tag.rake
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace :exercises do
namespace :tag do
desc "add a module to exercises that are tagged with another module"
desc 'add a module to exercises that are tagged with another module'
task :import_from_module_map, [:file] => :environment do |t, args|
Tag.transaction do
CSV.foreach(args[:file]) do |src_uuid, new_uuid|
Expand All @@ -24,7 +24,7 @@ namespace :exercises do
# filename, [skip_first_row]
# Example: rake exercises:tag:spreadsheet[tags.xlsx]
# will tag exercises based on tags.xlsx
desc "tags exercises using a spreadsheet"
desc 'tags exercises using a spreadsheet'
task :spreadsheet, [:filename, :skip_first_row] => :environment do |t, args|
# Output import logging info to the console (except in the test environment)
original_logger = Rails.logger
Expand All @@ -44,7 +44,7 @@ namespace :exercises do
# filename, book_uuid, [skip_first_row]
# Example: rake exercises:tag:assessments[tags.xlsx,12345]
# will tag exercises based on tags.xlsx with the book_uuid 12345
desc "tags exercises for OpenStax Assessments using a spreadsheet"
desc 'tags exercises for OpenStax Assessments using a spreadsheet'
task :assessments, [:filename, :book_uuid, :skip_first_row] => :environment do |t, args|
# Output import logging info to the console (except in the test environment)
original_logger = Rails.logger
Expand Down Expand Up @@ -78,36 +78,34 @@ namespace :exercises do
has_units = ActiveModel::Type::Boolean.new.cast args[:has_units]
chapters = has_units ? parts.flat_map { |part| get_parts.call(part) } : parts

chapter_uuid_by_page_uuid = {}
chapters.each do |chapter|
chapter.all_pages.each do |page|
chapter_uuid_by_page_uuid[page.uuid] = chapter.uuid
end
end
chapter_uuids = chapters.map(&:uuid)

Check warning on line 81 in lib/tasks/exercises/tag.rake

View check run for this annotation

Codecov / codecov/patch

lib/tasks/exercises/tag.rake#L81

Added line #L81 was not covered by tests

Rails.logger.info { "Processing \"#{args[:filename]}\"" }

output_filename = "#{book.slug}.csv"
downcased_headers = nil
chapter_index = nil
exercise_id_index = nil

Check warning on line 88 in lib/tasks/exercises/tag.rake

View check run for this annotation

Codecov / codecov/patch

lib/tasks/exercises/tag.rake#L86-L88

Added lines #L86 - L88 were not covered by tests
CSV.open(output_filename, 'w') do |csv|
csv << [ 'Exercise UID', 'Tags...' ]

ProcessSpreadsheet.call(filename: args[:filename], offset: 2) do |row, index|
next Rails.logger.warn do
"Skipped row #{index + 1} because column 5 is blank"
end if row[4].blank?
ProcessSpreadsheet.call(filename: args[:filename], return_headers: true) do |headers, row, index|
downcased_headers ||= headers.map(&:downcase)

Check warning on line 93 in lib/tasks/exercises/tag.rake

View check run for this annotation

Codecov / codecov/patch

lib/tasks/exercises/tag.rake#L92-L93

Added lines #L92 - L93 were not covered by tests

next Rails.logger.warn do
"Skipped row #{index + 1} because column 4 says it is not in Exercises"
end if row[3].to_s == 'no'
chapter_index ||= downcased_headers.find_index { |header| header.include? 'chapter' }
raise ArgumentError, 'Could not find Chapter column' if chapter_index.nil?

Check warning on line 96 in lib/tasks/exercises/tag.rake

View check run for this annotation

Codecov / codecov/patch

lib/tasks/exercises/tag.rake#L95-L96

Added lines #L95 - L96 were not covered by tests

exercise_id_index ||= downcased_headers.find_index do |header|
header.include?('assessment') || header.include?('exercise')

Check warning on line 99 in lib/tasks/exercises/tag.rake

View check run for this annotation

Codecov / codecov/patch

lib/tasks/exercises/tag.rake#L98-L99

Added lines #L98 - L99 were not covered by tests
end
raise ArgumentError, 'Could not find Assessment ID column' if exercise_id_index.nil?

Check warning on line 101 in lib/tasks/exercises/tag.rake

View check run for this annotation

Codecov / codecov/patch

lib/tasks/exercises/tag.rake#L101

Added line #L101 was not covered by tests

page_uuid = row[0].to_s.strip
chapter_uuid = chapter_uuid_by_page_uuid[page_uuid]
next Rails.logger.warn do
"Skipped row #{index + 1} because page with UUID \"#{page_uuid}\" was not found"
end if chapter_uuid.nil?
chapter = row[chapter_index].to_s.strip

Check warning on line 103 in lib/tasks/exercises/tag.rake

View check run for this annotation

Codecov / codecov/patch

lib/tasks/exercises/tag.rake#L103

Added line #L103 was not covered by tests
# The value in the Chapter column may be a UUID or a chapter number
chapter_uuid = chapter_uuids.include?(chapter) ? chapter : chapter_uuids[Integer(chapter) - 1]

Check warning on line 105 in lib/tasks/exercises/tag.rake

View check run for this annotation

Codecov / codecov/patch

lib/tasks/exercises/tag.rake#L105

Added line #L105 was not covered by tests

csv << [
row[4].to_s.strip,
row[exercise_id_index].to_s.strip,
"assessment:practice:https://openstax.org/orn/book:subbook/#{
args[:book_uuid]}:#{chapter_uuid}"
]
Expand Down

0 comments on commit 6821a30

Please sign in to comment.