-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathpython.go
292 lines (250 loc) · 6.78 KB
/
python.go
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
package main
import (
"bytes"
"path/filepath"
"strings"
)
type pyw struct {
buff *bytes.Buffer
model *YamlModel
}
func (w *pyw) buffer() *bytes.Buffer {
return w.buff
}
func (w *pyw) indent() string {
return " "
}
func writePythonModule(args *args) {
// read and check the model
model, err := ReadYamlModel(args)
check(err, "could not read YAML model")
// prepare the package folder
modDir := filepath.Join(args.home, "py", "olca_schema")
mkdir(modDir)
// write the schema
var buffer bytes.Buffer
writer := pyw{
buff: &buffer,
model: model,
}
writer.writeSchema()
fileName := "schema.py"
modFile := filepath.Join(modDir, fileName)
writeFile(modFile, buffer.String())
}
func (w *pyw) writeSchema() {
w.writeHeader()
// RefType
ln(w, "class RefType(Enum):")
w.model.EachClass(func(class *YamlClass) {
if w.model.IsRefEntity(class) &&
class.Name != "Ref" {
lni(w, 1, class.Name, " = '", class.Name, "'")
}
})
ln(w)
w.writeEnumGetter("RefType")
ln(w)
// enums
w.model.EachEnum(func(enum *YamlEnum) {
w.writeEnum(enum)
})
// classes
for _, class := range w.model.TopoSortClasses() {
if w.model.IsAbstract(class) {
continue
}
w.writeClass(class)
}
// RootEntity and RefEntity
ln(w, "RootEntity = Union[")
w.model.EachClass(func(class *YamlClass) {
if w.model.IsRootEntity(class) {
lni(w, 1, class.Name, ",")
}
})
ln(w, "]")
ln(w)
ln(w)
ln(w, "RefEntity = Union[RootEntity, Unit, NwSet]")
}
func (w *pyw) writeHeader() {
ln(w, "# DO NOT CHANGE THIS CODE AS THIS IS GENERATED AUTOMATICALLY")
ln(w, `
# This module contains a Python API for reading and writing data sets in
# the JSON based openLCA data exchange format. For more information see
# http://greendelta.github.io/olca-schema
`)
// imports
ln(w, "import datetime")
ln(w, "import json")
ln(w, "import uuid")
ln(w)
ln(w, "from enum import Enum")
ln(w, "from dataclasses import dataclass")
ln(w, "from typing import Any, Dict, List, Optional, Union")
ln(w)
ln(w)
}
func (w *pyw) writeEnum(enum *YamlEnum) {
name := enum.Name
ln(w, "class ", name, "(Enum):")
ln(w)
for _, item := range enum.Items {
lni(w, 1, item.Name, " = '", item.Name, "'")
}
ln(w)
w.writeEnumGetter(name)
ln(w)
}
func (w *pyw) writeEnumGetter(name string) {
lni(w, 1, "@staticmethod")
lni(w, 1, "def get(v: Union[str, '", name, "'],")
lni(w, 3, "default: Optional['", name, "'] = None) -> Optional['", name, "']:")
lni(w, 2, "for i in ", name, ":")
lni(w, 3, "if i == v or i.value == v or i.name == v:")
lni(w, 4, "return i")
lni(w, 2, "return default")
ln(w)
}
func (w *pyw) writeClass(class *YamlClass) {
ln(w, "@dataclass")
ln(w, "class ", class.Name, ":")
ln(w)
// properties
for _, prop := range w.model.AllPropsOf(class) {
if prop.Name == "@type" {
continue
}
propType := YamlPropType(prop.Type)
lni(w, 1, prop.PyName(), ": Optional[", propType.ToPython(), "] = None")
}
if class.Name == "Ref" {
lni(w, 1, "ref_type: Optional[RefType] = None")
}
ln(w)
// __post_init__
if w.model.IsRootEntity(class) {
fields := []string{"id", "version", "last_change"}
inits := []string{"str(uuid.uuid4())", "'01.00.000'",
"datetime.datetime.now(datetime.timezone.utc).isoformat()"}
lni(w, 1, "def __post_init__(self):")
for i, field := range fields {
lni(w, 2, "if self.", field, " is None:")
lni(w, 3, "self.", field, " = ", inits[i])
}
ln(w)
}
// to_dict
lni(w, 1, "def to_dict(self) -> Dict[str, Any]:")
if w.model.IsRootEntity(class) {
lni(w, 2, "d: Dict[str, Any] = {'@type': '", class.Name, "'}")
} else {
lni(w, 2, "d: Dict[str, Any] = {}")
}
if class.Name == "Ref" {
lni(w, 2, "if self.ref_type is not None:")
lni(w, 3, "d['@type'] = self.ref_type.value")
}
for _, prop := range w.model.AllPropsOf(class) {
if prop.Name == "@type" {
continue
}
selfProp := "self." + prop.PyName()
dictProp := "d['" + prop.Name + "']"
propType := prop.PropType()
lni(w, 2, "if ", selfProp, " is not None:")
if propType.IsPrimitive() ||
(propType.IsList() && propType.UnpackList().IsPrimitive()) ||
propType == "GeoJSON" {
lni(w, 3, dictProp, " = ", selfProp)
} else if propType.IsEnumOf(w.model) {
lni(w, 3, dictProp, " = ", selfProp, ".value")
} else if propType.IsList() {
lni(w, 3, dictProp, " = [e.to_dict() for e in ", selfProp, "]")
} else {
lni(w, 3, dictProp, " = ", selfProp, ".to_dict()")
}
}
lni(w, 2, "return d")
ln(w)
// to_json
if w.model.IsRootEntity(class) {
lni(w, 1, "def to_json(self) -> str:")
lni(w, 2, "return json.dumps(self.to_dict(), indent=2)")
ln(w)
}
// to_ref
if w.model.IsRefEntity(class) {
lni(w, 1, "def to_ref(self) -> 'Ref':")
lni(w, 2, "ref = Ref(id=self.id, name=self.name)")
if w.model.IsRootEntity(class) {
lni(w, 2, "ref.category = self.category")
}
if class.Name != "Ref" {
lni(w, 2, "ref.ref_type = RefType.get('"+class.Name+"')")
} else {
lni(w, 2, "ref.ref_type = self.ref_type")
}
lni(w, 2, "return ref")
ln(w)
}
w.writeFromDict(class)
// from_json
if w.model.IsRootEntity(class) {
lni(w, 1, "@staticmethod")
lni(w, 1, "def from_json(data: Union[str, bytes]) -> '", class.Name, "':")
lni(w, 2, "return "+class.Name+".from_dict(json.loads(data))")
ln(w)
}
ln(w)
}
func (w *pyw) writeFromDict(class *YamlClass) {
classOf := func(propType YamlPropType) string {
if propType.IsRef() {
return "Ref"
} else {
return string(propType)
}
}
lni(w, 1, "@staticmethod")
lni(w, 1, "def from_dict(d: Dict[str, Any]) -> '", class.Name, "':")
instance := strings.ToLower(toSnakeCase(class.Name))
lni(w, 2, instance, " = ", class.Name, "()")
// clear the fields that are set in __post_init__
if w.model.IsRootEntity(class) {
for _, field := range []string{"id", "last_change", "version"} {
lni(w, 2, instance, ".", field, " = None")
}
}
if class.Name == "Ref" {
lni(w, 2, instance, ".ref_type = RefType.get(d.get('@type', ''))")
}
for _, prop := range w.model.AllPropsOf(class) {
if prop.Name == "@type" {
continue
}
lni(w, 2, "if (v := d.get('", prop.Name, "')) or v is not None:")
propType := prop.PropType()
modelProp := instance + "." + prop.PyName()
if propType.IsPrimitive() ||
(propType.IsList() && propType.UnpackList().IsPrimitive()) ||
propType == "GeoJSON" {
// direct assignments for primitives, list of primitives, or GeoJson
// objects
lni(w, 3, modelProp, " = v")
} else if propType.IsEnumOf(w.model) {
// enum getters
lni(w, 3, modelProp, " = ", prop.Type, ".get(v)")
} else if propType.IsList() {
// list conversion for non-primitive types
u := propType.UnpackList()
lni(w, 3, modelProp, " = [", classOf(u), ".from_dict(e) for e in v]")
} else {
// from-dict calls for entity types
lni(w, 3, modelProp, " = ", classOf(propType), ".from_dict(v)")
}
}
lni(w, 2, "return "+instance)
ln(w)
}