Skip to content

Commit

Permalink
Implement "prepare session-list"
Browse files Browse the repository at this point in the history
  • Loading branch information
j2blake committed Jun 24, 2015
1 parent e272959 commit 03909e3
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 8 deletions.
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
90 changes: 87 additions & 3 deletions cmd_prepare_session_list/cmd_prepare_session_list.rb
Original file line number Diff line number Diff line change
@@ -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

6 changes: 4 additions & 2 deletions cmd_prepare_uri_list/cmd_prepare_uri_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down
24 changes: 24 additions & 0 deletions common.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down

0 comments on commit 03909e3

Please sign in to comment.