Skip to content

Commit

Permalink
fix formating errors with black
Browse files Browse the repository at this point in the history
  • Loading branch information
jbdesbas committed Nov 29, 2023
1 parent ac55544 commit 92cb49e
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 21 deletions.
16 changes: 10 additions & 6 deletions clevercsv/console/commands/standardize.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,9 @@ def register(self) -> None:
help="Set the encoding of the output file(s)",
description=(
"If ommited, the output file encoding while be the same "
"as that of the original file."
"as that of the original file."
),
type=str
type=str,
)
self.add_argument(
"-i",
Expand Down Expand Up @@ -165,7 +165,7 @@ def handle(self) -> int:
encoding=encoding,
verbose=verbose,
num_chars=num_chars,
target_encoding=target_encoding
target_encoding=target_encoding,
)
if retval > 0 and global_retval == 0:
global_retval = retval
Expand All @@ -180,7 +180,7 @@ def handle_path(
encoding: Optional[str] = None,
num_chars: Optional[int] = None,
verbose: bool = False,
target_encoding: Optional[str] = None
target_encoding: Optional[str] = None,
) -> int:
encoding = encoding or get_encoding(path)
target_encoding = target_encoding or encoding
Expand Down Expand Up @@ -238,7 +238,11 @@ def _write_to_stream(
self._write_direct(path, stream, dialect, encoding)

def _in_place(
self, path: StrPath, dialect: SimpleDialect, encoding: Optional[str], target_encoding: Optional[str]
self,
path: StrPath,
dialect: SimpleDialect,
encoding: Optional[str],
target_encoding: Optional[str],
) -> int:
"""In-place mode overwrites the input file, if necessary
Expand Down Expand Up @@ -277,7 +281,7 @@ def _to_file(
output: StrPath,
dialect: SimpleDialect,
encoding: Optional[str],
target_encoding: Optional[str]
target_encoding: Optional[str],
) -> int:
with open(output, "w", newline="", encoding=target_encoding) as fp:
self._write_to_stream(path, fp, dialect, encoding)
Expand Down
40 changes: 25 additions & 15 deletions tests/test_unit/test_console.py
Original file line number Diff line number Diff line change
Expand Up @@ -643,17 +643,19 @@ def test_standardize_in_place_multi_noop(self) -> None:
any(map(os.unlink, tmpfnames))

def test_standardize_target_encoding(self) -> None:
table: TableType = [["Å", "B", "C"], ['é', 'ü', '中'], [4, 5, 6]]
table: TableType = [["Å", "B", "C"], ["é", "ü", "中"], [4, 5, 6]]
dialect = SimpleDialect(delimiter=";", quotechar="", escapechar="")
encoding='utf-8'
encoding = "utf-8"
tmpfname = self._build_file(table, dialect, encoding=encoding)

tmpfd, tmpoutname = tempfile.mkstemp(prefix="ccsv_", suffix=".csv")
os.close(tmpfd)

application = build_application()
tester = Tester(application)
tester.test_command("standardize", ["-o", tmpoutname, '-E', 'utf-8', tmpfname])
tester.test_command(
"standardize", ["-o", tmpoutname, "-E", "utf-8", tmpfname]
)

# Excel format (i.e. RFC4180) *requires* CRLF
crlf = "\r\n"
Expand All @@ -668,22 +670,27 @@ def test_standardize_target_encoding(self) -> None:
os.unlink(tmpoutname)

def test_standardize_target_encoding2(self) -> None:
table: TableType = [["A", "B", "C"], ['é', 'è', 'à'], [4, 5, 6]]
table: TableType = [["A", "B", "C"], ["é", "è", "à"], [4, 5, 6]]
dialect = SimpleDialect(delimiter=";", quotechar="", escapechar="")
encoding='latin-1'
encoding = "latin-1"
tmpfname = self._build_file(table, dialect, encoding=encoding)
self.assertEqual("ISO-8859-1", get_encoding(tmpfname, try_cchardet=False))
self.assertEqual(
"ISO-8859-1", get_encoding(tmpfname, try_cchardet=False)
)
tmpfd, tmpoutname = tempfile.mkstemp(prefix="ccsv_", suffix=".csv")
os.close(tmpfd)

application = build_application()
tester = Tester(application)
tester.test_command("standardize", ["-o", tmpoutname, '-e', 'latin-1', '-E', 'utf-8', tmpfname])
tester.test_command(
"standardize",
["-o", tmpoutname, "-e", "latin-1", "-E", "utf-8", tmpfname],
)

# Excel format (i.e. RFC4180) *requires* CRLF
crlf = "\r\n"
exp = crlf.join(["A,B,C", "é,è,à", "4,5,6", ""])

self.assertEqual("utf-8", get_encoding(tmpoutname, try_cchardet=False))
with open(tmpoutname, "r", newline="") as fp:
output = fp.read()
Expand All @@ -694,23 +701,26 @@ def test_standardize_target_encoding2(self) -> None:
finally:
os.unlink(tmpfname)
os.unlink(tmpoutname)



def test_standardize_target_encoding_raise_UnicodeEncodeError(self) -> None:
table: TableType = [["Å", "B", "C"], ['é', 'ü', '中'], [4, 5, 6]]
def test_standardize_target_encoding_raise_UnicodeEncodeError(
self,
) -> None:
table: TableType = [["Å", "B", "C"], ["é", "ü", "中"], [4, 5, 6]]
dialect = SimpleDialect(delimiter=";", quotechar="", escapechar="")
encoding='utf-8'
encoding = "utf-8"
tmpfname = self._build_file(table, dialect, encoding=encoding)

tmpfd, tmpoutname = tempfile.mkstemp(prefix="ccsv_", suffix=".csv")
os.close(tmpfd)

application = build_application()
tester = Tester(application)
try :
try:
with self.assertRaises(UnicodeEncodeError):
tester.test_command("standardize", ["-o", tmpoutname, '-E', 'latin-1', tmpfname])
tester.test_command(
"standardize",
["-o", tmpoutname, "-E", "latin-1", tmpfname],
)
finally:
os.unlink(tmpfname)
os.unlink(tmpoutname)

0 comments on commit 92cb49e

Please sign in to comment.