chubin / chubin/cheat.sh

Use pure Python ansi2html

Open
#222 2 comments 0 reactions 0 assignees View on GitHub
Dominant language
Python
Stars
41.7k
Forks
1.9k
PR merge metrics
No merged PRs in 30d

Description

I noticed that web server runs `bash` and `gawk` as a subprocess to present text info as HTML.

Traceback

```python
✗ podman run -it --rm -p 8002 cheat.sh bin/srv.py --debug
Starting server on 0.0.0.0:8002
Traceback (most recent call last):
File "/usr/lib/python3.8/site-packages/gevent/pywsgi.py", line 968, in handle_one_response
self.run_application()
File "/usr/lib/python3.8/site-packages/gevent/pywsgi.py", line 915, in run_application
self.result = self.application(self.environ, self.start_response)
File "/usr/lib/python3.8/site-packages/flask/app.py", line 2464, in __call__
return self.wsgi_app(environ, start_response)
File "/usr/lib/python3.8/site-packages/flask/app.py", line 2450, in wsgi_app
response = self.handle_exception(e)
File "/usr/lib/python3.8/site-packages/flask/app.py", line 1867, in handle_exception
reraise(exc_type, exc_value, tb)
File "/usr/lib/python3.8/site-packages/flask/_compat.py", line 39, in reraise
raise value
File "/usr/lib/python3.8/site-packages/flask/app.py", line 2447, in wsgi_app
response = self.full_dispatch_request()
File "/usr/lib/python3.8/site-packages/flask/app.py", line 1952, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/usr/lib/python3.8/site-packages/flask/app.py", line 1821, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/usr/lib/python3.8/site-packages/flask/_compat.py", line 39, in reraise
raise value
File "/usr/lib/python3.8/site-packages/flask/app.py", line 1950, in full_dispatch_request
rv = self.dispatch_request()
File "/usr/lib/python3.8/site-packages/flask/app.py", line 1936, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "bin/srv.py", line 267, in answer
result, found = cheat_wrapper(topic, request_options=options, output_format=output_format)
File "/app/lib/cheat_wrapper.py", line 117, in cheat_wrapper
return frontend.html.visualize(answer_data, request_options)
File "/app/lib/frontend/html.py", line 50, in visualize
return _render_html(query, result, editable, repository_button, topics_list, request_options), found
File "/app/lib/frontend/html.py", line 89, in _render_html
result = _html_wrapper(result)
File "/app/lib/frontend/html.py", line 78, in _html_wrapper
proc = Popen(
File "/usr/lib/python3.8/site-packages/gevent/subprocess.py", line 745, in __init__
self._execute_child(args, executable, preexec_fn, close_fds,
File "/usr/lib/python3.8/site-packages/gevent/subprocess.py", line 1637, in _execute_child
raise child_exception
FileNotFoundError: [Errno 2] No such file or directory: 'bash'
2020-07-28T20:07:46Z {'REMOTE_ADDR': '127.0.0.1', 'REMOTE_PORT': '49666', 'HTTP_HOST': 'localhost:8002', (hidden keys: 26)} failed with FileNotFoundError
```

It should be possible to replace https://github.com/chubin/cheat.sh/blob/master/share/ansi2html.sh with pure Python alternative https://pypi.org/project/ansi2html/

Contributor guide

Open the contributing guide

Research direction

Start with _html_wrapper in lib/frontend/html.py and the existing share/ansi2html.sh implementation; the traceback shows this path is reached from bin/srv.py. Compare the current output with the ansi2html Python package, then exercise the web-server rendering path and confirm it works without the bash and gawk subprocesses.

Written by the indexing model from the issue text.

Assessment

Tech stack
awk, bash, python
Domain
frontend
Issue type
Refactor
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.