Skip to content

Commit

Permalink
[save] add options.save_encoding (default: 'utf-8') to differentiate …
Browse files Browse the repository at this point in the history
…from options.encoding when saving a file

Part of #1078
  • Loading branch information
anjakefala committed Mar 31, 2023
1 parent d92b66a commit 0ad3f17
Show file tree
Hide file tree
Showing 13 changed files with 17 additions and 14 deletions.
2 changes: 2 additions & 0 deletions tests/invalid_unicode_sqlite.vd
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
sheet col row longname input keystrokes comment
global encoding set-option latin-1
invalid_unicode encoding set-option latin-1
global save_encoding set-option latin-1
invalid_unicode save_encoding set-option latin-1
open-file tests/invalid_unicode.sqlite o
invalid_unicode キTest open-row Enter open sheet with copies of rows referenced in current row
2 changes: 1 addition & 1 deletion visidata/cmdlog.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def open_vdj(vd, p):

@VisiData.api
def save_vdj(vd, p, *vsheets):
with p.open_text(mode='w', encoding=vsheets[0].options.encoding) as fp:
with p.open_text(mode='w', encoding=vsheets[0].options.save_encoding) as fp:
fp.write("#!vd -p\n")
for vs in vsheets:
vs.write_jsonl(fp)
Expand Down
2 changes: 1 addition & 1 deletion visidata/loaders/csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def iterload(self):
@VisiData.api
def save_csv(vd, p, sheet):
'Save as single CSV file, handling column names as first line.'
with p.open_text(mode='w', encoding=sheet.options.encoding, newline='') as fp:
with p.open_text(mode='w', encoding=sheet.options.save_encoding, newline='') as fp:
cw = csv.writer(fp, **options.getall('csv_'))
colnames = [col.name for col in sheet.visibleCols]
if ''.join(colnames):
Expand Down
2 changes: 1 addition & 1 deletion visidata/loaders/fixed_width.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def setCols(self, headerlines):

@VisiData.api
def save_fixed(vd, p, *vsheets):
with p.open_text(mode='w', encoding=vsheets[0].options.encoding) as fp:
with p.open_text(mode='w', encoding=vsheets[0].options.save_encoding) as fp:
for sheet in vsheets:
if len(vsheets) > 1:
fp.write('%s\n\n' % sheet.name)
Expand Down
2 changes: 1 addition & 1 deletion visidata/loaders/jrnl.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def iterload(self):

@VisiData.api
def save_jrnl(vd, p, *vsheets):
with p.open_text(mode='w', encoding=vsheets[0].options.encoding) as fp:
with p.open_text(mode='w', encoding=vsheets[0].options.save_encoding) as fp:
for vs in vsheets:
for r in vs.iterrows():
fp.write(f'[{r.date} {r.time}] {r.title}\n')
Expand Down
4 changes: 2 additions & 2 deletions visidata/loaders/json.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def encode_json(vd, row, cols, enc=_vjsonEncoder(sort_keys=False)):
@VisiData.api
def save_json(vd, p, *vsheets):
vs = vsheets[0]
with p.open_text(mode='w', encoding=vs.options.encoding) as fp:
with p.open_text(mode='w', encoding=vs.options.save_encoding) as fp:
try:
indent = int(vs.options.json_indent)
except Exception:
Expand Down Expand Up @@ -151,7 +151,7 @@ def write_jsonl(vs, fp):

@VisiData.api
def save_jsonl(vd, p, *vsheets):
with p.open_text(mode='w', encoding=vsheets[0].options.encoding) as fp:
with p.open_text(mode='w', encoding=vsheets[0].options.save_encoding) as fp:
for vs in vsheets:
vs.write_jsonl(fp)

Expand Down
2 changes: 1 addition & 1 deletion visidata/loaders/jsonla.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,6 @@ def write_jsonla(vs, fp):

@VisiData.api
def save_jsonla(vd, p, *vsheets):
with p.open_text(mode='w', encoding=vsheets[0].options.encoding) as fp:
with p.open_text(mode='w', encoding=vsheets[0].options.save_encoding) as fp:
for vs in vsheets:
write_jsonla(vs, fp)
2 changes: 1 addition & 1 deletion visidata/loaders/markdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def write_md(p, *vsheets, md_style='orgmode'):
else:
delim = '|'

with p.open_text(mode='w', encoding=vsheets[0].options.encoding) as fp:
with p.open_text(mode='w', encoding=vsheets[0].options.save_encoding) as fp:
for vs in vsheets:
if len(vsheets) > 1:
fp.write('# %s\n\n' % vs.name)
Expand Down
2 changes: 1 addition & 1 deletion visidata/loaders/orgmode.py
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ def sysopen_row(sheet, row):

@VisiData.api
def save_org(vd, p, *vsheets):
with p.open_text(mode='w', encoding=vsheets[0].options.encoding) as fp:
with p.open_text(mode='w', encoding=vsheets[0].options.save_encoding) as fp:
for vs in vsheets:
if isinstance(vs, OrgSheet):
for row in vs.rows:
Expand Down
4 changes: 2 additions & 2 deletions visidata/loaders/tsv.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def iterload(self):
delim = self.delimiter or self.options.delimiter
rowdelim = self.row_delimiter or self.options.row_delimiter

with self.source.open_text(encoding=self.options.encoding) as fp:
with self.source.open_text(encoding=self.options.save_encoding) as fp:
for line in splitter(fp, rowdelim):
if not line:
continue
Expand All @@ -65,7 +65,7 @@ def save_tsv(vd, p, vs, delimiter='', row_delimiter=''):
rowsep = row_delimiter or vs.options.row_delimiter
trdict = vs.safe_trdict()

with p.open_text(mode='w', encoding=vs.options.encoding) as fp:
with p.open_text(mode='w', encoding=vs.options.save_encoding) as fp:
colhdr = unitsep.join(col.name.translate(trdict) for col in vs.visibleCols) + rowsep
fp.write(colhdr)

Expand Down
2 changes: 1 addition & 1 deletion visidata/loaders/vdx.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def iterload(self):

@VisiData.api
def save_vdx(vd, p, *vsheets):
with p.open_text(mode='w', encoding=vsheets[0].options.encoding) as fp:
with p.open_text(mode='w', encoding=vsheets[0].options.save_encoding) as fp:
fp.write(f"# {visidata.__version_info__}\n")
for vs in vsheets:
prevrow = None
Expand Down
2 changes: 1 addition & 1 deletion visidata/path.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

from visidata import *

vd.option('encoding', 'utf-8', 'encoding passed to codecs.open', replay=True)
vd.option('encoding', 'utf-8', 'encoding passed to codecs.open when opening a file', replay=True)
vd.option('encoding_errors', 'surrogateescape', 'encoding_errors passed to codecs.open', replay=True)

@lru_cache()
Expand Down
3 changes: 2 additions & 1 deletion visidata/save.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

vd.option('confirm_overwrite', True, 'whether to prompt for overwrite confirmation on save')
vd.option('safe_error', '#ERR', 'error string to use while saving', replay=True)
vd.option('save_encoding', 'utf-8', 'encoding passed to codecs.open when saving a file', replay=True)

@Sheet.api
def safe_trdict(vs):
Expand Down Expand Up @@ -170,7 +171,7 @@ def save_zip(vd, p, *vsheets):

@VisiData.api
def save_txt(vd, p, *vsheets):
with p.open_text(mode='w', encoding=vsheets[0].options.encoding) as fp:
with p.open_text(mode='w', encoding=vsheets[0].options.save_encoding) as fp:
for vs in vsheets:
unitsep = vs.options.delimiter
rowsep = vs.options.row_delimiter
Expand Down

0 comments on commit 0ad3f17

Please sign in to comment.