github.com/goplus/llgo@v0.8.3/chore/_xtool/pydump/pydump.go (about) 1 /* 2 * Copyright (c) 2024 The GoPlus Authors (goplus.org). All rights reserved. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package main 18 19 import ( 20 "github.com/goplus/llgo/c" 21 "github.com/goplus/llgo/c/cjson" 22 "github.com/goplus/llgo/py" 23 "github.com/goplus/llgo/py/inspect" 24 ) 25 26 func main() { 27 if c.Argc < 2 { 28 c.Fprintf(c.Stderr, c.Str("Usage: pydump <pythonLibPath>\n")) 29 return 30 } 31 pyLib := c.Index(c.Argv, 1) 32 33 py.Initialize() 34 35 root := cjson.Object() 36 root.SetItem(c.Str("name"), cjson.String(pyLib)) 37 38 items := cjson.Array() 39 mod := py.ImportModule(pyLib) 40 keys := mod.ModuleGetDict().DictKeys() 41 for i, n := uintptr(0), keys.ListLen(); i < n; i++ { 42 key := keys.ListItem(i) 43 val := mod.GetAttr(key) 44 doc := val.GetAttrString(c.Str("__doc__")) 45 sym := cjson.Object() 46 sym.SetItem(c.Str("type"), cjson.String(val.Type().TypeName().CStr())) 47 sym.SetItem(c.Str("name"), cjson.String(key.CStr())) 48 if doc != nil { 49 sym.SetItem(c.Str("doc"), cjson.String(doc.CStr())) 50 } 51 if val.Callable() != 0 { 52 sig := inspect.Signature(val) 53 sym.SetItem(c.Str("sig"), cjson.String(sig.Str().CStr())) 54 } 55 items.AddItem(sym) 56 } 57 root.SetItem(c.Str("items"), items) 58 59 c.Printf(c.Str("%s\n"), root.CStr()) 60 }