structured_csv 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 5f0d139cdc9ff3d124d745b783e00c1e9128efaf723df4976c19605a823dec6f
4
+ data.tar.gz: 396d9c993b850a571746a68cd64ee1c7a295d41d612c534196cb48c87c0415d7
5
+ SHA512:
6
+ metadata.gz: 656fa1acf23c402402f8bc3ddc5b00c81fb805dbd3128be8c6ea301e7e3fdc4fa2105126ac2e452e5050178442bd0910f1ad141cc8820f39af68a09ce2c7f2de
7
+ data.tar.gz: d5371f377396641b87f878ada7e0e9c15cdff14afe46c2c3035e1da4d92bfc6b7a1323e2d9df0447574d35861d04abec394f1237d57e605479cc67dd00264c01
@@ -0,0 +1,18 @@
1
+ name: Ruby
2
+
3
+ on: [push,pull_request]
4
+
5
+ jobs:
6
+ build:
7
+ runs-on: ubuntu-latest
8
+ steps:
9
+ - uses: actions/checkout@v2
10
+ - name: Set up Ruby
11
+ uses: ruby/setup-ruby@v1
12
+ with:
13
+ ruby-version: 2.6.6
14
+ - name: Run the default task
15
+ run: |
16
+ gem install bundler -v 2.2.15
17
+ bundle install
18
+ bundle exec rake
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,13 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.4
3
+
4
+ Style/StringLiterals:
5
+ Enabled: true
6
+ EnforcedStyle: double_quotes
7
+
8
+ Style/StringLiteralsInInterpolation:
9
+ Enabled: true
10
+ EnforcedStyle: double_quotes
11
+
12
+ Layout/LineLength:
13
+ Max: 120
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at ronald.tse@ribose.com. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in structured_csv.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "rspec", "~> 3.0"
11
+
12
+ gem "rubocop", "~> 1.7"
data/README.adoc ADDED
@@ -0,0 +1,205 @@
1
+ = Process Structured CSV files (`structured_csv`)
2
+
3
+ == Purpose
4
+
5
+ The `structured_csv_to_yaml.rb` script converts a "`Structured CSV`" file into a YAML file.
6
+
7
+ When you have data of a yet-undefined data structure, it is useful to manage
8
+ them inside a CSV file which can be viewed and edited by a CSV editor,
9
+ such as Excel.
10
+
11
+ This is extremely useful in developing a normalized structure for such data,
12
+ as you can ensure that the existing data can be normalized according to a
13
+ defined structure.
14
+
15
+ Ultimately, the data is to be meant to exported to a YAML file.
16
+
17
+ This script supports UTF-8 CSV files.
18
+
19
+ NOTE: This was originally developed to create over 50 normalized data models for ITU Operational Bulletin data. See https://github.com/ituob/ for more details.
20
+
21
+
22
+ == Usage
23
+
24
+ [source,sh]
25
+ ----
26
+ $ exe/structured_csv_to_yaml.rb [input-file.csv]
27
+ ----
28
+
29
+ Where,
30
+
31
+ `input-file.csv`:: is the input CSV file, the output will be named as `input-file.yaml`.
32
+
33
+
34
+ == Details
35
+
36
+ A Structured CSV file has these properties:
37
+
38
+ Two structured sections. A section is defined by the first column on an otherwise empty row that is either the first row or a row preceded by an empty row. Two section types are allowed: `METADATA` and `DATA`.
39
+
40
+ The `METADATA` section has values organized like key-value pairs:
41
+
42
+ * Column 1 is the name of key
43
+ * Column 2 is the value
44
+
45
+ The `key` can be a normal string or namespaced:
46
+
47
+ * `foobar`, this maps to the YAML key `foobar:`
48
+
49
+ * `foo.bar.boo`, this maps to the YAML structure: +
50
+ +
51
+ [source,yaml]
52
+ ----
53
+ foo:
54
+ bar:
55
+ boo:
56
+ ----
57
+
58
+ A typical YAML output is like:
59
+
60
+ [source,yaml]
61
+ ----
62
+ ---
63
+ metadata:
64
+ locale:
65
+ bar:
66
+ en: beef
67
+ fr: boeuf
68
+ jp: 牛肉
69
+ data:
70
+ foo:
71
+ bar:
72
+ ...
73
+ ----
74
+
75
+
76
+
77
+ A sample METADATA section looks like this table:
78
+
79
+ [cols,"a,a"]
80
+ |===
81
+ |METADATA |
82
+ |locale.bar.en | beef
83
+ |locale.bar.fr | boeuf
84
+ |locale.bar.jp | 牛肉
85
+ |===
86
+
87
+ And generates this YAML:
88
+
89
+ [source,yaml]
90
+ ----
91
+ ---
92
+ metadata:
93
+ locale:
94
+ bar:
95
+ en: beef
96
+ fr: boeuf
97
+ jp: 牛肉
98
+ ----
99
+
100
+
101
+ The `DATA` section has values organized in a table form. The first row is the header row.
102
+ The first column is assumed to be the key.
103
+
104
+
105
+ A sample DATA section looks like this table:
106
+
107
+ [cols,"a,a,a,a"]
108
+ |===
109
+ |DATA | | |
110
+ |foo.bar.en | foo.bar.fr | foo.bar.jp | description
111
+ |beef | boeuf | 牛肉 | Yummy!
112
+ |pork | porc | 豚肉 | Delicious!
113
+ |===
114
+
115
+ By default, this table generates this YAML format:
116
+
117
+ [source,yaml]
118
+ ----
119
+ ---
120
+ data:
121
+ beef:
122
+ foo:
123
+ bar:
124
+ en: beef
125
+ fr: boeuf
126
+ jp: 牛肉
127
+ description: Yummy!
128
+ pork:
129
+ foo:
130
+ bar:
131
+ en: pork
132
+ fr: porc
133
+ jp: 豚肉
134
+ description: Delicious!
135
+ ...
136
+ ----
137
+
138
+ In cases where there is no DATA key, you have to specify the `type=array` to generate an array:
139
+
140
+ [cols,"a,a,a,a"]
141
+ |===
142
+ |DATA | type=array | |
143
+ |foo.bar.en | foo.bar.fr | foo.bar.jp | description
144
+ |beef | boeuf | 牛肉 | Yummy!
145
+ |pork | porc | 豚肉 | Delicious!
146
+ |===
147
+
148
+ [source,yaml]
149
+ ----
150
+ ---
151
+ data:
152
+ - foo:
153
+ bar:
154
+ en: beef
155
+ fr: boeuf
156
+ jp: 牛肉
157
+ description: Yummy!
158
+ - foo:
159
+ bar:
160
+ en: pork
161
+ fr: porc
162
+ jp: 豚肉
163
+ description: Delicious!
164
+ ...
165
+ ----
166
+
167
+
168
+ You are also allowed to specify the data types of columns. The types of `string`, `boolean` and `integer` are supported.
169
+
170
+ [cols,"a,a,a,a"]
171
+ |===
172
+ |DATA | | |
173
+ |foo.bar.en[string] | foo.bar.fr[string] | yummy[boolean] | availability[integer]
174
+ |beef | boeuf | TRUE | 3
175
+ |pork | porc | FALSE | 10
176
+ |===
177
+
178
+ [source,yaml]
179
+ ----
180
+ ---
181
+ data:
182
+ beef:
183
+ foo:
184
+ bar:
185
+ en: beef
186
+ fr: boeuf
187
+ yummy: true
188
+ availability: 3
189
+ pork:
190
+ foo:
191
+ bar:
192
+ en: pork
193
+ fr: porc
194
+ yummy: false
195
+ availability: 10
196
+ ...
197
+ ----
198
+
199
+
200
+
201
+
202
+ == Examples
203
+
204
+ The `samples/` folder contains a number of complex examples.
205
+
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "structured_csv"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require "irb"
15
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/exe/csv_join.rb ADDED
@@ -0,0 +1,95 @@
1
+ #!/usr/bin/env ruby
2
+ require 'csv'
3
+ require 'yaml'
4
+ require 'pathname'
5
+
6
+ module CsvJoin
7
+
8
+ def self.join(csv, section_name)
9
+ first_row = nil
10
+ last_row = -1
11
+
12
+ puts "section_name #{section_name}"
13
+
14
+ csv.each_with_index do |row, index|
15
+ if first_row.nil? && is_start_of_portion?(row, section_name)
16
+ puts "found first"
17
+ first_row = index+1
18
+ next
19
+ end
20
+
21
+ if !first_row.nil? && is_row_empty?(row)
22
+ puts "found last"
23
+ last_row = index
24
+ break
25
+ end
26
+ end
27
+
28
+ puts "first #{first_row} last #{last_row}"
29
+ csv[first_row..last_row]
30
+ end
31
+
32
+ def self.load_csv(csvfile)
33
+ # puts csvfile
34
+
35
+ content = File.read(csvfile, encoding: "bom|utf-8").scrub
36
+ CSV.parse(content, liberal_parsing: true, encoding: "UTF-8")
37
+ end
38
+
39
+ def self.convert(csvdir, outfile)
40
+ raise "first argument must be a directory!" unless File.directory?(csvdir)
41
+
42
+ csv = CSV.open(outfile, "wb", encoding: "UTF-8")
43
+
44
+ csvfiles = Dir.glob(File.join(csvdir, "**", "*.csv")).sort
45
+ raise "directory must contain .csv files!" if csvfiles.empty?
46
+
47
+ # Assume all files use the same header structure as the first CSV file
48
+ header = []
49
+ csvheader = ""
50
+
51
+ csvfiles.each do |csvfile|
52
+ content = load_csv(csvfile)
53
+
54
+ csvheader = content.shift
55
+ if header.empty?
56
+ header = ['name'] + csvheader
57
+ csv << header
58
+ end
59
+
60
+ basename = Pathname.new(csvfile).basename.sub_ext('').to_s
61
+ content.each do |filerow|
62
+ row = []
63
+ filerow.each do |value|
64
+ row << case value
65
+ when String
66
+ value.strip
67
+ else
68
+ value
69
+ end
70
+ end
71
+
72
+ all_empty = row.all? do |f|
73
+ f.nil? || f.empty?
74
+ end
75
+ next if all_empty
76
+
77
+ row.unshift(basename)
78
+
79
+ csv << row
80
+ end
81
+ end
82
+
83
+ csv
84
+ end
85
+
86
+ end
87
+
88
+ csvdir = ARGV.pop
89
+ outfile = Pathname.new(csvdir).sub_ext(".csv").to_s
90
+
91
+ # puts outfile
92
+
93
+ CsvJoin.convert(csvdir, outfile)
94
+
95
+ # puts CsvJoin.convert(csvdir)