module Columbus3::CommandSemantics

Constants

APPNAME
VERSION

Public Class Methods

console(opts, argv = []) click to toggle source
# File lib/columbus3/cli/command_semantics.rb, line 41
def self.console opts, argv = []
  all_commands = CommandSyntax.commands
  all_commands.delete(:console)
  
  i = 0
  while true
    string = Readline.readline("#{APPNAME}:%03d> " % i, true)
    string.gsub!(/^#{APPNAME} /, "") # as a courtesy, remove any leading appname string
    if string == "exit" or string == "quit" or string == "." then
      exit 0
    end
    reps all_commands, string.split(' ')
    i = i + 1
  end
end
convert(opts, args) click to toggle source
# File lib/columbus3/cli/command_semantics.rb, line 116
def self.convert opts, args
  force = opts[:force]

  input_filename = args[0]

  if input_filename
    tracks = GPX2V900::gpx2v900 input_filename
    tracks.each do |track|
      if File.exist?(track.filename) and not force
        puts "Error: #{track.filename} already exists (--force to force updating)"
      else
        track.save
        puts "Track #{track.filename} created"
      end
    end
  else
    puts "Error: please specify a track file"
  end
end
export(opts, args) click to toggle source
# File lib/columbus3/cli/command_semantics.rb, line 139
def self.export opts, args
end
graph(opts, args) click to toggle source
# File lib/columbus3/cli/command_semantics.rb, line 112
def self.graph opts, args
  show_machinery opts, args, Columbus3::FlotRenderer
end
help(opts = nil, argv = []) click to toggle source
# File lib/columbus3/cli/command_semantics.rb, line 25
def self.help opts = nil, argv = []
  all_commands = CommandSyntax.commands
  
  if argv != []
    argv.map { |x| puts all_commands[x.to_sym][2] }
  else
    puts "#{APPNAME} command [options] [args]"
    puts ""
    puts "Available commands:"
    puts ""
    all_commands.keys.each do |key|
      puts "  " + all_commands[key][0].banner
    end
  end
end
man(opts = nil, argv = []) click to toggle source
# File lib/columbus3/cli/command_semantics.rb, line 18
def self.man opts = nil, argv = []
  path = File.join(File.dirname(__FILE__), "/../../../README.md")
  file = File.open(path, "r")
  contents = file.read
  puts contents
end
process(opts, argv) click to toggle source

APP SPECIFIC COMMANDS

# File lib/columbus3/cli/command_semantics.rb, line 61
def self.process opts, argv
  force = opts[:force]
  
  argv.each do |arg|
    track = V900Track.new filename: arg
    sidecar = Columbus3::Sidecar.new arg
    if sidecar.exist? and not force
      puts "Sidecar file for #{track.filename} already exists. Use --force if you want to overwrite it."
    else
      backup(sidecar.filename) if sidecar.exist?
      printf "Processing %s ...", track.filename
      sidecar.metadata = track.metadata
      sidecar.save
      puts " done!"
    end
  end
end
reps(all_commands, argv) click to toggle source

read-eval-print step

# File lib/columbus3/cli/command_semantics.rb, line 143
def self.reps all_commands, argv
  if argv == [] or argv[0] == "--help" or argv[0] == "-h"
    CommandSemantics.help
    exit 0
  else
    command = argv[0]
    syntax_and_semantics = all_commands[command.to_sym]
    if syntax_and_semantics
      opts = syntax_and_semantics[0]
      function = syntax_and_semantics[1]
      
      begin
        parser = Slop::Parser.new(opts)

        result = parser.parse(argv[1..-1])
        options = result.to_hash
        arguments = result.arguments

        eval "CommandSemantics::#{function}(options, arguments)"
      rescue Slop::Error => e
        puts "#{APPNAME}: #{e}"
      rescue Exception => e
        puts e
      end
    else
      puts "#{APPNAME}: '#{command}' is not a valid command. See '#{APPNAME} help'"
    end
  end
end
show(opts, args) click to toggle source
# File lib/columbus3/cli/command_semantics.rb, line 108
def self.show opts, args
  show_machinery opts, args, Columbus3::LeafletRenderer
end
split(opts, args) click to toggle source
# File lib/columbus3/cli/command_semantics.rb, line 136
def self.split opts, args
end
version(opts = nil, argv = []) click to toggle source

Main App Starts Here!

# File lib/columbus3/cli/command_semantics.rb, line 14
def self.version opts = nil, argv = []
  puts "#{APPNAME} version #{VERSION}"
end

Private Class Methods

backup(filename) click to toggle source
# File lib/columbus3/cli/command_semantics.rb, line 175
def self.backup filename
  FileUtils::cp filename, filename + "~"
  puts "Backup copy #{filename} created in #{filename}~."
end
backup_and_write(filename, content) click to toggle source
# File lib/columbus3/cli/command_semantics.rb, line 180
def self.backup_and_write filename, content
   backup(filename) if File.exist?(filename)
   File.open(filename, "w") { |f| f.puts content }
 end
show_machinery(opts, args, renderer) click to toggle source

low level machinery for all renderers

  • opts list of options (filename and force)

  • args: CSV files to process

  • renderer, the renderer module to use, which has to expose the following calls: OUTPUT_FILENAME, to_javascript_filename, to_javascript_file,

# File lib/columbus3/cli/command_semantics.rb, line 190
def self.show_machinery opts, args, renderer
   filename = opts[:filename] || renderer::OUTPUT_FILENAME
   force = opts[:force]

   # build the argument list, which is either provided from the
   # command line or, if args is empty, from the standard input
   files = (args == [] ? STDIN.read.split("\n") : args)

   if files.empty?
     puts "Error! Please pipe or pass as argument the list of files to show on the map."
     exit 1
   end

   files.each do |file|
     javascript_filename = renderer::to_javascript_filename file
                          
     # backup if the geojson file exists and we are forcing writing
     backup(javascript_filename) if File.exist?(javascript_filename) and force

     # print some messages
     if force or not File.exist?(javascript_filename)
       printf "Creating javascript file for #{file} ..."
       print_done = true
     else
       puts "Javascript file for #{file} already exists (--force to force updating)"
       print_done = false
     end
     # create the geojson file (if necessary or required to do so)
     renderer::to_javascript_file file, force
     # print some messages
     puts "done!" if print_done
   end
   
   backup(filename) if File.exist? filename
   printf "Writing #{filename}... "
   renderer::show filename: filename, files: files
   puts "done!"
   puts "You might now want to open #{filename}."
 end