cmd_creator 0.0.1

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.
data/LICENSE ADDED
@@ -0,0 +1,3 @@
1
+ == cmd_creator
2
+
3
+ Put appropriate LICENSE for your project here.
data/README ADDED
@@ -0,0 +1,7 @@
1
+ == cmd_creator
2
+
3
+ Create Commands from Specifications
4
+ arguments + specification = command
5
+
6
+ svn
7
+ https://yuzhenpin-nemo.googlecode.com/svn/trunk/cmd_creator
data/Rakefile ADDED
@@ -0,0 +1,52 @@
1
+ #
2
+ # To change this template, choose Tools | Templates
3
+ # and open the template in the editor.
4
+
5
+
6
+ require 'rubygems'
7
+ require 'rake'
8
+ require 'rake/clean'
9
+ require 'rake/gempackagetask'
10
+ require 'rake/rdoctask'
11
+ require 'rake/testtask'
12
+ require 'spec/rake/spectask'
13
+
14
+ spec = Gem::Specification.new do |s|
15
+ s.name = 'cmd_creator'
16
+ s.version = '0.0.1'
17
+ s.has_rdoc = true
18
+ s.extra_rdoc_files = ['README', 'LICENSE']
19
+ s.summary = 'Create Commands from Specifications'
20
+ s.description = s.summary
21
+ s.author = 'YuZhenpin'
22
+ s.email = 'yuzhenpin@126.com'
23
+ # s.executables = ['your_executable_here']
24
+ s.files = %w(LICENSE README Rakefile) + Dir.glob("{bin,lib,spec}/**/*")
25
+ s.require_path = "lib"
26
+ s.bindir = "bin"
27
+ s.homepage = "https://yuzhenpin-nemo.googlecode.com/svn/trunk/cmd_creator"
28
+ end
29
+
30
+ Rake::GemPackageTask.new(spec) do |p|
31
+ p.gem_spec = spec
32
+ p.need_tar = true
33
+ p.need_zip = true
34
+ end
35
+
36
+ Rake::RDocTask.new do |rdoc|
37
+ files =['README', 'LICENSE', 'lib/**/*.rb']
38
+ rdoc.rdoc_files.add(files)
39
+ rdoc.main = "README" # page to start on
40
+ rdoc.title = "cmd_creator Docs"
41
+ rdoc.rdoc_dir = 'doc/rdoc' # rdoc output folder
42
+ rdoc.options << '--line-numbers'
43
+ end
44
+
45
+ Rake::TestTask.new do |t|
46
+ t.test_files = FileList['test/**/*.rb']
47
+ end
48
+
49
+ Spec::Rake::SpecTask.new do |t|
50
+ t.spec_files = FileList['spec/**/*.rb']
51
+ t.libs << Dir["lib"]
52
+ end
@@ -0,0 +1,192 @@
1
+ class CmdCreator
2
+ MandatoryStart = "<"
3
+ MandatoryStop = ">"
4
+ OptionalStart = "["
5
+ OptionalStop = "]"
6
+ Argument = "%"
7
+
8
+ # command spec
9
+ class Spec < Array
10
+ attr_accessor :type
11
+ end
12
+
13
+ class NotMandatoryError < RuntimeError; end
14
+
15
+ class SpecParser
16
+ def initialize(words, args)
17
+ @args = args
18
+ @words = words
19
+ end
20
+
21
+ def parse
22
+ spec = Spec.new
23
+ spec.type = true
24
+
25
+ atom = ""
26
+ while word = @words[0]
27
+ case word
28
+ when CmdCreator::MandatoryStart
29
+ @words.shift
30
+
31
+ ssp = SubSpecParser.new(
32
+ CmdCreator::MandatoryStop, @words, @args
33
+ ).parse
34
+ unless replace(spec, atom, ssp)
35
+ raise CmdCreator::NotMandatoryError.new(atom)
36
+ end
37
+
38
+ atom = ""
39
+
40
+ when CmdCreator::OptionalStart
41
+ @words.shift
42
+ ssp = SubSpecParser.new(
43
+ CmdCreator::OptionalStop, @words, @args
44
+ ).parse
45
+ unless replace(spec, atom, ssp)
46
+ raise CmdCreator::NotMandatoryError.new(atom)
47
+ end
48
+ atom = ""
49
+ when CmdCreator::MandatoryStop
50
+ return spec
51
+ when CmdCreator::OptionalStop
52
+ return spec
53
+ else
54
+ @words.shift
55
+ atom << word
56
+ end
57
+ end
58
+
59
+ if atom.size > 0
60
+ unless replace(spec, atom)
61
+ raise CmdCreator::NotMandatoryError.new(atom)
62
+ end
63
+ end
64
+
65
+ return spec
66
+ end
67
+
68
+ def asub(atom)
69
+ arr = atom.split(CmdCreator::Argument)
70
+ tmp = []
71
+ arr.each_with_index {|ea, ind|
72
+ if 1 == ind % 2
73
+ if @args[ea.to_sym]
74
+ tmp << @args[ea.to_sym].to_s
75
+ else
76
+ return nil
77
+ end
78
+ else
79
+ tmp << ea
80
+ end
81
+ }
82
+
83
+ tmp.join("")
84
+ end
85
+
86
+ def replace(spec, atom, sspe = nil)
87
+ if atom.size > 0
88
+ subs = asub(atom)
89
+ if subs
90
+ spec << subs
91
+ else
92
+ return false
93
+ end
94
+ end
95
+ spec << sspe if sspe.size > 0
96
+ return true
97
+ end
98
+ end
99
+
100
+ class SubSpecParser
101
+ def initialize(sym, words, args)
102
+ @sym, @words, @args = sym, words, args
103
+ end
104
+
105
+ def parse
106
+ spec = Spec.new
107
+ if @sym == CmdCreator::MandatoryStop
108
+ spec.type = true
109
+ else
110
+ spec.type = false
111
+ end
112
+
113
+ atom = ""
114
+ while word = @words.shift
115
+ case word
116
+ when CmdCreator::MandatoryStart
117
+ ssp = SubSpecParser.new(
118
+ CmdCreator::MandatoryStop, @words, @args
119
+ ).parse
120
+ unless replace(spec, atom, ssp)
121
+ raise CmdCreator::NotMandatoryError.new(atom)
122
+ end
123
+
124
+ atom = ""
125
+ when CmdCreator::OptionalStart
126
+ ssp = SubSpecParser.new(
127
+ CmdCreator::OptionalStop, @words, @args
128
+ ).parse
129
+ unless replace(spec, atom, ssp)
130
+ return ""
131
+ end
132
+
133
+ atom = ""
134
+ when @sym
135
+ spec << asub(atom) if atom.size > 0
136
+ spar = SpecParser.new(@words, @args).parse
137
+ if spar.size > 0
138
+ spec << spec
139
+ end
140
+ return spec
141
+ else
142
+ atom << word
143
+ end
144
+ end
145
+ return spec
146
+ end
147
+
148
+ def asub(atom)
149
+ arr = atom.split(CmdCreator::Argument)
150
+ tmp = []
151
+ arr.each_with_index {|ea, ind|
152
+ if 1 == ind % 2
153
+ if @args[ea.to_sym]
154
+ tmp << @args[ea.to_sym].to_s
155
+ else
156
+ return nil
157
+ end
158
+ else
159
+ tmp << ea
160
+ end
161
+ }
162
+
163
+ tmp.join("")
164
+ end
165
+
166
+ def replace(spec, atom, sspe)
167
+ if atom.size > 0
168
+ subs = asub(atom)
169
+ if subs
170
+ spec << subs
171
+ else
172
+ return false
173
+ end
174
+ end
175
+ spec << sspe if sspe.size > 0
176
+ return true
177
+ end
178
+ end
179
+
180
+ def spec(cmd_spec)
181
+ @spec = cmd_spec
182
+ end
183
+
184
+ def args(cmd_args = {})
185
+ @args = cmd_args
186
+ end
187
+
188
+ def cmd
189
+ sp = SpecParser.new(@spec.split(""), @args)
190
+ sp.parse.flatten.join("").strip
191
+ end
192
+ end
@@ -0,0 +1,58 @@
1
+ # To change this template, choose Tools | Templates
2
+ # and open the template in the editor.
3
+
4
+ require "rubygems"
5
+ libpath = File.expand_path( File.join(File.dirname(__FILE__), "../lib") )
6
+ $LOAD_PATH.unshift libpath
7
+ require 'cmd_creator'
8
+
9
+ describe CmdCreator do
10
+ before(:each) do
11
+ @cmd_creator = CmdCreator.new
12
+ end
13
+
14
+ it "should no arguments" do
15
+ @cmd_creator.spec "vlan [%vlan_id% %vlan_type% [subvlan %subvlan% [type]]]"
16
+ @cmd_creator.args
17
+ @cmd_creator.cmd.should == "vlan"
18
+ end
19
+
20
+ it "should one options" do
21
+ @cmd_creator.spec "vlan [%vlan_id% %vlan_type% [subvlan %subvlan% [type]]]"
22
+ @cmd_creator.args :vlan_id => 11, :vlan_type => "access"
23
+ @cmd_creator.cmd.should == "vlan 11 access"
24
+ end
25
+
26
+ it "should options in options" do
27
+ @cmd_creator.spec "vlan [%vlan_id% %vlan_type% [subvlan %subvlan% [type]]]"
28
+ @cmd_creator.args :vlan_id => 11, :vlan_type => "access", :subvlan => 200
29
+ @cmd_creator.cmd.should == "vlan 11 access subvlan 200 type"
30
+ end
31
+
32
+ it "should options in options" do
33
+ @cmd_creator.spec "vlan [%vlan_id% %vlan_type% [subvlan %subvlan% [type]]]"
34
+ @cmd_creator.args :vlan_type => "access", :subvlan => 200
35
+ @cmd_creator.cmd.should == "vlan"
36
+ end
37
+
38
+ it "should a invalid spec" do
39
+ @cmd_creator.spec "vlan [%vlan_id%]]]]]"
40
+ @cmd_creator.args :vlan_id => 200
41
+ @cmd_creator.cmd.should == "vlan 200"
42
+ end
43
+
44
+ it "should a mandatory" do
45
+ @cmd_creator.spec "vlan <%vlan_id%>"
46
+ @cmd_creator.args :vlan_id => 200
47
+ @cmd_creator.cmd.should == "vlan 200"
48
+ end
49
+
50
+ it "should a mandatory raise error" do
51
+ @cmd_creator.spec "vlan1 %vlan_id%"
52
+ @cmd_creator.args
53
+ lambda {
54
+ @cmd_creator.cmd
55
+ }.should raise_error(CmdCreator::NotMandatoryError)
56
+ end
57
+ end
58
+
metadata ADDED
@@ -0,0 +1,70 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cmd_creator
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - YuZhenpin
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2012-05-02 00:00:00 Z
19
+ dependencies: []
20
+
21
+ description: Create Commands from Specifications
22
+ email: yuzhenpin@126.com
23
+ executables: []
24
+
25
+ extensions: []
26
+
27
+ extra_rdoc_files:
28
+ - README
29
+ - LICENSE
30
+ files:
31
+ - LICENSE
32
+ - README
33
+ - Rakefile
34
+ - lib/cmd_creator.rb
35
+ - spec/cmd_creator_spec.rb
36
+ homepage: https://yuzhenpin-nemo.googlecode.com/svn/trunk/cmd_creator
37
+ licenses: []
38
+
39
+ post_install_message:
40
+ rdoc_options: []
41
+
42
+ require_paths:
43
+ - lib
44
+ required_ruby_version: !ruby/object:Gem::Requirement
45
+ none: false
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ hash: 3
50
+ segments:
51
+ - 0
52
+ version: "0"
53
+ required_rubygems_version: !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ hash: 3
59
+ segments:
60
+ - 0
61
+ version: "0"
62
+ requirements: []
63
+
64
+ rubyforge_project:
65
+ rubygems_version: 1.8.22
66
+ signing_key:
67
+ specification_version: 3
68
+ summary: Create Commands from Specifications
69
+ test_files: []
70
+