riel 1.1.17 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,266 +0,0 @@
1
- #!/usr/bin/ruby -w
2
- # -*- ruby -*-
3
- #
4
- # = logger.rb
5
- #
6
- # Logging Module
7
- #
8
- # Author:: Jeff Pace <jpace@incava.org>
9
- # Documentation:: Author
10
- #
11
-
12
- require 'rubygems'
13
- require 'rainbow'
14
- require 'riel/log/severity'
15
- require 'riel/log/format'
16
-
17
- #
18
- # == Logger
19
- #
20
- # This class logs messages. You probably don't want to use this directly; Log is
21
- # the class containing the necessary class methods.
22
- #
23
- # == Examples
24
- #
25
- # See the unit tests in log_test.rb
26
- #
27
-
28
- module RIEL
29
- class Logger
30
- $LOGGING_LEVEL = nil
31
-
32
- attr_accessor :quiet
33
- attr_accessor :output
34
- attr_accessor :colorize_line
35
- attr_accessor :level
36
- attr_accessor :ignored_files
37
- attr_accessor :ignored_methods
38
- attr_accessor :ignored_classes
39
- attr_accessor :trim
40
-
41
- include Log::Severity
42
-
43
- FRAME_RE = Regexp.new('(.*):(\d+)(?::in \`(.*)\')?')
44
-
45
- def initialize
46
- set_defaults
47
- end
48
-
49
- def verbose= v
50
- @level = case v
51
- when TrueClass
52
- DEBUG
53
- when FalseClass
54
- FATAL
55
- when Integer
56
- v
57
- end
58
- end
59
-
60
- def set_defaults
61
- $LOGGING_LEVEL = @level = FATAL
62
- @ignored_files = Hash.new
63
- @ignored_methods = Hash.new
64
- @ignored_classes = Hash.new
65
- @width = 0
66
- @output = $stdout
67
- @colors = Array.new
68
- @colorize_line = false
69
- @quiet = false
70
- @trim = true
71
-
72
- set_default_widths
73
- end
74
-
75
- def set_default_widths
76
- set_widths Log::DEFAULT_FILENAME_WIDTH, Log::DEFAULT_LINENUM_WIDTH, Log::DEFAULT_FUNCTION_WIDTH
77
- end
78
-
79
- def verbose
80
- level <= DEBUG
81
- end
82
-
83
- # Assigns output to a file with the given name. Returns the file; client
84
- # is responsible for closing it.
85
- def outfile= f
86
- @output = f.kind_of?(IO) ? f : File.new(f, "w")
87
- end
88
-
89
- # Creates a printf format for the given widths, for aligning output. To lead
90
- # lines with zeros (e.g., "00317") the line_width argument must be a string,
91
- # not an integer.
92
- def set_widths file_width, line_width, func_width
93
- @file_width = file_width
94
- @line_width = line_width
95
- @function_width = func_width
96
-
97
- @format = "[%#{file_width}s:%#{line_width}d] {%#{func_width}s}"
98
- end
99
-
100
- def ignore_file fname
101
- ignored_files[fname] = true
102
- end
103
-
104
- def ignore_method methname
105
- ignored_methods[methname] = true
106
- end
107
-
108
- def ignore_class classname
109
- ignored_classes[classname] = true
110
- end
111
-
112
- def log_file fname
113
- ignored_files.delete fname
114
- end
115
-
116
- def log_method methname
117
- ignored_methods.delete methname
118
- end
119
-
120
- def log_class classname
121
- ignored_classes.delete classname
122
- end
123
-
124
- def debug msg = "", depth = 1, &blk
125
- log msg, DEBUG, depth + 1, &blk
126
- end
127
-
128
- def info msg = "", depth = 1, &blk
129
- log msg, INFO, depth + 1, &blk
130
- end
131
-
132
- def warn msg = "", depth = 1, &blk
133
- log msg, WARN, depth + 1, &blk
134
- end
135
-
136
- def error msg = "", depth = 1, &blk
137
- log msg, ERROR, depth + 1, &blk
138
- end
139
-
140
- def fatal msg = "", depth = 1, &blk
141
- log msg, FATAL, depth + 1, &blk
142
- end
143
-
144
- # Logs the given message.
145
- def log msg = "", lvl = DEBUG, depth = 1, cname = nil, &blk
146
- if lvl >= level
147
- frame = nil
148
-
149
- stk = caller 0
150
- stk.reverse.each_with_index do |frm, idx|
151
- if frm.index(%r{riel/log/log.*:\d+:in\b})
152
- break
153
- else
154
- frame = frm
155
- end
156
- end
157
-
158
- print_stack_frame frame, cname, msg, lvl, &blk
159
- end
160
- end
161
-
162
- # Shows the current stack.
163
- def stack msg = "", lvl = DEBUG, depth = 1, cname = nil, &blk
164
- if lvl >= level
165
- stk = caller depth
166
- for frame in stk
167
- print_stack_frame frame, cname, msg, lvl, &blk
168
- cname = nil
169
- msg = ""
170
- end
171
- end
172
- end
173
-
174
- def print_stack_frame frame, cname, msg, lvl, &blk
175
- md = FRAME_RE.match frame
176
- file, line, func = md[1], md[2], (md[3] || "")
177
- # file.sub!(/.*\//, "")
178
-
179
- # Ruby 1.9 expands the file name, but 1.8 doesn't:
180
- pn = Pathname.new(file).expand_path
181
-
182
- file = pn.to_s
183
-
184
- if cname
185
- func = cname + "#" + func
186
- end
187
-
188
- if ignored_files[file] || (cname && ignored_classes[cname]) || ignored_methods[func]
189
- # skip this one.
190
- else
191
- print_formatted(file, line, func, msg, lvl, &blk)
192
- end
193
- end
194
-
195
- def print_formatted file, line, func, msg, lvl, &blk
196
- if trim
197
- fmt = Format.new
198
- file = fmt.trim_right file, @file_width
199
- line = fmt.trim_left line, @line_width
200
- func = fmt.trim_left func, @function_width
201
- end
202
-
203
- hdr = sprintf @format, file, line, func
204
- print hdr, msg, lvl, &blk
205
- end
206
-
207
- def print hdr, msg, lvl, &blk
208
- if blk
209
- x = blk.call
210
- if x.kind_of? String
211
- msg = x
212
- else
213
- return
214
- end
215
- end
216
-
217
- msg = msg.to_s.chomp
218
-
219
- if lvlcol = @colors[lvl]
220
- if colorize_line
221
- line = hdr + " " + msg
222
- @output.puts line.color(lvlcol)
223
- else
224
- @output.puts hdr + " " + msg.color(lvlcol)
225
- end
226
- else
227
- @output.puts hdr + " " + msg
228
- end
229
- end
230
-
231
- def set_color lvl, color
232
- @colors[lvl] = color
233
- end
234
-
235
- def method_missing meth, *args, &blk
236
- validcolors = Sickill::Rainbow::TERM_COLORS
237
- # only handling foregrounds, not backgrounds
238
- if code = validcolors[meth]
239
- add_color_method meth.to_s, code + 30
240
- send meth, *args, &blk
241
- else
242
- super
243
- end
244
- end
245
-
246
- def respond_to? meth
247
- validcolors = Sickill::Rainbow::TERM_COLORS
248
- validcolors.include?(meth) || super
249
- end
250
-
251
- def add_color_method color, code
252
- instmeth = Array.new
253
- instmeth << "def #{color}(msg = \"\", lvl = DEBUG, depth = 1, cname = nil, &blk)"
254
- instmeth << " log(\"\\e[#{code}m\#{msg\}\\e[0m\", lvl, depth + 1, cname, &blk)"
255
- instmeth << "end"
256
- instance_eval instmeth.join("\n")
257
-
258
- clsmeth = Array.new
259
- clsmeth << "def #{color}(msg = \"\", lvl = DEBUG, depth = 1, cname = nil, &blk)"
260
- clsmeth << " logger.#{color}(\"\\e[#{code}m\#{msg\}\\e[0m\", lvl, depth + 1, cname, &blk)"
261
- clsmeth << "end"
262
-
263
- class_eval clsmeth.join("\n")
264
- end
265
- end
266
- end
@@ -1,22 +0,0 @@
1
- #!/usr/bin/ruby -w
2
- # -*- ruby -*-
3
- #
4
- # = severity.rb
5
- #
6
- # Logging Module
7
- #
8
- # Author:: Jeff Pace <jpace@incava.org>
9
- # Documentation:: Author
10
- #
11
-
12
- module RIEL
13
- class Log
14
- module Severity
15
- DEBUG = 0
16
- INFO = 1
17
- WARN = 2
18
- ERROR = 3
19
- FATAL = 4
20
- end
21
- end
22
- end
data/lib/riel/log.rb DELETED
@@ -1,21 +0,0 @@
1
- #!/usr/bin/ruby -w
2
- # -*- ruby -*-
3
- #
4
- # = log.rb
5
- #
6
- # Logging Module
7
- #
8
- # Author:: Jeff Pace <jpace@incava.org>
9
- # Documentation:: Author
10
- #
11
-
12
- require 'riel/log/loggable'
13
- require 'riel/log/log'
14
-
15
- module RIEL
16
- class AppLog < Log
17
- include Log::Severity
18
- end
19
- end
20
-
21
- include RIEL
data/lib/riel/optproc.rb DELETED
@@ -1,296 +0,0 @@
1
- #!/usr/bin/ruby -w
2
- # -*- ruby -*-
3
-
4
- require 'riel/log'
5
-
6
- module OptProc
7
- class Option
8
- include Loggable
9
-
10
- attr_reader :md, :tags, :res
11
-
12
- ARG_INTEGER = %r{^ ([\-\+]?\d+) $ }x
13
- ARG_FLOAT = %r{^ ([\-\+]?\d* (?:\.\d+)?) $ }x
14
- ARG_STRING = %r{^ [\"\']? (.*?) [\"\']? $ }x
15
- ARG_BOOLEAN = %r{^ (yes|true|on|no|false|off) $ }ix
16
-
17
- ARG_TYPES = Array.new
18
- ARG_TYPES << [ :integer, ARG_INTEGER ]
19
- ARG_TYPES << [ :float, ARG_FLOAT ]
20
- ARG_TYPES << [ :string, ARG_STRING ]
21
- ARG_TYPES << [ :boolean, ARG_BOOLEAN ]
22
-
23
- def initialize args = Hash.new, &blk
24
- @tags = args[:tags] || Array.new
25
- @rc = args[:rc]
26
- @rc = [ @rc ] if @rc.kind_of?(String)
27
- @md = nil
28
- @set = blk || args[:set]
29
-
30
- @type = nil
31
- @valuere = nil
32
-
33
- @argtype = nil
34
-
35
- @res = args[:res]
36
- @res = [ @res ] if @res.kind_of?(Regexp)
37
-
38
- if args[:arg]
39
- demargs = args[:arg].dup
40
- while arg = demargs.shift
41
- case arg
42
- when :required
43
- @type = "required"
44
- when :optional
45
- @type = "optional"
46
- when :none
47
- @type = nil
48
- when :regexp
49
- @valuere = demargs.shift
50
- else
51
- if re = ARG_TYPES.assoc(arg)
52
- @valuere = re[1]
53
- @argtype = arg
54
- @type ||= "required"
55
- end
56
- end
57
- end
58
- end
59
- end
60
-
61
- def inspect
62
- '[' + @tags.collect { |t| t.inspect }.join(" ") + ']'
63
- end
64
-
65
- def to_str
66
- to_s
67
- end
68
-
69
- def to_s
70
- @tags.join " "
71
- end
72
-
73
- def match_rc? field
74
- @rc && @rc.include?(field)
75
- end
76
-
77
- def match_value val
78
- @md = @valuere && @valuere.match(val)
79
- @md && @md[1]
80
- end
81
-
82
- def match_tag tag
83
- if tm = @tags.detect do |t|
84
- t.index(tag) == 0 && tag.length <= t.length
85
- end
86
-
87
- if tag.length == tm.length
88
- 1.0
89
- else
90
- tag.length.to_f * 0.01
91
- end
92
- else
93
- nil
94
- end
95
- end
96
-
97
- def match args, opt = args[0]
98
- return nil unless %r{^-}.match opt
99
-
100
- tag = opt.split('=', 2)[0] || opt
101
-
102
- @md = nil
103
-
104
- if @res && (@md = @res.collect { |re| re.match(opt) }.detect)
105
- 1.0
106
- else
107
- match_tag tag
108
- end
109
- end
110
-
111
- def set_value args, opt = args[0]
112
- val = opt.split('=', 2)[1]
113
- args.shift
114
-
115
- if @md
116
- # already have match data
117
- elsif @type == "required"
118
- if val
119
- # already have value
120
- elsif args.size > 0
121
- val = args.shift
122
- else
123
- $stderr.puts "value expected"
124
- end
125
-
126
- if val
127
- match_value val
128
- end
129
- elsif @type == "optional"
130
- if val
131
- # already have value
132
- match_value val
133
- elsif args.size > 0
134
- if %r{^-}.match args[0]
135
- # skipping next value; apparently option
136
- elsif match_value args[0]
137
- # value matches
138
- args.shift
139
- end
140
- end
141
- end
142
-
143
- value = value_from_match
144
-
145
- set value, opt, args
146
- end
147
-
148
- def value_from_match
149
- if @md
150
- if @argtype.nil? || @argtype == :regexp
151
- @md
152
- else
153
- convert_value @md[1]
154
- end
155
- elsif @argtype == :boolean
156
- true
157
- end
158
- end
159
-
160
- def convert_value val
161
- if val
162
- case @argtype
163
- when :string
164
- val
165
- when :integer
166
- val.to_i
167
- when :float
168
- val.to_f
169
- when :boolean
170
- to_boolean val
171
- when :regexp
172
- val
173
- when nil
174
- val
175
- else
176
- log { "unknown argument type: #{@type.inspect}" }
177
- end
178
- elsif @argtype == :boolean
179
- true
180
- end
181
- end
182
-
183
- def to_boolean val
184
- %w{ yes true on soitenly }.include? val.downcase
185
- end
186
-
187
- def set val, opt = nil, args = nil
188
- setargs = [ val, opt, args ].select_with_index { |x, i| i < @set.arity }
189
- @set.call(*setargs)
190
- end
191
- end
192
-
193
- class OptionSet
194
- attr_reader :options
195
-
196
- def initialize data
197
- @options = Array.new
198
- @shortopts = Array.new
199
- @longopts = Array.new
200
- @regexps = Hash.new
201
-
202
- data.each do |optdata|
203
- opt = OptProc::Option.new optdata
204
- @options << opt
205
-
206
- opt.tags.each do |tag|
207
- ch = tag[0]
208
- if ch == 45 # 45 = '-'
209
- ch = tag[1]
210
- assocopts = nil
211
- if ch == tag
212
- ch = tag[2]
213
- assocopts = @longopts
214
- else
215
- assocopts = @shortopts
216
- end
217
-
218
- (assocopts[ch] ||= Array.new) << opt
219
- end
220
-
221
- if res = opt.res
222
- res.each do |re|
223
- (@regexps[re] ||= Array.new) << opt
224
- end
225
- end
226
- end
227
- end
228
- end
229
-
230
- COMBINED_OPTS_RES = [
231
- # -number non-num
232
- Regexp.new('^ ( - \d+ ) ( \D+.* ) $ ', Regexp::EXTENDED),
233
- # -letter anything
234
- Regexp.new('^ ( - [a-zA-Z] ) ( .+ ) $ ', Regexp::EXTENDED)
235
- ]
236
-
237
- def process_option args
238
- opt = args[0]
239
-
240
- if md = COMBINED_OPTS_RES.collect { |re| re.match opt }.detect
241
- lhs = md[1]
242
- rhs = "-" + md[2]
243
- args[0, 1] = lhs, rhs
244
-
245
- return process_option args
246
- elsif opt[0] == 45
247
- ch = opt[1]
248
- assocopts = if ch == 45 # 45 = '-'
249
- ch = opt[2]
250
- @longopts[ch]
251
- elsif ch.nil?
252
- nil
253
- else
254
- @shortopts[ch]
255
- end
256
-
257
- if assocopts && x = set_option(assocopts, args)
258
- return x
259
- end
260
- end
261
-
262
- if x = set_option(@options, args)
263
- return x
264
- elsif @bestmatch
265
- if @bestopts.size == 1
266
- @bestopts[0].set_value args
267
- return @bestopts[0]
268
- else
269
- optstr = @bestopts.collect { |y| '(' + y.tags.join(', ') + ')' }.join(', ')
270
- $stderr.puts "ERROR: ambiguous match of '#{args[0]}'; matches options: #{optstr}"
271
- exit 2
272
- end
273
- end
274
-
275
- nil
276
- end
277
-
278
- def set_option optlist, args
279
- @bestmatch = nil
280
- @bestopts = Array.new
281
-
282
- optlist.each do |option|
283
- next unless mv = option.match(args)
284
- if mv >= 1.0
285
- # exact match:
286
- option.set_value args
287
- return option
288
- elsif !@bestmatch || @bestmatch <= mv
289
- @bestmatch = mv
290
- @bestopts << option
291
- end
292
- end
293
- nil
294
- end
295
- end
296
- end
@@ -1,22 +0,0 @@
1
- #!/usr/bin/ruby -w
2
- # -*- ruby -*-
3
-
4
- require 'test/unit'
5
- require 'riel/array'
6
-
7
- class ArrayTestCase < Test::Unit::TestCase
8
-
9
- def test_to_s
10
- a = %w{ this is a test }
11
- assert_equal "[ this, is, a, test ]", a.to_s
12
- end
13
-
14
- def test_rand
15
- a = %w{ this is a test }
16
- 10.times do
17
- r = a.rand
18
- assert_not_nil r
19
- assert a.include?(r)
20
- end
21
- end
22
- end
@@ -1,48 +0,0 @@
1
- #!/usr/bin/ruby -w
2
- # -*- ruby -*-
3
-
4
- require 'pathname'
5
- require 'test/unit'
6
- require 'stringio'
7
- require 'riel/log/format'
8
- require 'riel/log/loggable'
9
-
10
- module RIEL
11
- class FormatTestCase < Test::Unit::TestCase
12
- include RIEL::Loggable
13
-
14
- def run_trim_left_test expected, length, str = "something"
15
- trimmed = Format.new.trim_left(str, length)
16
- assert_equal expected, trimmed
17
- end
18
-
19
- def test_trim_left_short_positive_number
20
- run_trim_left_test "some", 4
21
- end
22
-
23
- def test_trim_left_long
24
- run_trim_left_test "something", 10
25
- end
26
-
27
- def test_trim_left_short_negative_number
28
- run_trim_left_test "some", -4
29
- end
30
-
31
- def run_trim_right_test expected, length, str = "something"
32
- trimmed = Format.new.trim_right(str, length)
33
- assert_equal expected, trimmed
34
- end
35
-
36
- def test_trim_right_short_positive_number
37
- run_trim_right_test " ...", 5
38
- end
39
-
40
- def test_trim_right_long
41
- run_trim_right_test "something", 10
42
- end
43
-
44
- def test_trim_right_short_negative_number
45
- run_trim_right_test " ...", -5
46
- end
47
- end
48
- end