Skip to content

Commit b773f2e

Browse files
committed
stdlib/glob: first import
Signed-off-by: Sebastien Binet <binet@cern.ch>
1 parent 3f367aa commit b773f2e

File tree

5 files changed

+138
-0
lines changed

5 files changed

+138
-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: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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 norm(vs):
8+
if len(vs) == 0:
9+
return vs
10+
if type(vs[0]) == type(""):
11+
return normStr(vs)
12+
return normBytes(vs)
13+
14+
def normStr(vs):
15+
from os import sep
16+
x = []
17+
for v in vs:
18+
x.append(v.replace('/', sep))
19+
return x
20+
21+
def normBytes(vs):
22+
from os import sep
23+
x = []
24+
for v in vs:
25+
x.append(v.replace(b'/', bytes(sep, encoding="utf-8")))
26+
return x
27+
28+
def assertEqual(x, y):
29+
xx = norm(x)
30+
yy = norm(y)
31+
assert xx == yy, "got: %s, want: %s" % (repr(x), repr(y))
32+
33+
34+
## test strings
35+
assertEqual(glob.glob('*'), ["glob.go", "glob_test.go", "testdata"])
36+
assertEqual(glob.glob('*test*'), ["glob_test.go", "testdata"])
37+
assertEqual(glob.glob('*/test*'), ["testdata/test.py", "testdata/test_golden.txt"])
38+
assertEqual(glob.glob('*/test*_*'), ["testdata/test_golden.txt"])
39+
assertEqual(glob.glob('*/t??t*_*'), ["testdata/test_golden.txt"])
40+
assertEqual(glob.glob('*/t[e]?t*_*'), ["testdata/test_golden.txt"])
41+
assertEqual(glob.glob('*/t[oe]?t*_*'), ["testdata/test_golden.txt"])
42+
assertEqual(glob.glob('*/t[o]?t*_*'), [])
43+
44+
## FIXME(sbinet)
45+
## assertEqual(glob.glob('*/t[!o]?t*_*'), ["testdata/test_golden.txt"])
46+
47+
## test bytes
48+
assertEqual(glob.glob(b'*'), [b"glob.go", b"glob_test.go", b"testdata"])
49+
assertEqual(glob.glob(b'*test*'), [b"glob_test.go", b"testdata"])
50+
assertEqual(glob.glob(b'*/test*'), [b"testdata/test.py", b"testdata/test_golden.txt"])
51+
assertEqual(glob.glob(b'*/test*_*'), [b"testdata/test_golden.txt"])
52+
assertEqual(glob.glob(b'*/t??t*_*'), [b"testdata/test_golden.txt"])
53+
assertEqual(glob.glob(b'*/t[e]?t*_*'), [b"testdata/test_golden.txt"])
54+
assertEqual(glob.glob(b'*/t[oe]?t*_*'), [b"testdata/test_golden.txt"])
55+
assertEqual(glob.glob(b'*/t[o]?t*_*'), [])
56+
57+
## FIXME(sbinet)
58+
## 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
@@ -20,6 +20,7 @@ import (
2020

2121
_ "github.com/go-python/gpython/stdlib/binascii"
2222
_ "github.com/go-python/gpython/stdlib/builtin"
23+
_ "github.com/go-python/gpython/stdlib/glob"
2324
_ "github.com/go-python/gpython/stdlib/math"
2425
_ "github.com/go-python/gpython/stdlib/os"
2526
_ "github.com/go-python/gpython/stdlib/string"

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy