Skip to content

Commit

Permalink
✨ Schema - add quotechar option
Browse files Browse the repository at this point in the history
  • Loading branch information
EwenBALOUIN committed Oct 31, 2023
1 parent ac5e0e0 commit c2b72a8
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
3 changes: 2 additions & 1 deletion magicparse/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,14 @@ 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
46 changes: 46 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 @@ -214,6 +215,51 @@ 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):
Expand Down

0 comments on commit c2b72a8

Please sign in to comment.