karma365 0.2.0__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.
karma365/__init__.py ADDED
@@ -0,0 +1,56 @@
1
+ """KARMA 365 SDK for Python. Your data stays on your phone; this talks to it."""
2
+
3
+ from .client import AsyncKarmaClient, KarmaClient
4
+ from .errors import (
5
+ AmbiguousNameError,
6
+ AppLockedError,
7
+ AppUpdateNeededError,
8
+ ConnectionLostError,
9
+ FileProblemError,
10
+ InvalidInputError,
11
+ KarmaError,
12
+ NotFoundError,
13
+ NotYourPhoneError,
14
+ ParentLockedError,
15
+ PhoneError,
16
+ PhoneNotFoundError,
17
+ PhoneStorageFullError,
18
+ PinLockedOutError,
19
+ PinRequiredError,
20
+ PinWrongError,
21
+ TokenError,
22
+ UploadInterruptedError,
23
+ )
24
+ from .link import Status
25
+ from .models import InfoResult, MajorTopic, Resource, Task, Topic, WorkspaceStats
26
+
27
+ __version__ = "0.2.0"
28
+ __all__ = [
29
+ "AsyncKarmaClient",
30
+ "KarmaClient",
31
+ "Status",
32
+ "MajorTopic",
33
+ "Topic",
34
+ "Resource",
35
+ "Task",
36
+ "WorkspaceStats",
37
+ "InfoResult",
38
+ "KarmaError",
39
+ "TokenError",
40
+ "PhoneNotFoundError",
41
+ "NotYourPhoneError",
42
+ "AppLockedError",
43
+ "ConnectionLostError",
44
+ "AppUpdateNeededError",
45
+ "PinRequiredError",
46
+ "PinWrongError",
47
+ "PinLockedOutError",
48
+ "ParentLockedError",
49
+ "NotFoundError",
50
+ "AmbiguousNameError",
51
+ "InvalidInputError",
52
+ "FileProblemError",
53
+ "PhoneStorageFullError",
54
+ "UploadInterruptedError",
55
+ "PhoneError",
56
+ ]
karma365/__main__.py ADDED
@@ -0,0 +1,76 @@
1
+ """`python -m karma365 status` and `python -m karma365 speedtest`."""
2
+
3
+ import asyncio
4
+ import os
5
+ import sys
6
+ import tempfile
7
+ import time
8
+
9
+ from .client import AsyncKarmaClient
10
+ from .errors import KarmaError
11
+ from .upload import cancel_file, send_file
12
+
13
+
14
+ async def status() -> int:
15
+ async with AsyncKarmaClient() as karma:
16
+ result = await karma.status()
17
+ if result.connected:
18
+ print(f"Connected to your phone at {result.phone_url} ({result.latency_ms} ms)")
19
+ return 0
20
+ print(f"Not connected: {result.message}")
21
+ if result.hint:
22
+ print(f"What to do: {result.hint}")
23
+ print(f"Code: {result.reason}")
24
+ return 1
25
+
26
+
27
+ async def speedtest(megabytes: int) -> int:
28
+ """Sends a throwaway file over the real path and measures it. Wi-Fi, the phone's
29
+ decrypting and its storage all count, so this is the speed you will really get."""
30
+ async with AsyncKarmaClient() as karma:
31
+ minors = await karma.list_minors()
32
+ if not minors:
33
+ print("Create a minor topic first. The test needs one, and leaves nothing in it.")
34
+ return 1
35
+ handle, path = tempfile.mkstemp(suffix=".bin", prefix="karma_speedtest_")
36
+ try:
37
+ with os.fdopen(handle, "wb") as file:
38
+ for _ in range(megabytes):
39
+ file.write(os.urandom(1024 * 1024))
40
+
41
+ def progress(sent: int, total: int) -> None:
42
+ print(f"\r{sent * 100 // total}%", end="", flush=True)
43
+
44
+ started = time.perf_counter()
45
+ resource_id = await send_file(
46
+ karma._link,
47
+ path=path,
48
+ file_type="video",
49
+ topic_id=minors[0].id,
50
+ title="KARMA 365 speed test",
51
+ on_progress=progress,
52
+ )
53
+ seconds = time.perf_counter() - started
54
+ await karma.delete(resource_id)
55
+ print(f"\n{megabytes} MB in {seconds:.1f} s: {megabytes / seconds:.1f} MB/s")
56
+ return 0
57
+ finally:
58
+ os.unlink(path)
59
+
60
+
61
+ def main() -> int:
62
+ command = sys.argv[1] if len(sys.argv) > 1 else "status"
63
+ try:
64
+ if command == "status":
65
+ return asyncio.run(status())
66
+ if command == "speedtest":
67
+ return asyncio.run(speedtest(int(sys.argv[2]) if len(sys.argv) > 2 else 100))
68
+ except KarmaError as error:
69
+ print(error)
70
+ return 1
71
+ print("Usage: python -m karma365 status\n python -m karma365 speedtest [MB]")
72
+ return 2
73
+
74
+
75
+ if __name__ == "__main__":
76
+ sys.exit(main())