fs: Add parent() and name() methods
This commit is contained in:
parent
6963589576
commit
a6f7a1d8c4
|
@ -68,6 +68,8 @@ fs.samefile(x, z) # true
|
||||||
|
|
||||||
## Filename modification
|
## Filename modification
|
||||||
|
|
||||||
|
The files need not actually exist yet for this method, as it's just string manipulation.
|
||||||
|
|
||||||
### replace_suffix
|
### replace_suffix
|
||||||
|
|
||||||
The `replace_suffix` method is a *string manipulation* convenient for filename modifications.
|
The `replace_suffix` method is a *string manipulation* convenient for filename modifications.
|
||||||
|
@ -101,4 +103,10 @@ original = '/opt/foo.dll.a'
|
||||||
new = fs.replace_suffix(original, '') # /opt/foo.dll
|
new = fs.replace_suffix(original, '') # /opt/foo.dll
|
||||||
```
|
```
|
||||||
|
|
||||||
The files need not actually exist yet for this method, as it's just string manipulation.
|
### parent
|
||||||
|
|
||||||
|
Returns the parent directory (i.e. dirname).
|
||||||
|
|
||||||
|
### name
|
||||||
|
|
||||||
|
Returns the last component of the path (i.e. basename).
|
||||||
|
|
|
@ -118,6 +118,23 @@ class FSModule(ExtensionModule):
|
||||||
new = original.with_suffix(args[1])
|
new = original.with_suffix(args[1])
|
||||||
return ModuleReturnValue(str(new), [])
|
return ModuleReturnValue(str(new), [])
|
||||||
|
|
||||||
|
@stringArgs
|
||||||
|
@noKwargs
|
||||||
|
def parent(self, state: 'ModuleState', args: typing.Sequence[str], kwargs: dict) -> ModuleReturnValue:
|
||||||
|
if len(args) != 1:
|
||||||
|
MesonException('method takes exactly one argument.')
|
||||||
|
original = PurePath(args[0])
|
||||||
|
new = original.parent
|
||||||
|
return ModuleReturnValue(str(new), [])
|
||||||
|
|
||||||
|
@stringArgs
|
||||||
|
@noKwargs
|
||||||
|
def name(self, state: 'ModuleState', args: typing.Sequence[str], kwargs: dict) -> ModuleReturnValue:
|
||||||
|
if len(args) != 1:
|
||||||
|
MesonException('method takes exactly one argument.')
|
||||||
|
original = PurePath(args[0])
|
||||||
|
new = original.name
|
||||||
|
return ModuleReturnValue(str(new), [])
|
||||||
|
|
||||||
def initialize(*args, **kwargs) -> FSModule:
|
def initialize(*args, **kwargs) -> FSModule:
|
||||||
return FSModule(*args, **kwargs)
|
return FSModule(*args, **kwargs)
|
||||||
|
|
|
@ -69,4 +69,7 @@ if not is_windows and build_machine.system() != 'cygwin'
|
||||||
assert(fs.samefile('a_symlink', 'meson.build'), 'symlink samefile fail')
|
assert(fs.samefile('a_symlink', 'meson.build'), 'symlink samefile fail')
|
||||||
endif
|
endif
|
||||||
|
|
||||||
|
assert(fs.parent('foo/bar') == 'foo', 'failed to get dirname')
|
||||||
|
assert(fs.name('foo/bar') == 'bar', 'failed to get basename')
|
||||||
|
|
||||||
subdir('subdir')
|
subdir('subdir')
|
||||||
|
|
Loading…
Reference in New Issue