Class: Wurfl::Command::Loader

Inherits:
Wurfl::Command show all
Includes:
Utils
Defined in:
lib/wurfl/command/loader.rb

Instance Method Summary collapse

Methods included from Utils

#load_wurfl_pstore, #save_wurfl_pstore

Instance Method Details

#executeObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/wurfl/command/loader.rb', line 21

def execute
  print = false
  insert = false
  wurflfile = nil
  patchfile = nil
  pstorefile = nil
  pstoreload = false

  begin
    options = GetoptLong.new(
                             ["-p","--print", GetoptLong::NO_ARGUMENT],
                             ["-h","--help", GetoptLong::NO_ARGUMENT],
                             ["-f","--file", GetoptLong::REQUIRED_ARGUMENT],
                             ["-e","--extension", GetoptLong::REQUIRED_ARGUMENT],
                             ["-d","--database", GetoptLong::REQUIRED_ARGUMENT],
                             ["-l","--load", GetoptLong::REQUIRED_ARGUMENT]
                             )
    
    options.each do |opt,arg|
      case opt
      when "-p"
        print = true
      when "-h"
        usage
        exit 1
      when "-f"
        wurflfile = arg
      when "-e"
        patchfile = arg
      when "-d"
        pstorefile = arg
      when "-l"
        pstorefile = arg
        pstoreload = true
      else
        STDERR.puts "Unknown argument #{opt}"
        usage
      exit 1
      end    
    end
  rescue => err
    STDERR.puts "Error: #{err}"
    usage
    exit 1
  end

  wurfll = Wurfl::Loader.new
  hands = nil
  fallbacks = nil

  if pstorefile && pstoreload
    begin
      puts "Loading  data from #{pstorefile}"
      hands = load_wurfl_pstore(pstorefile)
      puts "Loaded"
    rescue => err
      STDERR.puts "Error: Cannot load PStore file."
      STDERR.puts err.message
      exit 1
    end
  else    
    if !wurflfile 
      STDERR.puts "You must pass a wurflfile if you want to do more."
      usage
      exit 1
    end
    
    starttime = Time.now
    puts "Loading wurfl file #{wurflfile}" 
    
    hands = wurfll.load_wurfl(wurflfile)
    restime = Time.now - starttime
    
    puts "Done loading wurfl.  Load took #{restime} seconds." 

    if patchfile
      starttime = Time.now
      puts "Loading Patch file #{patchfile}"
      hands = wurfll.load_wurfl(patchfile)
      restime = Time.now - starttime
      puts "Done loading patchfile.  Load took #{restime} seconds." 
    end
    
  end

  if pstorefile && !pstoreload
    begin
      puts "Saving data into #{pstorefile}"
      save_wurfl_pstore(pstorefile, hands)
      puts "Saved"
    rescue => err
      STDERR.puts "Error: Cannot creat PStore file."
      STDERR.puts err.message
    end
  end

  if print 
    hands.each do |key,value|
      puts "********************************************\n\n"
      puts "#{key}\n"
      value.each { |key,value|   puts "#{key} = #{value}" }
    end
  end
    
end

#usageObject



10
11
12
13
14
15
16
17
18
19
# File 'lib/wurfl/command/loader.rb', line 10

def usage
  puts "Usage: wurfltools.rb loader [-p -v -h -e patchfile] -f wurflfile"
  puts "       --file, -f (wurflfile): The master WURFL file to load."
  puts "       --extension, -e (patchfile): A patch file to extend the traits of the master WURLF file."
  puts "       --print, -p : Prints out handsets."
  puts "       --help, -h : Prints this message."
  puts "       --database, -d (databasename): Makes a PStore database for quick loading of data with other tools."
  puts "       --load, -l (databasename): Loads handsets from a PStore database instead of XML file."
  exit 1
end