patchwork_csv_utils 0.1.6-x86_64-darwin → 0.1.7-x86_64-darwin

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: dc9127c25a2fb4b4a0f99bd3730a4a82975fb249637f4c69ff31e0f5a68288de
4
- data.tar.gz: 0b493f77942386540b3fc244fdfcdc853ce8dac8ae41df3fa37c06bff1777d50
3
+ metadata.gz: 6716d509dcd08fa0114772079eb4ee4c910d9fcba2e239292661f7394ae20579
4
+ data.tar.gz: 97b20c9b75784359b32bf352156827c62f1d63d29cb00833ce826ae9db1ba08a
5
5
  SHA512:
6
- metadata.gz: 23a5632d5cfbaf9eca4397bff6199a34cd52c902a07640e490633260c94c122a8b5fc567c09ad3264e03eb31c5e5db2d676b1efe66ec322d7a6a3ddb4e7017f5
7
- data.tar.gz: 656ca8052002ab45dadad145b141de4255ec72a36c9c4d975adfd1e9071e7ac3675647837492b0261e6385fc5159af3e02cf9af92aa1551fdd25efc05bd02589
6
+ metadata.gz: 4cb36630ed56de19331bc54206aaed38cfe1901b68ca4b4822c16d5141d8f0a7e374fa1c508b951dbf9182bcef3ffa5f2e0c8fa116b4ed63b148f6ebbddbf076
7
+ data.tar.gz: 5cc6c6c1a0edf9ef86b42fab23aaa2937b1a80fcad5518a1f15fd3fe22d0ace8d3a534bac85f46a83a25a1dadc6328d210806f616f561dc9fb5bbe712e2f5a8b
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- patchwork_csv_utils (0.1.6)
4
+ patchwork_csv_utils (0.1.7)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -67,4 +67,4 @@ DEPENDENCIES
67
67
  rubocop (~> 1.21)
68
68
 
69
69
  BUNDLED WITH
70
- 2.4.10
70
+ 2.4.4
data/README.md CHANGED
@@ -14,7 +14,8 @@ gem install patchwork_csv_utils
14
14
  ```irb
15
15
  require 'csv_utils'
16
16
  CsvUtils.dedup('file1.csv', 'file2.csv', 'output.csv')
17
- CsvUtils.to_csv('file1.xls', 'output_file1.csv', 'sheet_name')
17
+ CsvUtils.to_csv('file1.xls', 'output_file1.csv', ['request_ids_to_skip']])
18
+ CsvUtils.transform_csv('file1.xls', 'output_file1.csv', ['request_ids_to_skip']])
18
19
  ```
19
20
 
20
21
  ## Release
@@ -1,4 +1,5 @@
1
1
  use magnus::{define_module, function, prelude::*};
2
+ use crate::utils::csv::transform_csv;
2
3
  use crate::utils::dedup::dedup;
3
4
  use crate::utils::xls::to_csv;
4
5
 
@@ -9,5 +10,6 @@ fn init() -> Result<(), magnus::Error> {
9
10
  let module = define_module("CsvUtils")?;
10
11
  module.define_singleton_method("dedup", function!(dedup, 3))?;
11
12
  module.define_singleton_method("to_csv", function!(to_csv, 3))?;
13
+ module.define_singleton_method("transform_csv", function!(transform_csv, 3))?;
12
14
  Ok(())
13
15
  }
@@ -0,0 +1,92 @@
1
+ use std::collections::HashMap;
2
+ use std::fs::File;
3
+
4
+ use chrono::{NaiveDate, NaiveDateTime, NaiveTime, Utc};
5
+ use csv::{StringRecord, Writer};
6
+ use magnus::{RArray, Ruby};
7
+
8
+ use crate::utils::{FileExtension, magnus_err, missing_header};
9
+
10
+ pub fn transform_csv(ruby: &Ruby, csv_path: String, target_path: String, exclusions: RArray) -> magnus::error::Result<()> {
11
+ if !csv_path.has_extension(&["csv"]) {
12
+ return Err(magnus::Error::new(ruby.exception_standard_error(), "csv_path must be a csv file".to_string()));
13
+ }
14
+
15
+ let exclusions = RArray::to_vec(exclusions)?;
16
+
17
+ let csv_file = File::open(csv_path).map_err(|e| magnus_err(ruby, e, "csv_path"))?;
18
+ let mut csv: csv::Reader<File> = csv::Reader::from_reader(csv_file);
19
+ let mut wtr = Writer::from_path(target_path).map_err(|e| magnus_err(ruby, e, "target_path"))?;
20
+ let headers = csv.headers().map_err(|e| magnus_err(ruby, e, "csv_path headers"))?;
21
+ let header_map: HashMap<String, usize> = headers.iter().enumerate().map(|(i, h)| (h.to_string(), i)).collect();
22
+
23
+ wtr.write_byte_record(headers.as_byte_record()).map_err(|e| magnus_err(ruby, e, "write_byte_record"))?;
24
+
25
+ let request_id = header_map.get("Request Id").ok_or(missing_header(ruby, "Request Id"))?;
26
+ let date = header_map.get("Date").ok_or(missing_header(ruby, "Date"))?;
27
+ let start = header_map.get("Start").ok_or(missing_header(ruby, "Start"))?;
28
+ let end = header_map.get("End").ok_or(missing_header(ruby, "End"))?;
29
+ let actual_start = header_map.get("Actual Start").ok_or(missing_header(ruby, "Actual Start"))?;
30
+ let actual_end = header_map.get("Actual End").ok_or(missing_header(ruby, "Actual End"))?;
31
+
32
+ for (ri, record) in csv.records().enumerate() {
33
+ let record = record.map_err(|e| magnus_err(ruby, e, "record"))?;
34
+
35
+ if skip_excluded_rows(request_id, &record, &exclusions) { continue; }
36
+ if has_empty_row_skip(&record) { continue; }
37
+ if has_empty_first_col_skip_row(&record) { continue; }
38
+
39
+ let mut date_value = Utc::now().naive_utc();
40
+
41
+ let record = record.iter().enumerate().map(|(i, c)| {
42
+ let c = c.trim_end();
43
+ if i == *date {
44
+ let current = string_to_datetime(c).ok_or(to_datetime_error(ruby, c, ri, i))?;
45
+ date_value = current;
46
+ Ok(current.to_string())
47
+ } else if i == *start || i == *end || i == *actual_start || i == *actual_end {
48
+ let current_time = string_to_time(c).ok_or(to_datetime_error(ruby, c, ri, i))?;
49
+ let datetime = transform_time_to_datetime(date_value, current_time);
50
+ Ok(datetime.to_string())
51
+ } else {
52
+ Ok(c.to_string())
53
+ }
54
+ }).collect::<Result<StringRecord, magnus::Error>>()?;
55
+
56
+ let record = record.into_iter().map(|r| r.trim_end()).collect::<StringRecord>();
57
+ wtr.write_byte_record(record.as_byte_record()).map_err(|e| magnus_err(ruby, e, "write_byte_record"))?;
58
+ }
59
+
60
+ wtr.flush().map_err(|e| magnus_err(ruby, e, "flush"))?;
61
+
62
+ Ok(())
63
+ }
64
+
65
+ fn skip_excluded_rows(request_id: &usize, r: &StringRecord, exclusions: &Vec<String>) -> bool {
66
+ let value = r.get(*request_id).unwrap_or_default();
67
+ exclusions.contains(&value.to_string())
68
+ }
69
+
70
+ fn string_to_datetime(s: &str) -> Option<NaiveDateTime> {
71
+ NaiveDate::parse_from_str(s, "%d-%b-%y").ok().map(|d| d.and_hms_opt(0, 0, 0)).flatten()
72
+ }
73
+
74
+ fn string_to_time(s: &str) -> Option<NaiveTime> {
75
+ NaiveTime::parse_from_str(s, "%H:%M").ok()
76
+ }
77
+
78
+ fn transform_time_to_datetime(t1: NaiveDateTime, t2: NaiveTime) -> NaiveDateTime {
79
+ NaiveDateTime::new(t1.date(), t2)
80
+ }
81
+
82
+ fn to_datetime_error(ruby: &Ruby, value: &str, row: usize, col: usize) -> magnus::Error {
83
+ magnus::Error::new(ruby.exception_standard_error(), format!("Could not parse datetime '{}', row: {}, col: {}", value, row, col))
84
+ }
85
+
86
+ fn has_empty_first_col_skip_row(record: &StringRecord) -> bool {
87
+ record[0].is_empty()
88
+ }
89
+
90
+ fn has_empty_row_skip(record: &StringRecord) -> bool {
91
+ record.iter().all(|r| r.is_empty())
92
+ }
@@ -28,7 +28,7 @@ pub fn dedup(ruby: &Ruby, previous_csv_path: String, new_csv_path: String, targe
28
28
  return Err(magnus::Error::new(ruby.exception_standard_error(), "headers of both csv files must be the same".to_string()));
29
29
  }
30
30
 
31
- wtr.write_byte_record(previous_headers.as_byte_record()).unwrap();
31
+ wtr.write_byte_record(previous_headers.as_byte_record()).map_err(|e| magnus_err(ruby, e, "write_byte_record"))?;
32
32
 
33
33
  let mut previous_records = vec![];
34
34
  for previous_record in previous_csv.records() {
@@ -49,27 +49,21 @@ pub fn dedup(ruby: &Ruby, previous_csv_path: String, new_csv_path: String, targe
49
49
 
50
50
  let new_record = new_record.into_iter().map(|r| r.trim_end()).collect::<StringRecord>();
51
51
  if !previous_records.contains(&new_record) {
52
- wtr.write_byte_record(new_record.as_byte_record()).unwrap();
52
+ wtr.write_byte_record(new_record.as_byte_record()).map_err(|e| magnus_err(ruby, e, "write_byte_record"))?;
53
53
  }
54
54
  }
55
55
 
56
- wtr.flush().unwrap();
56
+ wtr.flush().map_err(|e| magnus_err(ruby, e, "flush"))?;
57
57
 
58
58
  Ok(())
59
59
  }
60
60
 
61
61
  fn has_empty_first_col_skip_row(previous_record: &StringRecord) -> bool {
62
- if previous_record[0].is_empty() {
63
- return true;
64
- }
65
- false
62
+ previous_record[0].is_empty()
66
63
  }
67
64
 
68
65
  fn has_empty_row_skip(record: &StringRecord) -> bool {
69
- if record.iter().all(|r| r.is_empty()) {
70
- return true;
71
- }
72
- false
66
+ record.iter().all(|r| r.is_empty())
73
67
  }
74
68
 
75
69
 
@@ -3,9 +3,14 @@ use std::ffi::OsStr;
3
3
  use std::path::Path;
4
4
  use magnus::Ruby;
5
5
 
6
+ pub mod csv;
6
7
  pub mod dedup;
7
8
  pub mod xls;
8
9
 
10
+ fn missing_header(ruby: &Ruby, header: &str) -> magnus::Error {
11
+ magnus::Error::new(ruby.exception_standard_error(), format!("Missing '{}' header", header))
12
+ }
13
+
9
14
  fn magnus_err<E: Error>(ruby: &Ruby, e: E, msg: &str) -> magnus::Error {
10
15
  magnus::Error::new(ruby.exception_standard_error(), format!("{}: {}", msg, e.to_string()))
11
16
  }
@@ -6,19 +6,15 @@ use calamine::{Data, open_workbook, Range, Reader, Xls};
6
6
  use chrono::{NaiveDateTime, Utc};
7
7
  use magnus::{RArray, Ruby};
8
8
 
9
- use crate::utils::{FileExtension, magnus_err};
9
+ use crate::utils::{FileExtension, magnus_err, missing_header};
10
10
 
11
11
  pub fn to_csv(ruby: &Ruby, xls_path: String, target_path: String, exclusions: RArray) -> magnus::error::Result<()> {
12
- let exclusions = RArray::to_vec(exclusions)?;
13
-
14
- println!("xls_path: {:?}", xls_path);
15
- println!("target_path: {:?}", target_path);
16
- println!("exclusions: {:?}", exclusions);
17
-
18
12
  if !xls_path.has_extension(&["xls"]) {
19
13
  return Err(magnus::Error::new(ruby.exception_standard_error(), "xls_path must be an xls file".to_string()));
20
14
  }
21
15
 
16
+ let exclusions = RArray::to_vec(exclusions)?;
17
+
22
18
  let mut workbook: Xls<_> = open_workbook(xls_path.clone()).map_err(|e| magnus_err(ruby, e, format!("could not open xls: {}", xls_path).as_str()))?;
23
19
  let range = workbook.worksheet_range_at(0)
24
20
  .ok_or(magnus::Error::new(ruby.exception_standard_error(), "no worksheet found in xls".to_string()))
@@ -34,18 +30,20 @@ pub fn to_csv(ruby: &Ruby, xls_path: String, target_path: String, exclusions: RA
34
30
 
35
31
  fn write_csv<W: Write>(ruby: &Ruby, dest: &mut W, range: &Range<Data>, header_map: HashMap<String, usize>, exclusions: Vec<String>) -> magnus::error::Result<()> {
36
32
  let n = range.get_size().1 - 1;
33
+
34
+ let request_id = header_map.get("Request Id").ok_or(missing_header(ruby, "Request Id"))?;
35
+ let date = header_map.get("Date").ok_or(missing_header(ruby, "Date"))?;
36
+ let start = header_map.get("Start").ok_or(missing_header(ruby, "Start"))?;
37
+ let end = header_map.get("End").ok_or(missing_header(ruby, "End"))?;
38
+ let actual_start = header_map.get("Actual Start").ok_or(missing_header(ruby, "Actual Start"))?;
39
+ let actual_end = header_map.get("Actual End").ok_or(missing_header(ruby, "Actual End"))?;
40
+
37
41
  for (ri, r) in range.rows().enumerate() {
38
42
  let mut date_value = Utc::now().naive_utc();
39
43
 
40
- if skip_excluded_rows(&header_map, r, &exclusions) { continue; }
44
+ if skip_excluded_rows(&request_id, r, &exclusions) { continue; }
41
45
  if skip_empty_rows(r) { continue; }
42
- if skip_rows_with_no_request_id(&header_map, r) { continue; }
43
-
44
- let date = header_map.get("Date").ok_or(missing_header(ruby, "Date"))?;
45
- let start = header_map.get("Start").ok_or(missing_header(ruby, "Start"))?;
46
- let end = header_map.get("End").ok_or(missing_header(ruby, "End"))?;
47
- let actual_start = header_map.get("Actual Start").ok_or(missing_header(ruby, "Actual Start"))?;
48
- let actual_end = header_map.get("Actual End").ok_or(missing_header(ruby, "Actual End"))?;
46
+ if skip_rows_with_no_request_id(&request_id, r) { continue; }
49
47
 
50
48
  for (i, c) in r.iter().enumerate() {
51
49
  match *c {
@@ -76,34 +74,17 @@ fn write_csv<W: Write>(ruby: &Ruby, dest: &mut W, range: &Range<Data>, header_ma
76
74
  Ok(())
77
75
  }
78
76
 
79
- fn skip_excluded_rows(header_map: &HashMap<String, usize>, r: &[Data], exclusions: &Vec<String>) -> bool {
80
- if let Some(request_id) = header_map.get("Request Id") {
81
- let value = r[*request_id].to_string();
82
- if exclusions.contains(&value) {
83
- return true;
84
- }
85
- }
86
- false
77
+ fn skip_excluded_rows(request_id: &usize, r: &[Data], exclusions: &Vec<String>) -> bool {
78
+ let value = r[*request_id].to_string();
79
+ exclusions.contains(&value.to_string())
87
80
  }
88
81
 
89
82
  fn skip_empty_rows(r: &[Data]) -> bool {
90
- if r.iter().all(|c| c == &Data::Empty) {
91
- return true;
92
- }
93
- false
94
- }
95
-
96
- fn skip_rows_with_no_request_id(header_map: &HashMap<String, usize>, r: &[Data]) -> bool {
97
- if let Some(request_id) = header_map.get("Request Id") {
98
- if r[*request_id] == Data::Empty {
99
- return true;
100
- }
101
- }
102
- false
83
+ r.iter().all(|c| c == &Data::Empty)
103
84
  }
104
85
 
105
- fn missing_header(ruby: &Ruby, header: &str) -> magnus::Error {
106
- magnus::Error::new(ruby.exception_standard_error(), format!("Missing '{}' header in xls", header))
86
+ fn skip_rows_with_no_request_id(request_id: &usize, r: &[Data]) -> bool {
87
+ r[*request_id] == Data::Empty
107
88
  }
108
89
 
109
90
  fn transform_time_to_datetime(t1: NaiveDateTime, t2: NaiveDateTime) -> NaiveDateTime {
Binary file
Binary file
Binary file
Binary file
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module CsvUtils
4
- VERSION = '0.1.6'
4
+ VERSION = '0.1.7'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: patchwork_csv_utils
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.6
4
+ version: 0.1.7
5
5
  platform: x86_64-darwin
6
6
  authors:
7
7
  - kingsley.hendrickse
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-08-06 00:00:00.000000000 Z
11
+ date: 2024-08-07 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Deduplication of CSV files and XLS to CSV conversion.
14
14
  email:
@@ -28,6 +28,7 @@ files:
28
28
  - ext/csv_utils/Cargo.toml
29
29
  - ext/csv_utils/extconf.rb
30
30
  - ext/csv_utils/src/lib.rs
31
+ - ext/csv_utils/src/utils/csv.rs
31
32
  - ext/csv_utils/src/utils/dedup.rs
32
33
  - ext/csv_utils/src/utils/mod.rs
33
34
  - ext/csv_utils/src/utils/xls.rs