Skip to content

Commit ac6bca1

Browse files
author
Giampaolo Cimino
committed
docstring added for copy module and raise exception in geturl in osfs
1 parent 50d0a4b commit ac6bca1

File tree

2 files changed

+70
-12
lines changed

2 files changed

+70
-12
lines changed

fs/copy.py

Lines changed: 67 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,32 @@ def copy_fs(src_fs, dst_fs, walker=None, on_copy=None):
3030
in ``src_fs``. Set this if you only want to consider a sub-set
3131
of the resources in ``src_fs``.
3232
:type walker: :class:`~fs.walk.Walker`
33-
:returns: a list with the copied files dst_path.
34-
33+
:param on_copy: A function callback called after a single file copy is executed.
34+
:type on_copy: Function, with signature ``(src_fs, src_path, dst_fs, dst_path)``.
3535
"""
3636
return copy_dir(src_fs, '/', dst_fs, '/', walker=walker, on_copy=on_copy)
3737

3838

3939
def copy_fs_if_newer(src_fs, dst_fs, walker=None, on_copy=None):
40+
"""
41+
Copy the contents of one filesystem to another. If both source and destination files exist,
42+
the copy is executed only if the source file is newer than the destination file.
43+
In case modification times of source or destination files are not available,
44+
copy file is always executed.
45+
46+
:param src_fs: Source filesystem.
47+
:type src_fs: :type src_fs: FS URL or instance
48+
:param src_path: A path to a directory on ``src_fs``.
49+
:type src_path: str
50+
:param dst_fs: Destination filesystem.
51+
:type dst_fs: FS URL or instance
52+
:param walker: A walker object that will be used to scan for files
53+
in ``src_fs``. Set this if you only want to consider a sub-set
54+
of the resources in ``src_fs``.
55+
:type walker: :class:`~fs.walk.Walker`
56+
:param on_copy: A function callback called after a single file copy is executed.
57+
:type on_copy: Function, with signature ``(src_fs, src_path, dst_fs, dst_path)``.
58+
"""
4059
return copy_dir_if_newer(src_fs, '/', dst_fs, '/', walker=walker, on_copy=on_copy)
4160

4261

@@ -50,11 +69,8 @@ def _source_is_newer(src_fs, src_path, dst_fs, dst_path):
5069
:type src_path: str
5170
:param dst_fs: Destination filesystem.
5271
:type dst_fs: FS URL or instance
53-
:param walker: A walker object that will be used to scan for files
54-
in ``src_fs``. Set this if you only want to consider a sub-set
55-
of the resources in ``src_fs``.
56-
:type walker: :class:`~fs.walk.Walker`
57-
72+
:returns: True if source file is newer than destination file or
73+
file modification time cannot be determined. False otherwise.
5874
"""
5975
try:
6076
if not dst_fs.exists(dst_path):
@@ -79,9 +95,7 @@ def _source_is_newer(src_fs, src_path, dst_fs, dst_path):
7995
def copy_file(src_fs, src_path, dst_fs, dst_path):
8096
"""
8197
Copy a file from one filesystem to another.
82-
83-
If the destination exists, and is a file, it will be first
84-
truncated.
98+
If the destination exists, and is a file, it will be first truncated.
8599
86100
:param src_fs: Source filesystem.
87101
:type src_fs: FS URL or instance
@@ -91,6 +105,7 @@ def copy_file(src_fs, src_path, dst_fs, dst_path):
91105
:type dst_fs: FS URL or instance
92106
:param dst_path: Path to a file on ``dst_fs``.
93107
:type dst_path: str
108+
:returns: True if the file copy was executed, False otherwise.
94109
95110
"""
96111
with manage_fs(src_fs, writeable=False) as src_fs:
@@ -110,6 +125,25 @@ def copy_file(src_fs, src_path, dst_fs, dst_path):
110125

111126

112127
def copy_file_if_newer(src_fs, src_path, dst_fs, dst_path):
128+
"""
129+
Copy a file from one filesystem to another.
130+
If the destination exists, and is a file, it will be first truncated.
131+
If both source and destination files exist,
132+
the copy is executed only if the source file is newer than the destination file.
133+
In case modification times of source or destination files are not available,
134+
copy is always executed.
135+
136+
:param src_fs: Source filesystem.
137+
:type src_fs: FS URL or instance
138+
:param src_path: Path to a file on ``src_fs``.
139+
:type src_path: str
140+
:param dst_fs: Destination filesystem.
141+
:type dst_fs: FS URL or instance
142+
:param dst_path: Path to a file on ``dst_fs``.
143+
:type dst_path: str
144+
:returns: True if the file copy was executed, False otherwise.
145+
146+
"""
113147
with manage_fs(src_fs, writeable=False) as src_fs:
114148
with manage_fs(dst_fs, create=True) as dst_fs:
115149
if src_fs is dst_fs:
@@ -169,7 +203,8 @@ def copy_dir(src_fs, src_path, dst_fs, dst_path, walker=None, on_copy=None):
169203
in ``src_fs``. Set this if you only want to consider a sub-set
170204
of the resources in ``src_fs``.
171205
:type walker: :class:`~fs.walk.Walker`
172-
:returns: a list with the copied files dst_path.
206+
:param on_copy: A function callback called after a single file copy is executed.
207+
:type on_copy: Function, with signature ``(src_fs, src_path, dst_fs, dst_path)``.
173208
174209
"""
175210

@@ -203,6 +238,27 @@ def copy_dir(src_fs, src_path, dst_fs, dst_path, walker=None, on_copy=None):
203238

204239

205240
def copy_dir_if_newer(src_fs, src_path, dst_fs, dst_path, walker=None, on_copy=None):
241+
"""
242+
Copy a directory from one filesystem to another. If both source and destination files exist,
243+
the copy is executed only if the source file is newer than the destination file.
244+
In case modification times of source or destination files are not available,
245+
copy is always executed.
246+
247+
:param src_fs: Source filesystem.
248+
:type src_fs: FS URL or instance
249+
:param src_path: A path to a directory on ``src_fs``.
250+
:type src_path: str
251+
:param dst_fs: Destination filesystem.
252+
:type dst_fs: FS URL or instance
253+
:param str dst_path: A path to a directory on ``dst_fs``.
254+
:param walker: A walker object that will be used to scan for files
255+
in ``src_fs``. Set this if you only want to consider a sub-set
256+
of the resources in ``src_fs``.
257+
:type walker: :class:`~fs.walk.Walker`
258+
:param on_copy: A function callback called after a single file copy is executed.
259+
:type on_copy: Function, with signature ``(src_fs, src_path, dst_fs, dst_path)``.
260+
261+
"""
206262
walker = walker or Walker()
207263
_src_path = abspath(normpath(src_path))
208264
_dst_path = abspath(normpath(dst_path))

fs/osfs.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
from .permissions import Permissions
2828
from .error_tools import convert_os_errors
2929
from .mode import Mode, validate_open_mode
30-
30+
from .errors import NoURL
3131

3232
log = logging.getLogger('fs.osfs')
3333

@@ -303,6 +303,8 @@ def getsyspath(self, path):
303303
return sys_path
304304

305305
def geturl(self, path, purpose='download'):
306+
if purpose != 'download':
307+
raise NoURL(path, purpose)
306308
return "file://" + self.getsyspath(path)
307309

308310
def gettype(self, path):

0 commit comments

Comments
 (0)