text-to-noise 0.1.4 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -7,10 +7,17 @@ require 'text_to_noise'
7
7
 
8
8
  player = TextToNoise::Player.new
9
9
 
10
- ARGV.each do |sound|
11
- sound += ".wav" unless sound =~ /\.wav$/
12
- player.play sound
13
- sleep 1
10
+ begin
11
+ ARGV.each do |sound|
12
+ sound += ".wav" unless sound =~ /\.wav$/
13
+ player.play sound
14
+ sleep 1
15
+ end
16
+ rescue TextToNoise::Player::SoundNotFound => e
17
+ $stderr.puts e.message
18
+ $stderr.puts "Sorry, but I know how to play these sounds:\n"
19
+ $stderr.print "\t"
20
+ $stderr.puts player.available_sounds.join( "\n\t" )
14
21
  end
15
22
 
16
23
  sleep 1 while player.playing?
@@ -12,15 +12,64 @@ options = {
12
12
  :config => "sample.sounds.rb"
13
13
  }
14
14
 
15
- OptionParser.new do |opts|
15
+ arg_parser = OptionParser.new do |opts|
16
16
  opts.banner = "Usage: #{$0} [options]"
17
-
17
+ opts.separator ""
18
+ opts.separator "Options:"
19
+
18
20
  opts.on( "-f", "--file FILE",
19
21
  "Read input from FILE. Defaults to stdin" ) { |f| options[:input] = File.open( f, "r" ) }
20
22
  opts.on( "-c", "--config MAPPING_CONFIG",
21
23
  "Read input to sound mapping configuration from MAPPING_CONFIG" ) { |c| options[:config] = c }
22
24
  opts.on( "-m", "--mute",
23
25
  "Don't play any sounds, just print what matched" ) { options[:mute] = true }
24
- end.parse!
26
+ opts.on_tail("-h", "--help", "Show this message") do
27
+ puts opts
28
+ exit
29
+ end
30
+ end
31
+
32
+ arg_parser.parse!
33
+
34
+ begin
35
+ TextToNoise::CommandLine.new( options ).run
36
+ rescue ArgumentError
37
+ $stderr.puts arg_parser
38
+ $stderr.puts <<-EOS
39
+
40
+ ERROR!
41
+
42
+ Could not locate a noise configuration.
43
+ Specify a configuration with the -c option.
44
+
45
+ Try this one if you're processing a Rails log:
46
+
47
+ match /Rendered/ => %w(crickets canary)
48
+ match /Rendering/ => "cardinal"
49
+ match /User Load/ => "nightingale"
50
+ match /Processing/ => "finch"
51
+ match /SessionsController#new/ => "owl"
52
+ match /404 Not Found/ => "hawk"
53
+
54
+ Or maybe you're watching your ssh access:
55
+
56
+ match /sshd.*Accepted/ => %w[rooster hawk chicken crow]
57
+
58
+ Copy one of those into a file and pass it along to text_to_noise.
59
+
60
+ For example:
61
+
62
+ echo 'match /sshd.*Accepted/ => %w[rooster hawk chicken crow]' > ssh.sounds.rb
63
+ tail -f /var/log/secure.log | text_to_noise -c ssh.sounds.rb
64
+
65
+ Still having problems?
66
+ Try logging an issue at https://github.com/tobytripp/text_to_noise/issues
67
+ or try bothering me on Twitter http://twitter.com/tobytripp
68
+
69
+
70
+ Thanks for playing!
71
+
25
72
 
26
- TextToNoise::CommandLine.new( options ).run
73
+ EOS
74
+ exit 1
75
+ end
@@ -0,0 +1,33 @@
1
+ Feature: Running text_to_noise
2
+
3
+ Use the command-line to tell text-to-noise where to find your config file, the
4
+ input to parse, and whether it should actually make any noise.
5
+
6
+ Scenario: Calling with no arguments
7
+ When I run `text_to_noise`
8
+
9
+ Then the output should contain:
10
+ """
11
+ Could not locate a noise configuration.
12
+ Specify a configuration with the -c option.
13
+
14
+ Try this one if you're processing a Rails log:
15
+
16
+ match /Rendered/ => %w(crickets canary)
17
+ match /Rendering/ => "cardinal"
18
+ match /User Load/ => "nightingale"
19
+ match /Processing/ => "finch"
20
+ match /SessionsController#new/ => "owl"
21
+ match /404 Not Found/ => "hawk"
22
+
23
+ Or maybe you're watching your ssh access:
24
+
25
+ match /sshd.*Accepted/ => %w[rooster hawk chicken crow]
26
+
27
+ Copy one of those into a file and pass it along to text_to_noise.
28
+
29
+ For example:
30
+
31
+ echo 'match /sshd.*Accepted/ => %w[rooster hawk chicken crow]' > ssh.sounds.rb
32
+ tail -f /var/log/secure.log | text_to_noise -c ssh.sounds.rb
33
+ """
@@ -2,11 +2,12 @@ LIB_DIR = File.dirname File.expand_path( __FILE__ )
2
2
  APP_ROOT = File.join LIB_DIR, '..'
3
3
  $LOAD_PATH << LIB_DIR
4
4
 
5
+ require 'logger'
6
+
5
7
  Dir["#{LIB_DIR}/text_to_noise/*.rb"].each { |lib|
6
8
  lib =~ %r<lib/(.*)\.rb$>
7
9
  require $1
8
10
  }
9
- require 'logger'
10
11
 
11
12
  module TextToNoise
12
13
  def self.player
@@ -1,11 +1,17 @@
1
+ require 'text_to_noise/logging'
2
+
1
3
  module TextToNoise
2
4
  class CommandLine
5
+ include Logging
3
6
  attr_reader :options, :mapping
4
7
 
5
8
  def initialize( options={} )
6
9
  @options = {
7
10
  :input => $stdin
8
11
  }.merge options
12
+
13
+ raise ArgumentError, "No configuration file provided." unless @options[:config]
14
+
9
15
  @mapping = Mapper.parse File.read( @options[:config] )
10
16
  TextToNoise.player = self.player
11
17
  rescue Errno::ENOENT => e
@@ -14,7 +20,7 @@ module TextToNoise
14
20
 
15
21
  def run
16
22
  LogReader.new( options[:input], mapping ).call
17
- puts "Input processing complete. Waiting for playback to finish..."
23
+ info "Input processing complete. Waiting for playback to finish..."
18
24
  while TextToNoise.player.playing?
19
25
  sleep 1
20
26
  end
@@ -10,14 +10,7 @@ module TextToNoise
10
10
  end
11
11
 
12
12
  def initialize()
13
- begin
14
- require 'rubygame'
15
- rescue LoadError
16
- require 'rubygems'
17
- require 'rubygame'
18
- end
19
-
20
- raise "No Mixer found! Make sure sdl_mixer is installed." unless defined? Rubygame::Sound
13
+ self.class.load_rubygame
21
14
 
22
15
  Rubygame::Sound.autoload_dirs << SOUND_DIR
23
16
  themes = Dir.new(SOUND_DIR).entries.reject { |e| e =~ /[.]/ }
@@ -41,5 +34,30 @@ module TextToNoise
41
34
  @sounds = @sounds.select &:playing?
42
35
  not @sounds.empty?
43
36
  end
37
+
38
+ def available_sounds()
39
+ Rubygame::Sound.autoload_dirs.map do |dir|
40
+ Dir[ "#{dir}/*.wav" ].map { |f| File.basename f }
41
+ end.flatten
42
+ end
43
+
44
+ def self.load_rubygame()
45
+ old_verbose, $VERBOSE = $VERBOSE, nil
46
+ old_stream, stream = $stdout.dup, $stdout
47
+ stream.reopen '/dev/null'
48
+ stream.sync = true
49
+
50
+ begin
51
+ require 'rubygame'
52
+ rescue LoadError
53
+ require 'rubygems'
54
+ require 'rubygame'
55
+ end
56
+
57
+ raise "No Mixer found! Make sure sdl_mixer is installed." unless defined? Rubygame::Sound
58
+ ensure
59
+ $VERBOSE = old_verbose
60
+ stream.reopen(old_stream)
61
+ end
44
62
  end
45
63
  end
@@ -1,3 +1,3 @@
1
1
  module TextToNoise
2
- VERSION = "0.1.4"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -1,70 +1,76 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  module TextToNoise
4
- describe TextToNoise::CommandLine do
5
- let( :mapping ) { double( Mapping ) }
6
- let( :reader ) { double( LogReader, :call => nil ) }
7
-
8
- before :each do
9
- Mapper.stub!( :parse ).and_return mapping
10
- LogReader.stub!( :new ).and_return reader
11
- File.stub!( :read ).with( "sound_map.rb" ).and_return "config"
12
- end
13
-
14
- describe "#initialize" do
15
- it "accepts an options object" do
16
- CommandLine.new( {} )
4
+ describe TextToNoise::CommandLine do
5
+ let( :mapping ) { double( Mapping ) }
6
+ let( :reader ) { double( LogReader, :call => nil ) }
7
+
8
+ before :each do
9
+ Mapper.stub!( :parse ).and_return mapping
10
+ LogReader.stub!( :new ).and_return reader
11
+ File.stub!( :read ).with( "sound_map.rb" ).and_return "config"
17
12
  end
18
13
 
19
- it "raises an error if the config file cannot be found" do
20
- File.stub!( :read ).and_raise Errno::ENOENT
21
- lambda {
22
- CommandLine.new :config => "not_found"
23
- }.should raise_error( ArgumentError )
24
- end
14
+ describe "#initialize" do
15
+ it "accepts an options object" do
16
+ CommandLine.new( { :config => "sound_map.rb" } )
17
+ end
18
+
19
+ it "throws an error if no config file is given" do
20
+ lambda {
21
+ CommandLine.new
22
+ }.should raise_error( ArgumentError )
23
+ end
24
+
25
+ it "raises an error if the config file cannot be found" do
26
+ File.stub!( :read ).and_raise Errno::ENOENT
27
+ lambda {
28
+ CommandLine.new :config => "not_found"
29
+ }.should raise_error( ArgumentError )
30
+ end
25
31
 
26
- context "when given a 'config' option" do
27
- it "instantiates a Mapping object with the specified configuration" do
28
- File.should_receive( :read ).with( "sound_map.rb" ).and_return "config"
29
- Mapper.should_receive( :parse ).with( "config" )
30
-
31
- CommandLine.new :config => "sound_map.rb"
32
+ context "when given a 'config' option" do
33
+ it "instantiates a Mapping object with the specified configuration" do
34
+ File.should_receive( :read ).with( "sound_map.rb" ).and_return "config"
35
+ Mapper.should_receive( :parse ).with( "config" )
36
+
37
+ CommandLine.new :config => "sound_map.rb"
38
+ end
32
39
  end
33
- end
34
40
 
35
- context "when given the 'mute' option" do
36
- it "sets the global Player to an instance of MutePlayer" do
37
- TextToNoise.should_receive( :player= ).with instance_of( MutePlayer )
38
- CommandLine.new :mute => true
41
+ context "when given the 'mute' option" do
42
+ it "sets the global Player to an instance of MutePlayer" do
43
+ TextToNoise.should_receive( :player= ).with instance_of( MutePlayer )
44
+ CommandLine.new :mute => true, :config => "sound_map.rb"
45
+ end
39
46
  end
40
47
  end
41
- end
42
48
 
43
- describe "#run" do
44
- let( :options ) { Hash[:input, :io, :config, "sound_map.rb"] }
45
-
46
- subject { CommandLine.new( options ) }
49
+ describe "#run" do
50
+ let( :options ) { Hash[:input, :io, :config, "sound_map.rb"] }
51
+
52
+ subject { CommandLine.new( options ) }
47
53
 
48
-
49
- it "creates a new LogReader object" do
50
- LogReader.should_receive( :new )
51
- subject.run
52
- end
54
+
55
+ it "creates a new LogReader object" do
56
+ LogReader.should_receive( :new )
57
+ subject.run
58
+ end
53
59
 
54
- it "passes the contents of the file in :input option to the reader" do
55
- LogReader.should_receive( :new ).with( :io, anything )
56
- subject.run
57
- end
60
+ it "passes the contents of the file in :input option to the reader" do
61
+ LogReader.should_receive( :new ).with( :io, anything )
62
+ subject.run
63
+ end
58
64
 
59
- it "passes an instance of a Mapping to the LogReader" do
60
- LogReader.should_receive( :new ).with( anything, mapping )
61
- subject.run
62
- end
65
+ it "passes an instance of a Mapping to the LogReader" do
66
+ LogReader.should_receive( :new ).with( anything, mapping )
67
+ subject.run
68
+ end
63
69
 
64
- it "calls #call on the LogReader" do
65
- reader.should_receive :call
66
- subject.run
70
+ it "calls #call on the LogReader" do
71
+ reader.should_receive :call
72
+ subject.run
73
+ end
67
74
  end
68
75
  end
69
76
  end
70
- end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: text-to-noise
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.1.4
5
+ version: 0.2.0
6
6
  platform: ruby
7
7
  authors:
8
8
  - Toby Tripp
@@ -77,6 +77,7 @@ files:
77
77
  - bin/play_noise
78
78
  - bin/text_to_noise
79
79
  - cucumber.yml
80
+ - features/commandline.feature
80
81
  - features/configuration.feature
81
82
  - features/step_definitions/debugger_steps.rb
82
83
  - features/support/env.rb
@@ -155,6 +156,7 @@ signing_key:
155
156
  specification_version: 3
156
157
  summary: Play sounds based on string matches.
157
158
  test_files:
159
+ - features/commandline.feature
158
160
  - features/configuration.feature
159
161
  - features/step_definitions/debugger_steps.rb
160
162
  - features/support/env.rb