Skip to content

Commit 4679429

Browse files
committed
stdlib/glob: first import
Signed-off-by: Sebastien Binet <[email protected]>
1 parent 6db2137 commit 4679429

File tree

5 files changed

+115
-0
lines changed

5 files changed

+115
-0
lines changed

stdlib/glob/glob.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// Copyright 2022 The go-python Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
// Package glob provides the implementation of the python's 'glob' module.
6+
package glob
7+
8+
import (
9+
"path/filepath"
10+
11+
"github.com/go-python/gpython/py"
12+
)
13+
14+
func init() {
15+
py.RegisterModule(&py.ModuleImpl{
16+
Info: py.ModuleInfo{
17+
Name: "glob",
18+
Doc: "Filename globbing utility.",
19+
},
20+
Methods: []*py.Method{
21+
py.MustNewMethod("glob", glob, 0, glob_doc),
22+
},
23+
})
24+
}
25+
26+
const glob_doc = `Return a list of paths matching a pathname pattern.
27+
The pattern may contain simple shell-style wildcards a la
28+
fnmatch. However, unlike fnmatch, filenames starting with a
29+
dot are special cases that are not matched by '*' and '?'
30+
patterns.`
31+
32+
func glob(self py.Object, args py.Tuple) (py.Object, error) {
33+
var (
34+
pypathname py.Object
35+
)
36+
err := py.ParseTuple(args, "s*:glob", &pypathname)
37+
if err != nil {
38+
return nil, err
39+
}
40+
41+
var (
42+
pathname string
43+
cnv func(v string) py.Object
44+
)
45+
switch n := pypathname.(type) {
46+
case py.String:
47+
pathname = string(n)
48+
cnv = func(v string) py.Object { return py.String(v) }
49+
case py.Bytes:
50+
pathname = string(n)
51+
cnv = func(v string) py.Object { return py.Bytes(v) }
52+
}
53+
matches, err := filepath.Glob(pathname)
54+
if err != nil {
55+
return nil, err
56+
}
57+
58+
lst := py.List{Items: make([]py.Object, len(matches))}
59+
for i, v := range matches {
60+
lst.Items[i] = cnv(v)
61+
}
62+
63+
return &lst, nil
64+
}

stdlib/glob/glob_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright 2022 The go-python Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package glob_test
6+
7+
import (
8+
"testing"
9+
10+
"github.com/go-python/gpython/pytest"
11+
)
12+
13+
func TestGlob(t *testing.T) {
14+
pytest.RunScript(t, "./testdata/test.py")
15+
}

stdlib/glob/testdata/test.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Copyright 2022 The go-python Authors. All rights reserved.
2+
# Use of this source code is governed by a BSD-style
3+
# license that can be found in the LICENSE file.
4+
5+
import glob
6+
7+
def assertEqual(x, y):
8+
assert x == y, "got: %s, want: %s" % (repr(x), repr(y))
9+
10+
11+
## test strings
12+
assertEqual(glob.glob('*'), ["glob.go", "glob_test.go", "testdata"])
13+
assertEqual(glob.glob('*test*'), ["glob_test.go", "testdata"])
14+
assertEqual(glob.glob('*/test*'), ["testdata/test.py", "testdata/test_golden.txt"])
15+
assertEqual(glob.glob('*/test*_*'), ["testdata/test_golden.txt"])
16+
assertEqual(glob.glob('*/t??t*_*'), ["testdata/test_golden.txt"])
17+
assertEqual(glob.glob('*/t[e]?t*_*'), ["testdata/test_golden.txt"])
18+
assertEqual(glob.glob('*/t[oe]?t*_*'), ["testdata/test_golden.txt"])
19+
assertEqual(glob.glob('*/t[o]?t*_*'), [])
20+
21+
## FIXME(sbinet)
22+
## assertEqual(glob.glob('*/t[!o]?t*_*'), ["testdata/test_golden.txt"])
23+
24+
## test bytes
25+
assertEqual(glob.glob(b'*'), [b"glob.go", b"glob_test.go", b"testdata"])
26+
assertEqual(glob.glob(b'*test*'), [b"glob_test.go", b"testdata"])
27+
assertEqual(glob.glob(b'*/test*'), [b"testdata/test.py", b"testdata/test_golden.txt"])
28+
assertEqual(glob.glob(b'*/test*_*'), [b"testdata/test_golden.txt"])
29+
assertEqual(glob.glob(b'*/t??t*_*'), [b"testdata/test_golden.txt"])
30+
assertEqual(glob.glob(b'*/t[e]?t*_*'), [b"testdata/test_golden.txt"])
31+
assertEqual(glob.glob(b'*/t[oe]?t*_*'), [b"testdata/test_golden.txt"])
32+
assertEqual(glob.glob(b'*/t[o]?t*_*'), [])
33+
34+
## FIXME(sbinet)
35+
## assertEqual(glob.glob(b'*/t[!o]?t*_*'), [b"testdata/test_golden.txt"])

stdlib/glob/testdata/test_golden.txt

Whitespace-only changes.

stdlib/stdlib.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"github.com/go-python/gpython/vm"
2020

2121
_ "github.com/go-python/gpython/stdlib/builtin"
22+
_ "github.com/go-python/gpython/stdlib/glob"
2223
_ "github.com/go-python/gpython/stdlib/math"
2324
_ "github.com/go-python/gpython/stdlib/string"
2425
_ "github.com/go-python/gpython/stdlib/sys"

0 commit comments

Comments
 (0)