FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
pythonnative/src/pythonnative/native_modules/device.py at main · pythonnative/pythonnative · GitHub
pythonnative
/
pythonnative
Public
Notifications
You must be signed in to change notification settings
Fork
23
Star
131
Code
Issues
11
Pull requests
4
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
pythonnative
/
src
/
pythonnative
/
native_modules
/
device.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
114 lines (94 loc) · 3.97 KB
Breadcrumbs
pythonnative
/
src
/
pythonnative
/
native_modules
/
device.py
Copy path
File metadata and controls
114 lines (94 loc) · 3.97 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
"""Static device and application facts.
[`Device.info`][pythonnative.native_modules.device.Device.info] returns
one [`DeviceInfo`][pythonnative.DeviceInfo] record read from the native
``Device`` module (``UIDevice`` and the main bundle on iOS, ``Build`` and
``PackageManager`` on Android, ``navigator`` in the browser preview).
Everything in it is already known to the OS, so the call is synchronous.
The native module returns the record's keys plus ``app_dir``, the
writable data directory that
[`FileSystem`][pythonnative.FileSystem] resolves relative paths against.
``DeviceInfo`` doesn't expose it; use ``FileSystem.app_dir()``.
Example:
```python
import pythonnative as pn
info = pn.Device.info()
if info.is_tablet:
columns = 3
print(f"{info.app_name} {info.app_version} ({info.build_number}) on {info.model}")
```
"""
from
__future__
import
annotations
from
dataclasses
import
dataclass
from
typing
import
Any
,
Dict
from
.
registry
import
native_module
@
dataclass
(
frozen
=
True
)
class
DeviceInfo
:
"""Facts about the device and the running application.
Attributes:
platform: ``"ios"``, ``"android"``, ``"web"`` (browser preview),
or ``"test"`` (off device).
os_version: OS version string (``"17.4"``, ``"14"``).
model: Hardware model (``"iPhone"``, ``"Pixel 8"``).
manufacturer: Hardware vendor (``"Apple"``, ``"Google"``).
is_simulator: Running in the iOS Simulator or an Android emulator.
is_tablet: The OS classifies the device as a tablet (iPad, or
an Android device with a large smallest width).
app_name: The display name of the app.
app_version: The marketing version (``CFBundleShortVersionString``
/ ``versionName``).
build_number: The build identifier (``CFBundleVersion`` /
``versionCode``), as a string.
bundle_id: Bundle identifier / application id.
scale: Physical pixels per layout unit.
font_scale: The user's text-size multiplier.
locale: The preferred locale as a BCP 47 language tag (``"en-US"``).
"""
platform
:
str
os_version
:
str
model
:
str
manufacturer
:
str
is_simulator
:
bool
is_tablet
:
bool
=
False
app_name
:
str
=
""
app_version
:
str
=
""
build_number
:
str
=
""
bundle_id
:
str
=
""
scale
:
float
=
1.0
font_scale
:
float
=
1.0
locale
:
str
=
""
def
_text
(
info
:
Dict
[
str
,
Any
],
key
:
str
,
default
:
str
=
""
)
->
str
:
value
=
info
.
get
(
key
)
return
default
if
value
is
None
else
str
(
value
)
def
_number
(
info
:
Dict
[
str
,
Any
],
key
:
str
,
default
:
float
)
->
float
:
value
=
info
.
get
(
key
)
if
isinstance
(
value
,
bool
)
or
not
isinstance
(
value
, (
int
,
float
))
or
value
<=
0
:
return
default
return
float
(
value
)
class
Device
:
"""Device information interface (synchronous).
Raises:
NativeModuleError: If the native module fails.
"""
@
staticmethod
def
info
()
->
DeviceInfo
:
"""Return the device and app facts as a [`DeviceInfo`][pythonnative.DeviceInfo].
Keys the native module leaves out fall back to ``""``, ``False``,
or ``1.0``; keys it adds (``app_dir``) are ignored here.
"""
raw
=
native_module
(
"Device"
).
call
(
"info"
)
info
:
Dict
[
str
,
Any
]
=
raw
if
isinstance
(
raw
,
dict
)
else
{}
return
DeviceInfo
(
platform
=
_text
(
info
,
"platform"
,
"unknown"
),
os_version
=
_text
(
info
,
"os_version"
),
model
=
_text
(
info
,
"model"
),
manufacturer
=
_text
(
info
,
"manufacturer"
),
is_simulator
=
bool
(
info
.
get
(
"is_simulator"
,
False
)),
is_tablet
=
bool
(
info
.
get
(
"is_tablet"
,
False
)),
app_name
=
_text
(
info
,
"app_name"
),
app_version
=
_text
(
info
,
"app_version"
),
build_number
=
_text
(
info
,
"build_number"
),
bundle_id
=
_text
(
info
,
"bundle_id"
),
scale
=
_number
(
info
,
"scale"
,
1.0
),
font_scale
=
_number
(
info
,
"font_scale"
,
1.0
),
locale
=
_text
(
info
,
"locale"
),
)
Back
|
FazBrowse Home
|
New Git URL