Files
llgo/chore/_xtool/pydump/pydump.go

61 lines
1.7 KiB
Go
Raw Normal View History

2024-05-13 18:24:00 +08:00
/*
* Copyright (c) 2024 The GoPlus Authors (goplus.org). All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package main
import (
"github.com/goplus/llgo/c"
"github.com/goplus/llgo/py"
2024-05-13 19:48:33 +08:00
"github.com/goplus/llgo/py/inspect"
2024-05-14 13:23:21 +08:00
"github.com/goplus/llgo/x/cjson"
2024-05-13 18:24:00 +08:00
)
func main() {
if c.Argc < 2 {
2024-05-14 13:23:21 +08:00
c.Fprintf(c.Stderr, c.Str("Usage: pydump <pythonLibPath>\n"))
2024-05-13 18:24:00 +08:00
return
}
pyLib := c.Index(c.Argv, 1)
py.Initialize()
2024-05-14 13:23:21 +08:00
root := cjson.Object()
root.SetItem(c.Str("name"), cjson.String(pyLib))
items := cjson.Array()
2024-05-13 18:24:00 +08:00
mod := py.ImportModule(pyLib)
2024-05-14 00:31:30 +08:00
keys := mod.ModuleGetDict().DictKeys()
for i, n := uintptr(0), keys.ListLen(); i < n; i++ {
key := keys.ListItem(i)
val := mod.GetAttr(key)
doc := val.GetAttrString(c.Str("__doc__"))
2024-05-14 13:23:21 +08:00
sym := cjson.Object()
2024-05-14 23:56:55 +08:00
sym.SetItem(c.Str("type"), cjson.String(val.Type().TypeName().CStr()))
2024-05-14 13:23:21 +08:00
sym.SetItem(c.Str("name"), cjson.String(key.CStr()))
2024-05-14 23:56:55 +08:00
if doc != nil {
sym.SetItem(c.Str("doc"), cjson.String(doc.CStr()))
}
2024-05-14 17:22:36 +08:00
if val.Callable() != 0 {
2024-05-13 19:48:33 +08:00
sig := inspect.Signature(val)
2024-05-14 13:23:21 +08:00
sym.SetItem(c.Str("sig"), cjson.String(sig.Str().CStr()))
2024-05-13 19:48:33 +08:00
}
2024-05-14 13:23:21 +08:00
items.AddItem(sym)
2024-05-13 18:24:00 +08:00
}
2024-05-14 13:23:21 +08:00
root.SetItem(c.Str("items"), items)
c.Printf(c.Str("%s\n"), root.CStr())
2024-05-13 18:24:00 +08:00
}