Skip to content

Commit

Permalink
Implement 'prepare sub-list'
Browse files Browse the repository at this point in the history
  • Loading branch information
j2blake committed Jul 22, 2015
1 parent 628d174 commit b94c47c
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 7 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 2 additions & 1 deletion cmd_prepare_session_list/cmd_prepare_session_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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(':')
Expand Down
66 changes: 63 additions & 3 deletions cmd_prepare_sub_list/cmd_prepare_sub_list.rb
Original file line number Diff line number Diff line change
@@ -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

9 changes: 8 additions & 1 deletion cmd_prepare_uri_list/cmd_prepare_uri_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down
15 changes: 14 additions & 1 deletion utils/args_checker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def confirm_vivo_home_url(url)
rescue
complain("Can't contact VIVO at '#{url}': #{$!}")
end

url
end

Expand All @@ -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

0 comments on commit b94c47c

Please sign in to comment.