tie 0.1

Sign up to get free protection for your applications and to get access to all the features.
data/README ADDED
@@ -0,0 +1,56 @@
1
+ --------------------
2
+ * To run tests:
3
+ spec test/*.spec
4
+
5
+ --------------------
6
+ * To get help:
7
+ ivec2: /usbwr/usr/chena/prj/phx/src/ruby/gem/tie >lib/tie.rb -h
8
+ Usage: tie.rb [-tpmv]
9
+
10
+ Specific options:
11
+ -t, --template=TEMPLATE The path to the template file, e.g. -t a.tmpl
12
+ -p, --parameter=PARAMETER One parameter file or multiple parameter files in single quote. (order matters). e.g. -p a.param or -p 'a.param b.param'
13
+ -m, --merged=MERGED Merged file path.
14
+
15
+ Common options:
16
+ --help Show this message
17
+ -v, --version Show version
18
+
19
+ --------------------
20
+ * To run:
21
+ * To get result to stdio:
22
+ ivec2: /usbwr/usr/chena/prj/phx/src/ruby/gem/tie >lib/tie.rb -t data/test/template/input.tmpl -p 'data/test/template/input.param'
23
+ fuel type: OFA
24
+ fuel enrichments:
25
+ 3.1
26
+ 3.2
27
+ 3.3
28
+ heights of each segment:
29
+ segment1 : 2.0
30
+ segment2 : 3.7
31
+
32
+ * To get result to file:
33
+ ivec2: /usbwr/usr/chena/prj/phx/src/ruby/gem/tie >lib/tie.rb -t data/test/template/input.tmpl -p 'data/test/template/input.param' -m input.inp
34
+ ivec2: /usbwr/usr/chena/prj/phx/src/ruby/gem/tie >cat input.inp
35
+ fuel type: OFA
36
+ fuel enrichments:
37
+ 3.1
38
+ 3.2
39
+ 3.3
40
+ heights of each segment:
41
+ segment1 : 2.0
42
+ segment2 : 3.7
43
+
44
+ * To merge a few parameter files into one template file:
45
+ ivec2: /usbwr/usr/chena/prj/phx/src/ruby/gem/tie >lib/tie.rb -t data/test/template/input.tmpl -p 'data/test/template/input.param data/test/template/input_1.param'
46
+ fuel type: NEW
47
+ fuel enrichments:
48
+ 3.1
49
+ 3.2
50
+ 3.3
51
+ heights of each segment:
52
+ segment1 : 2.0
53
+ segment2 : 3.7
54
+
55
+
56
+
@@ -0,0 +1,2 @@
1
+ abc
2
+ 345
@@ -0,0 +1,2 @@
1
+ 789
2
+ cde
@@ -0,0 +1,3 @@
1
+ cd981b6574d00ec0a4790f905bf3bcc8 md5data.txt
2
+ 6af87cf8cd41824bfff795a0c90d86f0 md5data2.txt
3
+ d41d8cd98f00b204e9800998ecf8427e md5sum.txt
@@ -0,0 +1,4 @@
1
+ key=value
2
+ age= 9
3
+ name = John Smith
4
+
@@ -0,0 +1,4 @@
1
+ key=value
2
+ array=[item1, item2, item3]
3
+ hash={key1 => v1, key2 => v2}
4
+
@@ -0,0 +1,11 @@
1
+ # comment line starts with '#'
2
+
3
+ # text in single quote(') or double quote (")
4
+ fuel_type="OFA"
5
+
6
+ # array elements are in [ , ]
7
+ enrichments=[3.1, 3.2, 3.3]
8
+
9
+ # hash elements are in { key => value , }
10
+ segment_height = { 'segment1' => 2.0,
11
+ "segment2" => 3.7}
@@ -0,0 +1,10 @@
1
+ fuel type: <%= fuel_type %>
2
+ fuel enrichments:
3
+ % enrichments.each do |enrichment|
4
+ <%= enrichment %>
5
+ % end
6
+ heights of each segment:
7
+ % segment_height.each_pair do |k, v|
8
+ <%= k %> : <%= v %>
9
+ % end
10
+
@@ -0,0 +1,2 @@
1
+ fuel_type="NEW"
2
+ test_flag=true
@@ -0,0 +1,5 @@
1
+ lattice = {
2
+ "RA30" => ["80", "81", "82"],
3
+ "RA40" => ["90"],
4
+ "RA50" => ["100", "101", "102"]
5
+ }
@@ -0,0 +1,9 @@
1
+ fuel type: NEW
2
+ fuel enrichments:
3
+ 3.1
4
+ 3.2
5
+ 3.3
6
+ heights of each segment:
7
+ segment1 : 2.0
8
+ segment2 : 3.7
9
+
@@ -0,0 +1,9 @@
1
+ fuel type: OFA
2
+ fuel enrichments:
3
+ 3.1
4
+ 3.2
5
+ 3.3
6
+ heights of each segment:
7
+ segment1 : 2.0
8
+ segment2 : 3.7
9
+
@@ -0,0 +1,61 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'erb'
4
+ require File.dirname(__FILE__) + '/utils'
5
+
6
+ class Template
7
+
8
+ attr :target_file
9
+ attr_reader :result
10
+
11
+ def initialize( template, parameter_array, target_file=nil)
12
+ if ! parameter_array.kind_of? Array
13
+ raise "parameter should be array"
14
+ end
15
+
16
+ if parameter_array.size == 0
17
+ raise "parameter files should not be empty"
18
+ end
19
+
20
+ check_file_existance template
21
+ @template_file = template
22
+
23
+ parameter_array.each_with_index do |param_file, index|
24
+ check_file_existance param_file
25
+ end
26
+
27
+ if parameter_array.size == 1
28
+ @param_file = parameter_array[0]
29
+ else
30
+ @param_file = Utils.merge_files parameter_array
31
+ end
32
+
33
+ merge
34
+
35
+ if target_file != nil
36
+ write target_file
37
+ else
38
+ puts @result
39
+ end
40
+ end
41
+
42
+ def merge
43
+ template = ERB.new(File.read(@template_file), 0, "%")
44
+ instance_eval(File.read(@param_file))
45
+ @result = template.result(binding)
46
+ end
47
+
48
+ def write target_file
49
+ File.open(target_file, "w") do |file|
50
+ file.puts @result
51
+ end
52
+ end
53
+
54
+ def check_file_existance file_path
55
+ if ! File.exist? file_path
56
+ raise "file: #{file_path} does not exist"
57
+ end
58
+ end
59
+ private :check_file_existance
60
+ end
61
+
@@ -0,0 +1,54 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'rubygems'
4
+ require 'choice'
5
+ require File.dirname(__FILE__) + '/template'
6
+
7
+ PROGRAM_VERSION = 0.1
8
+
9
+ Choice.options do
10
+ header ''
11
+ header 'Specific options:'
12
+
13
+ option :tmpl do
14
+ short '-t'
15
+ long '--template=TEMPLATE'
16
+ desc 'The path to the template file, e.g. -t a.tmpl'
17
+ end
18
+
19
+ option :param do
20
+ short '-p'
21
+ long '--parameter=PARAMETER'
22
+ desc 'One parameter file or multiple parameter files in single quote. (order matters). e.g. -p a.param or -p \'a.param b.param\''
23
+ end
24
+
25
+ option :merged do
26
+ short '-m'
27
+ long '--merged=MERGED'
28
+ desc 'Merged file path. '
29
+ end
30
+
31
+ separator ''
32
+ separator 'Common options: '
33
+
34
+ option :help do
35
+ long '--help'
36
+ desc 'Show this message'
37
+ end
38
+
39
+ option :version do
40
+ short '-v'
41
+ long '--version'
42
+ desc 'Show version'
43
+ action do
44
+ puts "tie.rb v#{PROGRAM_VERSION}"
45
+ exit
46
+ end
47
+ end
48
+ end
49
+
50
+ if Choice.choices[:tmpl] != nil
51
+ Template.new(Choice.choices[:tmpl], Choice.choices[:param].split(" "), Choice.choices[:merged])
52
+ else
53
+ puts "tie.rb --help"
54
+ end
@@ -0,0 +1,114 @@
1
+ module Utils
2
+ def Utils.read_properties file
3
+ path = file
4
+ if file.kind_of? File
5
+ path = file.path
6
+ end
7
+ properties = {}
8
+ IO.foreach(path) do |line|
9
+ next if line =~ /^\s*#/
10
+ if line =~ /([^=]*)=(.*)\/\/(.*)/ || line =~ /([^=]*)=(.*)/
11
+ key = $1.strip
12
+ value = $2.strip
13
+
14
+ if (value[0,1] == "[" && value[-1, 1]=="]")
15
+ value = value[1..-2]
16
+ value = value.split(",").collect {|v| v.strip}
17
+ end
18
+
19
+ if (value[0,1] == "{" && value[-1, 1]=="}")
20
+ hash = value[1..-2]
21
+ value = {}
22
+
23
+ hash_array= hash.split(",").collect {|v| v.strip}
24
+
25
+ hash_array.each do |item|
26
+ (k, v) = item.split("=>").collect {|item| item.strip}
27
+ value[k] = v
28
+ end
29
+ end
30
+
31
+ properties[key] = value
32
+ end
33
+
34
+ end
35
+ return properties
36
+ end
37
+
38
+ # not thread-safe
39
+ def Utils.unique_stamp
40
+ new_stamp = Time.new.strftime("%Y%m%d-%h%m%s")
41
+ if $stamp == new_stamp
42
+ $stamp = "#{$stamp}_"
43
+ else
44
+ $stamp = new_stamp
45
+ end
46
+ $stamp
47
+ end
48
+
49
+ def Utils.home_dir
50
+ ENV['HOME']
51
+ end
52
+
53
+ def Utils.tmp_dir
54
+ tmp_dir = "#{Utils.home_dir}/tmp_dir"
55
+ FileUtils.mkdir_p tmp_dir if (! File.exist? tmp_dir)
56
+ tmp_dir
57
+ end
58
+
59
+ def Utils.rm_tmp_dir
60
+ FileUtils.rm_r Utils.tmp_dir, :force => true
61
+ end
62
+
63
+ def Utils.tmp_file
64
+ "#{Utils.tmp_dir}/#{Utils.unique_stamp}.tmp"
65
+ end
66
+
67
+ def Utils.merge_files file_array
68
+ merged_file = File.new("#{Utils.tmp_dir}/#{Utils.unique_stamp}_merge", "w")
69
+
70
+ file_array.each do |file|
71
+ if (! File.exist? file)
72
+ puts "input file does not exist!"
73
+ next
74
+ end
75
+
76
+ merged_file.puts File.new(file).to_a
77
+ merged_file.puts "\n"
78
+ merged_file.flush
79
+ end
80
+ merged_file.close
81
+
82
+ merged_file.path
83
+ end
84
+
85
+ require 'md5'
86
+ def Utils.md5_key file
87
+ MD5.new(File.open(file, 'rb').read).hexdigest
88
+ end
89
+
90
+ def Utils.md5_line file
91
+ "#{Utils.md5_key file} #{file}"
92
+ end
93
+
94
+ def Utils.md5sum file_array
95
+ file_array.collect { |file| "#{Utils.md5_key file} #{file}" }
96
+ end
97
+
98
+ def Utils.md5_key_exists(key_file, key)
99
+ IO.read(key_file).grep(/#{key}/).size > 0
100
+ end
101
+
102
+ def Utils.md5sum_file(file_array, key_file_name)
103
+ File.open(key_file_name, 'w') do |file|
104
+ file.write Utils.md5sum(file_array).collect {|line| line+= "\n"}
105
+ end
106
+ end
107
+ end
108
+
109
+ class String
110
+ def chop_both
111
+ self.chop!.reverse.chop!.reverse!
112
+ end
113
+ end
114
+
@@ -0,0 +1,67 @@
1
+ require File.dirname(__FILE__) + '/../lib/template'
2
+
3
+ describe "Templating system specifications" do
4
+ before(:all) do
5
+ @tmpl = File.dirname(__FILE__) + '/../data/test/template/input.tmpl'
6
+ @param = File.dirname(__FILE__) + '/../data/test/template/input.param'
7
+ @merged_file= File.dirname(__FILE__) + '/merged_file'
8
+ @reference_merged_file= File.dirname(__FILE__) + '/../data/test/template/reference_merged_file'
9
+ File.delete(@merged_file) if File.exist? @merged_file
10
+ end
11
+
12
+ it "second parameter to the template input must be an array" do
13
+ begin
14
+ Template.new(@tmpl, @param)
15
+ rescue RuntimeError => e
16
+ e.message.should == "parameter should be array"
17
+ end
18
+ end
19
+
20
+ it "should handle one template file, and merged multiple parameter files as array" do
21
+ Template.new(@tmpl, [@param])
22
+ end
23
+
24
+ it "should raise exception if template file does not exist" do
25
+ begin
26
+ Template.new("none_exist_file", [@param])
27
+ rescue RuntimeError => e
28
+ e.message.should == "file: none_exist_file does not exist"
29
+ end
30
+ end
31
+
32
+ it "should check if each of the element of the 2nd parameter is valid existing file" do
33
+ begin
34
+ Template.new(@tmpl, ["none_exist_file"])
35
+ rescue RuntimeError => e
36
+ e.message.should == "file: none_exist_file does not exist"
37
+ end
38
+ end
39
+
40
+ it "should merge parameter into template" do
41
+ template = Template.new(@tmpl, [@param])
42
+ template.merge
43
+ template.result.should == "fuel type: OFA\nfuel enrichments:\n 3.1\n 3.2\n 3.3\nheights of each segment:\n segment1 : 2.0\n segment2 : 3.7\n\n"
44
+ end
45
+
46
+ it "should write merged data into one output file" do
47
+ Template.new(@tmpl, [@param], @merged_file)
48
+ File.size(@merged_file).should == File.size(@reference_merged_file)
49
+ end
50
+
51
+ it "should take output file name as the third input parameter" do
52
+ template = Template.new(@tmpl, [@param], @merged_file)
53
+ File.size(@merged_file).should == File.size(@reference_merged_file)
54
+ end
55
+
56
+ it "should handle multiple input files, with the values from the last parameter file override" do
57
+ param_1 = File.dirname(__FILE__) + '/../data/test/template/input_1.param'
58
+ template = Template.new(@tmpl, [@param, param_1], @merged_file)
59
+ reference_2_param_merged_file= File.dirname(__FILE__) + '/../data/test/template/reference_2_param_merged_file'
60
+ File.size(@merged_file).should == File.size(reference_2_param_merged_file)
61
+ end
62
+
63
+ after(:each) do
64
+ File.delete(@merged_file) if File.exist? @merged_file
65
+ end
66
+
67
+ end
@@ -0,0 +1,130 @@
1
+ require File.dirname(__FILE__) + '/../lib/utils'
2
+ require 'fileutils'
3
+
4
+ describe "Property file handler" do
5
+
6
+ before(:each) do
7
+ @property_file = File.dirname(__FILE__) + '/../data/test/property_file/property.txt'
8
+ @prop = Utils.read_properties @property_file
9
+ end
10
+
11
+ it "should read correct number of records from a property file" do
12
+ @prop.size.should == 3
13
+ end
14
+
15
+ it "should read property file into a hash" do
16
+ @prop.class.to_s == "Hash"
17
+ end
18
+
19
+ it "should read correct value in correct format" do
20
+ @prop['key'].should == "value"
21
+ @prop['name'].should == "John Smith"
22
+ @prop['age'].should == "9"
23
+ end
24
+ end
25
+
26
+ describe "Property file with ruby hash and array" do
27
+
28
+ before(:each) do
29
+ @ruby_param_file = File.dirname(__FILE__) + '/../data/test/property_file/ruby_property.txt'
30
+ @ruby_prop = Utils.read_properties @ruby_param_file
31
+ end
32
+
33
+ it "should read simple key from property file" do
34
+ @ruby_prop['key'].should == "value"
35
+ end
36
+
37
+ it "should read array from property file" do
38
+ @ruby_prop['array'].class.to_s.should == "Array"
39
+ @ruby_prop['array'].size.should == 3
40
+ @ruby_prop['array'][2].should == 'item3'
41
+ end
42
+
43
+ it "should read hash from property file" do
44
+ @ruby_prop['hash'].class.to_s.should == "Hash"
45
+ @ruby_prop['hash'].size.should == 2
46
+ @ruby_prop['hash']['key1'] == 'v1'
47
+ @ruby_prop['hash']['key2'] == 'v2'
48
+ end
49
+ end
50
+
51
+ describe "Misc utils" do
52
+ it "should provide unique time stamp" do
53
+ stamp = Utils.unique_stamp
54
+ stamp.should_not be_nil
55
+ Utils.unique_stamp.should_not eql(stamp)
56
+ end
57
+
58
+ it "should create tmp directory in ~/tmp_dir" do
59
+ dir = Utils.tmp_dir
60
+ File.exist?(dir).should be_true
61
+ File.exist?("#{ENV['HOME']}/tmp_dir").should be_true
62
+ end
63
+
64
+ it "should provide unique tmp file" do
65
+ Utils.tmp_file.should_not == Utils.tmp_file
66
+ end
67
+
68
+ it "should delete tmp directory in ~/tmp_dir" do
69
+ Utils.rm_tmp_dir
70
+ require 'rake'
71
+ FileList["#{ENV['HOME']}/tmp_dir/*"].size.should == 0
72
+ end
73
+
74
+ end
75
+
76
+ describe "String extentions" do
77
+ it "should chop one character from both ends of the string" do
78
+
79
+ "abcd".chop_both.should == 'bc'
80
+ end
81
+ end
82
+
83
+ describe "Md5sum utilities" do
84
+ it "should generate correct MD5 key from a file path" do
85
+ file = File.dirname(__FILE__) + '/../data/test/md5data.txt'
86
+ Utils.md5_key(file).should == "cd981b6574d00ec0a4790f905bf3bcc8"
87
+ end
88
+
89
+ it "should generate exact line as md5sum from linux command" do
90
+ file = File.dirname(__FILE__) + '/../data/test/md5data.txt'
91
+ Utils.md5_line(file).should == "cd981b6574d00ec0a4790f905bf3bcc8 #{file}"
92
+ end
93
+
94
+ it "should generate a md5sum file with input files as array" do
95
+ file1 = File.dirname(__FILE__) + '/../data/test/md5data.txt'
96
+ file2 = File.dirname(__FILE__) + '/../data/test/md5data.txt'
97
+ result = Utils.md5sum([file1, file2])
98
+ result.size.should == 2
99
+ end
100
+
101
+ it "should query md5 key from one file" do
102
+ key_file = File.dirname(__FILE__) + '/../data/test/md5sum.txt'
103
+ Utils.md5_key_exists(key_file, 'cd981b6574d00ec0a4790f905bf3bcc8').should be_true
104
+ end
105
+
106
+ it "should create md5sum file with files, and target" do
107
+ file1 = File.dirname(__FILE__) + '/../data/test/md5data.txt'
108
+ file2 = File.dirname(__FILE__) + '/../data/test/md5data2.txt'
109
+ target = Utils.unique_stamp
110
+
111
+ Utils.md5sum_file([file1, file2], target)
112
+
113
+ Utils.md5_key_exists(target, 'cd981b6574d00ec0a4790f905bf3bcc8').should be_true
114
+ Utils.md5_key_exists(target, '6af87cf8cd41824bfff795a0c90d86f0').should be_true
115
+ File.delete target
116
+ end
117
+ end
118
+
119
+ describe "File handler" do
120
+ it "should merge a few files into one big temporary file" do
121
+ file1 = File.dirname(__FILE__) + '/../data/test/template/input.param'
122
+ file2 = File.dirname(__FILE__) + '/../data/test/template/input_1.param'
123
+ file_array = [file1, file2]
124
+ new_file = Utils.merge_files(file_array)
125
+
126
+ File.size(new_file).should == (File.size(file1) + File.size(file2) + file_array.size)
127
+ FileUtils.remove_file(new_file, true)
128
+ end
129
+
130
+ end
metadata ADDED
@@ -0,0 +1,90 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tie
3
+ version: !ruby/object:Gem::Version
4
+ version: "0.1"
5
+ platform: ruby
6
+ authors:
7
+ - Andrew Chen
8
+ autorequire: tie
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-03-06 00:00:00 +01:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rake
17
+ version_requirement:
18
+ version_requirements: !ruby/object:Gem::Requirement
19
+ requirements:
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 0.8.1
23
+ version:
24
+ - !ruby/object:Gem::Dependency
25
+ name: choice
26
+ version_requirement:
27
+ version_requirements: !ruby/object:Gem::Requirement
28
+ requirements:
29
+ - - ">="
30
+ - !ruby/object:Gem::Version
31
+ version: 0.1.2
32
+ version:
33
+ description:
34
+ email: meihome gmail.com
35
+ executables: []
36
+
37
+ extensions: []
38
+
39
+ extra_rdoc_files:
40
+ - README
41
+ files:
42
+ - lib/template.rb
43
+ - lib/tie.rb
44
+ - lib/utils.rb
45
+ - test/template.spec
46
+ - test/utils_spec.rb
47
+ - data/test
48
+ - data/test/property_file
49
+ - data/test/property_file/ruby_property.txt
50
+ - data/test/property_file/property.txt
51
+ - data/test/template
52
+ - data/test/template/reference_2_param_merged_file
53
+ - data/test/template/lattice.param
54
+ - data/test/template/reference_merged_file
55
+ - data/test/template/input_1.param
56
+ - data/test/template/input.tmpl
57
+ - data/test/template/input.param
58
+ - data/test/md5sum.txt
59
+ - data/test/md5data.txt
60
+ - data/test/md5data2.txt
61
+ - README
62
+ has_rdoc: true
63
+ homepage: http://tie.sourceforge.org
64
+ post_install_message:
65
+ rdoc_options: []
66
+
67
+ require_paths:
68
+ - lib
69
+ required_ruby_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ version: "0"
74
+ version:
75
+ required_rubygems_version: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ version: "0"
80
+ version:
81
+ requirements: []
82
+
83
+ rubyforge_project:
84
+ rubygems_version: 1.0.1
85
+ signing_key:
86
+ specification_version: 2
87
+ summary: Merging template file and parameter files
88
+ test_files:
89
+ - test/template.spec
90
+ - test/utils_spec.rb