diff --git a/README.md b/README.md index f2a6729..0d13511 100644 --- a/README.md +++ b/README.md @@ -35,16 +35,14 @@ A group of commands that will allow you to: ``` vivosnap.rb prepare uri-list [class_list_file] [VIVO_homepage_URL] {uri_list_file {REPLACE}} ``` -**NOT IMPLEMENTED** Create a list of URIs. You provide a file with class URIs, and a URL for VIVO. The tool will make requests of VIVOs ListRDF API, and write the results. If uri_list_file already exists, you must specify REPLACE. If uri_list_file is not provided, the URI list goes to stdout, along with the summary info. ``` -vivosnap.rb prepare session-list [uri_list_file] [account_email] [account_password] [session_list_file] +prepare session-list [uri_list_file] {account_email:account_password} {session_list_file {REPLACE}} ``` -**NOT IMPLEMENTED** Create a session list. You provide a file of URIs, and the tool will generate the URLs needed to fetch the profile pages for those URIs. If you want a login on each session, provide the email address and password of the desired login account. diff --git a/cmd_prepare_session_list/cmd_prepare_session_list.rb b/cmd_prepare_session_list/cmd_prepare_session_list.rb index a1f1ce4..bbd4a4c 100644 --- a/cmd_prepare_session_list/cmd_prepare_session_list.rb +++ b/cmd_prepare_session_list/cmd_prepare_session_list.rb @@ -1,10 +1,94 @@ +=begin +-------------------------------------------------------------------------------- + +Create a session list. +You provide a file of URIs, and the tool will generate the URLs needed to fetch +the profile pages for those URIs. + +If you want a login on each session, provide the email address and password of +the desired login account. + +if credentials are provided, each session looks like this: +LOGIN email password ==> display?uri=uri_from_list +otherwise, like this: +display?uri=uri_from_list + +-------------------------------------------------------------------------------- + +vivosnap.rb prepare session-list [uri_list_file] [account_email:account_password] {session_list_file {REPLACE}} + +-------------------------------------------------------------------------------- +=end + class CmdPrepareSessionList + USAGE = 'prepare session-list [uri_list_file] {account_email:account_password} {session_list_file {REPLACE}}' def initialize(args) - bogus "CmdPrepareSessionList.initialize NOT IMPLEMENTED" + @args = args + @replace = true && args.delete('REPLACE') + + complain("usage: #{USAGE}") unless (1..3).include? args.size + + @uri_list_file = args[0] + complain("'#{@uri_list_file}' does not exist.") unless File.exist?(@uri_list_file) + + case args.size + when 1 + @credentials = nil + @session_list_file = nil + when 2 + if args[1].include?(':') + @credentials = args[1].split(':') + @session_list_file = nil + else + @credentials = nil + @session_list_file = args[1] + end + else + @credentials = args[1].split(':') + @session_list_file = args[2] + end + + complain("usage: #{USAGE}") if @credentials && @credentials.size != 2 + + if (@session_list_file) + complain("#{@session_list_file} already exists. Specify REPLACE to overwrite.") if File.exist?(@session_list_file) unless @replace + @output = File.open(@session_list_file, 'w') + else + @output = $stdout + end end - + def run() - bogus "CmdPrepareSessionList.run NOT IMPLEMENTED" + write_heading + @counter = 0 + + File.open(@uri_list_file) do |f| + f.each_line do |uri| + next if uri.start_with?('#') || uri.strip.empty? + @output.puts(make_session(uri)) + @counter += 1 + end + end + + write_report + end + + def write_heading() + @output.puts "#" + @output.puts "# prepare session-list #{@args.join(' ')}" + @output.puts "# #{Time.now.strftime('%Y-%m-%d %H:%M:%S')}" + @output.puts "#" + end + + def make_session(uri) + session = @credentials ? "LOGIN %s %s ==> " % @credentials : '' + session += "display?uri=#{uri}" + end + + def write_report() + report = "#\n# wrote #{@counter} session lines.\n#" + @output.puts(report) + puts(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 406a688..f14c273 100644 --- a/cmd_prepare_uri_list/cmd_prepare_uri_list.rb +++ b/cmd_prepare_uri_list/cmd_prepare_uri_list.rb @@ -13,7 +13,9 @@ class CmdPrepareUriList USAGE = 'prepare uri-list [class_list_file] [VIVO_homepage_URL] {uri_list_file {REPLACE}}' def initialize(args) @args = args - complain("usage: #{USAGE}") unless (2..4).include? args.size + + @replace = args.delete('REPLACE') + complain("usage: #{USAGE}") unless (2..3).include? args.size @class_list_file = args[0] complain("'#{@class_list_file}' does not exist.") unless File.exist?(@class_list_file) @@ -31,7 +33,7 @@ def initialize(args) @uri_list_file = args[2] if @uri_list_file - complain("'#{@uri_list_file}' already exists. Specify REPLACE to replace it.") if File.exist?(@uri_list_file) unless 'REPLACE' == args[3] + complain("'#{@uri_list_file}' already exists. Specify REPLACE to replace it.") if File.exist?(@uri_list_file) unless @replace @output = File.open(@uri_list_file, 'w') else @output = $stdout diff --git a/common.rb b/common.rb index 8c41c35..2bc4059 100644 --- a/common.rb +++ b/common.rb @@ -19,6 +19,30 @@ def complain(message) raise UserInputError.new(message) end +# +# Record stdout and stderr, even while they are being displayed. +# +class MultiIO < IO + def initialize(*targets) + @targets = targets + end + + def write(*args) + @targets.each {|t| t.write(*args)} + end + + def flush() + @targets.each(&:flush) + end + + def close + @targets.each(&:close) + end +end + +$stdout = MultiIO.new(STDOUT, File.open(File.expand_path('~/vivosnap.stdout'), "w")) +$stderr = MultiIO.new(STDERR, File.open(File.expand_path('~/vivosnap.stderr'), "w")) + require_relative 'cmd_prepare_uri_list/cmd_prepare_uri_list' require_relative 'cmd_prepare_session_list/cmd_prepare_session_list' require_relative 'cmd_prepare_self_editor_account/cmd_prepare_self_editor_account'