50
50
import stat
51
51
from typing import IO , Callable , Optional , Union
52
52
53
+ PathLike = Union [str , pathlib .Path , os .PathLike ]
53
54
54
- def append (var : str , filename : Union [str , pathlib .Path , os .PathLike ]):
55
+
56
+ def append (var : str , filename : PathLike ):
55
57
"""
56
58
Append ``var`` to the file ``filename`` in the current directory.
57
59
@@ -63,26 +65,23 @@ def append(var: str, filename: Union[str, pathlib.Path, os.PathLike]):
63
65
64
66
:param var: The value to append to the file
65
67
:param filename: The file to append to
66
- :type filename: str or pathlib.Path or os.PathLike
67
68
"""
68
69
69
70
with open (os .path .join (os .getcwd (), filename ), 'a' ) as f :
70
71
f .write (var )
71
72
72
73
73
74
def copytree (
74
- src : Union [ str , pathlib . Path , os . PathLike ] ,
75
- dst : Union [ str , pathlib . Path , os . PathLike ] ,
75
+ src : PathLike ,
76
+ dst : PathLike ,
76
77
symlinks : bool = False ,
77
78
ignore : Optional [Callable ] = None ,
78
79
):
79
80
"""
80
81
Alternative to :func:`shutil.copytree` to work in some situations where it doesn't.
81
82
82
83
:param src: Source file to copy
83
- :type src: str or pathlib.Path or os.PathLike
84
84
:param dst: Destination to copy file to
85
- :type dst: str or pathlib.Path or os.PathLike
86
85
:param symlinks: Whether to represent symbolic links in the source as symbolic
87
86
links in the destination. If false or omitted, the contents and metadata
88
87
of the linked files are copied to the new tree. When symlinks is false,
@@ -101,7 +100,6 @@ def copytree(
101
100
:class:`python:shutil.ignore_patterns` can be used to create such a callable
102
101
that ignores names based on
103
102
glob-style patterns.
104
- :type ignore: ~typing.Callable, optional
105
103
"""
106
104
107
105
for item in os .listdir (src ):
@@ -113,7 +111,7 @@ def copytree(
113
111
shutil .copy2 (s , d )
114
112
115
113
116
- def delete (filename : Union [ str , pathlib . Path , os . PathLike ] ):
114
+ def delete (filename : PathLike ):
117
115
"""
118
116
Delete the file in the current directory.
119
117
@@ -124,14 +122,13 @@ def delete(filename: Union[str, pathlib.Path, os.PathLike]):
124
122
TODO: make this the file in the given directory, by default the current directory
125
123
126
124
:param filename: The file to delete
127
- :type filename: str or pathlib.Path or os.PathLike
128
125
"""
129
126
130
127
os .remove (os .path .join (os .getcwd (), filename ))
131
128
132
129
133
130
def maybe_make (
134
- directory : Union [ str , pathlib . Path , os . PathLike ] ,
131
+ directory : PathLike ,
135
132
mode = 0o777 ,
136
133
parents : bool = False ,
137
134
exist_ok : bool = False
@@ -140,7 +137,6 @@ def maybe_make(
140
137
Create a directory at this given path, but only if the directory does not already exist.
141
138
142
139
:param directory: Directory to create
143
- :type directory: str or pathlib.Path or os.PathLike
144
140
:param mode: Combined with the process’ umask value to determine the file mode and access flags
145
141
:type mode:
146
142
:param parents: If ``False`` (the default), a missing parent raises a :class:`~python:FileNotFoundError`.
@@ -161,15 +157,13 @@ def maybe_make(
161
157
directory .mkdir (mode , parents , exist_ok )
162
158
163
159
164
- def parent_path (path : Union [ str , pathlib . Path , os . PathLike ] ) -> pathlib .Path :
160
+ def parent_path (path : PathLike ) -> pathlib .Path :
165
161
"""
166
162
Returns the path of the parent directory for the given file or directory
167
163
168
164
:param path: Path to find the parent for
169
- :type path: str or pathlib.Path or os.PathLike
170
165
171
166
:return: The parent directory
172
- :rtype: pathlib.Path
173
167
"""
174
168
175
169
if not isinstance (path , pathlib .Path ):
@@ -178,7 +172,7 @@ def parent_path(path: Union[str, pathlib.Path, os.PathLike]) -> pathlib.Path:
178
172
return path .parent
179
173
180
174
181
- def read (filename : Union [ str , pathlib . Path , os . PathLike ] ) -> str :
175
+ def read (filename : PathLike ) -> str :
182
176
"""
183
177
Read a file in the current directory (in text mode)
184
178
@@ -189,7 +183,6 @@ def read(filename: Union[str, pathlib.Path, os.PathLike]) -> str:
189
183
TODO: make this the file in the given directory, by default the current directory
190
184
191
185
:param filename: The file to read from
192
- :type filename: str or pathlib.Path or os.PathLike
193
186
194
187
:return: The contents of the file
195
188
:rtype: str
@@ -202,20 +195,18 @@ def read(filename: Union[str, pathlib.Path, os.PathLike]) -> str:
202
195
203
196
204
197
def relpath (
205
- path : Union [ str , pathlib . Path , os . PathLike ] ,
206
- relative_to : Optional [Union [ str , pathlib . Path , os . PathLike ] ] = None
198
+ path : PathLike ,
199
+ relative_to : Optional [PathLike ] = None
207
200
) -> pathlib .Path :
208
201
"""
209
202
Returns the path for the given file or directory relative to the given
210
203
directory or, if that would require path traversal, returns the absolute path.
211
204
212
205
:param path: Path to find the relative path for
213
- :type path: str or pathlib.Path or os.PathLike
214
206
:param relative_to: The directory to find the path relative to.
215
207
Defaults to the current directory
216
- :type relative_to: str or pathlib.Path or os.PathLike, optional
217
208
218
- :rtype: pathlib.Path
209
+ :return:
219
210
"""
220
211
221
212
if not isinstance (path , pathlib .Path ):
@@ -240,29 +231,27 @@ def relpath(
240
231
relpath2 = relpath
241
232
242
233
243
- def write (var : str , filename : Union [ str , pathlib . Path , os . PathLike ]) :
234
+ def write (var : str , filename : PathLike ) -> None :
244
235
"""
245
236
Write a variable to file in the current directory
246
237
247
238
TODO: make this the file in the given directory, by default the current directory
248
239
249
240
:param var: The value to write to the file
250
241
:param filename: The file to write to
251
- :type filename: str or pathlib.Path or os.PathLike
252
242
"""
253
243
254
244
with open (os .path .join (os .getcwd (), filename ), 'w' ) as f :
255
245
f .write (var )
256
246
257
247
258
- def clean_writer (string : str , fp : IO [ str ]) :
248
+ def clean_writer (string : str , fp : IO ) -> None :
259
249
"""
260
- Write string to fp without trailing spaces
250
+ Write string to ``fp`` without trailing spaces
261
251
262
252
:param string:
263
- :type string:
253
+ :type string: str
264
254
:param fp:
265
- :type fp:
266
255
"""
267
256
268
257
buffer = []
@@ -278,13 +267,15 @@ def clean_writer(string: str, fp: IO[str]):
278
267
fp .write ("\n " )
279
268
280
269
281
- def make_executable (filename ) :
270
+ def make_executable (filename : PathLike ) -> None :
282
271
"""
283
272
Make the given file executable
284
273
285
274
:param filename:
286
- :type filename: str or pathlib.Path
287
275
"""
288
276
277
+ if not isinstance (filename , pathlib .Path ):
278
+ filename = pathlib .Path (filename )
279
+
289
280
st = os .stat (str (filename ))
290
281
os .chmod (str (filename ), st .st_mode | stat .S_IXUSR | stat .S_IXGRP | stat .S_IXOTH )
0 commit comments