MCPcopy Create free account
hub / github.com/go-python/gopy / genVarGetter

Method genVarGetter

bind/gen_varconst.go:35–76  ·  view source on GitHub ↗
(v *Var)

Source from the content-addressed store, hash-verified

33}
34
35func (g *pyGen) genVarGetter(v *Var) {
36 gopkg := g.pkg.Name()
37 pkgname := g.cfg.Name
38 cgoFn := v.Name() // plain name is the getter
39 if g.cfg.RenameCase {
40 cgoFn = toSnakeCase(cgoFn)
41 }
42 qCgoFn := gopkg + "_" + cgoFn
43 qFn := "_" + pkgname + "." + qCgoFn
44 qVn := gopkg + "." + v.Name()
45
46 g.pywrap.Printf("def %s():\n", cgoFn)
47 g.pywrap.Indent()
48 g.pywrap.Printf("%s\n%s Gets Go Variable: %s\n%s\n%s\n", `"""`, cgoFn, qVn, v.doc, `"""`)
49 if v.sym.hasHandle() {
50 cvnm := v.sym.pyPkgId(g.pkg.pkg)
51 g.pywrap.Printf("return %s(handle=%s())\n", cvnm, qFn)
52 } else {
53 g.pywrap.Printf("return %s()\n", qFn)
54 }
55 g.pywrap.Outdent()
56 g.pywrap.Printf("\n")
57
58 g.gofile.Printf("//export %s\n", qCgoFn)
59 g.gofile.Printf("func %s() %s {\n", qCgoFn, v.sym.cgoname)
60 g.gofile.Indent()
61 g.gofile.Printf("return ")
62 if v.sym.go2py != "" {
63 if v.sym.hasHandle() && !v.sym.isPtrOrIface() {
64 g.gofile.Printf("%s(&%s)%s", v.sym.go2py, qVn, v.sym.go2pyParenEx)
65 } else {
66 g.gofile.Printf("%s(%s)%s", v.sym.go2py, qVn, v.sym.go2pyParenEx)
67 }
68 } else {
69 g.gofile.Printf("%s", qVn)
70 }
71 g.gofile.Printf("\n")
72 g.gofile.Outdent()
73 g.gofile.Printf("}\n\n")
74
75 g.pybuild.Printf("mod.add_function('%s', retval('%s'), [])\n", qCgoFn, v.sym.cpyname)
76}
77
78func (g *pyGen) genVarSetter(v *Var) {
79 gopkg := g.pkg.Name()

Callers 1

genVarMethod · 0.95

Calls 8

toSnakeCaseFunction · 0.85
PrintfMethod · 0.80
IndentMethod · 0.80
hasHandleMethod · 0.80
pyPkgIdMethod · 0.80
OutdentMethod · 0.80
isPtrOrIfaceMethod · 0.80
NameMethod · 0.45

Tested by

no test coverage detected