<commit_before>require 'rubygems'
require 'right_aws'
require 'optparse'
require 'date'
require 'highline/import'
require 'tempfile'

require 's3cp/utils'

# Parse arguments
options = {}
options[:tty] = $stdout.isatty

op = OptionParser.new do |opts|
  opts.banner = "s3cat [path]"
  opts.separator ''

  opts.on("--debug", "Debug mode") do
    options[:debug] = true
  end

  opts.on("--tty", "TTY mode") do |tty|
    options[:tty] = tty
  end

  opts.on_tail("-h", "--help", "Show this message") do
    puts op
    exit
  end
end
op.parse!(ARGV)

unless ARGV.size > 0
  puts op
  exit
end

url = ARGV[0]

if options[:debug]
  puts "URL: #{url}"
  puts "Options: \n#{options.inspect}"
end

@bucket, @prefix = S3CP.bucket_and_key(url)
fail "Your URL looks funny, doesn't it?" unless @bucket

@s3 = S3CP.connect().interface

# if TTY, store contents to file
if options[:tty]
  file = Tempfile.new('s3cat')
  out = File.new(file.path, File::CREAT|File::RDWR)
else
  out = STDOUT
end

# write contents to file or STDOUT
begin
  @s3.get(@bucket, @prefix) do |chunk|
    if file
      out.write(chunk)
    else
      STDOUT.print(chunk)
    end
  end
ensure
  out.close() if file
end

# if TTY, use PAGER to display the file
if file
  exec "#{ENV['PAGER'] || 'less'} #{file.path}"
  file.delete()
end

<commit_msg>Minor refactoring<commit_after>require 'rubygems'
require 'right_aws'
require 'optparse'
require 'date'
require 'highline/import'
require 'tempfile'

require 's3cp/utils'

# Parse arguments
options = {}
options[:tty] = $stdout.isatty

op = OptionParser.new do |opts|
  opts.banner = "s3cat [path]"
  opts.separator ''

  opts.on("--debug", "Debug mode") do
    options[:debug] = true
  end

  opts.on("--tty", "TTY mode") do |tty|
    options[:tty] = tty
  end

  opts.on_tail("-h", "--help", "Show this message") do
    puts op
    exit
  end
end
op.parse!(ARGV)

unless ARGV.size > 0
  puts op
  exit
end

url = ARGV[0]

if options[:debug]
  puts "URL: #{url}"
  puts "Options: \n#{options.inspect}"
end

@bucket, @prefix = S3CP.bucket_and_key(url)
fail "Your URL looks funny, doesn't it?" unless @bucket

@s3 = S3CP.connect().interface

if options[:tty]
  # store contents to file to display with PAGER
  file = Tempfile.new('s3cat')
  out = File.new(file.path, File::CREAT|File::RDWR)
  begin
    @s3.get(@bucket, @prefix) do |chunk|
      out.write(chunk)
    end
  ensure
    out.close()
  end
  exec "#{ENV['PAGER'] || 'less'} #{file.path}"
  file.delete()
else
  @s3.get(@bucket, @prefix) do |chunk|
    STDOUT.print(chunk)
  end
end


{'fraction_non_alphanumeric': 0.15438871473354232, 'fraction_numerical': 0.007445141065830721, 'mean_word_length': 3.5589285714285714, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1729468', 'n_tokens_mistral': 989, 'n_tokens_neox': 977, 'n_words': 267}