From c2ee7a5f314aa8cc63093caa19896b3bbed213c5 Mon Sep 17 00:00:00 2001 From: Doeke Zanstra Date: Wed, 24 Jul 2019 20:33:43 +0200 Subject: [PATCH 1/6] Removed UTF-8 setup, and added exception-handling --- ok-show.py | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/ok-show.py b/ok-show.py index cd9ea8e..4a28033 100755 --- a/ok-show.py +++ b/ok-show.py @@ -143,15 +143,21 @@ def main(): print('comment_align:', args.comment_align) print('terminal_width:', args.terminal_width) - #setup UTF-8 - if sys.stdin.encoding != 'UTF-8': - sys.stdin = codecs.getreader('utf8')(sys.stdin, 'strict') - if sys.stdout.encoding != 'UTF-8': - sys.stdout = codecs.getwriter('utf-8')(sys.stdout, 'strict') - if sys.stderr.encoding != 'UTF-8': - sys.stderr = codecs.getwriter('utf-8')(sys.stderr, 'strict') - # prepare - lines = sys.stdin.readlines() + # prepare (read stdin parse, transform, and calculate stuff) + # Unicode: best to ignore other encodings? SO doesn't seem to give good advice + # See https://stackoverflow.com/q/2737966/56 + try: + lines = sys.stdin.readlines() + except UnicodeDecodeError as err: + print('UTF-8 (unicode) should be used as sole encoding for .ok-files', file=sys.stderr) + if args.verbose > 1: + print(f'UnicodeDecodeError exception properties (error on: {err.object[err.start:err.end]}):', file=sys.stderr) + print(f'* encoding: {err.encoding}', file=sys.stderr) + print(f'* reason__: {err.reason}', file=sys.stderr) + print(f'* object__: {err.object}', file=sys.stderr) + print(f'* start___: {err.start}', file=sys.stderr) + print(f'* end_____: {err.end}', file=sys.stderr) + exit(1) p_lines = parse_lines(lines) cmd_lines = [pl.line_nr for pl in p_lines if pl.line_nr] nr_positions_line_nr = len(str(max(cmd_lines))) if len(cmd_lines)>0 else 0 From e72d4ef1157b3bb86509978eac8e307ce8b68875 Mon Sep 17 00:00:00 2001 From: Doeke Zanstra Date: Wed, 24 Jul 2019 20:44:37 +0200 Subject: [PATCH 2/6] Python 2 and <3.6 don't recognize formatted string literals --- .ok | 1 + ok-show.py | 12 ++++++------ 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/.ok b/.ok index 4c1429f..0f85c3e 100644 --- a/.ok +++ b/.ok @@ -12,6 +12,7 @@ echo "Passed arguments: 1:[$1], 2:[$2], 3:[$3], 4+:[${*:4}] (nr args: $#)" # Com echo "All passed arguments (no comment on this line): [$*]" ok help --verbose # Show help page of 🆗, including environment variables +export PYTHONIOENCODING="UTF-8" # When set, ok doesn't accept nothing but UTF-8 files and will raise an error otherwise set | grep "^_OK_" # Show all set environment variables, used with ok-bash echo -e "$PROMPT_COMMAND" # This variable can be changed with the reset/auto_show install helpers alias SSH='ok -v -f ~/.ssh/.ok -a SSH' # Keep a list of all your `ssh` connections; use it via `SSH` diff --git a/ok-show.py b/ok-show.py index 4a28033..1929d7e 100755 --- a/ok-show.py +++ b/ok-show.py @@ -151,12 +151,12 @@ def main(): except UnicodeDecodeError as err: print('UTF-8 (unicode) should be used as sole encoding for .ok-files', file=sys.stderr) if args.verbose > 1: - print(f'UnicodeDecodeError exception properties (error on: {err.object[err.start:err.end]}):', file=sys.stderr) - print(f'* encoding: {err.encoding}', file=sys.stderr) - print(f'* reason__: {err.reason}', file=sys.stderr) - print(f'* object__: {err.object}', file=sys.stderr) - print(f'* start___: {err.start}', file=sys.stderr) - print(f'* end_____: {err.end}', file=sys.stderr) + print('UnicodeDecodeError exception properties (error on: %s):' % err.object[err.start:err.end], file=sys.stderr) + print('* encoding: %s' % err.encoding, file=sys.stderr) + print('* reason__: %s' % err.reason, file=sys.stderr) + print('* object__: %s' % err.object, file=sys.stderr) + print('* start___: %s' % err.start, file=sys.stderr) + print('* end_____: %s' % err.end, file=sys.stderr) exit(1) p_lines = parse_lines(lines) cmd_lines = [pl.line_nr for pl in p_lines if pl.line_nr] From 825ddb1fff4f901725dff404dbde475ad1f4d8d6 Mon Sep 17 00:00:00 2001 From: Doeke Zanstra Date: Wed, 24 Jul 2019 20:52:00 +0200 Subject: [PATCH 3/6] Clarified comment in .ok + changed demo/fmt/.ok-file --- .ok | 2 +- demo/fmt/.ok | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/.ok b/.ok index 0f85c3e..ff0b528 100644 --- a/.ok +++ b/.ok @@ -12,7 +12,7 @@ echo "Passed arguments: 1:[$1], 2:[$2], 3:[$3], 4+:[${*:4}] (nr args: $#)" # Com echo "All passed arguments (no comment on this line): [$*]" ok help --verbose # Show help page of 🆗, including environment variables -export PYTHONIOENCODING="UTF-8" # When set, ok doesn't accept nothing but UTF-8 files and will raise an error otherwise +export PYTHONIOENCODING="UTF-8" # When set, ok doesn't accept nothing but UTF-8 files and will raise an error otherwise (python3). When not set, or python2, you get question marks set | grep "^_OK_" # Show all set environment variables, used with ok-bash echo -e "$PROMPT_COMMAND" # This variable can be changed with the reset/auto_show install helpers alias SSH='ok -v -f ~/.ssh/.ok -a SSH' # Keep a list of all your `ssh` connections; use it via `SSH` diff --git a/demo/fmt/.ok b/demo/fmt/.ok index 228ec43..ef5c31c 100644 --- a/demo/fmt/.ok +++ b/demo/fmt/.ok @@ -1,5 +1,7 @@ +termtosvg termtosvg_demo_fmt.svg -g 15x75 -t window_frame_js export PS1='> ' # Hide computer details in demo export _OK_VERBOSE=0 # No echoing the command +printf '\e[8;15;75t\e[2J\e[;H' # Resize current window + clear screen # Demo of comment indentation ok list --comment_align 0 # No indentation ok l --comment_align 1 # Group adjecent indentations From 81fc8ff7ebef20c086d4b50ae50f029148a3ef4d Mon Sep 17 00:00:00 2001 From: Doeke Zanstra Date: Wed, 24 Jul 2019 21:27:57 +0200 Subject: [PATCH 4/6] Made an error look like an error --- ok-show.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ok-show.py b/ok-show.py index 1929d7e..12843bb 100755 --- a/ok-show.py +++ b/ok-show.py @@ -149,7 +149,7 @@ def main(): try: lines = sys.stdin.readlines() except UnicodeDecodeError as err: - print('UTF-8 (unicode) should be used as sole encoding for .ok-files', file=sys.stderr) + print('ERROR: UTF-8 (unicode) should be used as sole encoding for .ok-files', file=sys.stderr) if args.verbose > 1: print('UnicodeDecodeError exception properties (error on: %s):' % err.object[err.start:err.end], file=sys.stderr) print('* encoding: %s' % err.encoding, file=sys.stderr) From 5785f061289a228854f1e6b3cb4349c3e1474fba Mon Sep 17 00:00:00 2001 From: Doeke Zanstra Date: Wed, 24 Jul 2019 21:28:28 +0200 Subject: [PATCH 5/6] Added some files that aid testing --- .ok | 2 +- test/.ok | 13 +++++++++++++ test/iso_latin_1/.ok | 7 +++++++ test/mac_os_roman/.ok | 2 ++ test/utf_8/.ok | 3 +++ 5 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 test/.ok create mode 100644 test/iso_latin_1/.ok create mode 100644 test/mac_os_roman/.ok create mode 100644 test/utf_8/.ok diff --git a/.ok b/.ok index ff0b528..f54c4d2 100644 --- a/.ok +++ b/.ok @@ -10,9 +10,9 @@ _OK_PROMPT="% "; _OK_VERBOSE=0; _OK_PROMPT_DEFAULT=0; _OK_COMMENT_ALIGN=0 # Show # Tests arguments passing (you can pass arguments after , both at the bash-prompt and the ok-prompt) echo "Passed arguments: 1:[$1], 2:[$2], 3:[$3], 4+:[${*:4}] (nr args: $#)" # Comment-color starts too early; clearly a bug (so better echo "All passed arguments (no comment on this line): [$*]" +cd test ok help --verbose # Show help page of 🆗, including environment variables -export PYTHONIOENCODING="UTF-8" # When set, ok doesn't accept nothing but UTF-8 files and will raise an error otherwise (python3). When not set, or python2, you get question marks set | grep "^_OK_" # Show all set environment variables, used with ok-bash echo -e "$PROMPT_COMMAND" # This variable can be changed with the reset/auto_show install helpers alias SSH='ok -v -f ~/.ssh/.ok -a SSH' # Keep a list of all your `ssh` connections; use it via `SSH` diff --git a/test/.ok b/test/.ok new file mode 100644 index 0000000..b5cf597 --- /dev/null +++ b/test/.ok @@ -0,0 +1,13 @@ +# Control what python to use +_OK__PATH_TO_PYTHON=$(command -v python) # set python2 +_OK__PATH_TO_PYTHON=$(command -v python3) # set python3 +echo "Path to python: '$_OK__PATH_TO_PYTHON'" # check path +$_OK__PATH_TO_PYTHON --version # check python version +# Control encoding or not +echo "Python IO Encoding: ${PYTHONIOENCODING:--=not-set=-}" +export PYTHONIOENCODING="UTF-8" # only python3 will choke on non-UTF-8, when this is set +unset PYTHONIOENCODING # nonetheless, python will treat everything as ASCII +# Go to the test cases +cd iso_latin_1 +cd mac_os_roman +cd utf_8 diff --git a/test/iso_latin_1/.ok b/test/iso_latin_1/.ok new file mode 100644 index 0000000..a3d9aba --- /dev/null +++ b/test/iso_latin_1/.ok @@ -0,0 +1,7 @@ +# This file is encoded with ISO-8859-1 (or Western ISO Latin 1) +echo "A0-AF: (including NO-BREAK SPACE and SOFT HYPHEN)" +echo "B0-BF: " +echo "C0-CF: " +echo "D0-DF: (look at that MULTIPLICATION SIGN)" +echo "E0-EF: " +echo "F0-FF: (the DIVISION SIGN is placed in the middle)" diff --git a/test/mac_os_roman/.ok b/test/mac_os_roman/.ok new file mode 100644 index 0000000..5847c79 --- /dev/null +++ b/test/mac_os_roman/.ok @@ -0,0 +1,2 @@ +# only one comment +echo 'H, r s gn bl p de TV!' diff --git a/test/utf_8/.ok b/test/utf_8/.ok new file mode 100644 index 0000000..e22b8ee --- /dev/null +++ b/test/utf_8/.ok @@ -0,0 +1,3 @@ +# A full Unicode UTF-8 encoded .ok-file +echo "Hi ㋛, how is it going?" +echo "Ⓘ ⓐ ⓜ ⓦ ⓔ ⓛ ⓛ , 🅣 🅗 🅐 🅽 🅚 🆈 🅾 🆄 ❕" From bb0f2dc995b0f1f230ddaffcbf9f535ead78a8e6 Mon Sep 17 00:00:00 2001 From: Doeke Zanstra Date: Wed, 24 Jul 2019 21:38:11 +0200 Subject: [PATCH 6/6] Added PYTHONIOENCODING per encoding --- test/iso_latin_1/.ok | 1 + test/mac_os_roman/.ok | 1 + test/utf_8/.ok | 1 + 3 files changed, 3 insertions(+) diff --git a/test/iso_latin_1/.ok b/test/iso_latin_1/.ok index a3d9aba..9b62380 100644 --- a/test/iso_latin_1/.ok +++ b/test/iso_latin_1/.ok @@ -1,4 +1,5 @@ # This file is encoded with ISO-8859-1 (or Western ISO Latin 1) +export PYTHONIOENCODING="latin_1" echo "A0-AF: (including NO-BREAK SPACE and SOFT HYPHEN)" echo "B0-BF: " echo "C0-CF: " diff --git a/test/mac_os_roman/.ok b/test/mac_os_roman/.ok index 5847c79..2f305b7 100644 --- a/test/mac_os_roman/.ok +++ b/test/mac_os_roman/.ok @@ -1,2 +1,3 @@ # only one comment +export PYTHONIOENCODING="mac_roman" echo 'H, r s gn bl p de TV!' diff --git a/test/utf_8/.ok b/test/utf_8/.ok index e22b8ee..91f1d3e 100644 --- a/test/utf_8/.ok +++ b/test/utf_8/.ok @@ -1,3 +1,4 @@ # A full Unicode UTF-8 encoded .ok-file +export PYTHONIOENCODING="UTF-8" echo "Hi ㋛, how is it going?" echo "Ⓘ ⓐ ⓜ ⓦ ⓔ ⓛ ⓛ , 🅣 🅗 🅐 🅽 🅚 🆈 🅾 🆄 ❕"