patchwork_csv_utils 0.1.6-arm64-darwin → 0.1.7-arm64-darwin

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 29f014ebb0d7fe82e824325e564767deb1cfed92e24c1f487171457214316ac2
4
- data.tar.gz: 608a2edb81ccedebc584762b5c30a2eb24e2c002fae1d28190cf7cf307cc23b3
3
+ metadata.gz: f0a84b0c5c4d9eee5d49fdb63c5f1381967eda72a68a16b35c602fa0143cd0e2
4
+ data.tar.gz: d1e0f1077c2abbda112ddcee7dae55e198666566097e95bd96aa6665c363eec7
5
5
  SHA512:
6
- metadata.gz: de08a499be3bd3106978ba9b56b81afd3b1bfb634262405aefae74cab36543390d9f825e2bcdf2d25601bbdcea2fec3f4853bff470a39a89a484846477b2dd59
7
- data.tar.gz: 6b25c5917e837b3de71570f0d12c14d92cf3ef588d36ca660611fbae4a6ca17cc2c04af641304f7dc755cb69ea57b5a38c324ca09b9322af62dd8d0900659718
6
+ metadata.gz: 011f0adde455db75eeb27492bbbb8617f2a406ef56278c54024fb3c874a7038935e11cd70176e2706c1e4700f9a3d6dc1cfde1abc076bbe2561c3c886f1140ec
7
+ data.tar.gz: a5d81faa6efd2b56941003638bd2c5c0829670179ee8a76a475724b6db688c6430bea189397ebd7c53a7b790f8f8ae5a38b4049c2aef7bb7a4c62dcd152021c3
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: arm64-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