FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
pythonnative/src/pythonnative/platform.py at docs/cli-platform-help · glenlouis8/pythonnative · GitHub
glenlouis8
/
pythonnative
Public
forked from
pythonnative/pythonnative
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
pythonnative
/
src
/
pythonnative
/
platform.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
166 lines (129 loc) · 5.19 KB
Breadcrumbs
pythonnative
/
src
/
pythonnative
/
platform.py
Copy path
File metadata and controls
166 lines (129 loc) · 5.19 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
"""Platform-aware constants and selectors.
A small, RN-style helper for branching app code on the host platform.
The public surface is the [`Platform`][pythonnative.Platform] class
exposing ``OS``, ``Version``, ``is_ios``, ``is_android``, ``is_web``,
and ``select`` so user code can write ``Platform.select({"ios": ..., ...})``
without importing the ``IS_*`` flags directly.
Example:
```python
import pythonnative as pn
title_size = pn.Platform.select({"ios": 17, "android": 16, "default": 16})
@pn.component
def App():
return pn.Text(
f"Running on {pn.Platform.OS} {pn.Platform.Version}",
style={"font_size": title_size},
)
```
"""
from
__future__
import
annotations
import
os
import
sys
from
typing
import
Any
,
Dict
,
Optional
from
.
utils
import
IS_ANDROID
,
IS_IOS
,
IS_WEB
def
_detect_os
()
->
str
:
if
IS_ANDROID
:
return
"android"
if
IS_IOS
:
return
"ios"
if
IS_WEB
:
return
"web"
return
"test"
def
_detect_version
()
->
str
:
"""Return a human-readable platform version string.
On device this asks the native ``Device`` module (``UIDevice
.systemVersion`` / ``Build.VERSION.RELEASE``); the browser preview
answers with the browser's user agent family. Off-device (the test
environment), returns the host Python's version so user code can
still introspect *something*.
"""
if
IS_IOS
or
IS_ANDROID
or
IS_WEB
:
try
:
from
.
native_modules
.
registry
import
native_module
info
=
native_module
(
"Device"
).
call
(
"info"
)
if
isinstance
(
info
,
dict
)
and
info
.
get
(
"os_version"
):
return
str
(
info
[
"os_version"
])
except
Exception
:
pass
sim_version
=
os
.
environ
.
get
(
"SIMULATOR_RUNTIME_VERSION"
)
if
sim_version
:
return
sim_version
return
f"python-
{
sys
.
version_info
.
major
}
.
{
sys
.
version_info
.
minor
}
"
class
_LazyVersion
:
"""Resolve ``Platform.Version`` on first access (it needs the bridge on device)."""
_value
:
Optional
[
str
]
=
None
def
__get__
(
self
,
obj
:
object
,
owner
:
Optional
[
type
]
=
None
)
->
str
:
if
_LazyVersion
.
_value
is
None
:
_LazyVersion
.
_value
=
_detect_version
()
return
_LazyVersion
.
_value
class
Platform
:
"""Platform-aware constants and the ``select`` dispatcher.
All attributes are read at import time. ``OS`` is one of
``"ios"``, ``"android"``, ``"web"`` (the browser preview), or
``"test"`` (when running off-device, e.g., in unit tests).
"""
OS
:
str
=
_detect_os
()
"""``"ios"``, ``"android"``, ``"web"``, or ``"test"``."""
Version
:
str
=
_LazyVersion
()
# type: ignore[assignment]
"""Best-effort OS version string (``"17.4"``, ``"14"``, ``"python-3.11"``)."""
is_ios
:
bool
=
IS_IOS
"""``True`` when running inside an iOS app bundle."""
is_android
:
bool
=
IS_ANDROID
"""``True`` when running inside an Android process."""
is_web
:
bool
=
IS_WEB
"""``True`` when running the browser preview (``pn preview``)."""
is_test
:
bool
=
OS
==
"test"
"""``True`` when running off-device (no native runtime)."""
@
staticmethod
def
select
(
spec
:
Dict
[
str
,
Any
],
default
:
Any
=
None
)
->
Any
:
"""Pick the value matching the current platform.
Looks up ``spec[Platform.OS]``, then falls back to
``spec["native"]`` (matches iOS and Android, *not* the browser
preview, which is a development surface), then to
``spec["default"]``, then to the explicit ``default`` argument.
Args:
spec: Mapping from platform name to value. Recognized keys:
``"ios"``, ``"android"``, ``"web"``, ``"test"``,
``"native"``, ``"default"``.
default: Value returned when ``spec`` has no matching key
and no ``"default"`` entry.
Returns:
The matching value, or ``default`` when nothing matches.
Example:
```python
font = pn.Platform.select(
{"ios": "Helvetica", "android": "Roboto", "default": None}
)
```
"""
if
Platform
.
OS
in
spec
:
return
spec
[
Platform
.
OS
]
if
(
Platform
.
is_ios
or
Platform
.
is_android
)
and
"native"
in
spec
:
return
spec
[
"native"
]
if
"default"
in
spec
:
return
spec
[
"default"
]
return
default
def
get_platform
()
->
str
:
"""Return the active platform name.
Equivalent to reading ``Platform.OS``, exposed as a function for
introspection from non-component code.
"""
return
Platform
.
OS
def
_set_platform_for_test
(
name
:
Optional
[
str
])
->
None
:
"""Override ``Platform.OS`` for unit tests.
Production code should not call this. Tests can pass ``"ios"``,
``"android"``, ``"web"``, ``"test"``, or ``None`` (to reset to
autodetect).
"""
if
name
is
None
:
Platform
.
OS
=
_detect_os
()
Platform
.
is_ios
=
IS_IOS
Platform
.
is_android
=
IS_ANDROID
Platform
.
is_web
=
IS_WEB
Platform
.
is_test
=
Platform
.
OS
==
"test"
return
Platform
.
OS
=
name
Platform
.
is_ios
=
name
==
"ios"
Platform
.
is_android
=
name
==
"android"
Platform
.
is_web
=
name
==
"web"
Platform
.
is_test
=
name
==
"test"
Back
|
FazBrowse Home
|
New Git URL