Skip to content

Commit

Permalink
fetcher: input any
Browse files Browse the repository at this point in the history
  • Loading branch information
xushiwei committed May 18, 2024
1 parent 5b65669 commit 436d6e0
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 40 deletions.
16 changes: 8 additions & 8 deletions fetcher/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,27 @@ import (
"github.com/goplus/hdq"
)

// func(url string, doc hdq.NodeSet) <any-object>
// func(input any, doc hdq.NodeSet) <any-object>
type Conv = any

// -----------------------------------------------------------------------------

// Convert converts a html source to an object.
func Convert(conv reflect.Value, url string, source any) any {
func Convert(conv reflect.Value, input, source any) any {
doc := reflect.ValueOf(hdq.Source(source))
out := conv.Call([]reflect.Value{reflect.ValueOf(url), doc})
out := conv.Call([]reflect.Value{reflect.ValueOf(input), doc})
return out[0].Interface()
}

// -----------------------------------------------------------------------------

// New creates a new object from a html source by a registered converter.
func New(pageType string, url string, source any) any {
func New(pageType string, input, source any) any {
page, ok := convs[pageType]
if !ok {
panic("fetcher: unknown pageType - " + pageType)
}
return Convert(page.Conv, url, source)
return Convert(page.Conv, input, source)
}

// FromInput creates a new object from the html source with the specified input.
Expand All @@ -51,21 +51,21 @@ func FromInput(pageType string, input any) any {
panic("fetcher: unknown pageType - " + pageType)
}
url := page.URL(input)
return Convert(page.Conv, url, url)
return Convert(page.Conv, input, url)
}

// sitePageType represents a site page type.
type sitePageType struct {
Conv reflect.Value
URL func(any) string
URL func(input any) string
}

var (
convs = map[string]sitePageType{}
)

// Register registers a convType with a convert function.
func Register(pageType string, conv Conv, urlOf func(any) string) {
func Register(pageType string, conv Conv, urlOf func(input any) string) {
vConv := reflect.ValueOf(conv)
convs[pageType] = sitePageType{vConv, urlOf}
}
Expand Down
2 changes: 1 addition & 1 deletion fetcher/torch/_testdata/eye/out.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "eye",
"name": "",
"doc": "",
"sig": "(n, m=None, *, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False) → Tensor"
}
58 changes: 33 additions & 25 deletions fetcher/torch/gop_autogen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 9 additions & 4 deletions fetcher/torch/pysig_torch.gop
Original file line number Diff line number Diff line change
Expand Up @@ -30,24 +30,29 @@ const (

type Result struct {
Name string `json:"name"`
Type string `json:"type"`
Doc string `json:"doc"`
Sig string `json:"sig"`
URL string `json:"url,omitempty"`
}

// New creates a new Result from a html document.
func New(url string, doc hdq.NodeSet) Result {
func New(input any, doc hdq.NodeSet) Result {
name := input.(string)
url := name
if name != "" {
url = URL(input)
}
if doc.ok {
fn := doc.any.dl.class("py function")
decl := fn.firstElementChild.dt.text!
pos := strings.indexByte(decl, '(')
if pos > 0 {
name := strings.trimPrefix(decl[:pos], "torch.")
sig := decl[pos:]
return {strings.trimSpace(name), "", strings.trimRight(sig, spaces), url}
return {name, "function", "", strings.trimRight(sig, spaces), url}
}
}
return {"", "", "<NULL>", url}
return {name, "", "", "<NULL>", url}
}

// URL returns the input URL for the given name.
Expand Down
2 changes: 1 addition & 1 deletion tutorial/02-GithubRepos/gop_autogen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion tutorial/02-GithubRepos/repos.gop
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ type Result struct {
}

// New creates a new Result from a html document.
func New(url string, doc hdq.NodeSet) Result {
func New(_ any, doc hdq.NodeSet) Result {
divRepos := doc.any.div.id("user-repositories-list").one
repoList := divRepos.child.ul.one
repos := [newRepo(x) for x <- repoList.child.li]
Expand Down

0 comments on commit 436d6e0

Please sign in to comment.