Skip to content

Commit 64ba8ce

Browse files
committed
Fix docstrings of various fs submodules to pass the doctests
1 parent 8e7a2e9 commit 64ba8ce

10 files changed

Lines changed: 71 additions & 67 deletions

File tree

fs/_repr.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def make_repr(class_name, *args, **kwargs):
2727
>>> MyClass('Will')
2828
MyClass('foo', name='Will')
2929
>>> MyClass(None)
30-
MyClass()
30+
MyClass('foo')
3131
3232
"""
3333
arguments = [repr(arg) for arg in args]

fs/base.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -619,7 +619,7 @@ def download(self, path, file, chunk_size=None, **options):
619619
620620
Example:
621621
>>> with open('starwars.mov', 'wb') as write_file:
622-
... my_fs.download('/movies/starwars.mov', write_file)
622+
... my_fs.download('/Videos/starwars.mov', write_file)
623623
624624
"""
625625
with self._lock:
@@ -986,6 +986,7 @@ def lock(self):
986986
Example:
987987
>>> with my_fs.lock(): # May block
988988
... # code here has exclusive access to the filesystem
989+
... pass
989990
990991
It is a good idea to put a lock around any operations that you
991992
would like to be *atomic*. For instance if you are copying
@@ -1561,9 +1562,9 @@ def match(self, patterns, name):
15611562
names).
15621563
15631564
Example:
1564-
>>> home_fs.match(['*.py'], '__init__.py')
1565+
>>> my_fs.match(['*.py'], '__init__.py')
15651566
True
1566-
>>> home_fs.match(['*.jpg', '*.png'], 'foo.gif')
1567+
>>> my_fs.match(['*.jpg', '*.png'], 'foo.gif')
15671568
False
15681569
15691570
Note:

fs/filesize.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def traditional(size):
6161
`str`: A string containing an abbreviated file size and units.
6262
6363
Example:
64-
>>> filesize.traditional(30000)
64+
>>> fs.filesize.traditional(30000)
6565
'29.3 KB'
6666
6767
"""
@@ -87,7 +87,7 @@ def binary(size):
8787
`str`: A string containing a abbreviated file size and units.
8888
8989
Example:
90-
>>> filesize.binary(30000)
90+
>>> fs.filesize.binary(30000)
9191
'29.3 KiB'
9292
9393
"""
@@ -112,7 +112,7 @@ def decimal(size):
112112
`str`: A string containing a abbreviated file size and units.
113113
114114
Example:
115-
>>> filesize.decimal(30000)
115+
>>> fs.filesize.decimal(30000)
116116
'30.0 kB'
117117
118118
"""

fs/glob.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -172,9 +172,8 @@ def count(self):
172172
"""Count files / directories / data in matched paths.
173173
174174
Example:
175-
>>> import fs
176-
>>> fs.open_fs('~/projects').glob('**/*.py').count()
177-
Counts(files=18519, directories=0, data=206690458)
175+
>>> my_fs.glob('**/*.py').count()
176+
Counts(files=2, directories=0, data=55)
178177
179178
Returns:
180179
`~Counts`: A named tuple containing results.
@@ -199,9 +198,8 @@ def count_lines(self):
199198
`~LineCounts`: A named tuple containing line counts.
200199
201200
Example:
202-
>>> import fs
203-
>>> fs.open_fs('~/projects').glob('**/*.py').count_lines()
204-
LineCounts(lines=5767102, non_blank=4915110)
201+
>>> my_fs.glob('**/*.py').count_lines()
202+
LineCounts(lines=4, non_blank=3)
205203
206204
"""
207205
lines = 0
@@ -222,9 +220,8 @@ def remove(self):
222220
int: Number of file and directories removed.
223221
224222
Example:
225-
>>> import fs
226-
>>> fs.open_fs('~/projects/my_project').glob('**/*.pyc').remove()
227-
29
223+
>>> my_fs.glob('**/*.pyc').remove()
224+
2
228225
229226
"""
230227
removes = 0

fs/info.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,9 @@ def get(self, namespace, key, default=None): # noqa: F811
106106
is not found.
107107
108108
Example:
109-
>>> info.get('access', 'permissions')
110-
['u_r', 'u_w', '_wx']
109+
>>> info = my_fs.getinfo("foo.py", namespaces=["details"])
110+
>>> info.get('details', 'type')
111+
2
111112
112113
"""
113114
try:
@@ -189,12 +190,10 @@ def suffix(self):
189190
In case there is no suffix, an empty string is returned.
190191
191192
Example:
192-
>>> info
193-
<info 'foo.py'>
193+
>>> info = my_fs.getinfo("foo.py")
194194
>>> info.suffix
195195
'.py'
196-
>>> info2
197-
<info 'bar'>
196+
>>> info2 = my_fs.getinfo("bar")
198197
>>> info2.suffix
199198
''
200199
@@ -211,8 +210,7 @@ def suffixes(self):
211210
"""`List`: a list of any suffixes in the name.
212211
213212
Example:
214-
>>> info
215-
<info 'foo.tar.gz'>
213+
>>> info = my_fs.getinfo("foo.tar.gz")
216214
>>> info.suffixes
217215
['.tar', '.gz']
218216
@@ -228,8 +226,7 @@ def stem(self):
228226
"""`str`: the name minus any suffixes.
229227
230228
Example:
231-
>>> info
232-
<info 'foo.tar.gz'>
229+
>>> info = my_fs.getinfo("foo.tar.gz")
233230
>>> info.stem
234231
'foo'
235232

fs/opener/registry.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -260,10 +260,13 @@ def manage_fs(
260260
required logic for that.
261261
262262
Example:
263-
>>> def print_ls(list_fs):
264-
... '''List a directory.'''
265-
... with manage_fs(list_fs) as fs:
266-
... print(' '.join(fs.listdir()))
263+
The `~Registry.manage_fs` method can be used to define a small
264+
utility function::
265+
266+
>>> def print_ls(list_fs):
267+
... '''List a directory.'''
268+
... with manage_fs(list_fs) as fs:
269+
... print(' '.join(fs.listdir()))
267270
268271
This function may be used in two ways. You may either pass
269272
a ``str``, as follows::

fs/path.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
import re
1515
import typing
1616

17+
import six
18+
1719
from .errors import IllegalBackReference
1820

1921
if typing.TYPE_CHECKING:
@@ -64,9 +66,9 @@ def normpath(path):
6466
>>> normpath("/foo//bar/frob/../baz")
6567
'/foo/bar/baz'
6668
>>> normpath("foo/../../bar")
67-
Traceback (most recent call last)
69+
Traceback (most recent call last):
6870
...
69-
IllegalBackReference: path 'foo/../../bar' contains back-references outside of filesystem"
71+
fs.errors.IllegalBackReference: path 'foo/../../bar' contains back-references outside of filesystem
7072
7173
""" # noqa: E501
7274
if path in "/":
@@ -86,6 +88,7 @@ def normpath(path):
8688
else:
8789
components.append(component)
8890
except IndexError:
91+
# FIXME (@althonos): should be raised from the IndexError
8992
raise IllegalBackReference(path)
9093
return prefix + "/".join(components)
9194

fs/permissions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ class Permissions(object):
5858
>>> p.mode
5959
500
6060
>>> oct(p.mode)
61-
'0764'
61+
'0o764'
6262
6363
"""
6464

fs/walk.py

Lines changed: 33 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -146,24 +146,25 @@ def bind(cls, fs):
146146
Returns:
147147
~fs.walk.BoundWalker: a bound walker.
148148
149-
Example:
150-
>>> from fs import open_fs
151-
>>> from fs.walk import Walker
152-
>>> home_fs = open_fs('~/')
153-
>>> walker = Walker.bind(home_fs)
154-
>>> for path in walker.files(filter=['*.py']):
155-
... print(path)
156-
157-
Unless you have written a customized walker class, you will be
158-
unlikely to need to call this explicitly, as filesystem objects
159-
already have a ``walk`` attribute which is a bound walker
160-
object.
149+
Examples:
161150
162-
Example:
163-
>>> from fs import open_fs
164-
>>> home_fs = open_fs('~/')
165-
>>> for path in home_fs.walk.files(filter=['*.py']):
166-
... print(path)
151+
Use this method to explicitly bind a filesystem instance::
152+
153+
>>> walker = Walker.bind(my_fs)
154+
>>> for path in walker.files(filter=['*.py']):
155+
... print(path)
156+
/foo.py
157+
/bar.py
158+
159+
Unless you have written a customized walker class, you will
160+
be unlikely to need to call this explicitly, as filesystem
161+
objects already have a ``walk`` attribute which is a bound
162+
walker object::
163+
164+
>>> for path in my_fs.walk.files(filter=['*.py']):
165+
... print(path)
166+
/foo.py
167+
/bar.py
167168
168169
"""
169170
return BoundWalker(fs)
@@ -316,14 +317,16 @@ def walk(
316317
`~fs.info.Info` objects for directories and files in ``<path>``.
317318
318319
Example:
319-
>>> home_fs = open_fs('~/')
320320
>>> walker = Walker(filter=['*.py'])
321-
>>> namespaces = ['details']
322-
>>> for path, dirs, files in walker.walk(home_fs, namespaces)
321+
>>> for path, dirs, files in walker.walk(my_fs, namespaces=["details"]):
323322
... print("[{}]".format(path))
324323
... print("{} directories".format(len(dirs)))
325324
... total = sum(info.size for info in files)
326-
... print("{} bytes {}".format(total))
325+
... print("{} bytes".format(total))
326+
[/]
327+
2 directories
328+
55 bytes
329+
...
327330
328331
"""
329332
_path = abspath(normpath(path))
@@ -495,10 +498,9 @@ class BoundWalker(typing.Generic[_F]):
495498
`BoundWalker` object.
496499
497500
Example:
498-
>>> import fs
499-
>>> home_fs = fs.open_fs('~/')
500-
>>> home_fs.walk
501-
BoundWalker(OSFS('/Users/will', encoding='utf-8'))
501+
>>> tmp_fs = fs.tempfs.TempFS()
502+
>>> tmp_fs.walk
503+
BoundWalker(TempFS())
502504
503505
A `BoundWalker` is callable. Calling it is an alias for the
504506
`~fs.walk.BoundWalker.walk` method.
@@ -575,13 +577,16 @@ def walk(
575577
`~fs.info.Info` objects for directories and files in ``<path>``.
576578
577579
Example:
578-
>>> home_fs = open_fs('~/')
579580
>>> walker = Walker(filter=['*.py'])
580-
>>> for path, dirs, files in walker.walk(home_fs, namespaces=['details']):
581+
>>> for path, dirs, files in walker.walk(my_fs, namespaces=['details']):
581582
... print("[{}]".format(path))
582583
... print("{} directories".format(len(dirs)))
583584
... total = sum(info.size for info in files)
584-
... print("{} bytes {}".format(total))
585+
... print("{} bytes".format(total))
586+
[/]
587+
2 directories
588+
55 bytes
589+
...
585590
586591
This method invokes `Walker.walk` with bound `FS` object.
587592

fs/wrap.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,12 @@
22
33
Here's an example that opens a filesystem then makes it *read only*::
44
5-
>>> from fs import open_fs
6-
>>> from fs.wrap import read_only
7-
>>> projects_fs = open_fs('~/projects')
8-
>>> read_only_projects_fs = read_only(projects_fs)
9-
>>> read_only_projects_fs.remove('__init__.py')
5+
>>> home_fs = fs.open_fs('~')
6+
>>> read_only_home_fs = fs.wrap.read_only(home_fs)
7+
>>> read_only_home_fs.removedir('Desktop')
108
Traceback (most recent call last):
119
...
12-
fs.errors.ResourceReadOnly: resource '__init__.py' is read only
10+
fs.errors.ResourceReadOnly: resource 'Desktop' is read only
1311
1412
"""
1513

0 commit comments

Comments
 (0)