Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle seeds with utf-8 BOM (#1177) #1452

Merged
merged 4 commits into from
May 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion core/dbt/clients/agate_helper.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
from codecs import BOM_UTF8

import dbt.compat

import agate

BOM = BOM_UTF8.decode('utf-8') # '\ufeff'

DEFAULT_TYPE_TESTER = agate.TypeTester(types=[
agate.data_types.Number(null_values=('null', '')),
agate.data_types.TimeDelta(null_values=('null', '')),
Expand Down Expand Up @@ -41,4 +46,7 @@ def as_matrix(table):


def from_csv(abspath):
return agate.Table.from_csv(abspath, column_types=DEFAULT_TYPE_TESTER)
with dbt.compat.open_file(abspath) as fp:
if fp.read(1) != BOM:
fp.seek(0)
return agate.Table.from_csv(fp, column_types=DEFAULT_TYPE_TESTER)
17 changes: 13 additions & 4 deletions core/dbt/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,20 @@ def to_native_string(s):

def write_file(path, s):
if WHICH_PYTHON == 2:
with codecs.open(path, 'w', encoding='utf-8') as f:
return f.write(to_string(s))
open = codecs.open
else:
with open(path, 'w') as f:
return f.write(to_string(s))
open = builtins.open
with open(path, 'w', encoding='utf-8') as f:
return f.write(to_string(s))


def open_file(path):
"""Open the path for reading. It must be utf-8 encoded."""
if WHICH_PYTHON == 2:
open = codecs.open
else:
open = builtins.open
return open(path, encoding='utf-8')


if WHICH_PYTHON == 2:
Expand Down
Loading