1
1
mirror of https://github.com/chubin/cheat.sh.git synced 2024-11-22 01:40:48 +03:00

python 3 updates

- sorted imports with isort
- removed inherits of object
 - no need to prefix strings with u
 - tidied whitespace/indents/newlines
This commit is contained in:
Mark Mayo 2022-11-22 17:25:34 +13:00
parent 571377f2f7
commit 3866041bba
38 changed files with 137 additions and 104 deletions

View File

@ -17,26 +17,26 @@ Configuration parameters:
from __future__ import print_function
import sys
if sys.version_info[0] < 3:
reload(sys)
sys.setdefaultencoding('utf8')
import sys
import logging
import os
import requests
import sys
import jinja2
from flask import Flask, request, send_from_directory, redirect, Response
import requests
from flask import Flask, Response, redirect, request, send_from_directory
sys.path.append(os.path.abspath(os.path.join(__file__, "..", "..", "lib")))
from cheat_wrapper import cheat_wrapper
from config import CONFIG
from limits import Limits
from cheat_wrapper import cheat_wrapper
from post import process_post_request
from options import parse_args
from stateful_queries import save_query, last_query
from post import process_post_request
from stateful_queries import last_query, save_query
if not os.path.exists(os.path.dirname(CONFIG["path.log.main"])):
os.makedirs(os.path.dirname(CONFIG["path.log.main"]))
@ -58,7 +58,7 @@ stderr_handler.setFormatter(logging.Formatter('%(filename)s:%(lineno)s: %(messag
# (https://github.com/pallets/werkzeug/issues/1969)
# resulting in duplicating lines. In that case we need root
# stderr handler to skip lines from werkzeug.
class SkipFlaskLogger(object):
class SkipFlaskLogger():
def filter(self, record):
if record.name != 'werkzeug':
return True

View File

@ -1,7 +1,8 @@
import sys
import redis
REDIS = redis.Redis(host='localhost', port=6379, db=0)
for key in sys.argv[1:]:
REDIS.delete(key)

View File

@ -2,14 +2,14 @@
from __future__ import print_function
from datetime import datetime
import os
from os import path
import re
import shutil
import subprocess
from subprocess import Popen
import sys
from datetime import datetime
from os import path
from subprocess import Popen
SHARE_DIR = path.join(path.dirname(__file__), "../share/")

View File

@ -5,13 +5,13 @@
from gevent.monkey import patch_all
from gevent.pywsgi import WSGIServer
patch_all()
import os
import sys
from app import app, CONFIG
from app import CONFIG, app
if '--debug' in sys.argv:
# Not all debug mode features are available under `gevent`

View File

@ -6,13 +6,13 @@ and make them available for import as
# pylint: disable=wildcard-import,relative-import
from os.path import dirname, basename, isfile, join
import glob
from os.path import basename, dirname, isfile, join
__all__ = [
basename(f)[:-3]
for f in glob.glob(join(dirname(__file__), "*.py"))
if isfile(f) and not f.endswith('__init__.py')]
from .adapter import all_adapters
from . import *
from .adapter import all_adapters

View File

@ -8,8 +8,10 @@ Configuration parameters:
import abc
import os
from six import with_metaclass
from config import CONFIG
from six import with_metaclass
class AdapterMC(type):
"""
@ -21,7 +23,7 @@ class AdapterMC(type):
return getattr(cls, '_class_repr')()
return super(AdapterMC, cls).__repr__()
class Adapter(with_metaclass(AdapterMC, object)):
class Adapter(with_metaclass(AdapterMC)):
"""
An abstract class, defines methods:

View File

@ -9,6 +9,7 @@ Each cheat sheet is a separate file without extension
from .git_adapter import GitRepositoryAdapter
class Cheat(GitRepositoryAdapter):
"""
cheat/cheat adapter

View File

@ -6,11 +6,12 @@ sheets covering programming languages are are located in subdirectories.
# pylint: disable=relative-import
import os
import glob
import os
from .git_adapter import GitRepositoryAdapter
def _remove_initial_underscore(filename):
if filename.startswith('_'):
filename = filename[1:]

View File

@ -5,7 +5,7 @@
import os.path
import re
from subprocess import Popen, PIPE
from subprocess import PIPE, Popen
from .adapter import Adapter

View File

@ -1,6 +1,5 @@
class Adapter(object):
class Adapter():
pass
class cheatAdapter(Adapter):
pass

View File

@ -5,7 +5,8 @@ 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)]

View File

@ -5,21 +5,22 @@ Configuration parameters:
path.internal.pages
"""
import sys
import os
import collections
import os
import sys
try:
from rapidfuzz import process, fuzz
from rapidfuzz import fuzz, process
_USING_FUZZYWUZZY=False
except ImportError:
from fuzzywuzzy import process, fuzz
from fuzzywuzzy import fuzz, process
_USING_FUZZYWUZZY=True
from config import CONFIG
from .adapter import Adapter
from fmt.internal import colorize_internal
from .adapter import Adapter
_INTERNAL_TOPICS = [
":cht.sh",
":bash_completion",

View File

@ -8,10 +8,12 @@ The adapter exposes one page ("latencies") and several its aliases
# pylint: disable=relative-import
import sys
import os
import sys
from .git_adapter import GitRepositoryAdapter
class Latenz(GitRepositoryAdapter):
"""

View File

@ -9,11 +9,15 @@ Configuration parameters:
# pylint: disable=relative-import
from __future__ import print_function
import os
import re
from config import CONFIG
from .git_adapter import GitRepositoryAdapter
class LearnXinY(GitRepositoryAdapter):
"""
@ -62,7 +66,7 @@ class LearnXinY(GitRepositoryAdapter):
return self.adapters[lang].is_valid(topic)
class LearnXYAdapter(object):
class LearnXYAdapter():
"""
Parent class of all languages adapters

View File

@ -10,13 +10,13 @@ from __future__ import print_function
import os
import re
from subprocess import Popen, PIPE
from polyglot.detect import Detector
from polyglot.detect.base import UnknownLanguage
from subprocess import PIPE, Popen
from config import CONFIG
from languages_data import SO_NAME
from polyglot.detect import Detector
from polyglot.detect.base import UnknownLanguage
from .upstream import UpstreamAdapter
NOT_FOUND_MESSAGE = """404 NOT FOUND

View File

@ -8,12 +8,14 @@ Exports:
# pylint: disable=relative-import
import os
import glob
import os
import yaml
from .git_adapter import GitRepositoryAdapter
from .cheat_sheets import CheatSheets
from .git_adapter import GitRepositoryAdapter
class Rosetta(GitRepositoryAdapter):

View File

@ -9,11 +9,12 @@ The pages are formatted with a markdown dialect
# pylint: disable=relative-import,abstract-method
import re
import os
import re
from .git_adapter import GitRepositoryAdapter
class Tldr(GitRepositoryAdapter):
"""

View File

@ -10,11 +10,13 @@ Configuration parameters:
# pylint: disable=relative-import
import textwrap
import requests
import requests
from config import CONFIG
from .adapter import Adapter
def _are_you_offline():
return textwrap.dedent(
"""

View File

@ -17,4 +17,3 @@ GITHUB_BUTTON_FOOTER = """
<!-- Place this tag right after the last button or just before your close body tag. -->
<script async defer id="github-bjs" src="https://buttons.github.io/buttons.js"></script>
"""

View File

@ -12,8 +12,9 @@ Configuration parameters:
cache.redis.port
"""
import os
import json
import os
from config import CONFIG
_REDIS = None

View File

@ -8,16 +8,16 @@ Exports:
cheat_wrapper()
"""
import re
import json
import re
import frontend.ansi
import frontend.html
import postprocessing
from languages_data import LANGUAGE_ALIAS, rewrite_editor_section_name
from routing import get_answers, get_topics_list
from search import find_answers_by_keyword
from languages_data import LANGUAGE_ALIAS, rewrite_editor_section_name
import postprocessing
import frontend.html
import frontend.ansi
def _add_section_name(query):
# temporary solution before we don't find a fixed one

View File

@ -42,9 +42,11 @@ specified by an environment variable is not an integer, it is ignored.
"""
from __future__ import print_function
import os
from pygments.styles import get_all_styles
#def get_all_styles():
# return []

View File

@ -12,17 +12,17 @@ Configuration parameters:
from __future__ import print_function
import sys
import logging
import os
import subprocess
import sys
import textwrap
from globals import fatal
import adapter
import cache
from config import CONFIG
from globals import fatal
def _log(*message):
logging.info(*message)

View File

@ -20,18 +20,18 @@ Configuration parameters:
from __future__ import print_function
import sys
import os
import textwrap
import hashlib
import os
import re
from itertools import groupby, chain
import sys
import textwrap
from itertools import chain, groupby
from subprocess import Popen
from tempfile import NamedTemporaryFile
import cache
from config import CONFIG
from languages_data import VIM_NAME
import cache
FNULL = open(os.devnull, 'w')
TEXT = 0

View File

@ -5,8 +5,8 @@ Will be merged with panela later.
import re
from colorama import Fore, Back, Style
import colored
from colorama import Back, Fore, Style
PALETTES = {
0: {

View File

@ -8,9 +8,11 @@ Uses external pygments formatters for highlighting (passed as an argument).
"""
import re
import ansiwrap
import colored
def format_text(text, config=None, highlighter=None):
"""
Renders `text` according to markdown rules.

View File

@ -23,19 +23,21 @@ Configuration parameters:
"""
import os
import sys
import re
import sys
import colored
from pygments import highlight as pygments_highlight
from pygments.formatters import Terminal256Formatter # pylint: disable=no-name-in-module
from pygments.formatters import \
Terminal256Formatter # pylint: disable=no-name-in-module
# pylint: disable=wrong-import-position
sys.path.append(os.path.abspath(os.path.join(__file__, '..')))
from config import CONFIG
import languages_data # pylint: enable=wrong-import-position
import fmt.internal
import fmt.comments
import fmt.internal
import languages_data # pylint: enable=wrong-import-position
from config import CONFIG
def visualize(answer_data, request_options):
"""

View File

@ -5,19 +5,19 @@ Configuration parameters:
path.internal.ansi2html
"""
import sys
import os
import re
from subprocess import Popen, PIPE
import sys
from subprocess import PIPE, Popen
MYDIR = os.path.abspath(os.path.join(__file__, '..', '..'))
sys.path.append("%s/lib/" % MYDIR)
import frontend.ansi
from buttons import GITHUB_BUTTON, GITHUB_BUTTON_FOOTER, TWITTER_BUTTON
# pylint: disable=wrong-import-position
from config import CONFIG
from globals import error
from buttons import TWITTER_BUTTON, GITHUB_BUTTON, GITHUB_BUTTON_FOOTER
import frontend.ansi
# temporary having it here, but actually we have the same data
# in the adapter module

View File

@ -6,8 +6,9 @@ For the configuration related things see `config.py`
from __future__ import print_function
import sys
import logging
import sys
def fatal(text):
"""

View File

@ -17,6 +17,7 @@ Usage:
"""
import time
from globals import log
_WHITELIST = ['5.9.243.177']
@ -28,7 +29,7 @@ def _time_caps(minutes, hours, days):
'day': days,
}
class Limits(object):
class Limits():
"""
Queries limitation (by IP).

View File

@ -1,5 +1,5 @@
import os
import json
import os
COLORS_JSON = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'colors.json')
COLOR_TABLE = json.loads(open(COLORS_JSON, 'r').read())
@ -7,19 +7,17 @@ VALID_COLORS = [x['hexString'] for x in COLOR_TABLE]
HEX_TO_ANSI = {x['hexString']:x['colorId'] for x in COLOR_TABLE}
def rgb_from_str(s):
# s starts with a #.
r, g, b = int(s[1:3],16), int(s[3:5], 16),int(s[5:7], 16)
return r, g, b
# s starts with a #.
r, g, b = int(s[1:3],16), int(s[3:5], 16),int(s[5:7], 16)
return r, g, b
def find_nearest_color(hex_color):
def find_nearest_color(hex_color):
R, G, B = rgb_from_str(hex_color)
mindiff = None
for d in VALID_COLORS:
r, g, b = rgb_from_str(d)
diff = abs(R -r)*256 + abs(G-g)* 256 + abs(B- b)* 256
if mindiff is None or diff < mindiff:
mindiff = diff
mincolorname = d
return mincolorname
for d in VALID_COLORS:
r, g, b = rgb_from_str(d)
diff = abs(R -r)*256 + abs(G-g)* 256 + abs(B- b)* 256
if mindiff is None or diff < mindiff:
mindiff = diff
mincolorname = d
return mincolorname

View File

@ -1,9 +1,10 @@
# vim: encoding=utf-8
import itertools
import os
import sys
import colored
import itertools
from globals import MYDIR
"""
@ -18,9 +19,9 @@ TODO:
"""
from wcwidth import wcswidth
from colors import find_nearest_color, HEX_TO_ANSI, rgb_from_str
import pyte
from colors import HEX_TO_ANSI, find_nearest_color, rgb_from_str
from wcwidth import wcswidth
# http://stackoverflow.com/questions/19782975/convert-rgb-color-to-the-nearest-color-in-palette-web-safe-color
@ -35,7 +36,7 @@ def color_mapping(clr):
return None
return clr
class Point(object):
class Point():
"""
One point (character) on a terminal
"""
@ -452,9 +453,9 @@ class Panela:
"""
frame_chars = {
'ascii': u'++++-|',
'single': u'┌┐└┘─│',
'double': u'┌┐└┘─│',
'ascii': '++++-|',
'single': '┌┐└┘─│',
'double': '┌┐└┘─│',
}
if frame in frame_chars:
chars = frame_chars[frame]
@ -493,16 +494,16 @@ class Panela:
self.put_point(x0, y0 - radius, char=char, color=color, background=background)
self.put_point(x0 + k(radius), y0, char=char, color=color, background=background)
self.put_point(x0 - k(radius), y0, char=char, color=color, background=background)
char = "x"
while x < y:
if f >= 0:
if f >= 0:
y -= 1
ddf_y += 2
f += ddf_y
x += 1
ddf_x += 2
f += ddf_x
f += ddf_x
self.put_point(x0 + k(x), y0 + y, char=char, color=color, background=background)
self.put_point(x0 - k(x), y0 + y, char=char, color=color, background=background)
self.put_point(x0 + k(x), y0 - y, char=char, color=color, background=background)
@ -558,7 +559,7 @@ class Panela:
########################################################################################################
class Template(object):
class Template():
def __init__(self):
self._mode = 'page'
self.page = []

View File

@ -7,11 +7,13 @@ Configuration parameters:
path.spool
"""
import string
import os
import random
import string
from config import CONFIG
def _save_cheatsheet(topic_name, cheatsheet):
"""
Save posted cheat sheet `cheatsheet` with `topic_name`

View File

@ -1,5 +1,6 @@
import search
import fmt.comments
import search
def postprocess(answer, keyword, options, request_options=None):
answer = _answer_add_comments(answer, request_options=request_options)

View File

@ -11,7 +11,6 @@ import random
import re
from typing import Any, Dict, List
import cache
import adapter.cheat_sheets
import adapter.cmd
import adapter.internal
@ -19,9 +18,11 @@ import adapter.latenz
import adapter.learnxiny
import adapter.question
import adapter.rosetta
import cache
from config import CONFIG
class Router(object):
class Router():
"""
Implementation of query routing. Routing is based on `routing_table`
@ -133,24 +134,24 @@ class Router(object):
#Here we still check that cleaned_topic_list in not empty
if not cleaned_topic_list:
return prefix
random_topic = random.choice(cleaned_topic_list)
return prefix + random_topic
if topic.endswith('/:random') or topic.lstrip('/') == ':random':
#We strip the :random part and see if the query is valid by running a get_topics_list()
if topic.lstrip('/') == ':random' :
topic = topic.lstrip('/')
topic = topic.lstrip('/')
prefix = topic[:-7]
topic_list = [x[len(prefix):]
for x in self.get_topics_list()
if x.startswith(prefix)]
if '' in topic_list:
if '' in topic_list:
topic_list.remove('')
if topic_list:
if topic_list:
# This is a correct formatted random query like /cpp/:random as the topic_list is not empty.
random_topic = __select_random_topic(prefix, topic_list)
return random_topic
@ -162,7 +163,7 @@ class Router(object):
#Here if not a random requst, we just forward the topic
return topic
def get_answers(self, topic: str, request_options:Dict[str, str] = None) -> List[Dict[str, Any]]:
"""
Find cheat sheets for the topic.
@ -173,7 +174,7 @@ class Router(object):
Returns:
[answer_dict]: list of answers (dictionaries)
"""
# if topic specified as <topic_type>:<topic>,
# cut <topic_type> off
topic_type = ""

View File

@ -24,6 +24,7 @@ import re
from config import CONFIG
from routing import get_answers, get_topics_list
def _limited_entry():
return {
'topic_type': 'LIMITED',

View File

@ -6,17 +6,20 @@ from __future__ import print_function
import sys
import textwrap
try:
import urlparse
except ModuleNotFoundError:
import urllib.parse as urlparse
import config
config.CONFIG["cache.type"] = "none"
import cheat_wrapper
import options
def show_usage():
"""
Show how to use the program in the standalone mode

View File

@ -4,6 +4,7 @@ Support for the stateful queries
import cache
def save_query(client_id, query):
"""
Save the last query `query` for the client `client_id`