Skip to content

Commit

Permalink
Merge pull request #44 from xushiwei/q
Browse files Browse the repository at this point in the history
hdq: TestText; refactor hdq/stream; TestGithub
  • Loading branch information
xushiwei authored May 18, 2024
2 parents 993a81b + 0b0efb1 commit 7e4877a
Show file tree
Hide file tree
Showing 18 changed files with 199 additions and 385 deletions.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,13 +1,4 @@
package repos

import (
"encoding/json"
"testing"

"github.com/goplus/hdq"
)

const outTestNew = `{
{
"Repos": [
{
"Repo": "/xushiwei/linguist",
Expand Down Expand Up @@ -251,17 +242,4 @@ const outTestNew = `{
}
],
"Next": "https://github.com/xushiwei?after=Y3Vyc29yOnYyOpK0MjAyMC0wNS0xMlQxMToxNTozN1rOD7hDgA%3D%3D\u0026tab=repositories"
}`

func TestNew(t *testing.T) {
doc := hdq.Source("zip://data.zip#index.htm")
ret := New(doc)
b, err := json.MarshalIndent(ret, "", "\t")
if err != nil {
t.Fatal("json.MarshalIndent:", err)
}
out := string(b)
if out != outTestNew {
t.Fatal("TestNew failed:", out)
}
}
}
2 changes: 2 additions & 0 deletions _testdata/text/eyesig/in.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<dt class="sig sig-object py" id="torch.eye">
<span class="sig-prename descclassname"><span class="pre">torch.</span></span><span class="sig-name descname"><span class="pre">eye</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">n</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">m</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">*</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">out</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">dtype</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">layout</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">torch.strided</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">device</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">requires_grad</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em><span class="sig-paren">)</span> <span class="sig-return"><span class="sig-return-icon">&#x2192;</span> <span class="sig-return-typehint"><a class="reference internal" href="../tensors.html#torch.Tensor" title="torch.Tensor"><span class="pre">Tensor</span></a></span></span><a class="headerlink" href="#torch.eye" title="Permalink to this definition"></a></dt>
1 change: 1 addition & 0 deletions _testdata/text/eyesig/out.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"torch.eye(n, m=None, *, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False) → Tensor¶"
43 changes: 43 additions & 0 deletions hdq_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
Copyright 2024 The GoPlus Authors (goplus.org)
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 hdq_test

import (
"testing"

"github.com/goplus/hdq"
"github.com/goplus/hdq/hdqtest"
"github.com/goplus/hdq/pysig/torch"

repos "github.com/goplus/hdq/tutorial/02-GithubRepos"
)

func textOf(doc hdq.NodeSet) (ret string) {
ret, _ = doc.Text__0()
return
}

func TestText(t *testing.T) {
hdqtest.FromDir(t, "", "./_testdata/text", textOf)
}

func TestGithub(t *testing.T) {
hdqtest.FromDir(t, "", "./_testdata/github", repos.New, "data.zip#index.htm", "zip")
}

func TestTorch(t *testing.T) {
hdqtest.FromDir(t, "", "./pysig/torch/_testdata", torch.New)
}
16 changes: 12 additions & 4 deletions hdqtest/hdqtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ import (
type Converter = any

// FromDir tests all html files in a directory.
func FromDir(t *testing.T, sel, relDir string, conv Converter) {
// optional params: [filename, scheme]
func FromDir(t *testing.T, sel, relDir string, conv Converter, params ...string) {
dir, err := os.Getwd()
if err != nil {
t.Fatal("Getwd failed:", err)
Expand All @@ -42,23 +43,30 @@ func FromDir(t *testing.T, sel, relDir string, conv Converter) {
t.Fatal("ReadDir failed:", err)
}
vConv := reflect.ValueOf(conv)
scheme, fname := "", "/in.html"
if len(params) > 0 {
fname = "/" + params[0]
if len(params) > 1 {
scheme = params[1] + ":"
}
}
for _, fi := range fis {
name := fi.Name()
if !fi.IsDir() || strings.HasPrefix(name, "_") {
continue
}
t.Run(name, func(t *testing.T) {
testFrom(t, dir+"/"+name, sel, vConv)
testFrom(t, dir+"/"+name, sel, vConv, fname, scheme)
})
}
}

func testFrom(t *testing.T, pkgDir, sel string, conv reflect.Value) {
func testFrom(t *testing.T, pkgDir, sel string, conv reflect.Value, fname, scheme string) {
if sel != "" && !strings.Contains(pkgDir, sel) {
return
}
log.Println("Parsing", pkgDir)
in := pkgDir + "/in.html"
in := scheme + pkgDir + fname
out := pkgDir + "/out.json"
b, err := os.ReadFile(out)
if err != nil {
Expand Down
27 changes: 23 additions & 4 deletions html_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ func lastChild(node *html.Node, nodeType html.NodeType) (p *html.Node, err error

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

const (
spaces = " \t\r\n"
)

// childEqualText returns true if the type of node's child is TextNode and it's Data equals `text`.
func childEqualText(node *html.Node, text string) bool {
p := node.FirstChild
Expand Down Expand Up @@ -104,7 +108,7 @@ func hasPrefixText(node *html.Node, text string) bool {
if node.Type != html.TextNode {
return false
}
return strings.Contains(strings.TrimLeft(node.Data, " \t\r\n"), text)
return strings.Contains(strings.TrimLeft(node.Data, spaces), text)
}

// exactText returns text of node if the type of node is TextNode.
Expand All @@ -125,26 +129,28 @@ func textOf(node *html.Node) string {
type textPrinter struct {
data []byte
notLineStart bool
hasSpace bool
}

func (p *textPrinter) printText(v string) {
func (p *textPrinter) printText(v string, hasRightSpace bool) {
if v == "" {
return
}
if p.notLineStart {
if p.notLineStart && p.hasSpace {
p.data = append(p.data, ' ')
} else {
p.notLineStart = true
}
p.data = append(p.data, v...)
p.hasSpace = hasRightSpace
}

func (p *textPrinter) printNode(node *html.Node) {
if node == nil {
return
}
if node.Type == html.TextNode {
p.printText(strings.Trim(node.Data, " \t\r\n¶"))
p.printText(textTrimRight(textTrimLeft(node.Data, &p.hasSpace)))
return
}
for child := node.FirstChild; child != nil; child = child.NextSibling {
Expand All @@ -157,4 +163,17 @@ func (p *textPrinter) printNode(node *html.Node) {
}
}

func textTrimLeft(v string, hasSpace *bool) string {
ret := strings.TrimLeft(v, spaces)
if len(v) != len(ret) {
*hasSpace = true
}
return ret
}

func textTrimRight(v string) (string, bool) {
ret := strings.TrimRight(v, spaces)
return ret, len(v) != len(ret)
}

// -----------------------------------------------------------------------------
2 changes: 1 addition & 1 deletion pysig/torch/_testdata/eye/out.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "eye",
"doc": "",
"sig": "( n , m = None , * , out = None , dtype = None , layout = torch.strided , device = None , requires_grad = False ) → Tensor"
"sig": "(n, m=None, *, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False) → Tensor"
}
3 changes: 3 additions & 0 deletions pysig/torch/_testdata/invalid/in.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<dl class="py function">
<dt class="sig sig-object py">foo</dt>
</dl>
5 changes: 5 additions & 0 deletions pysig/torch/_testdata/invalid/out.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "",
"doc": "",
"sig": "\u003cNULL\u003e"
}
35 changes: 18 additions & 17 deletions pysig/torch/gop_autogen.go

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

6 changes: 5 additions & 1 deletion pysig/torch/pysig_torch.gop
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ import (

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

const (
spaces = " \t\r\n¶"
)

type Result struct {
Name string `json:"name"`
Doc string `json:"doc"`
Expand All @@ -36,7 +40,7 @@ func New(doc hdq.NodeSet) Result {
if pos > 0 {
name := strings.trimPrefix(decl[:pos], "torch.")
sig := decl[pos:]
return {strings.trimSpace(name), "", strings.trimSpace(sig)}
return {strings.trimSpace(name), "", strings.trimRight(sig, spaces)}
}
return {"", "", "<NULL>"}
}
17 changes: 4 additions & 13 deletions stream/http/httpstrm.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,8 @@ var (

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

// Open opens a zipped file object.
func Open(file string) (io.ReadCloser, error) {
return httpOpen("http://" + file)
}

func Opens(file string) (io.ReadCloser, error) {
return httpOpen("https://" + file)
}

func httpOpen(url string) (io.ReadCloser, error) {
// Open opens a http file object.
func Open(url string) (io.ReadCloser, error) {
resp, err := httpGet(url)
if err != nil {
return nil, err
Expand All @@ -62,9 +54,8 @@ func httpGet(url string) (resp *http.Response, err error) {
}

func init() {
// http://path, https://path
stream.RegisterSchema("http", Open)
stream.RegisterSchema("https", Opens)
stream.Register("http", Open)
stream.Register("https", Open)
}

// -------------------------------------------------------------------------------------
42 changes: 42 additions & 0 deletions stream/inline/inline.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
Copyright 2024 The GoPlus Authors (goplus.org)
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 inline

import (
"io"
"strings"

"github.com/goplus/hdq/stream"
)

type nilCloser struct {
io.Reader
}

func (p *nilCloser) Close() error {
return nil
}

// Open opens a inline text object.
func Open(url string) (io.ReadCloser, error) {
file := strings.TrimPrefix(url, "inline:")
r := strings.NewReader(file)
return &nilCloser{r}, nil
}

func init() {
stream.Register("inline", Open)
}
Loading

0 comments on commit 7e4877a

Please sign in to comment.