Class: Pho::CommandLine

Inherits:
Object
  • Object
show all
Defined in:
lib/pho/command_line.rb

Overview

Class that implements the command-line behaviour

Instance Method Summary collapse

Constructor Details

#initialize(opts, env, store = nil) ⇒ CommandLine

Returns a new instance of CommandLine.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/pho/command_line.rb', line 6

def initialize(opts, env, store=nil)
 @opts = opts
 @env = env          
  
 if username() == nil
   raise "no <username>"
 end
 if password() == nil
    raise "no <password>"
 end
 if storename() == nil && store == nil
    raise "no <store>"
 end

 @store = Pho::Store.new(storename(), username(), password()) if store == nil
 @store = store if store != nil
       
end

Instance Method Details

#add_mapping(out = $stdout) ⇒ Object



270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
# File 'lib/pho/command_line.rb', line 270

def add_mapping(out=$stdout)
  fpmap = Pho::FieldPredicateMap.read_from_store(@store)
  mapping = Pho::FieldPredicateMap.create_mapping(@store, @opts["predicate"], @opts["field"], @opts["analyzer"])

  removed = fpmap.remove_by_name(@opts["field"])
  if removed != nil
    out.puts("Replacing mapping for #{@opts["field"]}")  
  end      
  fpmap << mapping
  resp = fpmap.upload(@store)
  if resp.status != 200
    out.puts "Unable to update FieldPredicate map in store. Response code was #{resp.status}"
  else
    out.puts "FieldPredicate map successfully updated"
  end
end

#add_weight(out = $stdout) ⇒ Object



287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
# File 'lib/pho/command_line.rb', line 287

def add_weight(out=$stdout)
  qp = Pho::QueryProfile.read_from_store(@store)
  weight = Pho::QueryProfile.create_weighting(@store, @opts["field"], @opts["weight"])
  
  removed = qp.remove_by_name( @opts["field"] )
  if removed != nil
      out.puts("Replacing weighting for #{@opts["field"]}")        
  end
  qp << weight
  resp = qp.upload(@store)
  if resp.status != 200
    out.puts "Unable to update QueryProfile in store. Response code was #{resp.status}"
  else
    out.puts "QueryProfile successfully updated"
  end      
end

#backupObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/pho/command_line.rb', line 52

def backup()
  puts "Submitting Snapshot Job to Store #{@store.storeuri}"
  resp = Pho::Jobs.submit_snapshot(@store, "Reindex", Time.now)
  puts "Monitoring Snapshot Job: #{resp.header["Location"].first}"
  job = Pho::Jobs.(resp, @store) do |job, message, time|
     puts "#{time} #{message}"
  end 
  puts "Job Completed"         
  snapshot = Pho::Snapshot.read_from_store(@store)
  puts "Retrieving #{snapshot.url}"
  dir = @opts["dir"] || Dir.tmpdir
  snapshot.backup(@store, dir)
  puts "Download complete. MD5 OK."      
end

#describeObject



115
116
117
118
119
120
121
122
123
# File 'lib/pho/command_line.rb', line 115

def describe()
  resp = @store.describe( @opts["url"] )
  if resp.status == 200
    puts resp.content
  else
    puts "Error: #{resp.status} #{resp.reason}"
    puts resp.content
  end    
end

#fpmap(out = $stdout) ⇒ Object



221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/pho/command_line.rb', line 221

def fpmap(out=$stdout)
  
  if @opts["raw"]
    resp = @store.get_field_predicate_map(Pho::ACCEPT_RDF)        
    if resp.status != 200
      out.puts "Unable to read Field Predicate Map from store. Response code was #{resp.status}"
    else
      out.puts resp.content
    end
  else  
    fpmap = Pho::FieldPredicateMap.read_from_store(@store)
    mappings = fpmap.datatype_properties.sort { |x,y| x.name <=> y.name }
    mappings.each do |mapping|        
      analyzer = mapping.analyzer
      if analyzer != nil
        Pho::Analyzers.constants.each do |c|
          if analyzer == Pho::Analyzers.const_get(c)
            analyzer = c
          end
        end
      end
      out.puts "#{mapping.name} -> #{mapping.property_uri}" if analyzer == nil
      out.puts "#{mapping.name} -> #{mapping.property_uri} (#{mapping.analyzer})" if analyzer != nil
    end 
  
  end
        
end

#passwordObject



31
32
33
34
35
# File 'lib/pho/command_line.rb', line 31

def password()
 return @opts["password"] if @opts["password"]
 return @env["TALIS_PASS"] if @env["TALIS_PASS"]
 return nil  
end

#queryprofile(out = $stdout) ⇒ Object



250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# File 'lib/pho/command_line.rb', line 250

def queryprofile(out=$stdout)

  if @opts["raw"]
    resp = @store.get_query_profile(Pho::ACCEPT_RDF)        
    if resp.status != 200
      out.puts "Unable to read Query Profile from store. Response code was #{resp.status}"
    else
      out.puts resp.content
    end
  else  
    queryprofile = Pho::QueryProfile.read_from_store(@store)
    field_weights = queryprofile.field_weights()
    field_weights = field_weights.sort { |x,y| x.name <=> y.name }
    field_weights.each do |weighting|
      out.puts "#{weighting.name} -> #{weighting.weight}"
    end      
  end
        
end

#reindexObject



77
78
79
80
81
82
83
84
85
86
87
# File 'lib/pho/command_line.rb', line 77

def reindex()
  puts "Submitting Reindex Job to Store #{@store.storeuri}"
  resp = Pho::Jobs.submit_reindex(@store, "Reindex", Time.now)
  if resp.status == 201
      puts "Monitoring Reindex Job: #{resp.header["Location"].first}"
  end
  job = Pho::Jobs.(resp, @store) do |job, message, time|
      puts "#{time} #{message}"
  end 
  puts "Reindex Completed"      
end

#resetObject



89
90
91
92
93
94
95
96
97
# File 'lib/pho/command_line.rb', line 89

def reset()
  puts "Resetting Store #{@store.storeuri}"
  resp = Pho::Jobs.submit_reset(@store, "Reset", Time.now)
  puts "Monitoring Reset Job: #{resp.header["Location"].first}"
  job = Pho::Jobs.(resp, @store) do |job, message, time|
    puts "#{time} #{message}"
  end 
  puts "Reset Completed"      
end

#restoreObject



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/pho/command_line.rb', line 99

def restore()
  url = @opts["url"]     
  if url == nil
    puts "Restoring #{@store.storeuri} from latest snapshot"
    snapshot = Pho::Snapshot.read_from_store(@store)
    url = snapshot.url
  end
  puts "Restoring from #{url}"    
  resp = Pho::Jobs.submit_restore(@store, url, "Reset", Time.now)
  puts "Monitoring Restore Job: #{resp.header["Location"].first}"
  job = Pho::Jobs.(resp, @store) do |job, message, time|
    puts "#{time} #{message}"
  end 
  puts "Restore Completed"      
end

#snapshotObject



67
68
69
70
71
72
73
74
75
# File 'lib/pho/command_line.rb', line 67

def snapshot()
  puts "Submitting Snapshot Job to Store #{@store.storeuri}"
  resp = Pho::Jobs.submit_snapshot(@store, "Reindex", Time.now)
  puts "Monitoring Snapshot Job #{resp.header["Location"].first}"
  job = Pho::Jobs.(resp, @store) do |job, message, time|
    puts "#{time} #{message}"
  end 
  puts "Snapshot Completed"      
end

#sparqlObject



125
126
127
128
129
130
131
132
133
134
# File 'lib/pho/command_line.rb', line 125

def sparql()
  query = File.new( @opts["file"] ).read()
  resp = @store.sparql(query)
  if resp.status == 200
    puts resp.content
  else
    puts "Error: #{resp.status} #{resp.reason}"
    puts resp.content          
  end      
end

#statusObject



47
48
49
50
# File 'lib/pho/command_line.rb', line 47

def status()
  status = Pho::Status.read_from_store(@store)
  puts "Status of Store #{@store.storeuri}:\nReadable: #{status.readable?}\nWritable: #{status.writeable?}"      
end

#storeObject



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/pho/command_line.rb', line 136

def store()
  resp = nil
  if @opts["url"]
    puts "Storing remote data: #{@opts["url"]}"
    resp = @store.store_url( @opts["url"] ) 
  elsif @opts["file"]
    puts "Storing local file: #{@opts["file"]}"
    f = File.new( @opts["file"] )
    if File.extname( @opts["file"] ) == ".nt"
      resp = @store.store_file( f, nil, "text/plain" )
    elsif File.extname( @opts["file"] ) == ".ttl"
      resp = @store.store_file( f, nil, "text/turtle" )
    else
      resp = @store.store_file( f )  
    end          
  elsif @opts["dir"]
    if @opts["dir"] = "."
      @opts["dir"] = File.expand_path(".")
    end        
    puts "Storing contents of directory: #{@opts["dir"]}"
    collection = Pho::RDFCollection.new(@store, @opts["dir"])
    store_collection(collection)         
  else     
    #noop
  end 

  if resp != nil 
    if resp.status == 204
      puts "Complete"
    else
      puts "Error: #{resp.status} #{resp.reason}"
      puts resp.content
    end         
  end         
end

#store_collection(collection) ⇒ Object



201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/pho/command_line.rb', line 201

def store_collection(collection)
  if @opts["force"]
    puts "Resetting tracking files for directory #{@opts["dir"]}"
    collection.reset( @opts["traverse"] ? true : false )
  end
  if @opts["retry"]
    puts "Retrying failures in: #{@opts["dir"]}"     
    collection.retry_failures( @opts["traverse"] ? true : false )
    puts collection.summary( @opts["traverse"] ? true : false )   
  elsif @opts["updates"]
    puts "Uploading updates in: #{@opts["dir"]}"     
    collection.store_updates( @opts["traverse"] ? true : false )
    puts collection.summary( @opts["traverse"] ? true : false )           
  else
    puts "Uploading contents of directory: #{@opts["dir"]}"
    collection.store( @opts["traverse"] ? true : false )
    puts collection.summary( @opts["traverse"] ? true : false )        
  end              
end

#storenameObject



37
38
39
40
41
42
43
44
45
# File 'lib/pho/command_line.rb', line 37

def storename()      
  store = nil
  store = @env["TALIS_STORE"] if @env["TALIS_STORE"]
  store = @opts["store"] if @opts["store"]
  if store != nil && !store.start_with?("http")
      store = "http://api.talis.com/stores/#{store}"  
  end
  return store      
end

#uploadObject



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/pho/command_line.rb', line 172

def upload()
  resp = nil
  if @opts["file"]
    f = File.new( @opts["file"] )
    uri = File.basename( @opts["file"] )
    uri = "#{@opts["base"]}/#{uri}" if @opts["base"]            
    mime = MIME::Types.type_for( @opts["file"] )[0].to_s
    puts "Uploading file: #{ @opts["file"] } to /items/#{ uri } as #{mime}"
    resp = @store.upload_item( f , mime , uri )
  elsif @opts["dir"]
    if @opts["dir"] = "."
      @opts["dir"] = File.expand_path(".")
    end
    collection = Pho::FileManagement::FileManager.new(@store, @opts["dir"], @opts["base"])
    store_collection(collection)
  else     
    #noop
  end 

  if resp != nil
    if resp.status == 204
      puts "Complete"
    else
      puts "Error: #{resp.status} #{resp.reason}"
      puts resp.content
    end         
  end                
end

#usernameObject



25
26
27
28
29
# File 'lib/pho/command_line.rb', line 25

def username()
 return @opts["username"] if @opts["username"]
 return @env["TALIS_USER"] if @env["TALIS_USER"]
 return nil  
end