forked from alibaba/tidevice
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b19a7f1
commit cddf7ff
Showing
7 changed files
with
90 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,51 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
"""Created on Fri Sep 24 2021 14:13:00 by codeskyblue | ||
""" | ||
|
||
import dataclasses | ||
import typing | ||
import enum | ||
from dataclasses import dataclass | ||
|
||
from ._proto import ConnectionType | ||
|
||
_T = typing.TypeVar("_T") | ||
|
||
|
||
class ConnectionType(str, enum.Enum): | ||
USB = "usb" | ||
NETWORK = "network" | ||
def alias_field(name: str) -> dataclasses.Field: | ||
return dataclasses.field(metadata={"alias": name}) | ||
|
||
|
||
class _BaseInfo: | ||
|
||
def _asdict(self) -> dict: | ||
""" for simplejson """ | ||
return self.__dict__.copy() | ||
|
||
|
||
@classmethod | ||
def from_json(cls: _T, data: dict) -> _T: | ||
kwargs = {} | ||
for field in dataclasses.fields(cls): | ||
possible_names = [field.name] | ||
if "alias" in field.metadata: | ||
possible_names.append(field.metadata["alias"]) | ||
for name in possible_names: | ||
if name in data: | ||
value = data[name] | ||
if field.type != type(value): | ||
value = field.type(value) | ||
kwargs[field.name] = value | ||
break | ||
return cls(**kwargs) | ||
|
||
def __repr__(self) -> str: | ||
attrs = [] | ||
for k, v in self.__dict__.items(): | ||
attrs.append(f"{k}={v!r}") | ||
return f"<{self.__class__.__name__} " + ", ".join(attrs) + ">" | ||
|
||
|
||
@dataclass(frozen=True) | ||
class DeviceInfo(_BaseInfo): | ||
udid: str | ||
device_id: int | ||
conn_type: ConnectionType | ||
udid: str = alias_field("SerialNumber") | ||
device_id: int = alias_field("DeviceID") | ||
conn_type: ConnectionType = alias_field("ConnectionType") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters