makcu 0.1.4__py3-none-any.whl → 0.2.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
makcu/__init__.py CHANGED
@@ -1,16 +1,60 @@
1
- from .controller import MakcuController
1
+ from typing import List
2
+ from .controller import MakcuController, create_controller, create_async_controller
2
3
  from .enums import MouseButton
3
- from .errors import MakcuError, MakcuConnectionError
4
+ from .errors import (
5
+ MakcuError,
6
+ MakcuConnectionError,
7
+ MakcuCommandError,
8
+ MakcuTimeoutError,
9
+ MakcuResponseError
10
+ )
4
11
 
5
- def create_controller(fallback_com_port="", debug=False, send_init=True):
6
- makcu = MakcuController(fallback_com_port, debug=debug, send_init=send_init)
7
- makcu.connect()
8
- return makcu
12
+ __version__: str = "2.0.0"
13
+ __author__: str = "SleepyTotem"
14
+ __license__: str = "GPL"
9
15
 
10
- __all__ = [
16
+ __all__: List[str] = [
11
17
  "MakcuController",
18
+ "create_controller",
19
+ "create_async_controller",
12
20
  "MouseButton",
13
21
  "MakcuError",
14
22
  "MakcuConnectionError",
15
- "create_controller",
16
- ]
23
+ "MakcuCommandError",
24
+ "MakcuTimeoutError",
25
+ "MakcuResponseError",
26
+ ]
27
+
28
+ from .controller import MakcuController as Controller
29
+
30
+ __doc__ = """
31
+ Makcu Python Library provides a high-performance interface for controlling
32
+ Makcu USB devices. Features include:
33
+
34
+ - Full async/await support for modern Python applications
35
+ - Zero-delay command execution with intelligent tracking
36
+ - Automatic device reconnection on disconnect
37
+ - Human-like mouse movement and clicking patterns
38
+ - Comprehensive button and axis locking
39
+ - Real-time button event monitoring
40
+
41
+ Quick Start:
42
+ >>> from makcu import create_controller, MouseButton
43
+ >>> makcu = create_controller()
44
+ >>> makcu.click(MouseButton.LEFT)
45
+ >>> makcu.move(100, 50)
46
+ >>> makcu.disconnect()
47
+
48
+ Async Usage:
49
+ >>> import asyncio
50
+ >>> from makcu import create_async_controller, MouseButton
51
+ >>>
52
+ >>> async def main():
53
+ ... async with await create_async_controller() as makcu:
54
+ ... await makcu.click(MouseButton.LEFT)
55
+ ... await makcu.move(100, 50)
56
+ >>>
57
+ >>> asyncio.run(main())
58
+
59
+ For more information, visit: https://github.com/SleepyTotem/makcu-py-lib
60
+ """
makcu/__main__.py CHANGED
@@ -1,9 +1,12 @@
1
1
  import sys
2
- import webbrowser
3
2
  import os
4
3
  from pathlib import Path
4
+ from typing import List, NoReturn
5
5
  import pytest
6
- from makcu import create_controller, MakcuConnectionError
6
+ import time
7
+ from makcu import create_controller, MakcuConnectionError, MakcuController
8
+ import json
9
+ import re
7
10
 
8
11
  def debug_console():
9
12
  controller = create_controller()
@@ -13,6 +16,8 @@ def debug_console():
13
16
  print("Type a raw command (e.g., km.version()) and press Enter.")
14
17
  print("Type 'exit' or 'quit' to leave.")
15
18
 
19
+ command_counter = 0
20
+
16
21
  while True:
17
22
  try:
18
23
  cmd = input(">>> ").strip()
@@ -21,8 +26,17 @@ def debug_console():
21
26
  if not cmd:
22
27
  continue
23
28
 
29
+ command_counter += 1
30
+
24
31
  response = transport.send_command(cmd, expect_response=True)
25
- print(f"{response or '(no response)'}")
32
+
33
+ if response and response.strip():
34
+ if response.strip() == cmd:
35
+ print(f"{cmd}")
36
+ else:
37
+ print(f"{response}")
38
+ else:
39
+ print("(no response)")
26
40
 
27
41
  except Exception as e:
28
42
  print(f"⚠️ Error: {e}")
@@ -30,48 +44,235 @@ def debug_console():
30
44
  controller.disconnect()
31
45
  print("Disconnected.")
32
46
 
33
- def test_port(port):
47
+ def test_port(port: str) -> None:
34
48
  try:
35
- print(f"Trying to connect to {port} (without init command)...")
36
- controller = create_controller(fallback_com_port=port, send_init=False)
37
- print(f"✅ Successfully connected to {port}")
38
- controller.disconnect()
49
+ print(f"Trying to connect to {port}...")
50
+ makcu = MakcuController(fallback_com_port=port, send_init=False, override_port=True)
51
+ makcu.connect()
52
+ if makcu.is_connected:
53
+ print(f"✅ Successfully connected to {port}.")
54
+ makcu.disconnect()
39
55
  except MakcuConnectionError as e:
40
- print(f" Failed to connect to {port}: {e}")
56
+ if "FileNotFoundError" in str(e):
57
+ print(f"❌ Port {port} does not exist. Please check the port name.")
58
+ else:
59
+ print(f"❌ Failed to connect to {port}: ")
41
60
  except Exception as e:
42
61
  print(f"❌ Unexpected error: {e}")
43
62
 
44
- def run_tests():
45
- print("🧪 Running Pytest Suite...")
46
-
47
- package_dir = Path(__file__).resolve().parent
48
- test_file = package_dir / "test_suite.py"
49
-
50
- result = pytest.main([
51
- str(test_file),
52
- "--rootdir", str(package_dir),
53
- "-v", "--tb=short",
54
- "--capture=tee-sys",
55
- "--html=latest_pytest.html",
56
- "--self-contained-html"
57
- ])
58
-
59
- report_path = os.path.abspath("latest_pytest.html")
60
- if os.path.exists(report_path):
61
- print(f"📄 Opening test report: {report_path}")
62
- webbrowser.open(f"file://{report_path}")
63
- else:
64
- print("❌ Report not found. Something went wrong.")
63
+ def parse_html_results(html_file: Path):
64
+ if not html_file.exists():
65
+ raise FileNotFoundError(f"HTML report not found: {html_file}")
66
+
67
+ with open(html_file, 'r', encoding='utf-8') as f:
68
+ content = f.read()
69
+
70
+ match = re.search(r'data-jsonblob="([^"]*)"', content)
71
+ if not match:
72
+ raise ValueError("Could not find JSON data in HTML report")
73
+
74
+ json_str = match.group(1)
75
+ json_str = json_str.replace('"', '"').replace(''', "'").replace('&', '&')
76
+
77
+ try:
78
+ data = json.loads(json_str)
79
+ except json.JSONDecodeError as e:
80
+ raise ValueError(f"Failed to parse JSON data: {e}")
81
+
82
+ test_results = []
83
+ total_ms = 0
84
+
85
+ skip_tests = {'test_connect_to_port'}
86
+
87
+ for test_id, test_data_list in data.get('tests', {}).items():
88
+ test_name = test_id.split('::')[-1]
89
+ if test_name in skip_tests:
90
+ continue
91
+
92
+ for test_data in test_data_list:
93
+ status = test_data.get('result', 'UNKNOWN')
94
+ duration_str = test_data.get('duration', '0 ms')
95
+
96
+ duration_match = re.search(r'(\d+)\s*ms', duration_str)
97
+ duration_ms = int(duration_match.group(1)) if duration_match else 0
98
+ total_ms += duration_ms
99
+
100
+ test_results.append((test_name, status, duration_ms))
101
+
102
+ return test_results, total_ms
65
103
 
66
- if result != 0:
67
- print("❌ Some tests failed.")
68
- else:
69
- print("✅ All tests passed.")
104
+ def run_tests() -> NoReturn:
105
+ try:
106
+ from rich.console import Console
107
+ from rich.table import Table
108
+ from rich.progress import Progress, SpinnerColumn, TextColumn, BarColumn, TimeElapsedColumn
109
+ from rich.panel import Panel
110
+ from rich.align import Align
111
+ from rich import print as rprint
112
+ from rich.text import Text
113
+ import subprocess
114
+
115
+ console = Console()
116
+
117
+ header = Panel.fit(
118
+ "[bold cyan]Makcu Test Suite v2.0[/bold cyan]\n[dim]High-Performance Python Library[/dim]",
119
+ border_style="bright_blue"
120
+ )
121
+ console.print(Align.center(header))
122
+ console.print()
123
+
124
+ package_dir: Path = Path(__file__).resolve().parent
125
+ test_file: Path = package_dir / "test_suite.py"
126
+ html_file: Path = package_dir.parent / "latest_pytest.html"
127
+
128
+ start_time = time.time()
129
+
130
+ with Progress(
131
+ SpinnerColumn(),
132
+ TextColumn("[progress.description]{task.description}"),
133
+ BarColumn(),
134
+ TextColumn("[progress.percentage]{task.percentage:>3.0f}%"),
135
+ TimeElapsedColumn(),
136
+ console=console,
137
+ transient=True
138
+ ) as progress:
139
+ task = progress.add_task("[cyan]Running tests...", total=100)
140
+
141
+ result = subprocess.run(
142
+ [
143
+ sys.executable, "-m", "pytest",
144
+ str(test_file),
145
+ "--rootdir", str(package_dir),
146
+ "-q",
147
+ "--tb=no",
148
+ "--html", str(html_file),
149
+ "--self-contained-html"
150
+ ],
151
+ stdout=subprocess.DEVNULL,
152
+ stderr=subprocess.DEVNULL,
153
+ text=True
154
+ )
155
+
156
+ progress.update(task, completed=100)
157
+
158
+ try:
159
+ test_results, total_ms = parse_html_results(html_file)
160
+ except (FileNotFoundError, ValueError) as e:
161
+ console.print(f"[red]❌ Failed to parse test results: {e}[/red]")
162
+ console.print(f"[yellow]⚠️ pytest exit code: {result.returncode}[/yellow]")
163
+ sys.exit(1)
164
+
165
+ elapsed_time = time.time() - start_time
166
+
167
+ table = Table(title="[bold]Test Results[/bold]", show_header=True, header_style="bold magenta")
168
+ table.add_column("Test", style="cyan", no_wrap=True)
169
+ table.add_column("Status", justify="center")
170
+ table.add_column("Time", justify="right", style="yellow")
171
+ table.add_column("Performance", justify="center")
172
+
173
+ passed = failed = skipped = 0
174
+
175
+ for test_name, status, duration_ms in test_results:
176
+ display_name = test_name.replace("test_", "").replace("_", " ").title()
177
+
178
+ if status.upper() == "PASSED":
179
+ status_text = "[green]✅ PASSED[/green]"
180
+ passed += 1
181
+ elif status.upper() == "FAILED":
182
+ status_text = "[red]❌ FAILED[/red]"
183
+ failed += 1
184
+ elif status.upper() == "SKIPPED":
185
+ status_text = "[yellow]⏭️ SKIPPED[/yellow]"
186
+ skipped += 1
187
+ else:
188
+ status_text = status
189
+
190
+ time_str = f"{duration_ms}ms" if duration_ms else "-"
191
+ if duration_ms <= 3:
192
+ perf = "[green]Excellent[/green]"
193
+ elif duration_ms <= 5:
194
+ perf = "[cyan]Great[/cyan]"
195
+ elif duration_ms <= 10:
196
+ perf = "[yellow]Good[/yellow]"
197
+ elif duration_ms > 0:
198
+ perf = "[red]🐌 Needs work[/red]"
199
+ else:
200
+ perf = "-"
201
+
202
+ table.add_row(display_name, status_text, time_str, perf)
203
+
204
+ console.print("\n")
205
+ console.print(table)
206
+ console.print()
207
+
208
+ summary = Table.grid(padding=1)
209
+ summary.add_column(style="bold cyan", justify="right")
210
+ summary.add_column(justify="left")
211
+ summary.add_row("Total Tests:", str(len(test_results)))
212
+ summary.add_row("Passed:", f"[green]{passed}[/green]")
213
+ summary.add_row("Failed:", f"[red]{failed}[/red]" if failed else str(failed))
214
+ summary.add_row("Skipped:", f"[yellow]{skipped}[/yellow]" if skipped else str(skipped))
215
+ summary.add_row("Total Time:", f"{elapsed_time:.2f}s")
216
+ summary.add_row("Avg Time/Test:", f"{total_ms/len(test_results):.1f}ms" if test_results else "0ms")
217
+
218
+ console.print(Align.center(Panel(summary, title="[bold]Summary[/bold]", border_style="blue", expand=False)))
219
+ console.print()
220
+
221
+ if test_results:
222
+ avg_time = total_ms / len(test_results)
223
+ if avg_time < 3:
224
+ perf_text = Text("Performance: ELITE - Ready for 360Hz+ gaming!", style="bold bright_green")
225
+ elif avg_time < 5:
226
+ perf_text = Text("Performance: EXCELLENT - Ready for 240Hz+ gaming!", style="bold green")
227
+ elif avg_time < 10:
228
+ perf_text = Text("Performance: GREAT - Ready for 144Hz gaming!", style="bold cyan")
229
+ else:
230
+ perf_text = Text("Performance: GOOD - Suitable for standard gaming", style="bold yellow")
231
+ else:
232
+ perf_text = Text("⚠️ No test results parsed. Check your test suite.", style="bold red")
233
+
234
+ console.print(Align.center(Panel(perf_text, border_style="green")))
235
+ sys.exit(0 if failed == 0 else 1)
236
+
237
+ except ImportError:
238
+ print("📦 Rich not installed. Install it via `pip install rich` for enhanced output.")
239
+ print("\nFallback to raw pytest output...\n")
240
+
241
+ package_dir: Path = Path(__file__).resolve().parent
242
+ test_file: Path = package_dir / "test_suite.py"
243
+ html_file: Path = package_dir.parent / "latest_pytest.html"
244
+
245
+ result = pytest.main([
246
+ str(test_file),
247
+ "--rootdir", str(package_dir),
248
+ "-q",
249
+ "--tb=no",
250
+ "--html", str(html_file),
251
+ "--self-contained-html"
252
+ ])
253
+
254
+ try:
255
+ test_results, total_ms = parse_html_results(html_file)
256
+ passed = sum(1 for _, status, _ in test_results if status.upper() == "PASSED")
257
+ failed = sum(1 for _, status, _ in test_results if status.upper() == "FAILED")
258
+ skipped = sum(1 for _, status, _ in test_results if status.upper() == "SKIPPED")
259
+
260
+ print(f"\n📊 Results: {passed} passed, {failed} failed, {skipped} skipped")
261
+ if test_results:
262
+ avg_time = total_ms / len(test_results)
263
+ print(f"⏱️ Average time per test: {avg_time:.1f}ms")
264
+ except (FileNotFoundError, ValueError):
265
+ print("\n⚠️ Could not parse HTML results for summary")
266
+
267
+ if result != 0:
268
+ print("\n❌ Some tests failed.")
269
+ else:
270
+ print("\n✅ All tests passed.")
70
271
 
71
- sys.exit(result)
272
+ sys.exit(result)
72
273
 
73
- def main():
74
- args = sys.argv[1:]
274
+ def main() -> None:
275
+ args: List[str] = sys.argv[1:]
75
276
 
76
277
  if not args:
77
278
  print("Usage:")
makcu/conftest.py CHANGED
@@ -1,22 +1,31 @@
1
1
  import pytest
2
- from makcu import create_controller
3
2
  import time
3
+ from makcu import MakcuController, MouseButton
4
4
 
5
5
  @pytest.fixture(scope="session")
6
- def makcu():
7
- ctrl = create_controller()
8
- yield ctrl
9
- ctrl.disconnect()
10
- time.sleep(0.2)
6
+ def makcu(request):
7
+ ctrl = MakcuController(fallback_com_port="COM1", debug=False)
11
8
 
12
- @pytest.fixture(autouse=True)
13
- def ensure_clean_exit(makcu):
14
- yield
15
- makcu.mouse.lock_left(False)
16
- makcu.mouse.lock_right(False)
17
- makcu.mouse.lock_middle(False)
18
- makcu.mouse.lock_side1(False)
19
- makcu.mouse.lock_side2(False)
20
- makcu.mouse.lock_x(False)
21
- makcu.mouse.lock_y(False)
22
- makcu.enable_button_monitoring(False)
9
+ def cleanup():
10
+ if ctrl.is_connected():
11
+ ctrl.lock_left(False)
12
+ ctrl.lock_right(False)
13
+ ctrl.lock_middle(False)
14
+ ctrl.lock_side1(False)
15
+ ctrl.lock_side2(False)
16
+ ctrl.lock_x(False)
17
+ ctrl.lock_y(False)
18
+
19
+ ctrl.release(MouseButton.LEFT)
20
+ ctrl.release(MouseButton.RIGHT)
21
+ ctrl.release(MouseButton.MIDDLE)
22
+ ctrl.release(MouseButton.MOUSE4)
23
+ ctrl.release(MouseButton.MOUSE5)
24
+
25
+ ctrl.enable_button_monitoring(False)
26
+
27
+ ctrl.disconnect()
28
+
29
+ request.addfinalizer(cleanup)
30
+
31
+ return ctrl