ods 0.1.0

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 42adc8c8dec7bc687e86198883d2616891e2fe88
4
+ data.tar.gz: a2015c1269f31acf444ca3a4fee01850e718afeb
5
+ SHA512:
6
+ metadata.gz: 441b4d3d76316a7761532d3f54524fd0236ee2524b7c5e00bb4009044a6c7b1113c3fbe33a875b3c2e674fe9d8ecb88098012e6ce46e568c507a80cc528c89ef
7
+ data.tar.gz: 261923e9398914e2ba64c38b4acab295a53bd63f56676a128cb24f9791e2291bbb774264d90f85cc3f8ecd095aa0c6fde518bdb868120417e19cc8d021529788
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ .rvmrc
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in ods.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Mike Park
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,29 @@
1
+ # Ods
2
+
3
+ Ods provides read-only access to OpenOffice.org or LibreOffice spreadsheets.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'ods'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install ods
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/bin/ods ADDED
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+ require 'rubygems'
3
+ require 'ods'
4
+
5
+ Ods::Cli.start
@@ -0,0 +1,9 @@
1
+ require "ods/version"
2
+ require 'ods/file'
3
+ require 'ods/sheet'
4
+ require 'ods/row'
5
+ require 'ods/cell'
6
+ require 'ods/cli'
7
+
8
+ module Ods
9
+ end
@@ -0,0 +1,38 @@
1
+ module Ods
2
+ class Cell
3
+ attr_reader :row, :content
4
+
5
+ def initialize(content, row)
6
+ @content = content
7
+ @row = row
8
+ end
9
+
10
+ def value
11
+ value_type = content['office:value-type']
12
+ #puts "value_type=#{value_type}"
13
+ case value_type
14
+ when nil, 'string'
15
+ text
16
+ when 'date'
17
+ Date.strptime(content['office:date-value'], "%Y-%m-%d")
18
+ when 'boolean'
19
+ content['office:boolean-value'] == 'true' ? true : false
20
+ when 'float'
21
+ str = content['office:value']
22
+ if str.match(/\./)
23
+ str.to_f
24
+ else
25
+ str.to_i
26
+ end
27
+ when 'percentage'
28
+ content['office:value'].to_f
29
+ else
30
+ warn "Unknown type: #{value_type} [#{text}]"
31
+ end
32
+ end
33
+
34
+ def text
35
+ content.text
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,16 @@
1
+ require 'thor'
2
+ require 'csv'
3
+
4
+ module Ods
5
+ class Cli < Thor
6
+ option :sheet, default: 0
7
+ option :file, required: true
8
+ option :verbose
9
+ desc 'csv', 'dumps --sheet x (0 by default) in csv format'
10
+ def csv
11
+ file = Ods::File.open(options[:file])
12
+ sheet = file.sheets[options[:sheet].to_i]
13
+ sheet && sheet.csv
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,39 @@
1
+ require 'zip/zip'
2
+ require 'nokogiri'
3
+
4
+ module Ods
5
+ class File
6
+ XPATH_SHEETS = '//office:body/office:spreadsheet/table:table'
7
+
8
+ def self.open(path)
9
+ ods_file = new(path)
10
+ if block_given?
11
+ yield ods_file
12
+ else
13
+ ods_file
14
+ end
15
+ end
16
+
17
+ attr_reader :path
18
+
19
+ def initialize(path)
20
+ @path = path
21
+ end
22
+
23
+ def sheets
24
+ content.root.xpath(XPATH_SHEETS).map {|sheet| Sheet.new(sheet) }
25
+ end
26
+
27
+ private
28
+
29
+ def content
30
+ @content ||= unzip_content
31
+ end
32
+
33
+ def unzip_content
34
+ Zip::ZipFile.open(path) do |zip|
35
+ Nokogiri::XML::Document.parse(zip.read('content.xml'))
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,23 @@
1
+ module Ods
2
+ class Row
3
+ attr_reader :sheet, :content
4
+
5
+ def initialize(content, sheet)
6
+ @content = content
7
+ @sheet = sheet
8
+ end
9
+
10
+ def cols
11
+ return @cols if @cols
12
+ @cols = []
13
+ content.xpath('table:table-cell').each do |node|
14
+ repeat = node['table:number-columns-repeated'] || 1
15
+ a_cell = Cell.new(node, self)
16
+ repeat.to_i.times do
17
+ @cols << a_cell
18
+ end
19
+ end
20
+ @cols
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,44 @@
1
+ module Ods
2
+ class Sheet
3
+ attr_reader :content
4
+
5
+ def initialize(content)
6
+ @content = content
7
+ end
8
+
9
+ def name
10
+ content.attribute('name').to_s
11
+ end
12
+
13
+ # access by (0,0) = top left
14
+ def [](row_index, col_index)
15
+ row = rows[row_index]
16
+ if row.kind_of?(Array)
17
+ puts "bad #{row_index} #{col_index}"
18
+ #puts row
19
+ end
20
+ row.nil? ? nil : row.cols[col_index]
21
+ end
22
+
23
+ def rows
24
+ return @rows if @rows
25
+ @rows = []
26
+ content.xpath('descendant::table:table-row').each do |node|
27
+ a_row = Row.new(node, self)
28
+ repeat = node['table:number-rows-repeated'] || 1
29
+ repeat.to_i.times do
30
+ @rows << a_row
31
+ end
32
+ end
33
+ @rows
34
+ end
35
+
36
+ def csv
37
+ CSV do |csv|
38
+ rows.each do |row|
39
+ csv << row.cols.map(&:value)
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,3 @@
1
+ module Ods
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'ods/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "ods"
8
+ spec.version = Ods::VERSION
9
+ spec.authors = ["Mike Park"]
10
+ spec.email = ["mikep@quake.net"]
11
+ spec.description = %q{Provides a command line and API interface to extract data from ods spreadsheets.}
12
+ spec.summary = %q{Gem to extract data from OpenOffice/LibreOffice .ods spreadsheets.}
13
+ spec.homepage = "https://github.com/mike-park/ods"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "nokogiri", "~> 1.6.0"
22
+ spec.add_dependency "rubyzip", "~> 0.9.9"
23
+ spec.add_dependency "thor"
24
+
25
+ spec.add_development_dependency "bundler", "~> 1.3"
26
+ spec.add_development_dependency "rake"
27
+ spec.add_development_dependency "rspec", "~> 2.6"
28
+ spec.add_development_dependency "awesome_print"
29
+ end
@@ -0,0 +1,8 @@
1
+ require 'spec_helper'
2
+
3
+ describe Ods::File do
4
+ it "should have 3 sheets" do
5
+ ods_file = Ods::File.open(SIMPLE_ODS_FILE)
6
+ expect(ods_file.sheets).to have_exactly(3).items
7
+ end
8
+ end
@@ -0,0 +1,99 @@
1
+ require 'spec_helper'
2
+
3
+ describe Ods::Sheet do
4
+ let(:file) { Ods::File.open(SIMPLE_ODS_FILE) }
5
+ let(:sheets) { file.sheets }
6
+
7
+ context "sheet 1" do
8
+ let(:sheet) { sheets.first }
9
+
10
+ it { sheet.name.should == 'Sheet1' }
11
+
12
+ context "sizes" do
13
+ it { sheet.rows.count.should > 4 }
14
+ it { sheet.rows[0].cols.count.should == 4 }
15
+ end
16
+
17
+ context "basic navigation" do
18
+ context "first row" do
19
+ it { sheet[0,0].value.should == 'A' }
20
+ it { sheet[0,0].value.should be_kind_of(String) }
21
+ it "have 4 columns" do
22
+ sheet.rows[0].cols.count.should == 4
23
+ end
24
+ it "contains" do
25
+ %w(A row of strings).each_with_index do |s, i|
26
+ sheet[0, i].value.should == s
27
+ end
28
+ end
29
+ end
30
+
31
+ context "second row" do
32
+ (1..4).each do |i|
33
+ it { sheet[1,i-1].value.should == Date.new(2011, 1, i) }
34
+ it { sheet[1,i-1].value.should be_kind_of(Date) }
35
+ end
36
+ end
37
+
38
+ context "third row integers" do
39
+ (1..4).each do |i|
40
+ it { sheet[2,i-1].value.should == i }
41
+ it { sheet[2,i-1].value.should be_kind_of(Fixnum) }
42
+ end
43
+ end
44
+
45
+ context "forth row matches third row" do
46
+ (1..4).each do |i|
47
+ it { sheet[3,i-1].value.should == i }
48
+ it { sheet[3,i-1].value.should be_kind_of(Fixnum) }
49
+ end
50
+ end
51
+
52
+ context "row five has mixed float&ints" do
53
+ it { sheet[4,0].value.should == 0 }
54
+ it { sheet[4,0].value.should be_kind_of(Fixnum) }
55
+
56
+ it { sheet[4,1].value.should == -1.1 }
57
+ it { sheet[4,1].value.should be_kind_of(Float) }
58
+
59
+ it { sheet[4,2].value.should == 2.2 }
60
+ it { sheet[4,2].value.should be_kind_of(Float) }
61
+
62
+ it { sheet[4,3].value.should == 3 }
63
+ it { sheet[4,3].value.should be_kind_of(Fixnum) }
64
+ end
65
+
66
+ context "row 6 has booleans" do
67
+ it { sheet[5,0].value.should == true }
68
+ it { sheet[5,0].value.should be_kind_of(TrueClass) }
69
+
70
+ it { sheet[5,1].value.should == false }
71
+ it { sheet[5,1].value.should be_kind_of(FalseClass) }
72
+ end
73
+
74
+ context "out of bounds" do
75
+ it { sheet[1000,100].should_not be }
76
+ end
77
+ end
78
+ end
79
+
80
+ context "sheet 2" do
81
+ let(:sheet) { sheets[1] }
82
+
83
+ it { sheet.name.should == "Sheet2" }
84
+
85
+ context "column A" do
86
+ (0..9).each do |row|
87
+ context "row #{row}" do
88
+ it { sheet[row, 0].value.should == "A#{row+1}" }
89
+ end
90
+ end
91
+
92
+ it { sheet[10,0].should_not be }
93
+ end
94
+
95
+ context "bottom right" do
96
+ it { sheet[9, 3].value.should == 'D10' }
97
+ end
98
+ end
99
+ end
Binary file
@@ -0,0 +1,21 @@
1
+ require 'ods'
2
+
3
+ # This file was generated by the `rspec --init` command. Conventionally, all
4
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
5
+ # Require this file using `require "spec_helper"` to ensure that it is only
6
+ # loaded once.
7
+ #
8
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
9
+ RSpec.configure do |config|
10
+ config.treat_symbols_as_metadata_keys_with_true_values = true
11
+ config.run_all_when_everything_filtered = true
12
+ config.filter_run :focus
13
+
14
+ # Run specs in random order to surface order dependencies. If you find an
15
+ # order dependency and want to debug it, you can fix the order by providing
16
+ # the seed, which is printed after each run.
17
+ # --seed 1234
18
+ config.order = 'random'
19
+ end
20
+
21
+ SIMPLE_ODS_FILE = Pathname.new(__FILE__).dirname + 'simple.ods'
metadata ADDED
@@ -0,0 +1,166 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ods
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Mike Park
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-07-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: nokogiri
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: 1.6.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: 1.6.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: rubyzip
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: 0.9.9
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: 0.9.9
41
+ - !ruby/object:Gem::Dependency
42
+ name: thor
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '1.3'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: '1.3'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ~>
88
+ - !ruby/object:Gem::Version
89
+ version: '2.6'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ~>
95
+ - !ruby/object:Gem::Version
96
+ version: '2.6'
97
+ - !ruby/object:Gem::Dependency
98
+ name: awesome_print
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - '>='
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - '>='
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description: Provides a command line and API interface to extract data from ods spreadsheets.
112
+ email:
113
+ - mikep@quake.net
114
+ executables:
115
+ - ods
116
+ extensions: []
117
+ extra_rdoc_files: []
118
+ files:
119
+ - .gitignore
120
+ - .rspec
121
+ - Gemfile
122
+ - LICENSE.txt
123
+ - README.md
124
+ - Rakefile
125
+ - bin/ods
126
+ - lib/ods.rb
127
+ - lib/ods/cell.rb
128
+ - lib/ods/cli.rb
129
+ - lib/ods/file.rb
130
+ - lib/ods/row.rb
131
+ - lib/ods/sheet.rb
132
+ - lib/ods/version.rb
133
+ - ods.gemspec
134
+ - spec/file_spec.rb
135
+ - spec/sheet_spec.rb
136
+ - spec/simple.ods
137
+ - spec/spec_helper.rb
138
+ homepage: https://github.com/mike-park/ods
139
+ licenses:
140
+ - MIT
141
+ metadata: {}
142
+ post_install_message:
143
+ rdoc_options: []
144
+ require_paths:
145
+ - lib
146
+ required_ruby_version: !ruby/object:Gem::Requirement
147
+ requirements:
148
+ - - '>='
149
+ - !ruby/object:Gem::Version
150
+ version: '0'
151
+ required_rubygems_version: !ruby/object:Gem::Requirement
152
+ requirements:
153
+ - - '>='
154
+ - !ruby/object:Gem::Version
155
+ version: '0'
156
+ requirements: []
157
+ rubyforge_project:
158
+ rubygems_version: 2.0.3
159
+ signing_key:
160
+ specification_version: 4
161
+ summary: Gem to extract data from OpenOffice/LibreOffice .ods spreadsheets.
162
+ test_files:
163
+ - spec/file_spec.rb
164
+ - spec/sheet_spec.rb
165
+ - spec/simple.ods
166
+ - spec/spec_helper.rb