Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
stdlib/glob: first import
Signed-off-by: Sebastien Binet <binet@cern.ch>
  • Loading branch information
sbinet committed May 24, 2022
commit 6c9f9e58775387175c21017d3285a37d48317053
64 changes: 64 additions & 0 deletions stdlib/glob/glob.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Copyright 2022 The go-python Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// Package glob provides the implementation of the python's 'glob' module.
package glob

import (
"path/filepath"

"github.com/go-python/gpython/py"
)

func init() {
py.RegisterModule(&py.ModuleImpl{
Info: py.ModuleInfo{
Name: "glob",
Doc: "Filename globbing utility.",
},
Methods: []*py.Method{
py.MustNewMethod("glob", glob, 0, glob_doc),
},
})
}

const glob_doc = `Return a list of paths matching a pathname pattern.
The pattern may contain simple shell-style wildcards a la
fnmatch. However, unlike fnmatch, filenames starting with a
dot are special cases that are not matched by '*' and '?'
patterns.`

func glob(self py.Object, args py.Tuple) (py.Object, error) {
var (
pypathname py.Object
)
err := py.ParseTuple(args, "s*:glob", &pypathname)
if err != nil {
return nil, err
}

var (
pathname string
cnv func(v string) py.Object
)
switch n := pypathname.(type) {
case py.String:
pathname = string(n)
cnv = func(v string) py.Object { return py.String(v) }
case py.Bytes:
pathname = string(n)
cnv = func(v string) py.Object { return py.Bytes(v) }
}
matches, err := filepath.Glob(pathname)
if err != nil {
return nil, err
}

lst := py.List{Items: make([]py.Object, len(matches))}
for i, v := range matches {
lst.Items[i] = cnv(v)
}

return &lst, nil
}
15 changes: 15 additions & 0 deletions stdlib/glob/glob_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright 2022 The go-python Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package glob_test

import (
"testing"

"github.com/go-python/gpython/pytest"
)

func TestGlob(t *testing.T) {
pytest.RunScript(t, "./testdata/test.py")
}
58 changes: 58 additions & 0 deletions stdlib/glob/testdata/test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# Copyright 2022 The go-python Authors. All rights reserved.
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file.

import glob

def norm(vs):
if len(vs) == 0:
return vs
if type(vs[0]) == type(""):
return normStr(vs)
return normBytes(vs)

def normStr(vs):
from os import sep
x = []
for v in vs:
x.append(v.replace('/', sep))
return x

def normBytes(vs):
from os import sep
x = []
for v in vs:
x.append(v.replace(b'/', bytes(sep, encoding="utf-8")))
return x

def assertEqual(x, y):
xx = norm(x)
yy = norm(y)
assert xx == yy, "got: %s, want: %s" % (repr(x), repr(y))


## test strings
assertEqual(glob.glob('*'), ["glob.go", "glob_test.go", "testdata"])
assertEqual(glob.glob('*test*'), ["glob_test.go", "testdata"])
assertEqual(glob.glob('*/test*'), ["testdata/test.py", "testdata/test_golden.txt"])
assertEqual(glob.glob('*/test*_*'), ["testdata/test_golden.txt"])
assertEqual(glob.glob('*/t??t*_*'), ["testdata/test_golden.txt"])
assertEqual(glob.glob('*/t[e]?t*_*'), ["testdata/test_golden.txt"])
assertEqual(glob.glob('*/t[oe]?t*_*'), ["testdata/test_golden.txt"])
assertEqual(glob.glob('*/t[o]?t*_*'), [])

## FIXME(sbinet)
## assertEqual(glob.glob('*/t[!o]?t*_*'), ["testdata/test_golden.txt"])

## test bytes
assertEqual(glob.glob(b'*'), [b"glob.go", b"glob_test.go", b"testdata"])
assertEqual(glob.glob(b'*test*'), [b"glob_test.go", b"testdata"])
assertEqual(glob.glob(b'*/test*'), [b"testdata/test.py", b"testdata/test_golden.txt"])
assertEqual(glob.glob(b'*/test*_*'), [b"testdata/test_golden.txt"])
assertEqual(glob.glob(b'*/t??t*_*'), [b"testdata/test_golden.txt"])
assertEqual(glob.glob(b'*/t[e]?t*_*'), [b"testdata/test_golden.txt"])
assertEqual(glob.glob(b'*/t[oe]?t*_*'), [b"testdata/test_golden.txt"])
assertEqual(glob.glob(b'*/t[o]?t*_*'), [])

## FIXME(sbinet)
## assertEqual(glob.glob(b'*/t[!o]?t*_*'), [b"testdata/test_golden.txt"])
Empty file.
1 change: 1 addition & 0 deletions stdlib/stdlib.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (

_ "github.com/go-python/gpython/stdlib/binascii"
_ "github.com/go-python/gpython/stdlib/builtin"
_ "github.com/go-python/gpython/stdlib/glob"
_ "github.com/go-python/gpython/stdlib/math"
_ "github.com/go-python/gpython/stdlib/os"
_ "github.com/go-python/gpython/stdlib/string"
Expand Down