Skip to content

Commit 7d7a234

Browse files
authored
Merge pull request #185 from spyoungtech/rewrite
v1
2 parents 7988c13 + 8d5688e commit 7d7a234

File tree

179 files changed

+18139
-6352
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

179 files changed

+18139
-6352
lines changed

.coveragerc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
[run]
2-
branch = True
32
source = ahk

.github/workflows/test.yaml

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
on: [ push, pull_request ]
2+
3+
jobs:
4+
build:
5+
strategy:
6+
fail-fast: false
7+
matrix:
8+
python_version: ["3.10", "3.9", "3.8", "3.11"]
9+
runs-on: windows-latest
10+
steps:
11+
- name: Checkout
12+
uses: actions/checkout@v2
13+
- name: Setup Python
14+
uses: actions/setup-python@v2
15+
with:
16+
python-version: ${{ matrix.python_version }}
17+
18+
- name: Install dependencies
19+
run: |
20+
python -m pip install --upgrade pip
21+
python -m pip install -r requirements-dev.txt
22+
python -m pip install .
23+
python -m pip install tox
24+
python -m pip install ahk-binary
25+
- name: Test with coverage/pytest
26+
timeout-minutes: 5
27+
env:
28+
PYTHONUNBUFFERED: "1"
29+
run: |
30+
tox -e py
31+
- name: Coveralls
32+
env:
33+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
34+
run: |
35+
pip install --upgrade coveralls
36+
coveralls --service=github

.gitignore

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,25 @@
1+
### JetBrains template
2+
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider
3+
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
4+
5+
# User-specific stuff
6+
.idea/
7+
8+
# CMake
9+
cmake-build-*/
10+
11+
12+
# File-based project format
13+
*.iws
14+
15+
# IntelliJ
16+
out/
17+
18+
# mpeltonen/sbt-idea plugin
19+
.idea_modules/
20+
21+
22+
### Python template
123
# Byte-compiled / optimized / DLL files
224
__pycache__/
325
*.py[cod]
@@ -20,7 +42,6 @@ parts/
2042
sdist/
2143
var/
2244
wheels/
23-
pip-wheel-metadata/
2445
share/python-wheels/
2546
*.egg-info/
2647
.installed.cfg
@@ -73,6 +94,7 @@ instance/
7394
docs/_build/
7495

7596
# PyBuilder
97+
.pybuilder/
7698
target/
7799

78100
# Jupyter Notebook
@@ -134,8 +156,5 @@ dmypy.json
134156
# pytype static type analyzer
135157
.pytype/
136158

137-
# VS Code settings
138-
.vscode
139-
140-
# Windows desktop icon
141-
desktop.ini
159+
# Cython debug symbols
160+
cython_debug/

.pre-commit-config.yaml

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,64 @@
11
repos:
2+
3+
- repo: local
4+
hooks:
5+
- id: unasync-rewrite
6+
name: unasync-rewrite
7+
entry: python .unasync-rewrite.py
8+
language: python
9+
types: [python]
10+
files: ^(ahk/_async/.*\.py|\.unasync-rewrite\.py|tests/_async/.*\.py)
11+
pass_filenames: false
12+
additional_dependencies:
13+
- unasync
14+
- tokenize_rt
15+
- black
16+
- id: set-constants
17+
name: set-constants
18+
entry: python _set_constants.py
19+
language: python
20+
types: [python]
21+
pass_filenames: false
22+
files: ^(ahk/daemon\.ahk|ahk/_constants\.py)
23+
224
- repo: https://github.com/pre-commit/pre-commit-hooks
3-
rev: v4.3.0
25+
rev: v4.4.0
426
hooks:
27+
- id: mixed-line-ending
28+
args: ["-f", "lf"]
529
- id: check-yaml
630
- id: end-of-file-fixer
731
- id: trailing-whitespace
832
- id: double-quote-string-fixer
933
- repo: https://github.com/psf/black
10-
rev: '22.10.0'
34+
rev: '23.1.0'
1135
hooks:
1236
- id: black
1337
args:
1438
- "-S"
1539
- "-l"
1640
- "120"
41+
exclude: ^(ahk/_sync/.*\.py)
42+
- repo: https://github.com/asottile/reorder_python_imports
43+
rev: v3.9.0
44+
hooks:
45+
- id: reorder-python-imports
46+
47+
- repo: https://github.com/pre-commit/mirrors-mypy
48+
rev: 'v0.991'
49+
hooks:
50+
- id: mypy
51+
args:
52+
- "--strict"
53+
exclude: ^(tests/.*|setup\.py|\.build\.py|\.unasync-rewrite\.py|_tests_setup\.py|buildunasync\.py)
54+
additional_dependencies:
55+
- jinja2
56+
57+
- repo: https://github.com/pycqa/flake8
58+
rev: '6.0.0' # pick a git hash / tag to point to
59+
hooks:
60+
- id: flake8
61+
args:
62+
- "--ignore"
63+
- "E501,E704,E301,W503"
64+
files: ahk\/(?!_sync).*

.unasync-rewrite.py

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
import ast
2+
import os
3+
import shutil
4+
import subprocess
5+
import sys
6+
7+
import black
8+
from black import check_stability_and_equivalence
9+
from tokenize_rt import reversed_enumerate
10+
from tokenize_rt import src_to_tokens
11+
from tokenize_rt import tokens_to_src
12+
13+
GIT_EXECUTABLE = shutil.which('git')
14+
15+
changes = 0
16+
17+
18+
def _rewrite_file(filename: str) -> int:
19+
with open(filename, encoding='UTF-8') as f:
20+
contents = f.read()
21+
tree = ast.parse(contents, filename=filename)
22+
tokens = src_to_tokens(contents)
23+
nodes_on_lines_to_remove = []
24+
for tok in tokens:
25+
if tok.name == 'COMMENT' and 'unasync: remove' in tok.src:
26+
nodes_on_lines_to_remove.append(tok.line)
27+
lines_to_remove = set()
28+
for node in ast.walk(tree):
29+
if hasattr(node, 'lineno') and node.lineno in nodes_on_lines_to_remove:
30+
for lineno in range(node.lineno, node.end_lineno + 1):
31+
lines_to_remove.add(lineno)
32+
for i, tok in reversed_enumerate(tokens):
33+
if tok.line in lines_to_remove:
34+
tokens.pop(i)
35+
new_contents = tokens_to_src(tokens)
36+
if new_contents != contents:
37+
with open(filename, 'w') as f:
38+
f.write(new_contents)
39+
return new_contents != contents
40+
41+
42+
def _copyfunc(src, dst, *, follow_symlinks=True):
43+
global changes
44+
with open(src, encoding='UTF-8') as f:
45+
contents = f.read()
46+
if os.path.exists(dst):
47+
with open(dst, encoding='UTF-8') as dst_f:
48+
dst_contents = dst_f.read()
49+
try:
50+
black.assert_equivalent(
51+
src=contents,
52+
dst=dst_contents,
53+
)
54+
except AssertionError:
55+
changes += 1
56+
print('MODIFIED', dst)
57+
shutil.copy2(src, dst, follow_symlinks=follow_symlinks)
58+
else:
59+
changes += 1
60+
print('ADDED', dst)
61+
shutil.copy2(src, dst, follow_symlinks=follow_symlinks)
62+
if GIT_EXECUTABLE is None:
63+
print('WARNING could not find git!', file=sys.stderr)
64+
else:
65+
subprocess.run([GIT_EXECUTABLE, 'add', '--intent-to-add', dst])
66+
return dst
67+
68+
69+
def main() -> int:
70+
if os.path.isdir('build'):
71+
shutil.rmtree('build')
72+
subprocess.run([sys.executable, 'setup.py', 'build_py'], check=True)
73+
for root, dirs, files in os.walk('build/lib/ahk/_sync'):
74+
for fname in files:
75+
if fname.endswith('.py'):
76+
fp = os.path.join(root, fname)
77+
_rewrite_file(fp)
78+
subprocess.run([sys.executable, '_tests_setup.py', 'build_py'], check=True)
79+
for root, dirs, files in os.walk('build/lib/tests/_sync'):
80+
for fname in files:
81+
if fname.endswith('.py'):
82+
fp = os.path.join(root, fname)
83+
_rewrite_file(fp)
84+
shutil.copytree('build/lib/ahk/_sync', 'ahk/_sync', dirs_exist_ok=True, copy_function=_copyfunc)
85+
shutil.copytree('build/lib/tests/_sync', 'tests/_sync', dirs_exist_ok=True, copy_function=_copyfunc)
86+
87+
return changes
88+
89+
90+
if __name__ == '__main__':
91+
raise SystemExit(main())

LICENSE

Lines changed: 0 additions & 21 deletions
This file was deleted.

MANIFEST.in

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1-
include LICENSE
1+
include ahk/templates/daemon.ahk
2+
include ahk/templates/hotkeys.ahk
23
include docs/README.md
3-
recursive-include ahk/templates *
4+
include buildunasync.py

README.md

Lines changed: 0 additions & 1 deletion
This file was deleted.

_set_constants.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import shutil
2+
import subprocess
3+
import sys
4+
5+
with open('ahk/templates/daemon.ahk') as f:
6+
daemon_script = f.read()
7+
8+
with open('ahk/templates/hotkeys.ahk') as hotkeyfile:
9+
hotkey_script = hotkeyfile.read()
10+
11+
GIT_EXECUTABLE = shutil.which('git')
12+
13+
if not GIT_EXECUTABLE:
14+
raise RuntimeError('git executable not found')
15+
16+
new_contents = f'''\
17+
# THIS FILE IS AUTOGENERATED BY _set_constants.py
18+
# DO NOT EDIT BY HAND
19+
20+
DAEMON_SCRIPT_TEMPLATE = r"""{daemon_script}
21+
"""
22+
23+
HOTKEYS_SCRIPT_TEMPLATE = r"""{hotkey_script}
24+
"""
25+
'''
26+
27+
with open('ahk/_constants.py', encoding='utf-8') as f:
28+
constants_text = f.read()
29+
30+
if constants_text != new_contents:
31+
with open('ahk/_constants.py', 'w', encoding='utf-8') as f:
32+
f.write(new_contents)
33+
print('MODIFIED _constants.py', file=sys.stderr)
34+
subprocess.run([GIT_EXECUTABLE, 'add', '--intent-to-add', 'ahk/_constants.py'])
35+
raise SystemExit(1)
36+
else:
37+
raise SystemExit(0)

_tests_setup.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
"""
2+
Not a real setup package
3+
This is just to unasync our tests files
4+
"""
5+
import setuptools
6+
import unasync
7+
8+
setuptools.setup(
9+
name='ahk',
10+
version='0.0.1',
11+
author='Example Author',
12+
author_email='[email protected]',
13+
description='A package used to test customized unasync',
14+
url='https://github.com/pypa/sampleproject',
15+
packages=['tests', 'tests._async'],
16+
cmdclass={
17+
'build_py': unasync.cmdclass_build_py(
18+
rules=[
19+
unasync.Rule(
20+
fromdir='/tests/_async/',
21+
todir='/tests/_sync/',
22+
additional_replacements={
23+
'AsyncAHK': 'AHK',
24+
'AsyncTransport': 'Transport',
25+
'AsyncWindow': 'Window',
26+
'AsyncDaemonProcessTransport': 'DaemonProcessTransport',
27+
'_AIOP': '_SIOP',
28+
'async_create_process': 'sync_create_process',
29+
'adrain_stdin': 'drain_stdin',
30+
'IsolatedAsyncioTestCase': 'TestCase',
31+
'asyncSetUp': 'setUp',
32+
'asyncTearDown': 'tearDown',
33+
'async_sleep': 'sleep',
34+
# "__aenter__": "__aenter__",
35+
},
36+
),
37+
]
38+
)
39+
},
40+
# package_dir={"": "src"},
41+
)

0 commit comments

Comments
 (0)