Skip to content

Commit 02e6de4

Browse files
author
Jouke Witteveen
committed
Add suffix function to the fs module
1 parent b9a3044 commit 02e6de4

File tree

4 files changed

+35
-2
lines changed

4 files changed

+35
-2
lines changed

docs/markdown/Fs-module.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,13 +206,26 @@ fs.name('foo/bar/baz.dll.a') # baz.dll.a
206206
*since 0.54.0*
207207

208208
Returns the last component of the path, dropping the last part of the
209-
suffix
209+
suffix.
210210

211211
```meson
212212
fs.stem('foo/bar/baz.dll') # baz
213213
fs.stem('foo/bar/baz.dll.a') # baz.dll
214214
```
215215

216+
### suffix
217+
218+
*since 1.9.0*
219+
220+
Returns the last dot-separated portion of the final component of the path
221+
including the dot, if any.
222+
223+
```meson
224+
fs.suffix('foo/bar/baz.dll') # .dll
225+
fs.suffix('foo/bar/baz.dll.a') # .a
226+
fs.suffix('foo/bar') # (empty)
227+
```
228+
216229
### read
217230
- `read(path, encoding: 'utf-8')` *(since 0.57.0)*:
218231
return a [string](Syntax.md#strings) with the contents of the given `path`.

docs/markdown/snippets/fs_suffix.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
## Added suffix function to the FS module
2+
3+
The basename and stem were already available. For completeness, expose also the
4+
suffix.

mesonbuild/modules/fs.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class FSModule(ExtensionModule):
4444

4545
INFO = ModuleInfo('fs', '0.53.0')
4646

47-
def __init__(self, interpreter: 'Interpreter') -> None:
47+
def __init__(self, interpreter: Interpreter) -> None:
4848
super().__init__(interpreter)
4949
self.methods.update({
5050
'as_posix': self.as_posix,
@@ -64,6 +64,7 @@ def __init__(self, interpreter: 'Interpreter') -> None:
6464
'replace_suffix': self.replace_suffix,
6565
'size': self.size,
6666
'stem': self.stem,
67+
'suffix': self.suffix,
6768
})
6869

6970
def _absolute_dir(self, state: ModuleState, arg: FileOrString) -> str:
@@ -225,6 +226,13 @@ def stem(self, state: ModuleState, args: T.Tuple[T.Union[FileOrString, BuildTarg
225226
path = self._obj_to_pathstr('fs.name', args[0], state)
226227
return os.path.splitext(os.path.basename(path))[0]
227228

229+
@noKwargs
230+
@typed_pos_args('fs.suffix', (str, File, CustomTarget, CustomTargetIndex, BuildTarget))
231+
@FeatureNew('fs.suffix', '1.9.0')
232+
def suffix(self, state: ModuleState, args: T.Tuple[T.Union[FileOrString, BuildTargetTypes]], kwargs: T.Dict[str, T.Any]) -> str:
233+
path = self._obj_to_pathstr('fs.suffix', args[0], state)
234+
return os.path.splitext(path)[1]
235+
228236
@FeatureNew('fs.read', '0.57.0')
229237
@typed_pos_args('fs.read', (str, File))
230238
@typed_kwargs('fs.read', KwargInfo('encoding', str, default='utf-8'))

test cases/common/220 fs module/meson.build

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,10 @@ assert(fs.name(f[1]) == 'meson.build', 'failed to get basename')
154154
assert(fs.name('foo/bar/baz.dll.a') == 'baz.dll.a', 'failed to get basename with compound suffix')
155155
if host_machine.system() in ['cygwin', 'windows']
156156
assert(fs.name(btgt) == 'btgt.exe', 'failed to get basename of build target')
157+
assert(fs.suffix(btgt) == '.exe', 'failed to get build target suffix')
157158
else
158159
assert(fs.name(btgt) == 'btgt', 'failed to get basename of build target')
160+
assert(fs.suffix(btgt) == '', 'failed to get build target suffix')
159161
endif
160162
assert(fs.name(ctgt) == 'ctgt.txt', 'failed to get basename of custom target')
161163
assert(fs.name(ctgt[0]) == 'ctgt.txt', 'failed to get basename of custom target index')
@@ -164,6 +166,12 @@ assert(fs.stem('foo/bar/baz.dll.a') == 'baz.dll', 'failed to get stem with compo
164166
assert(fs.stem(btgt) == 'btgt', 'failed to get stem of build target')
165167
assert(fs.stem(ctgt) == 'ctgt', 'failed to get stem of custom target')
166168
assert(fs.stem(ctgt[0]) == 'ctgt', 'failed to get stem of custom target index')
169+
assert(fs.suffix('foo/bar/baz') == '', 'failed to get missing suffix')
170+
assert(fs.suffix('foo/bar/baz.') == '.', 'failed to get empty suffix')
171+
assert(fs.suffix('foo/bar/baz.dll') == '.dll', 'failed to get plain suffix')
172+
assert(fs.suffix('foo/bar/baz.dll.a') == '.a', 'failed to get final suffix')
173+
assert(fs.suffix(ctgt) == '.txt', 'failed to get suffix of custom target')
174+
assert(fs.suffix(ctgt[0]) == '.txt', 'failed to get suffix of custom target index')
167175

168176
# relative_to
169177
if build_machine.system() == 'windows'

0 commit comments

Comments
 (0)