Skip to content

Commit

Permalink
refine json/nt converter examples
Browse files Browse the repository at this point in the history
  • Loading branch information
Ken Kundert authored and Ken Kundert committed Apr 7, 2021
1 parent 4549654 commit f4da8a7
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 5 deletions.
2 changes: 1 addition & 1 deletion doc/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# General

project = u'NestedText'
copyright = u'2020, Kenneth S. Kundert and Kale Kundert'
copyright = u'2020-21, Kenneth S. Kundert and Kale Kundert'
release = '1.3.1'
version = '.'.join(release.split('.'))

Expand Down
5 changes: 4 additions & 1 deletion examples/json-to-nestedtext
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ is written to stdout.
"""

from docopt import docopt
from inform import fatal, full_stop, os_error
from inform import done, fatal, full_stop, os_error
from pathlib import Path
import json
import nestedtext as nt
Expand Down Expand Up @@ -51,10 +51,13 @@ try:
output_path.write_text(nestedtext_content, encoding='utf-8')
else:
sys.stdout.write(nestedtext_content)

except OSError as e:
fatal(os_error(e))
except nt.NestedTextError as e:
e.terminate(culprit=input_filename)
except KeyboardInterrupt:
done()
except json.JSONDecodeError as e:
# create a nice error message with surrounding context
msg = e.msg
Expand Down
8 changes: 5 additions & 3 deletions examples/nestedtext-to-json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ is written to stdout.
"""

from docopt import docopt
from inform import fatal, os_error
from inform import done, fatal, os_error
from pathlib import Path
import json
import nestedtext as nt
Expand All @@ -38,17 +38,19 @@ try:
if input_filename:
input_path = Path(input_filename)
data = nt.load(input_path, top='any', on_dup=de_dup)
json_content = json.dumps(data, indent=4)
json_content = json.dumps(data, indent=4, ensure_ascii=False)
output_path = input_path.with_suffix('.json')
if output_path.exists():
if not cmdline['--force']:
fatal('file exists, use -f to force over-write.', culprit=output_path)
output_path.write_text(json_content, encoding='utf-8')
else:
data = nt.load(sys.stdin, top='any', on_dup=de_dup)
json_content = json.dumps(data, indent=4)
json_content = json.dumps(data, indent=4, ensure_ascii=False)
sys.stdout.write(json_content)
except OSError as e:
fatal(os_error(e))
except nt.NestedTextError as e:
e.terminate()
except KeyboardInterrupt:
done()

0 comments on commit f4da8a7

Please sign in to comment.