FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
pythonnative/scripts/generate-native-contracts.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
/
scripts
/
generate-native-contracts.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
128 lines (109 loc) · 5 KB
Breadcrumbs
pythonnative
/
scripts
/
generate-native-contracts.py
Copy path
File metadata and controls
128 lines (109 loc) · 5 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
#!/usr/bin/env python3
"""Regenerate checked-in built-in contracts and the bundled iOS test fixtures."""
import
json
import
shutil
import
tempfile
from
dataclasses
import
dataclass
from
pathlib
import
Path
from
typing
import
Literal
,
NotRequired
,
TypedDict
import
pythonnative
# noqa: F401 # Install built-in schemas in a clean process.
from
pythonnative
.
sdk
.
codegen
import
generate
from
pythonnative
.
sdk
.
contract_codegen
import
ContractCompiler
from
pythonnative
.
sdk
.
native_types
import
NativeTypes
,
quoted
from
pythonnative
.
sdk
.
types
import
type_schema
ROOT
=
Path
(
__file__
).
resolve
().
parents
[
1
]
NATIVE
=
ROOT
/
"src/pythonnative/native"
class
NativeFixtureNote
(
TypedDict
):
"""Exercise required null and nonrequired nullable fields in both compilers."""
required
:
str
|
None
optional
:
NotRequired
[
str
|
None
]
@
dataclass
class
NativeFixtureDefault
:
"""Exercise nested defaults and language-specific literal escaping."""
note
:
NativeFixtureNote
label
:
Literal
[
'$total "quoted"
\\
path
\n
\x00
\b
'
,
"ready"
]
=
'$total "quoted"
\\
path
\n
\x00
\b
'
nullable
:
str
|
None
=
None
def
validation_fixtures
(
kotlin
:
bool
)
->
str
:
"""Compile the shared schema corpus using the production predicate compiler."""
compiler
=
ContractCompiler
(
kotlin
)
cases
=
json
.
loads
((
ROOT
/
"tests/contracts/validation.json"
).
read_text
())
names
=
[
compiler
.
predicate
(
item
[
"schema"
])
for
item
in
cases
]
template
=
(
ROOT
/
"src/pythonnative/sdk/templates"
/
(
"contracts.kt"
if
kotlin
else
"contracts.swift"
)).
read_text
()
if
kotlin
:
helpers
=
template
.
split
(
" private fun isBoolean"
,
1
)[
1
].
split
(
" fun validate("
,
1
)[
0
]
dispatch
=
(
" fun matches(index: Int, value: Any): Boolean = when (index) {
\n
"
+
"
\n
"
.
join
(
f"
{
i
}
->
{
name
}
(value)"
for
i
,
name
in
enumerate
(
names
))
+
"
\n
else -> false
\n
}"
)
return
(
"
\n
object PNValidationFixtures {
\n
private fun isBoolean"
+
helpers
+
"
\n
"
+
"
\n
"
.
join
(
compiler
.
functions
)
+
"
\n
"
+
dispatch
+
"
\n
}
\n
"
)
helpers
=
template
.
split
(
" private static func isBoolean"
,
1
)[
1
].
split
(
" public static func validate("
,
1
)[
0
]
dispatch
=
(
" static func matches(_ index: Int, _ value: Any) -> Bool {
\n
switch index {
\n
"
+
"
\n
"
.
join
(
f" case
{
i
}
: return
{
name
}
(value)"
for
i
,
name
in
enumerate
(
names
))
+
"
\n
default: return false
\n
}
\n
}"
)
return
(
"
\n
import CoreFoundation
\n
enum PNValidationFixtures {
\n
private static func isBoolean"
+
helpers
+
"
\n
"
+
"
\n
"
.
join
(
compiler
.
functions
)
+
"
\n
"
+
dispatch
+
"
\n
}
\n
"
)
def
list_fixtures
(
kotlin
:
bool
)
->
str
:
"""Embed one shared list trace so XCTest needs no extra bundle resource."""
payload
=
json
.
dumps
(
json
.
loads
((
ROOT
/
"tests/contracts/list-trace.json"
).
read_text
()),
separators
=
(
","
,
":"
))
if
kotlin
:
return
"
\n
object PNListFixtures { val trace = "
+
quoted
(
payload
,
kotlin
=
True
)
+
" }
\n
"
return
"
\n
enum PNListFixtures { static let trace = "
+
quoted
(
payload
)
+
" }
\n
"
def
record_fixtures
()
->
dict
[
Path
,
str
]:
"""Generate native test records from real Python annotations."""
types
=
NativeTypes
()
types
.
types
(
type_schema
(
NativeFixtureDefault
),
"Fixture"
)
return
{
NATIVE
/
"ios/Tests/PythonNativeKitTests/PNRecordFixtures.swift"
: (
"// Generated by scripts/generate-native-contracts.py.
\n
"
"import Foundation
\n
import PythonNativeKit
\n
\n
"
+
"
\n
"
.
join
(
types
.
swift
)
+
validation_fixtures
(
False
)
+
list_fixtures
(
False
)
),
NATIVE
/
"android/src/test/java/com/pythonnative/generated/PNRecordFixtures.kt"
: (
"// Generated by scripts/generate-native-contracts.py.
\n
"
"package com.pythonnative.generated
\n
\n
"
"import org.json.JSONObject
\n
import org.json.JSONArray
\n
\n
"
+
"
\n
"
.
join
(
types
.
kotlin
)
+
validation_fixtures
(
True
)
+
list_fixtures
(
True
)
),
}
def
main
()
->
None
:
"""Generate both native libraries from the same Python definitions."""
with
tempfile
.
TemporaryDirectory
(
prefix
=
"pn-contracts-"
)
as
folder
:
generate
(
folder
)
for
extension
,
destination
in
(
(
"swift"
,
NATIVE
/
"ios/Sources/PythonNativeKit/Generated"
),
(
"kt"
,
NATIVE
/
"android/src/main/java/com/pythonnative/generated"
),
):
for
name
in
(
"PNContracts"
,
"NativeProps"
,
"NativeModules"
,
"NativeValues"
):
shutil
.
copyfile
(
Path
(
folder
)
/
f"
{
name
}
.
{
extension
}
"
,
destination
/
f"
{
name
}
.
{
extension
}
"
)
shutil
.
copyfile
(
ROOT
/
"tests/contracts/validation.json"
,
NATIVE
/
"ios/Tests/PythonNativeKitTests/Fixtures/validation.json"
)
for
path
,
content
in
record_fixtures
().
items
():
path
.
parent
.
mkdir
(
parents
=
True
,
exist_ok
=
True
)
path
.
write_text
(
content
)
if
__name__
==
"__main__"
:
main
()
Back
|
FazBrowse Home
|
New Git URL