From b384ec7fb9ec1f4eeb3097e25c15c18ae85dfccd Mon Sep 17 00:00:00 2001 From: Ben Kurtovic Date: Wed, 23 Mar 2016 03:12:03 -0400 Subject: [PATCH] More work --- lib/kgrader.rb | 4 ++++ lib/kgrader/cli.rb | 32 ++++++++++++++++---------------- lib/kgrader/course.rb | 24 ++++++++++++++++++++++++ lib/kgrader/filesystem.rb | 39 +++++++++++++++++++++++++++++++++++++++ lib/kgrader/roster.rb | 15 +++++++++++++++ lib/kgrader/task.rb | 23 +++++++++++++++++++++++ lib/kgrader/util.rb | 10 +++++++--- rakefile | 6 ++---- 8 files changed, 130 insertions(+), 23 deletions(-) create mode 100644 lib/kgrader/course.rb create mode 100644 lib/kgrader/filesystem.rb create mode 100644 lib/kgrader/roster.rb create mode 100644 lib/kgrader/task.rb diff --git a/lib/kgrader.rb b/lib/kgrader.rb index 723a311..518d37a 100644 --- a/lib/kgrader.rb +++ b/lib/kgrader.rb @@ -1,2 +1,6 @@ require_relative 'kgrader/cli' +require_relative 'kgrader/course' +require_relative 'kgrader/filesystem' +require_relative 'kgrader/roster' +require_relative 'kgrader/task' require_relative 'kgrader/util' diff --git a/lib/kgrader/cli.rb b/lib/kgrader/cli.rb index 078972a..621e26b 100644 --- a/lib/kgrader/cli.rb +++ b/lib/kgrader/cli.rb @@ -1,8 +1,11 @@ +require_relative 'course' +require_relative 'filesystem' + module KGrader class CLI def initialize(dir) - @dir = dir + @fs = Filesystem.new dir end def list @@ -11,23 +14,22 @@ module KGrader end def roster(course, semester, rosterfile) - # TODO - puts "[installing roster: c=#{course} s=#{semester} rf=#{rosterfile}]" + Course.new(@fs, course).roster(semester).load rosterfile end def grade(course, semester, assignment, options = {}) # TODO - puts "[grading c=#{course} s=#{semester} a=#{assignment}]" - puts " - [students=#{options[:students].inspect}]" - puts " - [due=#{options[:due].inspect}]" - puts " - [fetch=#{options.fetch(:fetch, true).inspect}]" - puts " - [regrade=#{options.fetch(:regrade, false).inspect}]" + # need to get default semester... + semester ||= 'DEFAULT' + task = Course.new(@fs, course).task semester, assignment + task.grade options end def commit(course, semester, assignment, options = {}) # TODO - puts "[committing c=#{course} s=#{semester} a=#{assignment}]" - puts " - [students=#{options[:students].inspect}]" + semester ||= 'DEFAULT' + task = Course.new(@fs, course).task semester, assignment + task.commit options end def clean @@ -43,15 +45,13 @@ module KGrader private def reset_jail - jail_dir = File.join(@dir, 'jail') - FileUtils.rm_rf jail_dir - FileUtils.mkdir jail_dir - FileUtils.touch File.join(jail_dir, '.gitkeep') + FileUtils.rm_rf @fs.jail + FileUtils.mkdir @fs.jail + FileUtils.touch File.join(@fs.jail, '.gitkeep') end def reset_desk - desk_dir = File.join(@dir, 'desk') - FileUtils.rm_rf Dir.glob(File.join(desk_dir, '*', '')) + FileUtils.rm_rf Dir[File.join @fs.desk, '*', ''] end end end diff --git a/lib/kgrader/course.rb b/lib/kgrader/course.rb new file mode 100644 index 0000000..c73d8c9 --- /dev/null +++ b/lib/kgrader/course.rb @@ -0,0 +1,24 @@ +require_relative 'roster' +require_relative 'task' + +module KGrader + class Course + attr_reader :name + + def initialize(filesystem, name) + @fs = filesystem + @name = name + @config = @fs.load @fs.course_config(@name) + end + + def roster(semester) + # TODO: cache + Roster.new @fs, self, semester + end + + def task(semester, assignment) + # TODO: cache + Task.new @fs, self, semester, assignment + end + end +end diff --git a/lib/kgrader/filesystem.rb b/lib/kgrader/filesystem.rb new file mode 100644 index 0000000..ca38535 --- /dev/null +++ b/lib/kgrader/filesystem.rb @@ -0,0 +1,39 @@ +require 'yaml' + +module KGrader + class Filesystem + + def initialize(root) + @root = root + end + + def course(name) + File.join @root, 'spec', name + end + + def course_config(name) + File.join course(name), '_config.yml' + end + + def courses + Dir[File.join @root, 'spec', '*', ''].each { |fn| File.basename fn } + end + + def desk + File.join @root, 'desk' + end + + def jail + File.join @root, 'jail' + end + + def load(path) + case File.extname path + when '.yml', '.yaml' + YAML.load File.read(path) + when '.csv' + # TODO + end + end + end +end diff --git a/lib/kgrader/roster.rb b/lib/kgrader/roster.rb new file mode 100644 index 0000000..faea976 --- /dev/null +++ b/lib/kgrader/roster.rb @@ -0,0 +1,15 @@ +module KGrader + class Roster + + def initialize(filesystem, course, semester) + @fs = filesystem + @course = course + @semester = semester + end + + def load(filename) + # TODO + puts "Loading roster for #{@course.name}:#{@semester} from [#{filename}]..." + end + end +end diff --git a/lib/kgrader/task.rb b/lib/kgrader/task.rb new file mode 100644 index 0000000..9af9ab7 --- /dev/null +++ b/lib/kgrader/task.rb @@ -0,0 +1,23 @@ +module KGrader + class Task + + def initialize(filesystem, course, semester, assignment) + @fs = filesystem + @course = course + @semester = semester + @assignment = assignment + end + + def grade(options = {}) + # TODO + puts "Grading #{@course.name}:#{@semester} assignment #{@assignment}..." + puts "- options: #{options}" + end + + def commit(options = {}) + # TODO + puts "Committing #{@course.name}:#{@semester} assignment #{@assignment}..." + puts "- options: #{options}" + end + end +end diff --git a/lib/kgrader/util.rb b/lib/kgrader/util.rb index 467e5b5..07adbd0 100644 --- a/lib/kgrader/util.rb +++ b/lib/kgrader/util.rb @@ -1,6 +1,10 @@ require 'date' module KGrader + def self.abort(error) + Kernel::abort "fatal: #{error}" + end + def self.parse_args(raw, num, keywords) args = [] options = {} @@ -9,7 +13,7 @@ module KGrader if arg.include? '=' key, val = arg.split('=', 2) key = key.to_sym - yield "unknown keyword #{key}" unless keywords.include? key + abort "unknown keyword #{key}" unless keywords.include? key options[key] = case keywords[key] when :string val @@ -25,8 +29,8 @@ module KGrader end end - yield "too few arguments" if args.size < num - yield "too many arguments" if args.size > num + abort "too few arguments" if args.size < num + abort "too many arguments" if args.size > num return args, options end end diff --git a/rakefile b/rakefile index 8bf7987..3f42597 100644 --- a/rakefile +++ b/rakefile @@ -4,10 +4,8 @@ def cli KGrader::CLI.new Rake.application.original_dir end -def parse_args(num, keywords) - args, options = KGrader::parse_args ARGV.drop(1), num, keywords do |err| - abort err - end +def parse_args(num, keywords = {}) + args, options = KGrader::parse_args ARGV.drop(1), num, keywords args.each { |arg| task arg.to_sym {} } args + [options] end