Switch from codecs.getreader to io.TextIOWrapper, refs #230

This commit is contained in:
Simon Willison 2021-02-14 10:33:26 -08:00
commit 1e9eb875a6
2 changed files with 4 additions and 4 deletions

View file

@ -1,6 +1,5 @@
import base64
import click
import codecs
from click_default_group import DefaultGroup
from datetime import datetime
import hashlib
@ -8,6 +7,7 @@ import pathlib
import sqlite_utils
from sqlite_utils.db import AlterError
import textwrap
import io
import itertools
import json
import os
@ -665,7 +665,7 @@ def insert_upsert_implementation(
if encoding and not (csv or tsv):
raise click.ClickException("--encoding must be used with --csv or --tsv")
encoding = encoding or "utf-8"
json_file = codecs.getreader(encoding)(json_file)
json_file = io.TextIOWrapper(json_file, encoding=encoding)
if pk and len(pk) == 1:
pk = pk[0]
if csv or tsv:

View file

@ -105,9 +105,9 @@ class UpdateWrapper:
@contextlib.contextmanager
def file_progress(file, silent=False, **kwargs):
if silent or file.raw.fileno() == 0: # 0 = stdin
if silent or file.fileno() == 0: # 0 = stdin
yield file
else:
file_length = os.path.getsize(file.raw.name)
file_length = os.path.getsize(file.name)
with click.progressbar(length=file_length, **kwargs) as bar:
yield UpdateWrapper(file, bar.update)