Skip to content

Commit

Permalink
Merge branch 'master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
magmax authored Feb 28, 2021
2 parents 205cb35 + 3317549 commit af7a57c
Show file tree
Hide file tree
Showing 13 changed files with 244 additions and 155 deletions.
2 changes: 2 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[flake8]
max-line-length=119
31 changes: 31 additions & 0 deletions .github/workflows/python-publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# This workflow will upload a Python Package using Twine when a release is created
# For more information see: https://help.github.com/en/actions/language-and-framework-guides/using-python-with-github-actions#publishing-to-package-registries

name: Upload Python Package

on:
release:
types: [created]

jobs:
deploy:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: '3.x'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install setuptools wheel twine
- name: Build and publish
env:
TWINE_USERNAME: ${{ secrets.PYPI_USERNAME }}
TWINE_PASSWORD: ${{ secrets.PYPI_PASSWORD }}
run: |
python setup.py sdist bdist_wheel
twine upload dist/*
38 changes: 38 additions & 0 deletions .github/workflows/python-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# This workflow will install Python dependencies, run tests and lint with a variety of Python versions
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-python-with-github-actions

name: Python package

on:
push:
branches: [ master ]
pull_request:
branches: [ master ]

jobs:
build:

runs-on: ubuntu-latest
strategy:
matrix:
python-version: [2.7, 3.4, 3.5, 3.6, 3.7, 3.8, 3.9]

steps:
- uses: actions/checkout@v2
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
python -m pip install --upgrade pip
python -m pip install -r requirements-test.txt
- name: Lint with flake8
run: |
# stop the build if there are Python syntax errors or undefined names
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
- name: Test with pytest
run: |
pytest
17 changes: 17 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
repos:
- repo: https://github.com/PyCQA/isort.git
rev: 5.7.0
hooks:
- id: isort
- repo: https://github.com/psf/black
rev: 20.8b1
hooks:
- id: black
language_version: python3
args:
- -l
- "119"
- repo: https://gitlab.com/pycqa/flake8
rev: 6de8252c035844f1e679f509b5f37340b44d5c39
hooks:
- id: flake8
4 changes: 1 addition & 3 deletions readchar/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
from .readchar import readchar, readkey
from . import key
from .readchar import readchar, readkey

__all__ = [readchar, readkey, key]

__version__ = '2.0.1'
104 changes: 52 additions & 52 deletions readchar/key.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# common
LF = '\x0d'
CR = '\x0a'
ENTER = '\x0d'
BACKSPACE = '\x7f'
SUPR = ''
SPACE = '\x20'
ESC = '\x1b'
LF = "\x0d"
CR = "\x0a"
ENTER = "\x0d"
BACKSPACE = "\x7f"
SUPR = ""
SPACE = "\x20"
ESC = "\x1b"

# CTRL
CTRL_A = '\x01'
Expand Down Expand Up @@ -36,63 +36,63 @@
CTRL_Z = '\x1a'

# ALT
ALT_A = '\x1b\x61'
ALT_A = "\x1b\x61"

# CTRL + ALT
CTRL_ALT_A = '\x1b\x01'
CTRL_ALT_A = "\x1b\x01"

# cursors
UP = '\x1b\x5b\x41'
DOWN = '\x1b\x5b\x42'
LEFT = '\x1b\x5b\x44'
RIGHT = '\x1b\x5b\x43'
UP = "\x1b\x5b\x41"
DOWN = "\x1b\x5b\x42"
LEFT = "\x1b\x5b\x44"
RIGHT = "\x1b\x5b\x43"

CTRL_ALT_SUPR = '\x1b\x5b\x33\x5e'
CTRL_ALT_SUPR = "\x1b\x5b\x33\x5e"

# other
F1 = '\x1b\x4f\x50'
F2 = '\x1b\x4f\x51'
F3 = '\x1b\x4f\x52'
F4 = '\x1b\x4f\x53'
F5 = '\x1b\x4f\x31\x35\x7e'
F6 = '\x1b\x4f\x31\x37\x7e'
F7 = '\x1b\x4f\x31\x38\x7e'
F8 = '\x1b\x4f\x31\x39\x7e'
F9 = '\x1b\x4f\x32\x30\x7e'
F10 = '\x1b\x4f\x32\x31\x7e'
F11 = '\x1b\x4f\x32\x33\x7e'
F12 = '\x1b\x4f\x32\x34\x7e'
F1 = "\x1b\x4f\x50"
F2 = "\x1b\x4f\x51"
F3 = "\x1b\x4f\x52"
F4 = "\x1b\x4f\x53"
F5 = "\x1b\x4f\x31\x35\x7e"
F6 = "\x1b\x4f\x31\x37\x7e"
F7 = "\x1b\x4f\x31\x38\x7e"
F8 = "\x1b\x4f\x31\x39\x7e"
F9 = "\x1b\x4f\x32\x30\x7e"
F10 = "\x1b\x4f\x32\x31\x7e"
F11 = "\x1b\x4f\x32\x33\x7e"
F12 = "\x1b\x4f\x32\x34\x7e"

PAGE_UP = '\x1b\x5b\x35\x7e'
PAGE_DOWN = '\x1b\x5b\x36\x7e'
HOME = '\x1b\x5b\x48'
END = '\x1b\x5b\x46'
PAGE_UP = "\x1b\x5b\x35\x7e"
PAGE_DOWN = "\x1b\x5b\x36\x7e"
HOME = "\x1b\x5b\x48"
END = "\x1b\x5b\x46"

INSERT = '\x1b\x5b\x32\x7e'
SUPR = '\x1b\x5b\x33\x7e'
INSERT = "\x1b\x5b\x32\x7e"
SUPR = "\x1b\x5b\x33\x7e"


ESCAPE_SEQUENCES = (
ESC,
ESC + '\x5b',
ESC + '\x5b' + '\x31',
ESC + '\x5b' + '\x32',
ESC + '\x5b' + '\x33',
ESC + '\x5b' + '\x35',
ESC + '\x5b' + '\x36',
ESC + '\x5b' + '\x31' + '\x35',
ESC + '\x5b' + '\x31' + '\x36',
ESC + '\x5b' + '\x31' + '\x37',
ESC + '\x5b' + '\x31' + '\x38',
ESC + '\x5b' + '\x31' + '\x39',
ESC + '\x5b' + '\x32' + '\x30',
ESC + '\x5b' + '\x32' + '\x31',
ESC + '\x5b' + '\x32' + '\x32',
ESC + '\x5b' + '\x32' + '\x33',
ESC + '\x5b' + '\x32' + '\x34',
ESC + '\x4f',
ESC + "\x5b",
ESC + "\x5b" + "\x31",
ESC + "\x5b" + "\x32",
ESC + "\x5b" + "\x33",
ESC + "\x5b" + "\x35",
ESC + "\x5b" + "\x36",
ESC + "\x5b" + "\x31" + "\x35",
ESC + "\x5b" + "\x31" + "\x36",
ESC + "\x5b" + "\x31" + "\x37",
ESC + "\x5b" + "\x31" + "\x38",
ESC + "\x5b" + "\x31" + "\x39",
ESC + "\x5b" + "\x32" + "\x30",
ESC + "\x5b" + "\x32" + "\x31",
ESC + "\x5b" + "\x32" + "\x32",
ESC + "\x5b" + "\x32" + "\x33",
ESC + "\x5b" + "\x32" + "\x34",
ESC + "\x4f",
ESC + ESC,
ESC + ESC + '\x5b',
ESC + ESC + '\x5b' + '\x32',
ESC + ESC + '\x5b' + '\x33',
ESC + ESC + "\x5b",
ESC + ESC + "\x5b" + "\x32",
ESC + ESC + "\x5b" + "\x33",
)
27 changes: 13 additions & 14 deletions readchar/readchar.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@
# So real authors are DannyYoo and company.
import sys


if sys.platform.startswith('linux'):
if sys.platform.startswith("linux"):
from .readchar_linux import readchar
elif sys.platform == 'darwin':
elif sys.platform == "darwin":
from .readchar_linux import readchar
elif sys.platform in ('win32', 'cygwin'):
elif sys.platform in ("win32", "cygwin"):
import msvcrt
from .readchar_windows import readchar

from . import key
from .readchar_windows import readchar
else:
raise NotImplemented('The platform %s is not supported yet' % sys.platform)
raise NotImplementedError("The platform %s is not supported yet" % sys.platform)


if sys.platform in ('win32', 'cygwin'):
if sys.platform in ("win32", "cygwin"):
#
# Windows uses scan codes for extended characters. The ordinal returned is
# 256 * the scan code. This dictionary translates scan codes to the
Expand All @@ -42,21 +42,17 @@
17408: key.F10,
22272: key.F11,
34528: key.F12,

7680: key.ALT_A,

# don't have table entries for...
# CTRL_ALT_A, # Ctrl-Alt-A, etc.
# CTRL_ALT_SUPR,
# CTRL-F1

21216: key.INSERT,
21472: key.SUPR, # key.py uses SUPR, not DELETE
21472: key.SUPR, # key.py uses SUPR, not DELETE
18912: key.PAGE_UP,
20960: key.PAGE_DOWN,
18400: key.HOME,
20448: key.END,

18656: key.UP,
20704: key.DOWN,
19424: key.LEFT,
Expand All @@ -82,14 +78,17 @@ def readkey(getchar_fn=None):
return x
else:
return ch.decode()


else:

def readkey(getchar_fn=None):
getchar = getchar_fn or readchar
c1 = getchar()
if ord(c1) != 0x1b:
if ord(c1) != 0x1B:
return c1
c2 = getchar()
if ord(c2) != 0x5b:
if ord(c2) != 0x5B:
return c1 + c2
c3 = getchar()
if ord(c3) != 0x33:
Expand Down
4 changes: 2 additions & 2 deletions readchar/readchar_linux.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
# http://code.activestate.com/recipes/134892/
# Thanks to Danny Yoo
import sys
import tty
import termios
import tty


def readchar():
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
try:
tty.setraw(sys.stdin.fileno())
tty.setcbreak(sys.stdin.fileno())
ch = sys.stdin.read(1)
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
Expand Down
11 changes: 3 additions & 8 deletions readchar/readchar_windows.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@
import msvcrt
import sys

win_encoding = "mbcs"

win_encoding = 'mbcs'


XE0_OR_00 = '\x00\xe0'
XE0_OR_00 = "\x00\xe0"


def readchar(blocking=False):
Expand All @@ -25,8 +24,4 @@ def readchar(blocking=False):
msvcrt.getch()
ch = msvcrt.getch()

return (
ch
if sys.version_info.major > 2
else ch.decode(encoding=win_encoding)
)
return ch if sys.version_info.major > 2 else ch.decode(encoding=win_encoding)
4 changes: 4 additions & 0 deletions requirements-test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
flake8
pytest
pytest-cov

Loading

0 comments on commit af7a57c

Please sign in to comment.