From adcac9bfebc17036c765eb1e35b40addd3abcba4 Mon Sep 17 00:00:00 2001 From: Igor Chubin Date: Sat, 11 May 2019 18:26:10 +0200 Subject: [PATCH] support for python3 --- lib/adapter/__init__.py | 2 +- lib/adapter/cheat_cheat.py | 2 +- lib/adapter/cheat_sheets.py | 4 ++-- lib/adapter/cmd.py | 3 +-- lib/adapter/git_adapter.py | 4 ++-- lib/adapter/internal.py | 2 +- lib/adapter/latenz.py | 4 ++-- lib/adapter/learnxiny.py | 4 ++-- lib/adapter/question.py | 3 +-- lib/adapter/rosetta.py | 4 ++-- lib/adapter/tldr.py | 2 +- lib/adapter/upstream.py | 2 +- lib/cache.py | 2 +- lib/config.py | 10 ++++++---- lib/fmt/comments.py | 1 - lib/frontend/html.py | 1 - lib/standalone.py | 16 +++++++++++----- requirements.txt | 1 + share/cht.sh.txt | 2 +- tests/tests.txt | 3 ++- 20 files changed, 39 insertions(+), 33 deletions(-) diff --git a/lib/adapter/__init__.py b/lib/adapter/__init__.py index de0c3eb..50d30cd 100644 --- a/lib/adapter/__init__.py +++ b/lib/adapter/__init__.py @@ -14,5 +14,5 @@ __all__ = [ for f in glob.glob(join(dirname(__file__), "*.py")) if isfile(f) and not f.endswith('__init__.py')] -from adapter import all_adapters +from .adapter import all_adapters from . import * diff --git a/lib/adapter/cheat_cheat.py b/lib/adapter/cheat_cheat.py index 6db5638..9844008 100644 --- a/lib/adapter/cheat_cheat.py +++ b/lib/adapter/cheat_cheat.py @@ -7,7 +7,7 @@ Each cheat sheet is a separate file without extension # pylint: disable=relative-import,abstract-method -from git_adapter import GitRepositoryAdapter +from .git_adapter import GitRepositoryAdapter class Cheat(GitRepositoryAdapter): """ diff --git a/lib/adapter/cheat_sheets.py b/lib/adapter/cheat_sheets.py index 3370829..01666a2 100644 --- a/lib/adapter/cheat_sheets.py +++ b/lib/adapter/cheat_sheets.py @@ -9,7 +9,7 @@ sheets covering programming languages are are located in subdirectories. import os import glob -from git_adapter import GitRepositoryAdapter +from .git_adapter import GitRepositoryAdapter def _remove_initial_underscore(filename): if filename.startswith('_'): @@ -84,7 +84,7 @@ class CheatSheets(GitRepositoryAdapter): # though it should not happen answer = "%s:%s not found" % (str(self.__class__), topic) - return answer.decode('utf-8') + return answer class CheatSheetsDir(CheatSheets): diff --git a/lib/adapter/cmd.py b/lib/adapter/cmd.py index 1c4cef7..de6b46a 100644 --- a/lib/adapter/cmd.py +++ b/lib/adapter/cmd.py @@ -5,9 +5,8 @@ from gevent.monkey import patch_all from gevent.subprocess import Popen, PIPE -patch_all() -from adapter import Adapter +from .adapter import Adapter class CommandAdapter(Adapter): """ diff --git a/lib/adapter/git_adapter.py b/lib/adapter/git_adapter.py index bfcca45..c285069 100644 --- a/lib/adapter/git_adapter.py +++ b/lib/adapter/git_adapter.py @@ -5,7 +5,7 @@ Implementation of `GitRepositoryAdapter`, adapter that is used to handle git rep import glob import os -from adapter import Adapter # pylint: disable=relative-import +from .adapter import Adapter # pylint: disable=relative-import def _get_filenames(path): return [os.path.split(topic)[1] for topic in glob.glob(path)] @@ -49,7 +49,7 @@ class RepositoryAdapter(Adapter): # though it should not happen answer = "%s:%s not found" % (str(self.__class__), topic) - return answer.decode('utf-8') + return answer class GitRepositoryAdapter(RepositoryAdapter): #pylint: disable=abstract-method diff --git a/lib/adapter/internal.py b/lib/adapter/internal.py index 735e796..baaa28e 100644 --- a/lib/adapter/internal.py +++ b/lib/adapter/internal.py @@ -12,7 +12,7 @@ import collections from fuzzywuzzy import process, fuzz from config import CONFIG -from adapter import Adapter +from .adapter import Adapter from fmt.internal import colorize_internal _INTERNAL_TOPICS = [ diff --git a/lib/adapter/latenz.py b/lib/adapter/latenz.py index 7ac6f33..4b73ee3 100644 --- a/lib/adapter/latenz.py +++ b/lib/adapter/latenz.py @@ -10,7 +10,7 @@ The adapter exposes one page ("latencies") and several its aliases import sys import os -from git_adapter import GitRepositoryAdapter +from .git_adapter import GitRepositoryAdapter class Latenz(GitRepositoryAdapter): @@ -25,7 +25,7 @@ class Latenz(GitRepositoryAdapter): def _get_page(self, topic, request_options=None): sys.path.append(os.path.join(self.local_repository_location(), 'bin')) import latencies - return latencies.render().decode("utf-8") + return latencies.render() def _get_list(self, prefix=None): return ['latencies'] diff --git a/lib/adapter/learnxiny.py b/lib/adapter/learnxiny.py index 0ced295..73dbb5e 100644 --- a/lib/adapter/learnxiny.py +++ b/lib/adapter/learnxiny.py @@ -12,7 +12,7 @@ from __future__ import print_function import os import re from config import CONFIG -from git_adapter import GitRepositoryAdapter +from .git_adapter import GitRepositoryAdapter class LearnXinY(GitRepositoryAdapter): @@ -37,7 +37,7 @@ class LearnXinY(GitRepositoryAdapter): lang, topic = topic.split('/', 1) if lang not in self.adapters: return '' - return self.adapters[lang].get_page(topic).decode("utf-8") + return self.adapters[lang].get_page(topic) def _get_list(self, prefix=None): """ diff --git a/lib/adapter/question.py b/lib/adapter/question.py index 3f88a5b..99abc8c 100644 --- a/lib/adapter/question.py +++ b/lib/adapter/question.py @@ -10,7 +10,6 @@ from __future__ import print_function from gevent.monkey import patch_all from gevent.subprocess import Popen, PIPE -patch_all() import os import re @@ -19,8 +18,8 @@ from polyglot.detect import Detector from polyglot.detect.base import UnknownLanguage from config import CONFIG -from upstream import UpstreamAdapter from languages_data import SO_NAME +from .upstream import UpstreamAdapter class Question(UpstreamAdapter): diff --git a/lib/adapter/rosetta.py b/lib/adapter/rosetta.py index 5808b04..2ed23b4 100644 --- a/lib/adapter/rosetta.py +++ b/lib/adapter/rosetta.py @@ -12,8 +12,8 @@ import os import glob import yaml -from git_adapter import GitRepositoryAdapter -from cheat_sheets import CheatSheets +from .git_adapter import GitRepositoryAdapter +from .cheat_sheets import CheatSheets class Rosetta(GitRepositoryAdapter): diff --git a/lib/adapter/tldr.py b/lib/adapter/tldr.py index 7e15a26..e0b397b 100644 --- a/lib/adapter/tldr.py +++ b/lib/adapter/tldr.py @@ -12,7 +12,7 @@ The pages are formatted with a markdown dialect import re import os -from git_adapter import GitRepositoryAdapter +from .git_adapter import GitRepositoryAdapter class Tldr(GitRepositoryAdapter): diff --git a/lib/adapter/upstream.py b/lib/adapter/upstream.py index 3cf65ea..7622b76 100644 --- a/lib/adapter/upstream.py +++ b/lib/adapter/upstream.py @@ -13,7 +13,7 @@ import textwrap import requests from config import CONFIG -from adapter import Adapter +from .adapter import Adapter def _are_you_offline(): return textwrap.dedent( diff --git a/lib/cache.py b/lib/cache.py index 1cc0323..5f55276 100644 --- a/lib/cache.py +++ b/lib/cache.py @@ -14,11 +14,11 @@ Configuration parameters: import os import json -import redis from config import CONFIG _REDIS = None if CONFIG['cache.type'] == 'redis': + import redis _REDIS = redis.StrictRedis( host=CONFIG['cache.redis.host'], port=CONFIG['cache.redis.port'], diff --git a/lib/config.py b/lib/config.py index 62f47f2..5732c18 100644 --- a/lib/config.py +++ b/lib/config.py @@ -44,8 +44,9 @@ specified by an environment variable is not an integer, it is ignored. from __future__ import print_function import os -import yaml -from pygments.styles import get_all_styles +# from pygments.styles import get_all_styles +def get_all_styles(): + return [] _ENV_VAR_PREFIX = "CHEATSH" @@ -221,6 +222,7 @@ def _get_nested(data, key): return None def _load_config_from_file(default_config, filename): + import yaml update = {} if not os.path.exists(filename): @@ -244,8 +246,8 @@ def _load_config_from_file(default_config, filename): CONFIG = Config() CONFIG.update(_CONFIG) -CONFIG.update(_load_config_from_file(_CONFIG, _CONF_FILE_MYDIR)) -CONFIG.update(_load_config_from_file(_CONFIG, _CONF_FILE_WORKDIR)) +# CONFIG.update(_load_config_from_file(_CONFIG, _CONF_FILE_MYDIR)) +# CONFIG.update(_load_config_from_file(_CONFIG, _CONF_FILE_WORKDIR)) CONFIG.update(_load_config_from_environ(_CONFIG)) if __name__ == "__main__": diff --git a/lib/fmt/comments.py b/lib/fmt/comments.py index 211ddce..a741113 100644 --- a/lib/fmt/comments.py +++ b/lib/fmt/comments.py @@ -24,7 +24,6 @@ from __future__ import print_function from gevent.monkey import patch_all from gevent.subprocess import Popen -patch_all() import sys import os diff --git a/lib/frontend/html.py b/lib/frontend/html.py index d928228..c639e04 100644 --- a/lib/frontend/html.py +++ b/lib/frontend/html.py @@ -7,7 +7,6 @@ Configuration parameters: from gevent.monkey import patch_all from gevent.subprocess import Popen, PIPE -patch_all() # pylint: disable=wrong-import-position,wrong-import-order import sys diff --git a/lib/standalone.py b/lib/standalone.py index 3553f0a..97b6b66 100644 --- a/lib/standalone.py +++ b/lib/standalone.py @@ -2,11 +2,18 @@ Standalone wrapper for the cheat.sh server. """ +from __future__ import print_function + import sys import textwrap -import urlparse +try: + import urlparse +except ModuleNotFoundError: + import urllib.parse as urlparse import config +config.CONFIG["cache.type"] = "none" + import cheat_wrapper import options @@ -15,13 +22,13 @@ def show_usage(): Show how to use the program in the standalone mode """ - print textwrap.dedent(""" + print(textwrap.dedent(""" Usage: lib/standalone.py [OPTIONS] QUERY For OPTIONS see :help - """)[1:-1] + """)[1:-1]) def parse_cmdline(args): """ @@ -50,10 +57,9 @@ def main(args): standalone wrapper for cheat_wrapper() """ - config.CONFIG["cache.type"] = "none" query, request_options = parse_cmdline(args) answer, _ = cheat_wrapper.cheat_wrapper(query, request_options=request_options) - sys.stdout.write(answer.encode("utf-8")) + sys.stdout.write(answer) if __name__ == '__main__': main(sys.argv[1:]) diff --git a/requirements.txt b/requirements.txt index 22f6433..3a4d172 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ +wheel gevent flask requests diff --git a/share/cht.sh.txt b/share/cht.sh.txt index 338dca5..8ecf98e 100755 --- a/share/cht.sh.txt +++ b/share/cht.sh.txt @@ -380,7 +380,7 @@ if [ "$CHTSH_MODE" = auto ] && [ -d "$CHEATSH_INSTALLATION" ]; then shift $((OPTIND - 1)) local url; url="$1"; shift - "$CHEATSH_INSTALLATION/ve/bin/python" "$CHEATSH_INSTALLATION/lib/standalone.py" "${url#"$CHTSH_URL"}" "$@" + PYTHONIOENCODING=UTF-8 "$CHEATSH_INSTALLATION/ve/bin/python" "$CHEATSH_INSTALLATION/lib/standalone.py" "${url#"$CHTSH_URL"}" "$@" } elif [ "$(uname -s)" = OpenBSD ] && [ -x /usr/bin/ftp ]; then # any better test not involving either OS matching or actual query? diff --git a/tests/tests.txt b/tests/tests.txt index 7557510..86bcc52 100644 --- a/tests/tests.txt +++ b/tests/tests.txt @@ -13,7 +13,7 @@ python/copy+file?QT # [online] / // python/:learn -latencies +latencies # [python3] az # chubin/cheat.sheets python/rosetta/Substring # rosetta python/rosetta/Substring?T # rosetta @@ -22,3 +22,4 @@ js/:learn # short names check javascript/:learn # short names check emacs:go-mode/:list # special editor names mkffs.ffatt # unknown +latencies # [python2]