grassgis 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 788c22870c881d792c8412e3f2ebbe44015c6e28
4
- data.tar.gz: 2f4204831ac3bd07ffa8ee5f774777ba5faabae0
3
+ metadata.gz: 299ce1dcdebe07da42c2207549ab9fdeacc9cb2b
4
+ data.tar.gz: 206df84d976c27567c4a5fe7fc3dd1949d5f92d6
5
5
  SHA512:
6
- metadata.gz: c17ad3bc200c2ac699e95d61ca32ab734b03f6bff74e1219ea3c18bd9dbab3fac9d07be77e44a0895efa3d33d8ee2f5acf2eff543c95de326f7b68e7c957adc1
7
- data.tar.gz: 8e144e6a0de01b5ba6e875284094114164847c76e545527f3e1756572cbe2a4ce1478d26b81e0c7402f8347efead8ef38a0b3030cfd80f575204cb562df33a01
6
+ metadata.gz: be5e141c19ebc4880733387277d61a47fca4993ba1b74e57de8461c52b93965d83d3380864acc3aa37ac9886c53674d463b098ee0b193f2b7742c843c49a845d
7
+ data.tar.gz: f9d883babf14d38607cc7430c964cfd10d017a246256b135ef6a26a6ba65db142481c94700744d975c081047506305aca059050870bb164355e8fc744ece2b29
data/README.md CHANGED
@@ -1,5 +1,8 @@
1
1
  # GrassGis
2
2
 
3
+ [![Gem Version](https://badge.fury.io/rb/grassgis.svg)](http://badge.fury.io/rb/grassgis)
4
+ [![Build Status](https://travis-ci.org/jgoizueta/grassgis.svg)](https://travis-ci.org/jgoizueta/grassgis)
5
+
3
6
  Support for scripting GRASS with Ruby.
4
7
 
5
8
  ## Installation
@@ -40,11 +43,13 @@ end
40
43
 
41
44
  ## Roadmap
42
45
 
43
- * Write the executed command history to the location's +.bash_history+
44
- or to another file specified in the session configuration.
45
- * Log GRASS command output / error output
46
- * Method to clean GRASS temporaries ($GISBASE/etc/clean_temp), or do
47
- it automatically when disposing the session.
46
+ * Write more documentation with examples.
47
+ * Add some session helpers:
48
+ - Method to clean GRASS temporaries ($GISBASE/etc/clean_temp), or do
49
+ it automatically when disposing the session.
50
+ - Methods to check if maps exist
51
+ - Methods that return information as objects (arrays, hashes), e.g.
52
+ values returned by r.what, the current region, etc.
48
53
 
49
54
  ### GRASS cooking DSL
50
55
 
@@ -17,13 +17,14 @@ module GrassGis
17
17
  end
18
18
  end
19
19
  config[:message_format] ||= 'plain'
20
- config[:true_color] = true unless config.has_key?(:true_color)
21
- config[:transparent] = true unless config.has_key?(:transparent)
22
- config[:png_auto_write] = true unless config.has_key?(:png_auto_write)
20
+ config[:true_color] = true unless config.key?(:true_color)
21
+ config[:transparent] = true unless config.key?(:transparent)
22
+ config[:png_auto_write] = true unless config.key?(:png_auto_write)
23
23
  config[:gnuplot] ||= 'gnuplot -persist'
24
24
  config[:gui] ||= 'wxpython'
25
25
 
26
26
  config[:errors] ||= :raise
27
+ config[:echo] = :commands unless config.key?(:echo)
27
28
 
28
29
  @config = config
29
30
 
@@ -76,6 +77,10 @@ module GrassGis
76
77
  last.error_output
77
78
  end
78
79
 
80
+ def configuration
81
+ @config
82
+ end
83
+
79
84
  def allocate
80
85
  @gisrc = Tempfile.new('gisrc')
81
86
  @gisrc.puts "LOCATION_NAME: #{@config[:location]}"
@@ -112,7 +117,7 @@ module GrassGis
112
117
  end
113
118
  insert_path 'PATH', *paths
114
119
  insert_path 'MANPATH', File.join(@config[:gisbase], 'man')
115
- @history = @config[:history] = []
120
+ @history = []
116
121
  end
117
122
 
118
123
  def dispose
@@ -129,7 +134,7 @@ module GrassGis
129
134
  define_method root_module.to_sym do
130
135
  var_name = "@#{root_module}"
131
136
  m = instance_variable_get(var_name)
132
- m ||= Module.new(root_module, configuration: @config)
137
+ m ||= Module.new(root_module, context: self)
133
138
  instance_variable_set var_name, m
134
139
  m
135
140
  end
@@ -159,8 +164,57 @@ module GrassGis
159
164
  instance_eval(&blk)
160
165
  end
161
166
 
167
+ def dry?
168
+ @config[:dry]
169
+ end
170
+
171
+ def execute(cmd)
172
+ @history << cmd
173
+ if @config[:echo]
174
+ puts cmd.to_s(with_input: false)
175
+ end
176
+ log_file = @config[:log] || @config[:history]
177
+ if log_file
178
+ log_timestamp log_file
179
+ log log_file, cmd.to_s(with_input: true)
180
+ end
181
+ unless dry?
182
+ cmd.run error_output: :reparate
183
+ end
184
+ if cmd.output
185
+ puts cmd.output if @config[:echo] == :output
186
+ end
187
+ handle_errors cmd
188
+ cmd
189
+ end
190
+
162
191
  private
163
192
 
193
+ def log_timestamp(file)
194
+ log file, Time.now
195
+ end
196
+
197
+ def log(file, message)
198
+ if file && message
199
+ File.open(file, 'a') do |log_file|
200
+ log_file.puts message
201
+ end
202
+ end
203
+ end
204
+
205
+ def handle_errors(cmd)
206
+ GrassGis.error cmd, @config[:errors]
207
+ if @config[:echo] == :output || @config[:log] || @config[:errors] == :console
208
+ error_info = GrassGis.error_info(cmd)
209
+ if error_info
210
+ if @config[:errors] == :console || @config[:echo] == :output
211
+ STDERR.puts error_info
212
+ end
213
+ log @config[:log], error_info
214
+ end
215
+ end
216
+ end
217
+
164
218
  def bool_var(value)
165
219
  value ? 'TRUE' : 'FALSE'
166
220
  end
@@ -221,15 +275,27 @@ module GrassGis
221
275
  #
222
276
  # Other pararameters:
223
277
  #
224
- # :errors to define the behaviour when a GRASS command fails:
225
- # * :raise is the default and raises on errors
226
- # * :console shows standar error output of commands
227
- # * :quiet error output is retained but not shown
278
+ # :errors to define the behaviour when a GRASS command fails:
279
+ # * :raise is the default and raises on errors
280
+ # * :console shows standar error output of commands
281
+ # * :quiet error output is retained but not shown
282
+ #
283
+ # If :errors is anything other than :raise, it is up to the user
284
+ # to check each command for errors. With the :console option
285
+ # the standar error output of commands is sent to the console
286
+ # and is not accessible through the command's error_output method.
287
+ #
288
+ # :log is used to define a loggin file where executed commands
289
+ # and its output is written.
290
+ #
291
+ # :history is used to define a loggin file where only
292
+ # executed commands are written.
228
293
  #
229
- # If :error is anything other than :raise, it is up to the user
230
- # to check each command for errors. With the :console option
231
- # the standar error output of commands is sent to the console
232
- # and is not accessible through the command's error_output method.
294
+ # :echo controls what is echoed to the standard output
295
+ # and can be one of the following options:
296
+ # * :commands show all executed commands (the default)
297
+ # * :output show the output of commands too
298
+ # * false don't echo anything
233
299
  #
234
300
  def self.session(config, &blk)
235
301
  context = Context.new(config)
@@ -246,10 +312,11 @@ module GrassGis
246
312
  command && (!!command.error || (command.status_value && command.status_value != 0))
247
313
  end
248
314
 
249
- def error_info(command)
315
+ def self.error_info(command)
250
316
  if command
251
317
  if command.error
252
- command.error.to_s
318
+ info = "Error (#{command.error.class}):\n"
319
+ info << command.error.to_s
253
320
  elsif (command.status_value && command.status_value != 0)
254
321
  info = "Exit code #{command.status_value}\n"
255
322
  info << command.error_output if command.error_output
@@ -14,9 +14,7 @@ module GrassGis
14
14
  def initialize(id, options = {})
15
15
  @id = id.to_s
16
16
  @parent = options[:parent]
17
- @configuration = options[:configuration] || {}
18
- @history = @configuration[:history] || []
19
- @errors = @configuration[:errors] || :raise
17
+ @context = options[:context]
20
18
  end
21
19
 
22
20
  def name
@@ -54,22 +52,14 @@ module GrassGis
54
52
  end
55
53
  end
56
54
  end
57
- @history << cmd
58
- unless @configuration[:dry]
59
- run_options = {}
60
- if @errors == :console
61
- run_options[:error_output] = :console
62
- else
63
- run_options[:error_output] = :separate
64
- end
65
- cmd.run run_options
55
+ if @context
56
+ @context.execute cmd
66
57
  end
67
- GrassGis.error cmd, @errors
68
58
  cmd
69
59
  end
70
60
 
71
61
  def method_missing(method, *args)
72
- m = Module.new(method, parent: self, configuration: @configuration)
62
+ m = Module.new(method, parent: self, context: @context)
73
63
  if args.size > 0
74
64
  m.run *args
75
65
  else
@@ -1,3 +1,3 @@
1
1
  module GrassGis
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: grassgis
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Javier Goizueta
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-07-22 00:00:00.000000000 Z
11
+ date: 2015-07-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sys_cmd