python-fsutil 0.11.0__py3-none-any.whl → 0.13.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- fsutil/__init__.py +86 -5
- fsutil/metadata.py +1 -1
- {python_fsutil-0.11.0.dist-info → python_fsutil-0.13.0.dist-info}/METADATA +20 -4
- python_fsutil-0.13.0.dist-info/RECORD +8 -0
- {python_fsutil-0.11.0.dist-info → python_fsutil-0.13.0.dist-info}/WHEEL +1 -1
- python_fsutil-0.11.0.dist-info/RECORD +0 -8
- {python_fsutil-0.11.0.dist-info → python_fsutil-0.13.0.dist-info}/LICENSE.txt +0 -0
- {python_fsutil-0.11.0.dist-info → python_fsutil-0.13.0.dist-info}/top_level.txt +0 -0
fsutil/__init__.py
CHANGED
|
@@ -80,6 +80,7 @@ __all__ = [
|
|
|
80
80
|
"get_file_size_formatted",
|
|
81
81
|
"get_filename",
|
|
82
82
|
"get_parent_dir",
|
|
83
|
+
"get_permissions",
|
|
83
84
|
"get_unique_name",
|
|
84
85
|
"is_dir",
|
|
85
86
|
"is_empty",
|
|
@@ -113,6 +114,7 @@ __all__ = [
|
|
|
113
114
|
"replace_file",
|
|
114
115
|
"search_dirs",
|
|
115
116
|
"search_files",
|
|
117
|
+
"set_permissions",
|
|
116
118
|
"split_filename",
|
|
117
119
|
"split_filepath",
|
|
118
120
|
"split_path",
|
|
@@ -788,6 +790,17 @@ def get_parent_dir(path: PathIn, *, levels: int = 1) -> str:
|
|
|
788
790
|
return join_path(path, *([os.pardir] * max(1, levels)))
|
|
789
791
|
|
|
790
792
|
|
|
793
|
+
def get_permissions(path: PathIn) -> int:
|
|
794
|
+
"""
|
|
795
|
+
Get the file/directory permissions.
|
|
796
|
+
"""
|
|
797
|
+
path = _get_path(path)
|
|
798
|
+
assert_exists(path)
|
|
799
|
+
st_mode = os.stat(path).st_mode
|
|
800
|
+
permissions = int(str(oct(st_mode & 0o777))[2:])
|
|
801
|
+
return permissions
|
|
802
|
+
|
|
803
|
+
|
|
791
804
|
def get_unique_name(
|
|
792
805
|
path: PathIn,
|
|
793
806
|
*,
|
|
@@ -797,7 +810,7 @@ def get_unique_name(
|
|
|
797
810
|
separator: str = "-",
|
|
798
811
|
) -> str:
|
|
799
812
|
"""
|
|
800
|
-
|
|
813
|
+
Get a unique name for a directory/file ath the given directory path.
|
|
801
814
|
"""
|
|
802
815
|
path = _get_path(path)
|
|
803
816
|
assert_dir(path)
|
|
@@ -1282,6 +1295,16 @@ def search_files(path: PathIn, pattern: str = "**/*.*") -> list[str]:
|
|
|
1282
1295
|
return _filter_paths(path, _search_paths(path, pattern), predicate=is_file)
|
|
1283
1296
|
|
|
1284
1297
|
|
|
1298
|
+
def set_permissions(path: PathIn, value: int) -> None:
|
|
1299
|
+
"""
|
|
1300
|
+
Set the file/directory permissions.
|
|
1301
|
+
"""
|
|
1302
|
+
path = _get_path(path)
|
|
1303
|
+
assert_exists(path)
|
|
1304
|
+
permissions = int(str(value), 8) & 0o777
|
|
1305
|
+
os.chmod(path, permissions)
|
|
1306
|
+
|
|
1307
|
+
|
|
1285
1308
|
def split_filename(path: PathIn) -> tuple[str, str]:
|
|
1286
1309
|
"""
|
|
1287
1310
|
Split a filename and returns its basename and extension.
|
|
@@ -1314,12 +1337,58 @@ def split_path(path: PathIn) -> list[str]:
|
|
|
1314
1337
|
return names
|
|
1315
1338
|
|
|
1316
1339
|
|
|
1340
|
+
def _write_file_atomic(
|
|
1341
|
+
path: PathIn,
|
|
1342
|
+
content: str,
|
|
1343
|
+
*,
|
|
1344
|
+
append: bool = False,
|
|
1345
|
+
encoding: str = "utf-8",
|
|
1346
|
+
) -> None:
|
|
1347
|
+
path = _get_path(path)
|
|
1348
|
+
mode = "a" if append else "w"
|
|
1349
|
+
if append:
|
|
1350
|
+
content = read_file(path, encoding=encoding) + content
|
|
1351
|
+
dirpath, _ = split_filepath(path)
|
|
1352
|
+
try:
|
|
1353
|
+
with tempfile.NamedTemporaryFile(
|
|
1354
|
+
mode=mode,
|
|
1355
|
+
dir=dirpath,
|
|
1356
|
+
delete=True,
|
|
1357
|
+
encoding=encoding,
|
|
1358
|
+
) as file:
|
|
1359
|
+
file.write(content)
|
|
1360
|
+
file.flush()
|
|
1361
|
+
os.fsync(file.fileno())
|
|
1362
|
+
temp_path = file.name
|
|
1363
|
+
if exists(path):
|
|
1364
|
+
set_permissions(temp_path, get_permissions(path))
|
|
1365
|
+
os.replace(temp_path, path)
|
|
1366
|
+
except FileNotFoundError:
|
|
1367
|
+
# success - the NamedTemporaryFile has not been able
|
|
1368
|
+
# to remove the temp file on __exit__ because the temp file
|
|
1369
|
+
# has replaced atomically the file at path.
|
|
1370
|
+
pass
|
|
1371
|
+
|
|
1372
|
+
|
|
1373
|
+
def _write_file_non_atomic(
|
|
1374
|
+
path: PathIn,
|
|
1375
|
+
content: str,
|
|
1376
|
+
*,
|
|
1377
|
+
append: bool = False,
|
|
1378
|
+
encoding: str = "utf-8",
|
|
1379
|
+
) -> None:
|
|
1380
|
+
mode = "a" if append else "w"
|
|
1381
|
+
with open(path, mode, encoding=encoding) as file:
|
|
1382
|
+
file.write(content)
|
|
1383
|
+
|
|
1384
|
+
|
|
1317
1385
|
def write_file(
|
|
1318
1386
|
path: PathIn,
|
|
1319
1387
|
content: str,
|
|
1320
1388
|
*,
|
|
1321
1389
|
append: bool = False,
|
|
1322
1390
|
encoding: str = "utf-8",
|
|
1391
|
+
atomic: bool = False,
|
|
1323
1392
|
) -> None:
|
|
1324
1393
|
"""
|
|
1325
1394
|
Write file with the specified content at the given path.
|
|
@@ -1327,14 +1396,20 @@ def write_file(
|
|
|
1327
1396
|
path = _get_path(path)
|
|
1328
1397
|
assert_not_dir(path)
|
|
1329
1398
|
make_dirs_for_file(path)
|
|
1330
|
-
|
|
1331
|
-
|
|
1332
|
-
|
|
1399
|
+
write_file_func = _write_file_atomic if atomic else _write_file_non_atomic
|
|
1400
|
+
write_file_func(
|
|
1401
|
+
path,
|
|
1402
|
+
content,
|
|
1403
|
+
append=append,
|
|
1404
|
+
encoding=encoding,
|
|
1405
|
+
)
|
|
1333
1406
|
|
|
1334
1407
|
|
|
1335
1408
|
def write_file_json(
|
|
1336
1409
|
path: PathIn,
|
|
1337
1410
|
data: Any,
|
|
1411
|
+
encoding: str = "utf-8",
|
|
1412
|
+
atomic: bool = False,
|
|
1338
1413
|
**kwargs: Any,
|
|
1339
1414
|
) -> None:
|
|
1340
1415
|
"""
|
|
@@ -1351,4 +1426,10 @@ def write_file_json(
|
|
|
1351
1426
|
|
|
1352
1427
|
kwargs.setdefault("default", default_encoder)
|
|
1353
1428
|
content = json.dumps(data, **kwargs)
|
|
1354
|
-
write_file(
|
|
1429
|
+
write_file(
|
|
1430
|
+
path,
|
|
1431
|
+
content,
|
|
1432
|
+
append=False,
|
|
1433
|
+
encoding=encoding,
|
|
1434
|
+
atomic=atomic,
|
|
1435
|
+
)
|
fsutil/metadata.py
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: python-fsutil
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.13.0
|
|
4
4
|
Summary: high-level file-system operations for lazy devs.
|
|
5
5
|
Author-email: Fabio Caccamo <fabio.caccamo@gmail.com>
|
|
6
6
|
Maintainer-email: Fabio Caccamo <fabio.caccamo@gmail.com>
|
|
@@ -132,6 +132,7 @@ import fsutil
|
|
|
132
132
|
- [`get_file_size_formatted`](#get_file_size_formatted)
|
|
133
133
|
- [`get_filename`](#get_filename)
|
|
134
134
|
- [`get_parent_dir`](#get_parent_dir)
|
|
135
|
+
- [`get_permissions`](#get_permissions)
|
|
135
136
|
- [`get_unique_name`](#get_unique_name)
|
|
136
137
|
- [`is_dir`](#is_dir)
|
|
137
138
|
- [`is_empty`](#is_empty)
|
|
@@ -165,6 +166,7 @@ import fsutil
|
|
|
165
166
|
- [`replace_file`](#replace_file)
|
|
166
167
|
- [`search_dirs`](#search_dirs)
|
|
167
168
|
- [`search_files`](#search_files)
|
|
169
|
+
- [`set_permissions`](#set_permissions)
|
|
168
170
|
- [`split_filename`](#split_filename)
|
|
169
171
|
- [`split_filepath`](#split_filepath)
|
|
170
172
|
- [`split_path`](#split_path)
|
|
@@ -494,10 +496,17 @@ filename = fsutil.get_filename(path)
|
|
|
494
496
|
parent_dir = fsutil.get_parent_dir(path, levels=1)
|
|
495
497
|
```
|
|
496
498
|
|
|
499
|
+
#### `get_permissions`
|
|
500
|
+
|
|
501
|
+
```python
|
|
502
|
+
# Get the file/directory permissions.
|
|
503
|
+
permissions = fsutil.get_permissions(path)
|
|
504
|
+
```
|
|
505
|
+
|
|
497
506
|
#### `get_unique_name`
|
|
498
507
|
|
|
499
508
|
```python
|
|
500
|
-
#
|
|
509
|
+
# Get a unique name for a directory/file ath the given directory path.
|
|
501
510
|
unique_name = fsutil.get_unique_name(path, prefix="", suffix="", extension="", separator="-")
|
|
502
511
|
```
|
|
503
512
|
|
|
@@ -747,6 +756,13 @@ dirs = fsutil.search_dirs(path, pattern="**/*")
|
|
|
747
756
|
files = fsutil.search_files(path, pattern="**/*.*")
|
|
748
757
|
```
|
|
749
758
|
|
|
759
|
+
#### `set_permissions`
|
|
760
|
+
|
|
761
|
+
```python
|
|
762
|
+
# Set the file/directory permissions.
|
|
763
|
+
fsutil.set_permissions(path, 700)
|
|
764
|
+
```
|
|
765
|
+
|
|
750
766
|
#### `split_filename`
|
|
751
767
|
|
|
752
768
|
```python
|
|
@@ -772,14 +788,14 @@ path_names = fsutil.split_path(path)
|
|
|
772
788
|
|
|
773
789
|
```python
|
|
774
790
|
# Write file with the specified content at the given path.
|
|
775
|
-
fsutil.write_file(path, content, append=False, encoding="utf-8")
|
|
791
|
+
fsutil.write_file(path, content, append=False, encoding="utf-8", atomic=False)
|
|
776
792
|
```
|
|
777
793
|
|
|
778
794
|
#### `write_file_json`
|
|
779
795
|
|
|
780
796
|
```python
|
|
781
797
|
# Write a json file at the given path with the specified data encoded in json format.
|
|
782
|
-
fsutil.write_file_json(path, data, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, cls=None, indent=None, separators=None, default=None, sort_keys=False)
|
|
798
|
+
fsutil.write_file_json(path, data, encoding="utf-8", atomic=False, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, cls=None, indent=None, separators=None, default=None, sort_keys=False)
|
|
783
799
|
```
|
|
784
800
|
|
|
785
801
|
## Testing
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
fsutil/__init__.py,sha256=IpqVSsROxOpfRTEpHEgmB8Y0Ig0OwLcOEOvl_xNRlCg,39444
|
|
2
|
+
fsutil/metadata.py,sha256=K36K01JFKjpV25tf7qT9WchCaxT2E7DfEJ2Dx-3OQjk,266
|
|
3
|
+
fsutil/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
4
|
+
python_fsutil-0.13.0.dist-info/LICENSE.txt,sha256=IcPfOD_lonKcLMnPRhiWdQUPW_LYW-5TTDljwi3UXQg,1078
|
|
5
|
+
python_fsutil-0.13.0.dist-info/METADATA,sha256=AKjGMjwCxQkDP5gbvq-q2XUow99v_FL53oHh5ewS3vs,24628
|
|
6
|
+
python_fsutil-0.13.0.dist-info/WHEEL,sha256=oiQVh_5PnQM0E3gPdiz09WCNmwiHDMaGer_elqB3coM,92
|
|
7
|
+
python_fsutil-0.13.0.dist-info/top_level.txt,sha256=KGTSjNLPWmxnZt8BFMZQrpnhkO7DsKrYWoXK-Sf3wBQ,7
|
|
8
|
+
python_fsutil-0.13.0.dist-info/RECORD,,
|
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
fsutil/__init__.py,sha256=BfBvX3rKqyo_UiiovROK1KRyiUlFaHhsdwTpoeqqBvg,37421
|
|
2
|
-
fsutil/metadata.py,sha256=LaJ9f5wd3szp7udufkvrJDJItKAP0J5PuTYCYZC83XQ,266
|
|
3
|
-
fsutil/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
4
|
-
python_fsutil-0.11.0.dist-info/LICENSE.txt,sha256=IcPfOD_lonKcLMnPRhiWdQUPW_LYW-5TTDljwi3UXQg,1078
|
|
5
|
-
python_fsutil-0.11.0.dist-info/METADATA,sha256=3cI2HrIDbgjcKgCXw41E1_AjZIDuAx7wtZcOnmos_S0,24268
|
|
6
|
-
python_fsutil-0.11.0.dist-info/WHEEL,sha256=Xo9-1PvkuimrydujYJAjF7pCkriuXBpUPEjma1nZyJ0,92
|
|
7
|
-
python_fsutil-0.11.0.dist-info/top_level.txt,sha256=KGTSjNLPWmxnZt8BFMZQrpnhkO7DsKrYWoXK-Sf3wBQ,7
|
|
8
|
-
python_fsutil-0.11.0.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|