Skip to content

Commit a9c4b71

Browse files
author
James (ODSC)
authored
Merge pull request #410 from OpenDataServices/csv-dialect
Line terminator option
2 parents d09ee50 + 5a24b5f commit a9c4b71

File tree

13 files changed

+70
-10
lines changed

13 files changed

+70
-10
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ examples/flatten/rollup/direct/actual
5050
examples/flatten/rollup/direct/actual.*
5151
examples/flatten/rollup/file/actual
5252
examples/flatten/rollup/file/actual.*
53+
examples/flatten/line-terminator/actual
54+
examples/flatten/line-terminator/actual.*
5355
examples/receipt/source-map/actual
5456
examples/receipt/source-map/actual.*
5557
examples/bods/unflatten/actual

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
66

77
## [Unreleased]
88

9+
## [0.20.0] - 2022-12-07
10+
11+
### Changed
12+
13+
- Add `--line-terminator` option to `flatten` and `create-template`
14+
915
## [0.19.0] - 2022-11-16
1016

1117
### Fixed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
$ flatten-tool flatten --line-terminator=LF --root-list-path=cafe --main-sheet-name=cafe --schema=examples/receipt/cafe.schema examples/receipt/normalised/expected.json -o examples/flatten/line-terminator/actual
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
id,name,address
2+
CAFE-HEALTH,Healthy Cafe,
3+
CAFE-VEG,Vegetarian Cafe,
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
id,table/0/id,table/0/dish/0/id,table/0/dish/0/name,table/0/dish/0/cost
2+
CAFE-HEALTH,TABLE-1,,Fish and Chips,9.95
3+
CAFE-HEALTH,TABLE-1,,Pesto Pasta Salad,6.95
4+
CAFE-HEALTH,TABLE-3,,Fish and Chips,9.95
5+
CAFE-VEG,TABLE-16,,Large Glass Sauvignon,5.95
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
id,table/0/id,table/0/number
2+
CAFE-HEALTH,TABLE-1,1
3+
CAFE-HEALTH,TABLE-2,2
4+
CAFE-HEALTH,TABLE-3,3
5+
CAFE-VEG,TABLE-16,16
6+
CAFE-VEG,TABLE-17,17

examples/help/create-template/expected.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ usage: flatten-tool create-template [-h] -s SCHEMA [-f {csv,ods,xlsx,all}]
44
[--disable-local-refs]
55
[--no-deprecated-fields]
66
[--truncation-length TRUNCATION_LENGTH]
7+
[--line-terminator LINE_TERMINATOR]
78

89
optional arguments:
910
-h, --help show this help message and exit
@@ -32,3 +33,6 @@ optional arguments:
3233
--truncation-length TRUNCATION_LENGTH
3334
The length of components of sub-sheet names (default
3435
3).
36+
--line-terminator LINE_TERMINATOR
37+
The line terminator to use when writing CSV files:
38+
CRLF or LF

examples/help/flatten/expected.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ usage: flatten-tool flatten [-h] [-s SCHEMA] [-f {csv,ods,xlsx,all}] [--xml]
99
[--preserve-fields PRESERVE_FIELDS]
1010
[--disable-local-refs]
1111
[--remove-empty-schema-columns]
12+
[--line-terminator LINE_TERMINATOR]
1213
input_name
1314

1415
positional arguments:
@@ -61,3 +62,6 @@ optional arguments:
6162
--remove-empty-schema-columns
6263
When using flatten with a schema, remove columns and
6364
sheets from the output that contain no data.
65+
--line-terminator LINE_TERMINATOR
66+
The line terminator to use when writing CSV files:
67+
CRLF or LF

flattentool/__init__.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from flattentool.json_input import JSONParser
99
from flattentool.lib import parse_sheet_configuration
1010
from flattentool.output import FORMATS as OUTPUT_FORMATS
11-
from flattentool.output import FORMATS_SUFFIX
11+
from flattentool.output import FORMATS_SUFFIX, LINE_TERMINATORS
1212
from flattentool.schema import SchemaParser
1313
from flattentool.xml_output import toxml
1414

@@ -24,7 +24,8 @@ def create_template(
2424
disable_local_refs=False,
2525
truncation_length=3,
2626
no_deprecated_fields=False,
27-
**_
27+
line_terminator="CRLF",
28+
**_,
2829
):
2930
"""
3031
Creates template file(s) from given inputs
@@ -33,6 +34,9 @@ def create_template(
3334
3435
"""
3536

37+
if line_terminator not in LINE_TERMINATORS.keys():
38+
raise Exception(f"{line_terminator} is not a valid line terminator")
39+
3640
parser = SchemaParser(
3741
schema_filename=schema,
3842
rollup=rollup,
@@ -46,7 +50,10 @@ def create_template(
4650

4751
def spreadsheet_output(spreadsheet_output_class, name):
4852
spreadsheet_output = spreadsheet_output_class(
49-
parser=parser, main_sheet_name=main_sheet_name, output_name=name
53+
parser=parser,
54+
main_sheet_name=main_sheet_name,
55+
output_name=name,
56+
line_terminator=LINE_TERMINATORS[line_terminator],
5057
)
5158
spreadsheet_output.write_sheets()
5259

@@ -87,7 +94,8 @@ def flatten(
8794
disable_local_refs=False,
8895
remove_empty_schema_columns=False,
8996
truncation_length=3,
90-
**_
97+
line_terminator="CRLF",
98+
**_,
9199
):
92100
"""
93101
Flatten a nested structure (JSON) to a flat structure (spreadsheet - csv or xlsx).
@@ -99,6 +107,9 @@ def flatten(
99107
):
100108
raise Exception("You must use filter_field and filter_value together")
101109

110+
if line_terminator not in LINE_TERMINATORS.keys():
111+
raise Exception(f"{line_terminator} is not a valid line terminator")
112+
102113
if schema:
103114
schema_parser = SchemaParser(
104115
schema_filename=schema,
@@ -136,6 +147,7 @@ def spreadsheet_output(spreadsheet_output_class, name):
136147
main_sheet_name=main_sheet_name,
137148
output_name=name,
138149
sheet_prefix=sheet_prefix,
150+
line_terminator=LINE_TERMINATORS[line_terminator],
139151
)
140152
spreadsheet_output.write_sheets()
141153

@@ -206,7 +218,7 @@ def unflatten(
206218
disable_local_refs=False,
207219
xml_comment=None,
208220
truncation_length=3,
209-
**_
221+
**_,
210222
):
211223
"""
212224
Unflatten a flat structure (spreadsheet - csv or xlsx) into a nested structure (JSON).

flattentool/cli.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,10 @@ def create_parser():
9696
default=3,
9797
help="The length of components of sub-sheet names (default 3).",
9898
)
99+
parser_create_template.add_argument(
100+
"--line-terminator",
101+
help="The line terminator to use when writing CSV files: CRLF or LF",
102+
)
99103

100104
parser_flatten = subparsers.add_parser("flatten", help="Flatten a JSON file")
101105
parser_flatten.add_argument("input_name", help="Name of the input JSON file.")
@@ -177,7 +181,10 @@ def create_parser():
177181
action="store_true",
178182
help="When using flatten with a schema, remove columns and sheets from the output that contain no data.",
179183
)
180-
184+
parser_flatten.add_argument(
185+
"--line-terminator",
186+
help="The line terminator to use when writing CSV files: CRLF or LF",
187+
)
181188
parser_unflatten = subparsers.add_parser(
182189
"unflatten", help="Unflatten a spreadsheet"
183190
)

0 commit comments

Comments
 (0)