Skip to content

Commit

Permalink
continue
Browse files Browse the repository at this point in the history
  • Loading branch information
Jan Bielecki committed Mar 29, 2024
1 parent de8678c commit 2199433
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 9 deletions.
3 changes: 3 additions & 0 deletions pdtable/io/parsers/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ def check_encoding(cell_rows: Iterable[Sequence]) -> Iterable[Sequence]:
This function checks if we loaded the file content with a correct encoding
and raise an EncodingException if not.
"""
if isinstance(cell_rows, list):
cell_rows = iter(cell_rows)

first_cell_row = next(cell_rows)

if first_cell_row is not None and len(first_cell_row) > 0 and len(first_cell_row[0]) > 0:
Expand Down
13 changes: 13 additions & 0 deletions pdtable/test/io/input/only_tables_no_bom.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
**generic_inf;;;;;;;;;;;;;
all;;;;;;;;;;;;;
FATIMA_alias;node;constraint_alias;symmetry;sn_curve;sectional_force_modification;pristrco;signco;alpha;cutpoint_tol;file_name;transformation;IO;detail_type
text;text;text;text;text;text;text;-;-;mm;text;text;text;-
C00001;B0C066;C00001;rotate;F3;-;0;3;0.8;2000;..\..\..\inputs\INF\J_tube\CHW2204_INF_Swan_Neck_a30_root_V2.txt;;I;1
;;;;;;;;;;;;;
;;;;;;;;;;;;;
**generic_inf_constraints;;;;;;;;;;;;;
all;;;;;;;;;;;;;
constraint_alias;element;symmetry;cut_point_name;node;cut_distance;;;;;;;;
text;text;text;text;text;m;;;;;;;;
C00001;C660L;rotate;BRACE1;B0C066;3.091;;;;;;;;
C00001;CJT1V;rotate;BRACE2;B0C066;1.5319;;;;;;;;
File renamed without changes.
27 changes: 18 additions & 9 deletions pdtable/test/io/test_csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -335,15 +335,18 @@ def test_read_csv__sep_is_comma(csv_data):
assert len(template_rows) == 1


_input_dir = Path(__file__).parent / "input"


def test_read_csv__from_stream():
with open(Path(__file__).parent / "input" / "bundle.csv", "r") as fh:
with open(_input_dir / "bundle.csv", "r") as fh:
bls = list(read_csv(fh))
tables = [bl for ty, bl in bls if ty == BlockType.TABLE]
assert tables[1].name == "spelling_numbers"

# raises exception on common error if not text stream
with raises(Exception):
with open(Path(__file__).parent / "input" / "bundle.csv", "rb") as fh: # binary stream!
with open(_input_dir / "bundle.csv", "rb") as fh: # binary stream!
bls = list(read_csv(fh))
tables = [bl for ty, bl in bls if ty == BlockType.TABLE]

Expand Down Expand Up @@ -421,13 +424,19 @@ def test__table_is_preserved_when_written_to_and_read_from_csv():
assert table_read.destinations == table_write.destinations


def test_read_csv_starting_with_bom():
only_tables_path = Path(__file__).parent / "input" / "only_tables.csv"
def test_read_csv_only_tables_starting_with_bom():
only_tables_starts_with_bom_path = _input_dir / "only_tables_starts_with_bom.csv"

with pytest.raises(EncodingException):
list(read_csv(source=only_tables_path))
list(read_csv(source=only_tables_starts_with_bom_path))

source = open(only_tables_path, mode='r', encoding='utf-8-sig')
bls = list(read_csv(source=source))
tables = [bl for ty, bl in bls if ty == BlockType.TABLE]
assert tables[0].name == "generic_inf"
source = open(only_tables_starts_with_bom_path, mode='r', encoding='utf-8-sig')
tables = list(read_csv(source=source))
assert tables[0][1].name == "generic_inf"


def test_read_csv_only_tables_no_bom():
only_tables_no_bom_path = _input_dir / "only_tables_no_bom.csv"
source = open(only_tables_no_bom_path, mode='r', encoding='utf-8-sig')
tables = list(read_csv(source=source))
assert tables[0][1].name == "generic_inf"

0 comments on commit 2199433

Please sign in to comment.