MergeAsXML 0.0.4

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA512:
3
+ metadata.gz: 9a7d718bf390dd637775aa9e006bacc4e7961d3dbe79bb73bdf1e051121b34694d299d7343a9359b215533a33435b47583abcca6a6be938164fe4b448c815be8
4
+ data.tar.gz: 4174ac6dbe6a2c963629efc077a11abe00497bc17f6de431861a93b73d082d0f6677d56bef71c2f9ac699b0e41d4efdee27a27dbe4ba8a3fb14500b3077153a8
5
+ SHA1:
6
+ metadata.gz: fc7c6b73892db829f0863d77fa83871fc2aadb08
7
+ data.tar.gz: 488371d5bd78fe88a3b16d7847bc5353824e6edd
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source "http://rubygems.org"
2
+
3
+ gem "nokogiri"
4
+ gem "rainbow"
5
+ gem "erubis"
@@ -0,0 +1,25 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = 'MergeAsXML'
3
+ s.version = '0.0.4'
4
+ s.date = '2013-05-16'
5
+ s.homepage = 'https://github.com/dmison/MergeAsXML'
6
+ s.summary = "CLI to takes data from a delimited text file and merges into XML"
7
+ s.description = "Takes data from a delimited text file and merges into XML"
8
+ s.authors = ["Darrin Mison"]
9
+ s.email = 'dmison@me.com'
10
+ s.executables << 'MergeAsXML'
11
+ s.files = [
12
+ "Gemfile",
13
+ "readme.md",
14
+ "MergeAsXML.gemspec",
15
+ "bin/MergeAsXML",
16
+ "lib/MergeAsXML.rb",
17
+ "lib/marker.xml.erb"
18
+ ]
19
+
20
+ s.add_runtime_dependency("rainbow", [">= 1.1.4"])
21
+ s.add_runtime_dependency("erubis", [">= 2.7.0"])
22
+
23
+
24
+ end
25
+
data/bin/MergeAsXML ADDED
@@ -0,0 +1,133 @@
1
+ #!/usr/bin/env ruby -W0
2
+
3
+ $LOAD_PATH.unshift File.dirname(__FILE__)
4
+
5
+ require 'rubygems'
6
+ require 'optparse'
7
+ require 'MergeAsXML.rb'
8
+
9
+ #process command line arguments and return options hash
10
+ def processCommandLine
11
+ options = {}
12
+
13
+ optparse = OptionParser.new do |opts|
14
+ opts.banner = "MergeAsXML v0.0.4".color("#800040").bright
15
+
16
+ options[:datafile] = ""
17
+ opts.on( '-i filename', '--input filename', 'File containing input data' ) do | datafile |
18
+ options[:datafile] = datafile
19
+ end
20
+
21
+ options[:delim] = "\t"
22
+ opts.on( '-s separator', 'The delimiter of each field in the input file (default: tab ''\\t'')') do | delim |
23
+ options[:delim] = "#{delim}"
24
+ end
25
+
26
+ options[:xmlfile] = ""
27
+ opts.on( '-X file', 'The XML file to merge the data into' ) do | xmlfile |
28
+ options[:xmlfile] = "#{xmlfile}"
29
+ end
30
+
31
+ options[:parentpath] = ""
32
+ opts.on( '-p parent path', 'The path of the parent node to insert at' ) do | parentpath |
33
+ options[:parentpath] = "#{parentpath}"
34
+ end
35
+ options[:prevsiblingpath] = ""
36
+ opts.on( '-b previous sibling path', 'The path of the sibling node to insert after' ) do | prevsiblingpath |
37
+ options[:prevsiblingpath] = "#{prevsiblingpath}"
38
+ end
39
+ options[:nextsiblingpath] = ""
40
+ opts.on( '-c next sibling path', 'The path of the sibling node to insert before' ) do | nextsiblingpath |
41
+ options[:nextsiblingpath] = "#{nextsiblingpath}"
42
+ end
43
+
44
+
45
+ options[:template] = ""
46
+ opts.on( '-t template', 'ERB template to populate (defaults to inbuilt default template)' ) do | template |
47
+ options[:template] = template
48
+ end
49
+
50
+ # options[:followXIInclude] = false
51
+ # opts.on( '-x', '--xiinclude', 'Follow' ) do
52
+ # options[:getSecurityLevels] = true
53
+ # end
54
+
55
+ opts.on( '-d', 'Dump default template to default.xml.erb' ) do
56
+ print "Saving built in template to "
57
+ print "default.xml.erb".bright.underline
58
+ puts "."
59
+
60
+ filename = File.expand_path "../../lib/marker.xml.erb", __FILE__
61
+
62
+ FileUtils.cp(filename, "default.xml.erb")
63
+ exit
64
+ end
65
+
66
+ opts.on( '-h', 'Display this screen' ) do
67
+ puts opts
68
+ exit
69
+ end
70
+
71
+ if (ARGV.size == 0) then
72
+ puts opts
73
+ exit
74
+ end
75
+
76
+ end
77
+
78
+ begin
79
+ optparse.parse!
80
+ rescue => error
81
+ puts "ERROR PARSING COMMANDLINE:".color("#ff0000")
82
+ puts "\t"+error.to_s
83
+ puts
84
+ puts optparse
85
+ exit
86
+ end
87
+
88
+ #check for other weirdness here
89
+ return options
90
+ end
91
+
92
+ options = processCommandLine
93
+
94
+ lines = fileToArray(options[:datafile])
95
+ data = stringsToArray(lines, options[:delim])
96
+
97
+ if options[:template] == ""
98
+ template_fn = File.expand_path "../../lib/marker.xml.erb", __FILE__
99
+ else
100
+ template_fn = "#{Dir.getwd}/#{options[:template]}"
101
+ end
102
+
103
+ puts template_fn
104
+
105
+ template = ERB.new File.new(template_fn).read, nil, "%"
106
+ output = template.result(binding)
107
+
108
+ if (options[:xmlfile] == "")
109
+ puts output
110
+ exit
111
+ end
112
+
113
+ if options[:prevsiblingpath] != ""
114
+ action = "after"
115
+ xpath = options[:prevsiblingpath]
116
+ end
117
+ if options[:nextsiblingpath] != ""
118
+ action = "before"
119
+ xpath = options[:nextsiblingpath]
120
+ end
121
+ if options[:parentpath] != ""
122
+ action = "as child of"
123
+ xpath = options[:parentpath]
124
+ end
125
+
126
+ puts "Merging data #{action.bright} #{xpath.underline} in #{options[:xmlfile]}"
127
+
128
+
129
+ outputstring = mergeOutputIntoXML(output, options[:xmlfile], xpath, action)
130
+
131
+ saveNewFileWithBackup(outputstring, options[:xmlfile])
132
+
133
+ puts "Done"
data/lib/MergeAsXML.rb ADDED
@@ -0,0 +1,67 @@
1
+ require 'rainbow'
2
+ require 'erb'
3
+ require 'FileUtils'
4
+ require 'rexml/document'
5
+ require 'rexml/XPath'
6
+
7
+ # takes a filename and returns an array of strings
8
+ def fileToArray (filename)
9
+ lines = Array.new
10
+ File.open(filename) do |fp|
11
+ fp.each do | line |
12
+ lines << line.strip
13
+ end
14
+ end
15
+ return lines
16
+ end
17
+
18
+ # takes an array of strings and a delimiter
19
+ # returns a two-dimensional array based on the delimiter
20
+ def stringsToArray(strings, delim)
21
+ lines = Array.new
22
+
23
+ strings.each do | string |
24
+ line = string.split(delim)
25
+ lines << line
26
+ end
27
+ return lines
28
+ end
29
+
30
+ def saveNewFileWithBackup(outputstring, filename)
31
+ #make backup, filename.backup
32
+ FileUtils.cp(filename, "#{filename}.backup")
33
+ # write output
34
+ File.open(filename, 'w') {|f| f.write(outputstring) }
35
+ end
36
+
37
+ #output is required to be a string containing valid XML
38
+ def mergeOutputIntoXML(output, xmlfilename, xpath, action)
39
+
40
+ #load XML doc from xmlfilename
41
+ doc = REXML::Document.new ( File.new(xmlfilename) ) #getSourceXMLDoc(xmlfilename)
42
+ fragment = REXML::Document.new("<wrapper>#{output}</wrapper>")
43
+
44
+ #insertpoint = doc.at_xpath(xpath)
45
+ insertpoint = REXML::XPath.first(doc, xpath)
46
+
47
+ fragment.root.elements.each do | child |
48
+ case action
49
+ when "after"
50
+ # insert as next siblings of specified xpath
51
+ insertpoint.insert_after(xpath, child)
52
+
53
+ when "before"
54
+ # insert as prev siblings of specified xpath
55
+ insertpoint.insert_before(xpath, child)
56
+ when "as child of"
57
+ # insert as child of specified xpath
58
+ insertpoint.add(child)
59
+ end
60
+ end
61
+
62
+ # return string
63
+ return doc.to_s
64
+
65
+ end
66
+
67
+
@@ -0,0 +1,17 @@
1
+
2
+ <!-- generated data begins -->
3
+ <% data.each_index do | index | %>
4
+ <marker>
5
+ <name>Marker #<%= index %></name>
6
+ <comment><%= data[index][1] %></comment>
7
+ <color>
8
+ <alpha>0</alpha>
9
+ <red>255</red>
10
+ <green>0</green>
11
+ <blue>0</blue>
12
+ </color>
13
+ <in><%= data[index][0] %></in>
14
+ <out>-1</out>
15
+ </marker>
16
+ <% end %>
17
+ <!-- generated data ends -->
data/readme.md ADDED
@@ -0,0 +1,79 @@
1
+ ## MergeAsXML
2
+
3
+ This is a simple Ruby based command line tool that takes a delimited data file (like a CSV file) and merges it into a already existing XML file.
4
+
5
+ MergeAsXML v1.0.0
6
+ -i, --input filename File containing input data
7
+ -s separator The delimiter of each field in the input file (default: tab \t)
8
+ -X file The XML file to merge the data into
9
+ -p parent path The path of the parent node to insert at
10
+ -b previous sibling path The path of the sibling node to insert after
11
+ -c next sibling path The path of the sibling node to insert before
12
+ -t template ERB template to populate (defaults to inbuilt default template)
13
+ -d Dump default template to default.xml.erb
14
+ -h Display this screen
15
+
16
+ The basic use case involves you having :
17
+
18
+ 1. A delimited datafile, like a CSV file
19
+ 2. An XML file
20
+
21
+ And you need to get the data from the datafile formatting and inserted into the right place in the XML file.
22
+
23
+ To do this you specify:
24
+
25
+ 1. The datafile to use
26
+ 2. The delimiter for the file. Default is tab.
27
+ 3. An ERB template to control how the data is turned into XML.
28
+ 4. The XML file to insert the output into.
29
+ 5. An xpath expression to indicate where to insert the new XML. Only the first match is used.
30
+ 6. Whether the xpath expression indicates the parent node for the new data (`-p`), or a sibling node to insert before (`-c`)or after (`-b`).
31
+
32
+ Here's an example of the command-line inserting CSV data as Markers into a Final Cut Pro XML export.
33
+
34
+ MergeAsXML -i timecode-data.csv -s , -X FC_XML_EXPORT.XML -b /xmeml/sequence/ismasterclip -t markers.xml.erb
35
+
36
+ A backup copy of the XML file is made with the suffix `.backup`.
37
+
38
+ ### Installing
39
+
40
+ It's available as a Ruby Gem.
41
+
42
+ sudo gem install MergeAsXML
43
+
44
+ ### Making templates
45
+
46
+ MergeAsXML uses ERB templates. You can read about ERB [here][1].
47
+
48
+ You can get a copy of the default template using the -d parameter. It saves a copy of the default template in your current directory.
49
+
50
+ MergeAsXML -d
51
+
52
+ The default template looks like this:
53
+
54
+ <!-- generated data begins -->
55
+ <% data.each_index do | index | %>
56
+ <marker>
57
+ <name>Marker #<%= index %></name>
58
+ <comment><%= data[index][1] %></comment>
59
+ <color>
60
+ <alpha>0</alpha>
61
+ <red>255</red>
62
+ <green>0</green>
63
+ <blue>0</blue>
64
+ </color>
65
+ <in><%= data[index][0] %></in>
66
+ <out>-1</out>
67
+ </marker>
68
+ <% end %>
69
+ <!-- generated data ends -->
70
+
71
+ The data from the input file goes into a simple 2D array called `data`. So each specific item can be accessed as such:
72
+
73
+ data[row][col]
74
+
75
+ And you can step through each item using the `.each_index` operation.
76
+
77
+
78
+
79
+ [1]: http://en.wikipedia.org/wiki/ERuby
metadata ADDED
@@ -0,0 +1,76 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: MergeAsXML
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.4
5
+ platform: ruby
6
+ authors:
7
+ - Darrin Mison
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2013-05-16 00:00:00 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rainbow
16
+ prerelease: false
17
+ requirement: &id001 !ruby/object:Gem::Requirement
18
+ requirements:
19
+ - - ">="
20
+ - !ruby/object:Gem::Version
21
+ version: 1.1.4
22
+ type: :runtime
23
+ version_requirements: *id001
24
+ - !ruby/object:Gem::Dependency
25
+ name: erubis
26
+ prerelease: false
27
+ requirement: &id002 !ruby/object:Gem::Requirement
28
+ requirements:
29
+ - - ">="
30
+ - !ruby/object:Gem::Version
31
+ version: 2.7.0
32
+ type: :runtime
33
+ version_requirements: *id002
34
+ description: Takes data from a delimited text file and merges into XML
35
+ email: dmison@me.com
36
+ executables:
37
+ - MergeAsXML
38
+ extensions: []
39
+
40
+ extra_rdoc_files: []
41
+
42
+ files:
43
+ - Gemfile
44
+ - readme.md
45
+ - MergeAsXML.gemspec
46
+ - bin/MergeAsXML
47
+ - lib/MergeAsXML.rb
48
+ - lib/marker.xml.erb
49
+ homepage: https://github.com/dmison/MergeAsXML
50
+ licenses: []
51
+
52
+ metadata: {}
53
+
54
+ post_install_message:
55
+ rdoc_options: []
56
+
57
+ require_paths:
58
+ - lib
59
+ required_ruby_version: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - &id003
62
+ - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: "0"
65
+ required_rubygems_version: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - *id003
68
+ requirements: []
69
+
70
+ rubyforge_project:
71
+ rubygems_version: 2.0.3
72
+ signing_key:
73
+ specification_version: 4
74
+ summary: CLI to takes data from a delimited text file and merges into XML
75
+ test_files: []
76
+