From 7a478b6712a7d08acca73352604379c3f6f0b81f Mon Sep 17 00:00:00 2001 From: Ben Kurtovic Date: Wed, 30 Mar 2016 19:52:22 -0500 Subject: [PATCH] Add some error handling; tweak. --- README.md | 11 ++++++++--- lib/kgrader.rb | 1 + lib/kgrader/cli.rb | 14 +++++++++++--- lib/kgrader/errors.rb | 7 +++++++ lib/kgrader/filesystem.rb | 4 ++++ lib/kgrader/util.rb | 8 ++++---- rakefile | 6 +++--- 7 files changed, 38 insertions(+), 13 deletions(-) create mode 100644 lib/kgrader/errors.rb diff --git a/README.md b/README.md index bbb4eca..2ee95a8 100644 --- a/README.md +++ b/README.md @@ -31,9 +31,13 @@ To show all known classes, semesters, and assignments: rake list -To load a roster for a course semester: +To load a roster for a specific semester: - rake roster cs123 sp2016 myroster.csv + rake roster cs123 myroster.csv semester=sp16 + +To load a roster for the current semester: + + rake roster cs123 myroster.csv ### Grading @@ -83,4 +87,5 @@ repo has changed since the last run. To forcibly regrade: rake grade cs123 mp1 regrade=yes -You can combine these arguments in any meaningful way. +You can combine these arguments in any meaningful way. `semester` is available +for `roster` and `commit`, and and `students` is available for `commit`. diff --git a/lib/kgrader.rb b/lib/kgrader.rb index 518d37a..708fdfc 100644 --- a/lib/kgrader.rb +++ b/lib/kgrader.rb @@ -1,5 +1,6 @@ require_relative 'kgrader/cli' require_relative 'kgrader/course' +require_relative 'kgrader/errors' require_relative 'kgrader/filesystem' require_relative 'kgrader/roster' require_relative 'kgrader/task' diff --git a/lib/kgrader/cli.rb b/lib/kgrader/cli.rb index c08d4ef..0227ade 100644 --- a/lib/kgrader/cli.rb +++ b/lib/kgrader/cli.rb @@ -26,17 +26,19 @@ module KGrader end def roster(course, semester, rosterfile) - Course.new(@fs, course).roster(semester).load rosterfile + course = fetch_course course + semester ||= course.current_semester + course.roster(semester).load rosterfile end def grade(course, semester, assignment, options = {}) - course = Course.new @fs, course + course = fetch_course course semester ||= course.current_semester course.task(semester, assignment).grade options end def commit(course, semester, assignment, options = {}) - course = Course.new @fs, course + course = fetch_course course semester ||= course.current_semester course.task(semester, assignment).commit options end @@ -56,6 +58,12 @@ module KGrader end private + def fetch_course(course) + Course.new(@fs, course) + rescue FilesystemError + KGrader::die "unknown course" + end + def reset_jail FileUtils.rm_rf @fs.jail FileUtils.mkdir @fs.jail diff --git a/lib/kgrader/errors.rb b/lib/kgrader/errors.rb new file mode 100644 index 0000000..77df351 --- /dev/null +++ b/lib/kgrader/errors.rb @@ -0,0 +1,7 @@ +module KGrader + class KGraderError < StandardError + end + + class FilesystemError < KGraderError + end +end diff --git a/lib/kgrader/filesystem.rb b/lib/kgrader/filesystem.rb index 746355e..071b48b 100644 --- a/lib/kgrader/filesystem.rb +++ b/lib/kgrader/filesystem.rb @@ -51,7 +51,11 @@ module KGrader YAML.load File.read(path) when '.csv' File.read(path).split("\n").map! { |line| line.split "," } + else + raise FilesystemError, "unknown file type" end + rescue SystemCallError # Errno::ENOENT, etc. + raise FilesystemError, "can't read file" end end end diff --git a/lib/kgrader/util.rb b/lib/kgrader/util.rb index 9855047..4113e12 100644 --- a/lib/kgrader/util.rb +++ b/lib/kgrader/util.rb @@ -1,7 +1,7 @@ require 'date' module KGrader - def self.abort(error) + def self.die(error) Kernel::abort "fatal: #{error}" end @@ -13,7 +13,7 @@ module KGrader if arg.include? '=' key, val = arg.split('=', 2) key = key.to_sym - abort "unknown keyword #{key}" unless keywords.include? key + die "unknown keyword #{key}" unless keywords.include? key options[key] = case keywords[key] when :string val @@ -29,8 +29,8 @@ module KGrader end end - abort "too few arguments" if args.size < num - abort "too many arguments" if args.size > num + die "too few arguments" if args.size < num + die "too many arguments" if args.size > num return args, options end diff --git a/rakefile b/rakefile index 3f42597..b071f5c 100644 --- a/rakefile +++ b/rakefile @@ -15,7 +15,7 @@ task :default => :help do ; end task :help do puts %{usage: - rake list -- rake roster +- rake roster [semester=<...>] - rake grade [semester=<...>] [students=<...>] [due=<...>] [fetch=] [regrade=] - rake commit [semester=<...>] [students=<...>] @@ -28,8 +28,8 @@ task :list do end task :roster do - course, semester, rosterfile = parse_args 3 - cli.roster course, semester, rosterfile + course, rosterfile, options = parse_args 2, { :semester => :string } + cli.roster course, options[:semester], rosterfile end task :grade do