github.com/emcfarlane/larking@v0.0.0-20220605172417-1704b45ee6c3/starlib/starlarkpath/path.go (about)

     1  package starlarkpath
     2  
     3  import (
     4  	"fmt"
     5  	"path"
     6  
     7  	"github.com/emcfarlane/larking/starlib/starext"
     8  	"github.com/emcfarlane/larking/starlib/starlarkstruct"
     9  	"go.starlark.net/starlark"
    10  )
    11  
    12  func NewModule() *starlarkstruct.Module {
    13  	return &starlarkstruct.Module{
    14  		Name: "errors",
    15  		Members: starlark.StringDict{
    16  			"base":   starext.MakeBuiltin("path.base", Base),
    17  			"clean":  starext.MakeBuiltin("path.clean", Clean),
    18  			"dir":    starext.MakeBuiltin("path.dir", Dir),
    19  			"ext":    starext.MakeBuiltin("path.ext", Ext),
    20  			"is_abs": starext.MakeBuiltin("path.is_abs", IsAbs),
    21  			"join":   starext.MakeBuiltin("path.join", Join),
    22  			"match":  starext.MakeBuiltin("path.match", Match),
    23  			"split":  starext.MakeBuiltin("path.split", Split),
    24  		},
    25  	}
    26  }
    27  
    28  func Base(thread *starlark.Thread, fnname string, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
    29  	var base string
    30  	if err := starlark.UnpackArgs(fnname, args, kwargs, "base", &base); err != nil {
    31  		return nil, err
    32  	}
    33  	return starlark.String(path.Base(base)), nil
    34  }
    35  func Clean(thread *starlark.Thread, fnname string, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
    36  	var clean string
    37  	if err := starlark.UnpackArgs(fnname, args, kwargs, "clean", &clean); err != nil {
    38  		return nil, err
    39  	}
    40  	return starlark.String(path.Clean(clean)), nil
    41  }
    42  func Dir(thread *starlark.Thread, fnname string, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
    43  	var dir string
    44  	if err := starlark.UnpackArgs(fnname, args, kwargs, "dir", &dir); err != nil {
    45  		return nil, err
    46  	}
    47  	return starlark.String(path.Dir(dir)), nil
    48  }
    49  func Ext(thread *starlark.Thread, fnname string, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
    50  	var ext string
    51  	if err := starlark.UnpackArgs(fnname, args, kwargs, "ext", &ext); err != nil {
    52  		return nil, err
    53  	}
    54  	return starlark.String(path.Ext(ext)), nil
    55  }
    56  func IsAbs(thread *starlark.Thread, fnname string, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
    57  	var name string
    58  	if err := starlark.UnpackArgs(fnname, args, kwargs, "path", &name); err != nil {
    59  		return nil, err
    60  	}
    61  	return starlark.Bool(path.IsAbs(name)), nil
    62  }
    63  func Join(thread *starlark.Thread, fnname string, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
    64  	n := len(args)
    65  	strs := make([]string, n)
    66  	for i := 0; i < n; i++ {
    67  		arg := args.Index(i)
    68  		str, ok := starlark.AsString(arg)
    69  		if !ok {
    70  			return nil, fmt.Errorf("expected string, got %v", arg.Type())
    71  		}
    72  		strs = append(strs, str)
    73  	}
    74  	return starlark.String(path.Join(strs...)), nil
    75  }
    76  func Match(thread *starlark.Thread, fnname string, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
    77  	var pattern, name string
    78  	if err := starlark.UnpackArgs(fnname, args, kwargs, "pattern", &pattern, "name", &name); err != nil {
    79  		return nil, err
    80  	}
    81  	ok, err := path.Match(pattern, name)
    82  	return starlark.Bool(ok), err
    83  }
    84  func Split(thread *starlark.Thread, fnname string, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
    85  	var name string
    86  	if err := starlark.UnpackArgs(fnname, args, kwargs, "path", &name); err != nil {
    87  		return nil, err
    88  	}
    89  	dir, file := path.Split(name)
    90  	return starlark.Tuple{starlark.String(dir), starlark.String(file)}, nil
    91  }