Skip to content

Commit

Permalink
Restyled by yapf
Browse files Browse the repository at this point in the history
  • Loading branch information
restyled-commits committed Feb 22, 2024
1 parent 48ddf4e commit 483e925
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 40 deletions.
42 changes: 20 additions & 22 deletions test/auto_tests/self_connection_status_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ class TestException(Exception):
class FriendInfo:
connection_status: core.Tox_Connection = core.TOX_CONNECTION_NONE
request_message: bytes = b""
messages: list[tuple[core.Tox_Message_Type, bytes]] = field(default_factory=list)
messages: list[tuple[core.Tox_Message_Type, bytes]] = field(
default_factory=list)


class TestTox(core.Tox_Ptr):
Expand All @@ -27,9 +28,9 @@ def __init__(self) -> None:
core.Tox_Ptr.__init__(self)
self.friends = collections.defaultdict(FriendInfo)

def handle_self_connection_status(
self, connection_status: core.Tox_Connection
) -> None:
def handle_self_connection_status(self,
connection_status: core.Tox_Connection
) -> None:
self.connection_status_from_cb = connection_status
raise TestException(connection_status)

Expand All @@ -38,13 +39,15 @@ def handle_friend_request(self, public_key: bytes, message: bytes) -> None:
self.friends[friend_number].request_message = message

def handle_friend_connection_status(
self, friend_number: int, connection_status: core.Tox_Connection,
self,
friend_number: int,
connection_status: core.Tox_Connection,
) -> None:
self.friends[friend_number].connection_status = connection_status

def handle_friend_message(
self, friend_number: int, type_: core.Tox_Message_Type, message: bytes
) -> None:
def handle_friend_message(self, friend_number: int,
type_: core.Tox_Message_Type,
message: bytes) -> None:
self.friends[friend_number].messages.append((type_, message))


Expand All @@ -62,34 +65,30 @@ def _iterate(self) -> None:
time.sleep(tox1.iteration_interval / 1000)

def _wait_for_self_online(self) -> None:
while (
tox1.connection_status == core.TOX_CONNECTION_NONE
or tox2.connection_status == core.TOX_CONNECTION_NONE
or tox3.connection_status == core.TOX_CONNECTION_NONE
):
while (tox1.connection_status == core.TOX_CONNECTION_NONE
or tox2.connection_status == core.TOX_CONNECTION_NONE
or tox3.connection_status == core.TOX_CONNECTION_NONE):
self._iterate()

def _wait_for_friend_online(self) -> None:
while (
tox1.friends[0].connection_status == core.TOX_CONNECTION_NONE
or tox2.friends[0].connection_status == core.TOX_CONNECTION_NONE
):
while (tox1.friends[0].connection_status == core.TOX_CONNECTION_NONE or
tox2.friends[0].connection_status == core.TOX_CONNECTION_NONE):
self._iterate()

def test_connection_status_cb(self) -> None:
for tox in (tox1, tox2, tox3):
# Test that exceptions can pass through C code.
with self.assertRaises(TestException) as ex:
self._wait_for_self_online()
self.assertEqual(tox.connection_status, tox.connection_status_from_cb)
self.assertEqual(tox.connection_status,
tox.connection_status_from_cb)
self.assertEqual(ex.exception.status, tox.connection_status)

def test_friend_add(self) -> None:
tox1.friend_add(tox2.address, b"are you gonna be my best friend?")
self._wait_for_friend_online()
self.assertEqual(
tox2.friends[0].request_message, b"are you gonna be my best friend?"
)
self.assertEqual(tox2.friends[0].request_message,
b"are you gonna be my best friend?")


# def test_friend_by_public_key(self) -> None:
Expand All @@ -102,6 +101,5 @@ def test_friend_add(self) -> None:
# self._iterate()
# self.assertEqual(tox2.friends[0].messages[0], (core.TOX_MESSAGE_TYPE_NORMAL, b"hello there!"))


if __name__ == "__main__":
unittest.main()
35 changes: 17 additions & 18 deletions tools/stubgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,12 @@
]


def process_class(
name: str, cls: list[str], imports: set[str], typevars: set[str], attr: object
) -> None:
def process_class(name: str, cls: list[str], imports: set[str],
typevars: set[str], attr: object) -> None:
for mem in dir(attr):
mem_attr = getattr(attr, mem)
if (
mem in ("__init__", "__enter__", "__exit__")
or "cython_function_or_method" in mem_attr.__class__.__name__
):
if (mem in ("__init__", "__enter__", "__exit__")
or "cython_function_or_method" in mem_attr.__class__.__name__):
doc = mem_attr.__doc__.split("\n")[0]
if " -> " not in doc:
doc += " -> None"
Expand Down Expand Up @@ -54,11 +51,11 @@ def process_class(


def process_type(
sym: str,
attr: object,
imports: set[str],
typevars: set[str],
classes: dict[str, list[str]],
sym: str,
attr: object,
imports: set[str],
typevars: set[str],
classes: dict[str, list[str]],
) -> None:
if not isinstance(attr, type):
return
Expand All @@ -83,9 +80,8 @@ def process_type(
process_class(sym, cls, imports, typevars, attr)


def process_value(
sym: str, attr: object, consts: list[str], classes: dict[str, list[str]]
) -> None:
def process_value(sym: str, attr: object, consts: list[str],
classes: dict[str, list[str]]) -> None:
if isinstance(attr, int):
enum = tuple(c for c in classes.keys() if sym.startswith(c.upper()))
if enum:
Expand All @@ -108,12 +104,14 @@ def process_package(out_dir: str, pkg: object, prefix: str, name: str) -> None:
continue
if isinstance(attr, pytox.common.__class__):
continue
if hasattr(attr, "__module__") and f"{prefix}.{attr.__module__}" != name:
if hasattr(attr,
"__module__") and f"{prefix}.{attr.__module__}" != name:
continue
attrs.append((sym, attr))

consts: list[str] = []
classes: collections.OrderedDict[str, list[str]] = collections.OrderedDict()
classes: collections.OrderedDict[str, list[str]] = collections.OrderedDict(
)
imports: set[str] = set()
typevars: set[str] = set()
missing: set[str] = set()
Expand All @@ -136,7 +134,8 @@ def process_package(out_dir: str, pkg: object, prefix: str, name: str) -> None:
for sym, attr in attrs:
process_value(sym, attr, consts, classes)

with open(os.path.join(out_dir, f"{name.replace('.', '/')}.pyi"), "w") as pyi:
with open(os.path.join(out_dir, f"{name.replace('.', '/')}.pyi"),
"w") as pyi:
pyi.write(f"# {name}\n")
for s in sorted(imports):
pyi.write(s + "\n")
Expand Down

0 comments on commit 483e925

Please sign in to comment.