44
44
#
45
45
#
46
46
47
+ # stdlib
47
48
import os
48
49
import pathlib
50
+ import shutil
51
+ from typing import AnyStr , Callable , Union
49
52
50
53
51
- def copytree ( src , dst , symlinks = False , ignore = None ):
54
+ def append ( var : AnyStr , filename : Union [ str , pathlib . Path , os . PathLike ] ):
52
55
"""
53
- Because shutil.copytree is borked
56
+ Append ``var`` to the file ``filename`` in the current directory.
54
57
55
- Some of this docstring from https://docs.python.org/3/library/shutil.html
58
+ .. warning::
59
+
60
+ This is currently untested
61
+
62
+ TODO: make this the file in the given directory, by default the current directory
63
+
64
+ :param var: The value to append to the file
65
+ :param filename: The file to append to
66
+ :type filename: str or pathlib.Path or os.PathLike
67
+ """
68
+
69
+ # TODO: docstring
70
+
71
+ with open (os .path .join (os .getcwd (), filename ), 'a' ) as f :
72
+ f .write (var )
73
+
74
+
75
+ def copytree (
76
+ src : Union [str , pathlib .Path , os .PathLike ],
77
+ dst : Union [str , pathlib .Path , os .PathLike ],
78
+ symlinks : bool = False ,
79
+ ignore : Callable = None ):
80
+ """
81
+ Alternative to :func:`shutil.copytree` to work in some situations where it doesn't.
56
82
57
83
:param src: Source file to copy
58
- :type src: str
84
+ :type src: str or pathlib.Path or os.PathLike
59
85
:param dst: Destination to copy file to
60
- :type dst: str
86
+ :type dst: str or pathlib.Path or os.PathLike
61
87
:param symlinks: Whether to represent symbolic links in the source as symbolic
62
88
links in the destination. If false or omitted, the contents and metadata
63
89
of the linked files are copied to the new tree. When symlinks is false,
@@ -66,7 +92,7 @@ def copytree(src, dst, symlinks=False, ignore=None):
66
92
the copy process. You can set the optional ignore_dangling_symlinks
67
93
flag to true if you want to silence this exception. Notice that this
68
94
option has no effect on platforms that don’t support :class:`python:os.symlink`.
69
- :type symlinks: bool
95
+ :type symlinks: bool, optional
70
96
:param ignore: A callable that will receive as its arguments the source
71
97
directory, and a list of its contents. The ignore callable will be
72
98
called once for each directory that is copied. The callable must return
@@ -76,40 +102,56 @@ def copytree(src, dst, symlinks=False, ignore=None):
76
102
:class:`python:shutil.ignore_patterns` can be used to create such a callable
77
103
that ignores names based on
78
104
glob-style patterns.
79
-
80
- :return:
81
- :rtype:
105
+ :type ignore: ~typing.Callable, optional
82
106
"""
83
107
84
- import shutil
85
-
86
108
for item in os .listdir (src ):
87
109
s = os .path .join (src , item )
88
110
d = os .path .join (dst , item )
89
111
if os .path .isdir (s ):
90
- return shutil .copytree (s , d , symlinks , ignore )
112
+ shutil .copytree (s , d , symlinks , ignore )
91
113
else :
92
- return shutil .copy2 (s , d )
114
+ shutil .copy2 (s , d )
93
115
94
116
95
- def maybe_make ( directory , mode = 0o777 , parents = False , exist_ok = False ):
117
+ def delete ( filename : Union [ str , pathlib . Path , os . PathLike ] ):
96
118
"""
97
- Create a directory at this given path, but only if the directory does
98
- not already exist.
119
+ Delete the file in the current directory.
120
+
121
+ .. warning::
122
+
123
+ This is currently untested
124
+
125
+ TODO: make this the file in the given directory, by default the current directory
126
+
127
+ :param filename: The file to delete
128
+ :type filename: str or pathlib.Path or os.PathLike
129
+ """
130
+
131
+ os .remove (os .path .join (os .getcwd (), filename ))
132
+
133
+
134
+ def maybe_make (
135
+ directory : Union [str , pathlib .Path , os .PathLike ],
136
+ mode = 0o777 ,
137
+ parents : bool = False ,
138
+ exist_ok : bool = False ):
139
+ """
140
+ Create a directory at this given path, but only if the directory does not already exist.
99
141
100
142
:param directory: Directory to create
101
- :type directory: str or pathlib.Path
143
+ :type directory: str or pathlib.Path or os.PathLike
102
144
:param mode: Combined with the process’ umask value to determine the file mode and access flags
103
145
:type mode:
104
146
:param parents: If ``False`` (the default), a missing parent raises a :class:`~python:FileNotFoundError`.
105
147
If ``True``, any missing parents of this path are created as needed; they are created with the
106
148
default permissions without taking mode into account (mimicking the POSIX mkdir -p command).
107
- :type parents: bool
149
+ :type parents: bool, optional
108
150
:param exist_ok: If ``False`` (the default), a :class:`~python:FileExistsError` is raised if the
109
151
target directory already exists. If ``True``, :class:`~python:FileExistsError` exceptions
110
152
will be ignored (same behavior as the POSIX mkdir -p command), but only if the last path
111
153
component is not an existing non-directory file.
112
- :type exist_ok: bool
154
+ :type exist_ok: bool, optional
113
155
"""
114
156
115
157
if not isinstance (directory , pathlib .Path ):
@@ -119,12 +161,12 @@ def maybe_make(directory, mode=0o777, parents=False, exist_ok=False):
119
161
directory .mkdir (mode , parents , exist_ok )
120
162
121
163
122
- def parent_path (path ) :
164
+ def parent_path (path : Union [ str , pathlib . Path , os . PathLike ]) -> pathlib . Path :
123
165
"""
124
166
Returns the path of the parent directory for the given file or directory
125
167
126
168
:param path: Path to find the parent for
127
- :type path: str or pathlib.Path
169
+ :type path: str or pathlib.Path or os.PathLike
128
170
129
171
:return: The parent directory
130
172
:rtype: pathlib.Path
@@ -136,46 +178,41 @@ def parent_path(path):
136
178
return path .parent
137
179
138
180
139
- # TODO: consolidate
140
- def relpath (path , relative_to = None ):
181
+ def read (filename : Union [str , pathlib .Path , os .PathLike ]) -> str :
141
182
"""
142
- Returns the path for the given file or directory relative to the given directory
183
+ Read a file in the current directory (in text mode)
143
184
144
- :param path: Path to find the relative path for
145
- :type path: str
146
- :param relative_to: The directory to find the path relative to.
147
- Defaults to the current working directory (i.e. os.getcwd())
148
- :type relative_to: str
185
+ .. warning::
186
+
187
+ This is currently untested
149
188
150
- :return:
189
+ TODO: make this the file in the given directory, by default the current directory
190
+
191
+ :param filename: The file to read from
192
+ :type filename: str or pathlib.Path or os.PathLike
193
+
194
+ :return: The contents of the file
195
+ :rtype: str
151
196
"""
152
197
153
- if relative_to is None :
154
- relative_to = os .getcwd ()
198
+ # TODO: docstring
155
199
156
- # if os.path.normpath(os.path.abspath(path)).startswith(
157
- # os.path.normpath(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))):
158
- if os .path .normpath (os .path .abspath (path )).startswith (
159
- os .path .normpath (os .path .dirname (os .path .dirname (os .path .abspath (relative_to ))))
160
- ):
161
- return os .path .relpath (os .path .abspath (path ))
162
- else :
163
- return os .path .abspath (path )
200
+ with open (os .path .join (os .getcwd (), filename )) as f :
201
+ return f .read ()
164
202
165
203
166
- def relpath2 (path , relative_to = None ):
204
+ def relpath (path : Union [ str , pathlib . Path , os . PathLike ], relative_to : Union [ str , pathlib . Path , os . PathLike ] = None ) -> pathlib . Path :
167
205
"""
168
206
Returns the path for the given file or directory relative to the given
169
- directory or, if that would require path traversal, returns the
170
- absolute path.
207
+ directory or, if that would require path traversal, returns the absolute path.
171
208
172
209
:param path: Path to find the relative path for
173
- :type path: str or pathlib.Path
210
+ :type path: str or pathlib.Path or os.PathLike
174
211
:param relative_to: The directory to find the path relative to.
175
212
Defaults to the current directory
176
- :type relative_to: str or pathlib.Path
213
+ :type relative_to: str or pathlib.Path or os.PathLike, optional
177
214
178
- :return:
215
+ :rtype: pathlib.Path
179
216
"""
180
217
181
218
if not isinstance (path , pathlib .Path ):
@@ -197,70 +234,20 @@ def relpath2(path, relative_to=None):
197
234
return abs_path
198
235
199
236
200
- def delete (filename ):
201
- """
202
- Delete the file in the current directory
237
+ relpath2 = relpath
203
238
204
- # TODO: make this the file in the given directory, by default the current directory
205
239
206
- :param filename:
207
-
208
- :return:
209
- """
210
-
211
- # TODO: docstring
212
-
213
- os .remove (os .path .join (os .getcwd (), filename ))
214
-
215
-
216
- def write (var , filename ):
240
+ def write (var : AnyStr , filename : Union [str , pathlib .Path , os .PathLike ]):
217
241
"""
218
242
Write a variable to file in the current directory
219
243
220
- # TODO: make this the file in the given directory, by default the current directory
221
-
222
- :param var:
223
- :param filename:
244
+ TODO: make this the file in the given directory, by default the current directory
224
245
225
- :return:
246
+ :param var: The value to write to the file
247
+ :param filename: The file to write to
248
+ :type filename: str or pathlib.Path or os.PathLike
226
249
"""
227
250
228
- # TODO: docstring
229
-
230
251
with open (os .path .join (os .getcwd (), filename ), 'w' ) as f :
231
252
f .write (var )
232
253
233
-
234
- def read (filename ):
235
- """
236
- Read a file in the current directory; Untested
237
-
238
- # TODO: make this the file in the given directory, by default the current directory
239
-
240
- :param filename:
241
-
242
- :return:
243
- """
244
-
245
- # TODO: docstring
246
-
247
- with open (os .path .join (os .getcwd (), filename )) as f :
248
- return f .read ()
249
-
250
-
251
- def append (var , filename ):
252
- """
253
- Append `var` to the file `filename` in the current directory; Untested
254
-
255
- # TODO: make this the file in the given directory, by default the current directory
256
-
257
- :param var:
258
- :param filename:
259
-
260
- :return:
261
- """
262
-
263
- # TODO: docstring
264
-
265
- with open (os .path .join (os .getcwd (), filename ), 'a' ) as f :
266
- f .write (var )
0 commit comments