Skip to content

Commit b86bcc7

Browse files
committed
changed behaviour of ext / suffixes
1 parent 226d857 commit b86bcc7

File tree

5 files changed

+69
-8
lines changed

5 files changed

+69
-8
lines changed

CHANGELOG.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,17 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
99
### Added
1010

1111
- Typing information
12-
- Added ext attribute to info
12+
- Added Info.suffix, Info.suffixes, Info.stem attributes
1313

1414
### Fixed
1515

1616
- Fixed issue with implied directories in TarFS
1717

18+
### Changed
19+
20+
- Changed path.splitext so that 'leading periods on the basename are
21+
ignored', which is the behaviour of os.path.splitext
22+
1823
## [2.0.20] - 2018-03-13
1924

2025
### Fixed

fs/info.py

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -183,17 +183,47 @@ def name(self):
183183
return self.get('basic', 'name')
184184

185185
@property
186-
def ext(self):
186+
def suffix(self):
187187
# type: () -> Text
188-
"""`str`: the resource extension (including dot).
188+
"""`str`: the final resource extension (including dot), or an
189+
empty string if there is no extension.
189190
190191
Example:
191-
>>> info.ext
192+
>>> info
193+
<info 'foo.py'>
194+
>>> info.suffix
192195
'.py'
193196
"""
194197
name = self.get('basic', 'name')
198+
if name.startswith('.') and name.count('.') == 1:
199+
return ''
195200
basename, dot, ext = name.rpartition('.')
196-
return dot + ext if dot else ''
201+
return '.' + ext if dot else ''
202+
203+
@property
204+
def suffixes(self):
205+
# type: () -> List[Text]
206+
"""`List`: a list of the resource's extensions.
207+
208+
Example:
209+
>>> info
210+
<info 'foo.tar.gz'>
211+
>>> info.suffixes
212+
['.tar', '.py']
213+
"""
214+
name = self.get('basic', 'name')
215+
if name.startswith('.') and name.count('.') == 1:
216+
return []
217+
return ['.' + suffix for suffix in name.split('.')[1:]]
218+
219+
@property
220+
def stem(self):
221+
# type: () -> Text
222+
"""`str`: the name minus any extensions."""
223+
name = self.get('basic', 'name')
224+
if name.startswith('.'):
225+
return name
226+
return name.split('.')[0]
197227

198228
@property
199229
def is_dir(self):

fs/path.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,9 +336,13 @@ def splitext(path):
336336
('baz', '.txt')
337337
>>> splitext('foo/bar/baz.txt')
338338
('foo/bar/baz', '.txt')
339+
>>> splitext('foo/bar/.foo')
340+
('foo/bar/.foo', '')
339341
340342
"""
341343
parent_path, pathname = split(path)
344+
if pathname.startswith('.') and pathname.count('.') == 1:
345+
return path, ''
342346
if '.' not in pathname:
343347
return path, ''
344348
pathname, ext = pathname.rsplit('.', 1)

tests/test_info.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ def test_basic(self):
7575
self.assertIsInstance(info.is_dir, bool)
7676
self.assertFalse(info.is_dir)
7777
self.assertEqual(repr(info), "<file 'bar.py'>")
78-
self.assertEqual(info.ext, '.py')
78+
self.assertEqual(info.suffix, '.py')
7979

8080
# Check dir
8181
info = Info({
@@ -86,7 +86,7 @@ def test_basic(self):
8686
})
8787
self.assertTrue(info.is_dir)
8888
self.assertEqual(repr(info), "<dir 'foo'>")
89-
self.assertEqual(info.ext, '')
89+
self.assertEqual(info.suffix, '')
9090

9191
def test_details(self):
9292
dates = [
@@ -137,3 +137,24 @@ def test_get(self):
137137
info = Info({'baz': {}})
138138
self.assertIsNone(info.get('foo', 'bar'))
139139
self.assertIsNone(info.get('baz', 'bar'))
140+
141+
def test_suffix(self):
142+
info = Info({
143+
'basic': {'name': 'foo.tar.gz'}
144+
})
145+
self.assertEqual(info.suffix, '.gz')
146+
self.assertEqual(info.suffixes, ['.tar', '.gz'])
147+
self.assertEqual(info.stem, 'foo')
148+
info = Info({
149+
'basic': {'name': 'foo'}
150+
})
151+
self.assertEqual(info.suffix, '')
152+
self.assertEqual(info.suffixes, [])
153+
self.assertEqual(info.stem, 'foo')
154+
155+
info = Info({
156+
'basic': {'name': '.foo'}
157+
})
158+
self.assertEqual(info.suffix, '')
159+
self.assertEqual(info.suffixes, [])
160+
self.assertEqual(info.stem, '.foo')

tests/test_path.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ def test_splitext(self):
135135
self.assertEqual(splitext('foo.bar'), ('foo', '.bar'))
136136
self.assertEqual(splitext('foo.bar.baz'), ('foo.bar', '.baz'))
137137
self.assertEqual(splitext('foo'), ('foo', ''))
138+
self.assertEqual(splitext('.foo'), ('.foo', ''))
138139

139140
def test_recursepath(self):
140141
self.assertEquals(recursepath("/"), ["/"])
@@ -147,7 +148,7 @@ def test_recursepath(self):
147148

148149
def test_isbase(self):
149150
self.assertTrue(isbase('foo', 'foo/bar'))
150-
self.assertFalse(isbase('baz', 'foo/bar'))
151+
self.assertFalse(isbase('baz', 'foo/bar'))
151152

152153
def test_isparent(self):
153154
self.assertTrue(isparent("foo/bar", "foo/bar/spam.txt"))

0 commit comments

Comments
 (0)