diff --git a/README.md b/README.md index b12b9e6..4426280 100644 --- a/README.md +++ b/README.md @@ -59,7 +59,6 @@ and to make it a proxy editor for each of the URLs in the list. ``` vivosnap.rb prepare sub-list [session_list_file] [count] [sub_list_file] ``` -**NOT IMPLEMENTED** Create a smaller session list from an existing one. The new list will have the specified number of entries, extracted at even intervals from the existing list. diff --git a/cmd_prepare_self_editor_account/cmd_prepare_self_editor_account.rb b/cmd_prepare_self_editor_account/cmd_prepare_self_editor_account.rb index e558345..2de5652 100644 --- a/cmd_prepare_self_editor_account/cmd_prepare_self_editor_account.rb +++ b/cmd_prepare_self_editor_account/cmd_prepare_self_editor_account.rb @@ -112,6 +112,7 @@ class CmdPrepareSelfEditorAccount AUTH_SELF_EDITOR = RDF::URI.new('http://vitro.mannlib.cornell.edu/ns/vitro/authorization#SELF_EDITOR') AUTH_STATUS = RDF::URI.new('http://vitro.mannlib.cornell.edu/ns/vitro/authorization#status') AUTH_USER_ACCOUNT = RDF::URI.new('http://vitro.mannlib.cornell.edu/ns/vitro/authorization#UserAccount') + def initialize(args) @args = args diff --git a/cmd_prepare_session_list/cmd_prepare_session_list.rb b/cmd_prepare_session_list/cmd_prepare_session_list.rb index 64dea4b..49e1c05 100644 --- a/cmd_prepare_session_list/cmd_prepare_session_list.rb +++ b/cmd_prepare_session_list/cmd_prepare_session_list.rb @@ -22,6 +22,7 @@ class CmdPrepareSessionList USAGE = 'prepare session-list [uri_list_file] {account_email:account_password} {session_list_file {REPLACE}}' + def initialize(args) @args = args @replace = true && args.delete('REPLACE') @@ -47,7 +48,7 @@ def parse_remaining_args(args) [split_credentials(args[1]), args[2]] end end - + def split_credentials(arg) complain("usage: #{USAGE}") unless 1 == arg.count(':') arg.split(':') diff --git a/cmd_prepare_sub_list/cmd_prepare_sub_list.rb b/cmd_prepare_sub_list/cmd_prepare_sub_list.rb index 247501b..641d1e3 100644 --- a/cmd_prepare_sub_list/cmd_prepare_sub_list.rb +++ b/cmd_prepare_sub_list/cmd_prepare_sub_list.rb @@ -1,10 +1,70 @@ +=begin +-------------------------------------------------------------------------------- + +Create a sub-list sample from a session list file. + +You provide the session list file, and how many sessions you want to put into +the sub-list, and the tool will extract that many session lines from the original +list, evenly spaced, and put them into the new file. + +-------------------------------------------------------------------------------- + +vivosnap.rb prepare sub-list [session_list_file] [count] [sub_list_file] + +-------------------------------------------------------------------------------- +=end + class CmdPrepareSubList + include ::ArgsChecker + + USAGE = "prepare sub-list [session_list_file] [count] {sub_list_file {REPLACE}}" def initialize(args) - bogus "CmdPrepareSubList.initialize NOT IMPLEMENTED" + @args = args + @replace = args.delete('REPLACE') + + complain("usage: #{USAGE}") unless (2..3).include? args.size + + @session_list_file = confirm_file_exists(args[0]) + @line_count = confirm_positive_integer(args[1]) + @output = set_output_io(args[2], @replace) end - + def run() - bogus "CmdPrepareSubList.run NOT IMPLEMENTED" + read_lines_and_remove_comments + complain("#{@session_list_file} only contains #{@lines.size} effective lines.") if @lines.size < @line_count + + write_heading() + write_selected_lines() + write_report() end + + def write_heading() + @output.puts "#" + @output.puts "# prepare sub-list #{@args.join(' ')}" + @output.puts "# #{Time.now.strftime('%Y-%m-%d %H:%M:%S')}" + @output.puts "#" + end + + def read_lines_and_remove_comments() + @lines = File.readlines(@session_list_file) + @lines.reject! do |line| + line.start_with?('#') || line.strip.empty? + end + end + + def write_selected_lines() + interval = (@lines.size / @line_count).to_i + (1..@line_count).each do |i| + @output.puts @lines[(i - 1) * interval] + end + end + + def write_report() + report = "#\n# Wrote #{@line_count} lines.\n#\n" + + @output.print(report) + print(report) if @output != $stdout + end + end diff --git a/cmd_prepare_uri_list/cmd_prepare_uri_list.rb b/cmd_prepare_uri_list/cmd_prepare_uri_list.rb index a06fd62..973b5c4 100644 --- a/cmd_prepare_uri_list/cmd_prepare_uri_list.rb +++ b/cmd_prepare_uri_list/cmd_prepare_uri_list.rb @@ -7,14 +7,21 @@ write the results to the file, one URI per line. -------------------------------------------------------------------------------- + +vivosnap.rb prepare uri-list [class_list_file] [VIVO_homepage_URL] {uri_list_file {REPLACE}} + +-------------------------------------------------------------------------------- + =end class CmdPrepareUriList + include ::ArgsChecker + USAGE = 'prepare uri-list [class_list_file] [VIVO_homepage_URL] {uri_list_file {REPLACE}}' def initialize(args) @args = args @replace = args.delete('REPLACE') - + complain("usage: #{USAGE}") unless (2..3).include? args.size @class_list_file = confirm_file_exists(args[0]) diff --git a/utils/args_checker.rb b/utils/args_checker.rb index 2ec17d7..e3fdc00 100644 --- a/utils/args_checker.rb +++ b/utils/args_checker.rb @@ -30,7 +30,7 @@ def confirm_vivo_home_url(url) rescue complain("Can't contact VIVO at '#{url}': #{$!}") end - + url end @@ -49,4 +49,17 @@ def set_output_io(path, replace) $stdout end end + + # + # The parameter should be a String representing an integer greater than 0. + # + def confirm_positive_integer(count_str) + begin + count = count_str.to_i + rescue + complain("Not a valid integer: '#{count_str}'") + end + complain("Expecting a positive integer, not '#{count_str}'") unless count > 0 + count + end end \ No newline at end of file