command-monkey 0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/command-monkey.rb +57 -0
  2. metadata +55 -0
@@ -0,0 +1,57 @@
1
+ require 'pty'
2
+ require 'expect'
3
+
4
+ # This library runs an interactive program, such as pacmd or irb, and provides
5
+ # a method to execute commands in the program and return the replies.
6
+ class CommandMonkey
7
+
8
+ # program: the interactive program to execute
9
+ # prompt: regex detecting the interactive program's prompt
10
+ def initialize(program, prompt)
11
+ @prompt = prompt
12
+
13
+ # run the program in a separate thread so that this library does not block
14
+ # the client program
15
+ @operator = Fiber.new do |command|
16
+ PTY.spawn program do |output, input, pid|
17
+ @output = output
18
+
19
+ get_reply
20
+
21
+ loop do
22
+ input.puts command
23
+ command = Fiber.yield get_reply(command)
24
+ end
25
+ end
26
+ end
27
+ end
28
+
29
+ # Send a command to the pacmd session, and return the output
30
+ def enter(command)
31
+ @operator.resume(command)
32
+ end
33
+
34
+ # Captured output from the program is filtered through this method before
35
+ # being returned
36
+ def filter_output(text)
37
+ text.strip
38
+ end
39
+
40
+ # Return the pattern which needs to be removed, based on the command text
41
+ def strip_command_pattern(command)
42
+ /\A\s+#{Regexp.quote command}/
43
+ end
44
+
45
+ private
46
+
47
+ # Wait for the program to show its prompt, and yield the output before the
48
+ # prompt to the block
49
+ def get_reply(command=nil)
50
+ @output.expect @prompt do |reply, *_|
51
+ reply.sub!(/#{@prompt}\z/m, '')
52
+ reply.sub!(strip_command_pattern(command), '') if command
53
+ return filter_output(reply)
54
+ end
55
+ end
56
+ end
57
+
metadata ADDED
@@ -0,0 +1,55 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: command-monkey
3
+ version: !ruby/object:Gem::Version
4
+ version: "0.1"
5
+ platform: ruby
6
+ authors:
7
+ - Yaohan Chen
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2010-04-01 00:00:00 -04:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: Command Monkey runs an interactive command-line program such as irb in the background, and provides an interface to send commands and return their output.
17
+ email: yaohan.chen@gmail.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - lib/command-monkey.rb
26
+ has_rdoc: true
27
+ homepage: http://github.com/hagabaka/command-monkey
28
+ licenses: []
29
+
30
+ post_install_message:
31
+ rdoc_options: []
32
+
33
+ require_paths:
34
+ - lib
35
+ required_ruby_version: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: 1.9.0
40
+ version:
41
+ required_rubygems_version: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: "0"
46
+ version:
47
+ requirements: []
48
+
49
+ rubyforge_project:
50
+ rubygems_version: 1.3.5
51
+ signing_key:
52
+ specification_version: 3
53
+ summary: Interface for interactive command-reply programs
54
+ test_files: []
55
+