Skip to content

Commit

Permalink
Merge pull request #11 from ZeroGachis/feature/int-280-settings-quoting
Browse files Browse the repository at this point in the history
✨ Schema - add quotechar option
  • Loading branch information
EwenBALOUIN authored Oct 31, 2023
2 parents ac5e0e0 + 3d6b27d commit 6634fd8
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
6 changes: 5 additions & 1 deletion magicparse/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,17 @@ class CsvSchema(Schema):
def __init__(self, options: Dict[str, Any]) -> None:
super().__init__(options)
self.delimiter = options.get("delimiter", ",")
self.quotechar = options.get("quotechar", '"')

def get_reader(self, stream: BytesIO) -> Iterable[List[str]]:
stream_reader = codecs.getreader(self.encoding)
stream_content = stream_reader(stream)

return csv.reader(
stream_content, delimiter=self.delimiter, quoting=csv.QUOTE_NONE
stream_content,
delimiter=self.delimiter,
quoting=csv.QUOTE_MINIMAL,
quotechar=self.quotechar,
)

@staticmethod
Expand Down
40 changes: 40 additions & 0 deletions tests/test_schema.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from decimal import Decimal
from magicparse import Schema
from magicparse.schema import ColumnarSchema, CsvSchema
from magicparse.fields import ColumnarField, CsvField
Expand Down Expand Up @@ -215,6 +216,45 @@ def test_errors_do_not_halt_parsing(self):
]


class TestQuotingSetting(TestCase):
def test_no_quote(self):
schema = Schema.build(
{
"file_type": "csv",
"has_header": True,
"fields": [{"key": "column_1", "type": "decimal", "column-number": 1}],
}
)
rows, errors = schema.parse(b"column_1\n6.66")
assert rows == [{"column_1": Decimal("6.66")}]
assert not errors

def test_single_quote(self):
schema = Schema.build(
{
"file_type": "csv",
"quotechar": "'",
"has_header": True,
"fields": [{"key": "column_1", "type": "decimal", "column-number": 1}],
}
)
rows, errors = schema.parse(b"column_1\n'6.66'")
assert rows == [{"column_1": Decimal("6.66")}]
assert not errors

def test_double_quote(self):
schema = Schema.build(
{
"file_type": "csv",
"has_header": True,
"fields": [{"key": "column_1", "type": "decimal", "column-number": 1}],
}
)
rows, errors = schema.parse(b'column_1\n"6.66"')
assert rows == [{"column_1": Decimal("6.66")}]
assert not errors


class TestRegister(TestCase):
class PipedSchema(Schema):
@staticmethod
Expand Down

0 comments on commit 6634fd8

Please sign in to comment.