File size: 52,716 Bytes
fa6fb51 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 |
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
# cython: language_level = 3
from cpython.datetime cimport datetime, PyDateTime_DateTime
from cython cimport binding
from pyarrow.includes.common cimport *
from pyarrow.includes.libarrow_python cimport PyDateTime_to_TimePoint
from pyarrow.lib import _detect_compression, frombytes, tobytes
from pyarrow.lib cimport *
from pyarrow.util import _stringify_path
from abc import ABC, abstractmethod
from datetime import datetime, timezone
import os
import pathlib
import sys
cdef _init_ca_paths():
cdef CFileSystemGlobalOptions options
import ssl
paths = ssl.get_default_verify_paths()
if paths.cafile:
options.tls_ca_file_path = os.fsencode(paths.cafile)
if paths.capath:
options.tls_ca_dir_path = os.fsencode(paths.capath)
check_status(CFileSystemsInitialize(options))
if sys.platform == 'linux':
# ARROW-9261: On Linux, we may need to fixup the paths to TLS CA certs
# (especially in manylinux packages) since the values hardcoded at
# compile-time in libcurl may be wrong.
_init_ca_paths()
cdef inline c_string _path_as_bytes(path) except *:
# handle only abstract paths, not bound to any filesystem like pathlib is,
# so we only accept plain strings
if not isinstance(path, (bytes, str)):
raise TypeError('Path must be a string')
# tobytes always uses utf-8, which is more or less ok, at least on Windows
# since the C++ side then decodes from utf-8. On Unix, os.fsencode may be
# better.
return tobytes(path)
cdef object _wrap_file_type(CFileType ty):
return FileType(<int8_t> ty)
cdef CFileType _unwrap_file_type(FileType ty) except *:
if ty == FileType.Unknown:
return CFileType_Unknown
elif ty == FileType.NotFound:
return CFileType_NotFound
elif ty == FileType.File:
return CFileType_File
elif ty == FileType.Directory:
return CFileType_Directory
assert 0
def _file_type_to_string(ty):
# Python 3.11 changed str(IntEnum) to return the string representation
# of the integer value: https://github.com/python/cpython/issues/94763
return f"{ty.__class__.__name__}.{ty._name_}"
cdef class FileInfo(_Weakrefable):
"""
FileSystem entry info.
Parameters
----------
path : str
The full path to the filesystem entry.
type : FileType
The type of the filesystem entry.
mtime : datetime or float, default None
If given, the modification time of the filesystem entry.
If a float is given, it is the number of seconds since the
Unix epoch.
mtime_ns : int, default None
If given, the modification time of the filesystem entry,
in nanoseconds since the Unix epoch.
`mtime` and `mtime_ns` are mutually exclusive.
size : int, default None
If given, the filesystem entry size in bytes. This should only
be given if `type` is `FileType.File`.
Examples
--------
Generate a file:
>>> from pyarrow import fs
>>> local = fs.LocalFileSystem()
>>> path_fs = local_path + '/pyarrow-fs-example.dat'
>>> with local.open_output_stream(path_fs) as stream:
... stream.write(b'data')
4
Get FileInfo object using ``get_file_info()``:
>>> file_info = local.get_file_info(path_fs)
>>> file_info
<FileInfo for '.../pyarrow-fs-example.dat': type=FileType.File, size=4>
Inspect FileInfo attributes:
>>> file_info.type
<FileType.File: 2>
>>> file_info.is_file
True
>>> file_info.path
'/.../pyarrow-fs-example.dat'
>>> file_info.base_name
'pyarrow-fs-example.dat'
>>> file_info.size
4
>>> file_info.extension
'dat'
>>> file_info.mtime # doctest: +SKIP
datetime.datetime(2022, 6, 29, 7, 56, 10, 873922, tzinfo=datetime.timezone.utc)
>>> file_info.mtime_ns # doctest: +SKIP
1656489370873922073
"""
def __init__(self, path, FileType type=FileType.Unknown, *,
mtime=None, mtime_ns=None, size=None):
self.info.set_path(tobytes(path))
self.info.set_type(_unwrap_file_type(type))
if mtime is not None:
if mtime_ns is not None:
raise TypeError("Only one of mtime and mtime_ns "
"can be given")
if isinstance(mtime, datetime):
self.info.set_mtime(PyDateTime_to_TimePoint(
<PyDateTime_DateTime*> mtime))
else:
self.info.set_mtime(TimePoint_from_s(mtime))
elif mtime_ns is not None:
self.info.set_mtime(TimePoint_from_ns(mtime_ns))
if size is not None:
self.info.set_size(size)
@staticmethod
cdef wrap(CFileInfo info):
cdef FileInfo self = FileInfo.__new__(FileInfo)
self.info = move(info)
return self
cdef inline CFileInfo unwrap(self) nogil:
return self.info
@staticmethod
cdef CFileInfo unwrap_safe(obj):
if not isinstance(obj, FileInfo):
raise TypeError("Expected FileInfo instance, got {0}"
.format(type(obj)))
return (<FileInfo> obj).unwrap()
def __repr__(self):
def getvalue(attr):
try:
return getattr(self, attr)
except ValueError:
return ''
s = (f'<FileInfo for {self.path!r}: '
f'type={_file_type_to_string(self.type)}')
if self.is_file:
s += f', size={self.size}'
s += '>'
return s
@property
def type(self):
"""
Type of the file.
The returned enum values can be the following:
- FileType.NotFound: target does not exist
- FileType.Unknown: target exists but its type is unknown (could be a
special file such as a Unix socket or character device, or
Windows NUL / CON / ...)
- FileType.File: target is a regular file
- FileType.Directory: target is a regular directory
Returns
-------
type : FileType
"""
return _wrap_file_type(self.info.type())
@property
def is_file(self):
"""
"""
return self.type == FileType.File
@property
def path(self):
"""
The full file path in the filesystem.
Examples
--------
>>> file_info = local.get_file_info(path)
>>> file_info.path
'/.../pyarrow-fs-example.dat'
"""
return frombytes(self.info.path())
@property
def base_name(self):
"""
The file base name.
Component after the last directory separator.
Examples
--------
>>> file_info = local.get_file_info(path)
>>> file_info.base_name
'pyarrow-fs-example.dat'
"""
return frombytes(self.info.base_name())
@property
def size(self):
"""
The size in bytes, if available.
Only regular files are guaranteed to have a size.
Returns
-------
size : int or None
"""
cdef int64_t size
size = self.info.size()
return (size if size != -1 else None)
@property
def extension(self):
"""
The file extension.
Examples
--------
>>> file_info = local.get_file_info(path)
>>> file_info.extension
'dat'
"""
return frombytes(self.info.extension())
@property
def mtime(self):
"""
The time of last modification, if available.
Returns
-------
mtime : datetime.datetime or None
Examples
--------
>>> file_info = local.get_file_info(path)
>>> file_info.mtime # doctest: +SKIP
datetime.datetime(2022, 6, 29, 7, 56, 10, 873922, tzinfo=datetime.timezone.utc)
"""
cdef int64_t nanoseconds
nanoseconds = TimePoint_to_ns(self.info.mtime())
return (datetime.fromtimestamp(nanoseconds / 1.0e9, timezone.utc)
if nanoseconds != -1 else None)
@property
def mtime_ns(self):
"""
The time of last modification, if available, expressed in nanoseconds
since the Unix epoch.
Returns
-------
mtime_ns : int or None
Examples
--------
>>> file_info = local.get_file_info(path)
>>> file_info.mtime_ns # doctest: +SKIP
1656489370873922073
"""
cdef int64_t nanoseconds
nanoseconds = TimePoint_to_ns(self.info.mtime())
return (nanoseconds if nanoseconds != -1 else None)
cdef class FileSelector(_Weakrefable):
"""
File and directory selector.
It contains a set of options that describes how to search for files and
directories.
Parameters
----------
base_dir : str
The directory in which to select files. Relative paths also work, use
'.' for the current directory and '..' for the parent.
allow_not_found : bool, default False
The behavior if `base_dir` doesn't exist in the filesystem.
If false, an error is returned.
If true, an empty selection is returned.
recursive : bool, default False
Whether to recurse into subdirectories.
Examples
--------
List the contents of a directory and subdirectories:
>>> selector_1 = fs.FileSelector(local_path, recursive=True)
>>> local.get_file_info(selector_1) # doctest: +SKIP
[<FileInfo for 'tmp/alphabet/example.dat': type=FileType.File, size=4>,
<FileInfo for 'tmp/alphabet/subdir': type=FileType.Directory>,
<FileInfo for 'tmp/alphabet/subdir/example_copy.dat': type=FileType.File, size=4>]
List only the contents of the base directory:
>>> selector_2 = fs.FileSelector(local_path)
>>> local.get_file_info(selector_2) # doctest: +SKIP
[<FileInfo for 'tmp/alphabet/example.dat': type=FileType.File, size=4>,
<FileInfo for 'tmp/alphabet/subdir': type=FileType.Directory>]
Return empty selection if the directory doesn't exist:
>>> selector_not_found = fs.FileSelector(local_path + '/missing',
... recursive=True,
... allow_not_found=True)
>>> local.get_file_info(selector_not_found)
[]
"""
def __init__(self, base_dir, bint allow_not_found=False,
bint recursive=False):
self.base_dir = base_dir
self.recursive = recursive
self.allow_not_found = allow_not_found
@staticmethod
cdef FileSelector wrap(CFileSelector wrapped):
cdef FileSelector self = FileSelector.__new__(FileSelector)
self.selector = move(wrapped)
return self
cdef inline CFileSelector unwrap(self) nogil:
return self.selector
@property
def base_dir(self):
return frombytes(self.selector.base_dir)
@base_dir.setter
def base_dir(self, base_dir):
self.selector.base_dir = _path_as_bytes(base_dir)
@property
def allow_not_found(self):
return self.selector.allow_not_found
@allow_not_found.setter
def allow_not_found(self, bint allow_not_found):
self.selector.allow_not_found = allow_not_found
@property
def recursive(self):
return self.selector.recursive
@recursive.setter
def recursive(self, bint recursive):
self.selector.recursive = recursive
def __repr__(self):
return ("<FileSelector base_dir={0.base_dir!r} "
"recursive={0.recursive}>".format(self))
cdef class FileSystem(_Weakrefable):
"""
Abstract file system API.
"""
def __init__(self):
raise TypeError("FileSystem is an abstract class, instantiate one of "
"the subclasses instead: LocalFileSystem or "
"SubTreeFileSystem")
@staticmethod
def from_uri(uri):
"""
Create a new FileSystem from URI or Path.
Recognized URI schemes are "file", "mock", "s3fs", "gs", "gcs", "hdfs" and "viewfs".
In addition, the argument can be a pathlib.Path object, or a string
describing an absolute local path.
Parameters
----------
uri : string
URI-based path, for example: file:///some/local/path.
Returns
-------
tuple of (FileSystem, str path)
With (filesystem, path) tuple where path is the abstract path
inside the FileSystem instance.
Examples
--------
Create a new FileSystem subclass from a URI:
>>> uri = 'file:///{}/pyarrow-fs-example.dat'.format(local_path)
>>> local_new, path_new = fs.FileSystem.from_uri(uri)
>>> local_new
<pyarrow._fs.LocalFileSystem object at ...
>>> path_new
'/.../pyarrow-fs-example.dat'
Or from a s3 bucket:
>>> fs.FileSystem.from_uri("s3://usgs-landsat/collection02/")
(<pyarrow._s3fs.S3FileSystem object at ...>, 'usgs-landsat/collection02')
"""
cdef:
c_string c_path
c_string c_uri
CResult[shared_ptr[CFileSystem]] result
if isinstance(uri, pathlib.Path):
# Make absolute
uri = uri.resolve().absolute()
c_uri = tobytes(_stringify_path(uri))
with nogil:
result = CFileSystemFromUriOrPath(c_uri, &c_path)
return FileSystem.wrap(GetResultValue(result)), frombytes(c_path)
cdef init(self, const shared_ptr[CFileSystem]& wrapped):
self.wrapped = wrapped
self.fs = wrapped.get()
@staticmethod
cdef wrap(const shared_ptr[CFileSystem]& sp):
cdef FileSystem self
typ = frombytes(sp.get().type_name())
if typ == 'local':
self = LocalFileSystem.__new__(LocalFileSystem)
elif typ == 'mock':
self = _MockFileSystem.__new__(_MockFileSystem)
elif typ == 'subtree':
self = SubTreeFileSystem.__new__(SubTreeFileSystem)
elif typ == 's3':
from pyarrow._s3fs import S3FileSystem
self = S3FileSystem.__new__(S3FileSystem)
elif typ == 'gcs':
from pyarrow._gcsfs import GcsFileSystem
self = GcsFileSystem.__new__(GcsFileSystem)
elif typ == 'abfs':
from pyarrow._azurefs import AzureFileSystem
self = AzureFileSystem.__new__(AzureFileSystem)
elif typ == 'hdfs':
from pyarrow._hdfs import HadoopFileSystem
self = HadoopFileSystem.__new__(HadoopFileSystem)
elif typ.startswith('py::'):
self = PyFileSystem.__new__(PyFileSystem)
else:
raise TypeError('Cannot wrap FileSystem pointer')
self.init(sp)
return self
cdef inline shared_ptr[CFileSystem] unwrap(self) nogil:
return self.wrapped
def equals(self, FileSystem other not None):
"""
Parameters
----------
other : pyarrow.fs.FileSystem
Returns
-------
bool
"""
return self.fs.Equals(other.unwrap())
def __eq__(self, other):
try:
return self.equals(other)
except TypeError:
return NotImplemented
@property
def type_name(self):
"""
The filesystem's type name.
"""
return frombytes(self.fs.type_name())
def get_file_info(self, paths_or_selector):
"""
Get info for the given files.
Any symlink is automatically dereferenced, recursively. A non-existing
or unreachable file returns a FileStat object and has a FileType of
value NotFound. An exception indicates a truly exceptional condition
(low-level I/O error, etc.).
Parameters
----------
paths_or_selector : FileSelector, path-like or list of path-likes
Either a selector object, a path-like object or a list of
path-like objects. The selector's base directory will not be
part of the results, even if it exists. If it doesn't exist,
use `allow_not_found`.
Returns
-------
FileInfo or list of FileInfo
Single FileInfo object is returned for a single path, otherwise
a list of FileInfo objects is returned.
Examples
--------
>>> local
<pyarrow._fs.LocalFileSystem object at ...>
>>> local.get_file_info("/{}/pyarrow-fs-example.dat".format(local_path))
<FileInfo for '/.../pyarrow-fs-example.dat': type=FileType.File, size=4>
"""
cdef:
CFileInfo info
c_string path
vector[CFileInfo] infos
vector[c_string] paths
CFileSelector selector
if isinstance(paths_or_selector, FileSelector):
with nogil:
selector = (<FileSelector>paths_or_selector).selector
infos = GetResultValue(self.fs.GetFileInfo(selector))
elif isinstance(paths_or_selector, (list, tuple)):
paths = [_path_as_bytes(s) for s in paths_or_selector]
with nogil:
infos = GetResultValue(self.fs.GetFileInfo(paths))
elif isinstance(paths_or_selector, (bytes, str)):
path =_path_as_bytes(paths_or_selector)
with nogil:
info = GetResultValue(self.fs.GetFileInfo(path))
return FileInfo.wrap(info)
else:
raise TypeError('Must pass either path(s) or a FileSelector')
return [FileInfo.wrap(info) for info in infos]
def create_dir(self, path, *, bint recursive=True):
"""
Create a directory and subdirectories.
This function succeeds if the directory already exists.
Parameters
----------
path : str
The path of the new directory.
recursive : bool, default True
Create nested directories as well.
"""
cdef c_string directory = _path_as_bytes(path)
with nogil:
check_status(self.fs.CreateDir(directory, recursive=recursive))
def delete_dir(self, path):
"""
Delete a directory and its contents, recursively.
Parameters
----------
path : str
The path of the directory to be deleted.
"""
cdef c_string directory = _path_as_bytes(path)
with nogil:
check_status(self.fs.DeleteDir(directory))
def delete_dir_contents(self, path, *,
bint accept_root_dir=False,
bint missing_dir_ok=False):
"""
Delete a directory's contents, recursively.
Like delete_dir, but doesn't delete the directory itself.
Parameters
----------
path : str
The path of the directory to be deleted.
accept_root_dir : boolean, default False
Allow deleting the root directory's contents
(if path is empty or "/")
missing_dir_ok : boolean, default False
If False then an error is raised if path does
not exist
"""
cdef c_string directory = _path_as_bytes(path)
if accept_root_dir and directory.strip(b"/") == b"":
with nogil:
check_status(self.fs.DeleteRootDirContents())
else:
with nogil:
check_status(self.fs.DeleteDirContents(directory,
missing_dir_ok))
def move(self, src, dest):
"""
Move / rename a file or directory.
If the destination exists:
- if it is a non-empty directory, an error is returned
- otherwise, if it has the same type as the source, it is replaced
- otherwise, behavior is unspecified (implementation-dependent).
Parameters
----------
src : str
The path of the file or the directory to be moved.
dest : str
The destination path where the file or directory is moved to.
Examples
--------
Create a new folder with a file:
>>> local.create_dir('/tmp/other_dir')
>>> local.copy_file(path,'/tmp/move_example.dat')
Move the file:
>>> local.move('/tmp/move_example.dat',
... '/tmp/other_dir/move_example_2.dat')
Inspect the file info:
>>> local.get_file_info('/tmp/other_dir/move_example_2.dat')
<FileInfo for '/tmp/other_dir/move_example_2.dat': type=FileType.File, size=4>
>>> local.get_file_info('/tmp/move_example.dat')
<FileInfo for '/tmp/move_example.dat': type=FileType.NotFound>
Delete the folder:
>>> local.delete_dir('/tmp/other_dir')
"""
cdef:
c_string source = _path_as_bytes(src)
c_string destination = _path_as_bytes(dest)
with nogil:
check_status(self.fs.Move(source, destination))
def copy_file(self, src, dest):
"""
Copy a file.
If the destination exists and is a directory, an error is returned.
Otherwise, it is replaced.
Parameters
----------
src : str
The path of the file to be copied from.
dest : str
The destination path where the file is copied to.
Examples
--------
>>> local.copy_file(path,
... local_path + '/pyarrow-fs-example_copy.dat')
Inspect the file info:
>>> local.get_file_info(local_path + '/pyarrow-fs-example_copy.dat')
<FileInfo for '/.../pyarrow-fs-example_copy.dat': type=FileType.File, size=4>
>>> local.get_file_info(path)
<FileInfo for '/.../pyarrow-fs-example.dat': type=FileType.File, size=4>
"""
cdef:
c_string source = _path_as_bytes(src)
c_string destination = _path_as_bytes(dest)
with nogil:
check_status(self.fs.CopyFile(source, destination))
def delete_file(self, path):
"""
Delete a file.
Parameters
----------
path : str
The path of the file to be deleted.
"""
cdef c_string file = _path_as_bytes(path)
with nogil:
check_status(self.fs.DeleteFile(file))
def _wrap_input_stream(self, stream, path, compression, buffer_size):
if buffer_size is not None and buffer_size != 0:
stream = BufferedInputStream(stream, buffer_size)
if compression == 'detect':
compression = _detect_compression(path)
if compression is not None:
stream = CompressedInputStream(stream, compression)
return stream
def _wrap_output_stream(self, stream, path, compression, buffer_size):
if buffer_size is not None and buffer_size != 0:
stream = BufferedOutputStream(stream, buffer_size)
if compression == 'detect':
compression = _detect_compression(path)
if compression is not None:
stream = CompressedOutputStream(stream, compression)
return stream
def open_input_file(self, path):
"""
Open an input file for random access reading.
Parameters
----------
path : str
The source to open for reading.
Returns
-------
stream : NativeFile
Examples
--------
Print the data from the file with `open_input_file()`:
>>> with local.open_input_file(path) as f:
... print(f.readall())
b'data'
"""
cdef:
c_string pathstr = _path_as_bytes(path)
NativeFile stream = NativeFile()
shared_ptr[CRandomAccessFile] in_handle
with nogil:
in_handle = GetResultValue(self.fs.OpenInputFile(pathstr))
stream.set_random_access_file(in_handle)
stream.is_readable = True
return stream
def open_input_stream(self, path, compression='detect', buffer_size=None):
"""
Open an input stream for sequential reading.
Parameters
----------
path : str
The source to open for reading.
compression : str optional, default 'detect'
The compression algorithm to use for on-the-fly decompression.
If "detect" and source is a file path, then compression will be
chosen based on the file extension.
If None, no compression will be applied. Otherwise, a well-known
algorithm name must be supplied (e.g. "gzip").
buffer_size : int optional, default None
If None or 0, no buffering will happen. Otherwise the size of the
temporary read buffer.
Returns
-------
stream : NativeFile
Examples
--------
Print the data from the file with `open_input_stream()`:
>>> with local.open_input_stream(path) as f:
... print(f.readall())
b'data'
"""
cdef:
c_string pathstr = _path_as_bytes(path)
NativeFile stream = NativeFile()
shared_ptr[CInputStream] in_handle
with nogil:
in_handle = GetResultValue(self.fs.OpenInputStream(pathstr))
stream.set_input_stream(in_handle)
stream.is_readable = True
return self._wrap_input_stream(
stream, path=path, compression=compression, buffer_size=buffer_size
)
def open_output_stream(self, path, compression='detect',
buffer_size=None, metadata=None):
"""
Open an output stream for sequential writing.
If the target already exists, existing data is truncated.
Parameters
----------
path : str
The source to open for writing.
compression : str optional, default 'detect'
The compression algorithm to use for on-the-fly compression.
If "detect" and source is a file path, then compression will be
chosen based on the file extension.
If None, no compression will be applied. Otherwise, a well-known
algorithm name must be supplied (e.g. "gzip").
buffer_size : int optional, default None
If None or 0, no buffering will happen. Otherwise the size of the
temporary write buffer.
metadata : dict optional, default None
If not None, a mapping of string keys to string values.
Some filesystems support storing metadata along the file
(such as "Content-Type").
Unsupported metadata keys will be ignored.
Returns
-------
stream : NativeFile
Examples
--------
>>> local = fs.LocalFileSystem()
>>> with local.open_output_stream(path) as stream:
... stream.write(b'data')
4
"""
cdef:
c_string pathstr = _path_as_bytes(path)
NativeFile stream = NativeFile()
shared_ptr[COutputStream] out_handle
shared_ptr[const CKeyValueMetadata] c_metadata
if metadata is not None:
c_metadata = pyarrow_unwrap_metadata(KeyValueMetadata(metadata))
with nogil:
out_handle = GetResultValue(
self.fs.OpenOutputStream(pathstr, c_metadata))
stream.set_output_stream(out_handle)
stream.is_writable = True
return self._wrap_output_stream(
stream, path=path, compression=compression, buffer_size=buffer_size
)
def open_append_stream(self, path, compression='detect',
buffer_size=None, metadata=None):
"""
Open an output stream for appending.
If the target doesn't exist, a new empty file is created.
.. note::
Some filesystem implementations do not support efficient
appending to an existing file, in which case this method will
raise NotImplementedError.
Consider writing to multiple files (using e.g. the dataset layer)
instead.
Parameters
----------
path : str
The source to open for writing.
compression : str optional, default 'detect'
The compression algorithm to use for on-the-fly compression.
If "detect" and source is a file path, then compression will be
chosen based on the file extension.
If None, no compression will be applied. Otherwise, a well-known
algorithm name must be supplied (e.g. "gzip").
buffer_size : int optional, default None
If None or 0, no buffering will happen. Otherwise the size of the
temporary write buffer.
metadata : dict optional, default None
If not None, a mapping of string keys to string values.
Some filesystems support storing metadata along the file
(such as "Content-Type").
Unsupported metadata keys will be ignored.
Returns
-------
stream : NativeFile
Examples
--------
Append new data to a FileSystem subclass with nonempty file:
>>> with local.open_append_stream(path) as f:
... f.write(b'+newly added')
12
Print out the content fo the file:
>>> with local.open_input_file(path) as f:
... print(f.readall())
b'data+newly added'
"""
cdef:
c_string pathstr = _path_as_bytes(path)
NativeFile stream = NativeFile()
shared_ptr[COutputStream] out_handle
shared_ptr[const CKeyValueMetadata] c_metadata
if metadata is not None:
c_metadata = pyarrow_unwrap_metadata(KeyValueMetadata(metadata))
with nogil:
out_handle = GetResultValue(
self.fs.OpenAppendStream(pathstr, c_metadata))
stream.set_output_stream(out_handle)
stream.is_writable = True
return self._wrap_output_stream(
stream, path=path, compression=compression, buffer_size=buffer_size
)
def normalize_path(self, path):
"""
Normalize filesystem path.
Parameters
----------
path : str
The path to normalize
Returns
-------
normalized_path : str
The normalized path
"""
cdef:
c_string c_path = _path_as_bytes(path)
c_string c_path_normalized
c_path_normalized = GetResultValue(self.fs.NormalizePath(c_path))
return frombytes(c_path_normalized)
cdef class LocalFileSystem(FileSystem):
"""
A FileSystem implementation accessing files on the local machine.
Details such as symlinks are abstracted away (symlinks are always followed,
except when deleting an entry).
Parameters
----------
use_mmap : bool, default False
Whether open_input_stream and open_input_file should return
a mmap'ed file or a regular file.
Examples
--------
Create a FileSystem object with LocalFileSystem constructor:
>>> from pyarrow import fs
>>> local = fs.LocalFileSystem()
>>> local
<pyarrow._fs.LocalFileSystem object at ...>
and write data on to the file:
>>> with local.open_output_stream('/tmp/local_fs.dat') as stream:
... stream.write(b'data')
4
>>> with local.open_input_stream('/tmp/local_fs.dat') as stream:
... print(stream.readall())
b'data'
Create a FileSystem object inferred from a URI of the saved file:
>>> local_new, path = fs.LocalFileSystem().from_uri('/tmp/local_fs.dat')
>>> local_new
<pyarrow._fs.LocalFileSystem object at ...
>>> path
'/tmp/local_fs.dat'
Check if FileSystems `local` and `local_new` are equal:
>>> local.equals(local_new)
True
Compare two different FileSystems:
>>> local2 = fs.LocalFileSystem(use_mmap=True)
>>> local.equals(local2)
False
Copy a file and print out the data:
>>> local.copy_file('/tmp/local_fs.dat', '/tmp/local_fs-copy.dat')
>>> with local.open_input_stream('/tmp/local_fs-copy.dat') as stream:
... print(stream.readall())
...
b'data'
Open an output stream for appending, add text and print the new data:
>>> with local.open_append_stream('/tmp/local_fs-copy.dat') as f:
... f.write(b'+newly added')
12
>>> with local.open_input_stream('/tmp/local_fs-copy.dat') as f:
... print(f.readall())
b'data+newly added'
Create a directory, copy a file into it and then delete the whole directory:
>>> local.create_dir('/tmp/new_folder')
>>> local.copy_file('/tmp/local_fs.dat', '/tmp/new_folder/local_fs.dat')
>>> local.get_file_info('/tmp/new_folder')
<FileInfo for '/tmp/new_folder': type=FileType.Directory>
>>> local.delete_dir('/tmp/new_folder')
>>> local.get_file_info('/tmp/new_folder')
<FileInfo for '/tmp/new_folder': type=FileType.NotFound>
Create a directory, copy a file into it and then delete
the content of the directory:
>>> local.create_dir('/tmp/new_folder')
>>> local.copy_file('/tmp/local_fs.dat', '/tmp/new_folder/local_fs.dat')
>>> local.get_file_info('/tmp/new_folder/local_fs.dat')
<FileInfo for '/tmp/new_folder/local_fs.dat': type=FileType.File, size=4>
>>> local.delete_dir_contents('/tmp/new_folder')
>>> local.get_file_info('/tmp/new_folder')
<FileInfo for '/tmp/new_folder': type=FileType.Directory>
>>> local.get_file_info('/tmp/new_folder/local_fs.dat')
<FileInfo for '/tmp/new_folder/local_fs.dat': type=FileType.NotFound>
Create a directory, copy a file into it and then delete
the file from the directory:
>>> local.create_dir('/tmp/new_folder')
>>> local.copy_file('/tmp/local_fs.dat', '/tmp/new_folder/local_fs.dat')
>>> local.delete_file('/tmp/new_folder/local_fs.dat')
>>> local.get_file_info('/tmp/new_folder/local_fs.dat')
<FileInfo for '/tmp/new_folder/local_fs.dat': type=FileType.NotFound>
>>> local.get_file_info('/tmp/new_folder')
<FileInfo for '/tmp/new_folder': type=FileType.Directory>
Move the file:
>>> local.move('/tmp/local_fs-copy.dat', '/tmp/new_folder/local_fs-copy.dat')
>>> local.get_file_info('/tmp/new_folder/local_fs-copy.dat')
<FileInfo for '/tmp/new_folder/local_fs-copy.dat': type=FileType.File, size=16>
>>> local.get_file_info('/tmp/local_fs-copy.dat')
<FileInfo for '/tmp/local_fs-copy.dat': type=FileType.NotFound>
To finish delete the file left:
>>> local.delete_file('/tmp/local_fs.dat')
"""
def __init__(self, *, use_mmap=False):
cdef:
CLocalFileSystemOptions opts
shared_ptr[CLocalFileSystem] fs
opts = CLocalFileSystemOptions.Defaults()
opts.use_mmap = use_mmap
fs = make_shared[CLocalFileSystem](opts)
self.init(<shared_ptr[CFileSystem]> fs)
cdef init(self, const shared_ptr[CFileSystem]& c_fs):
FileSystem.init(self, c_fs)
self.localfs = <CLocalFileSystem*> c_fs.get()
@staticmethod
@binding(True) # Required for cython < 3
def _reconstruct(kwargs):
# __reduce__ doesn't allow passing named arguments directly to the
# reconstructor, hence this wrapper.
return LocalFileSystem(**kwargs)
def __reduce__(self):
cdef CLocalFileSystemOptions opts = self.localfs.options()
return LocalFileSystem._reconstruct, (dict(
use_mmap=opts.use_mmap),)
cdef class SubTreeFileSystem(FileSystem):
"""
Delegates to another implementation after prepending a fixed base path.
This is useful to expose a logical view of a subtree of a filesystem,
for example a directory in a LocalFileSystem.
Note, that this makes no security guarantee. For example, symlinks may
allow to "escape" the subtree and access other parts of the underlying
filesystem.
Parameters
----------
base_path : str
The root of the subtree.
base_fs : FileSystem
FileSystem object the operations delegated to.
Examples
--------
Create a LocalFileSystem instance:
>>> from pyarrow import fs
>>> local = fs.LocalFileSystem()
>>> with local.open_output_stream('/tmp/local_fs.dat') as stream:
... stream.write(b'data')
4
Create a directory and a SubTreeFileSystem instance:
>>> local.create_dir('/tmp/sub_tree')
>>> subtree = fs.SubTreeFileSystem('/tmp/sub_tree', local)
Write data into the existing file:
>>> with subtree.open_append_stream('sub_tree_fs.dat') as f:
... f.write(b'+newly added')
12
Print out the attributes:
>>> subtree.base_fs
<pyarrow._fs.LocalFileSystem object at ...>
>>> subtree.base_path
'/tmp/sub_tree/'
Get info for the given directory or given file:
>>> subtree.get_file_info('')
<FileInfo for '': type=FileType.Directory>
>>> subtree.get_file_info('sub_tree_fs.dat')
<FileInfo for 'sub_tree_fs.dat': type=FileType.File, size=12>
Delete the file and directory:
>>> subtree.delete_file('sub_tree_fs.dat')
>>> local.delete_dir('/tmp/sub_tree')
>>> local.delete_file('/tmp/local_fs.dat')
For usage of the methods see examples for :func:`~pyarrow.fs.LocalFileSystem`.
"""
def __init__(self, base_path, FileSystem base_fs):
cdef:
c_string pathstr
shared_ptr[CSubTreeFileSystem] wrapped
pathstr = _path_as_bytes(base_path)
wrapped = make_shared[CSubTreeFileSystem](pathstr, base_fs.wrapped)
self.init(<shared_ptr[CFileSystem]> wrapped)
cdef init(self, const shared_ptr[CFileSystem]& wrapped):
FileSystem.init(self, wrapped)
self.subtreefs = <CSubTreeFileSystem*> wrapped.get()
def __repr__(self):
return ("SubTreeFileSystem(base_path={}, base_fs={}"
.format(self.base_path, self.base_fs))
def __reduce__(self):
return SubTreeFileSystem, (
frombytes(self.subtreefs.base_path()),
FileSystem.wrap(self.subtreefs.base_fs())
)
@property
def base_path(self):
return frombytes(self.subtreefs.base_path())
@property
def base_fs(self):
return FileSystem.wrap(self.subtreefs.base_fs())
cdef class _MockFileSystem(FileSystem):
def __init__(self, datetime current_time=None):
cdef shared_ptr[CMockFileSystem] wrapped
current_time = current_time or datetime.now()
wrapped = make_shared[CMockFileSystem](
PyDateTime_to_TimePoint(<PyDateTime_DateTime*> current_time)
)
self.init(<shared_ptr[CFileSystem]> wrapped)
cdef init(self, const shared_ptr[CFileSystem]& wrapped):
FileSystem.init(self, wrapped)
self.mockfs = <CMockFileSystem*> wrapped.get()
cdef class PyFileSystem(FileSystem):
"""
A FileSystem with behavior implemented in Python.
Parameters
----------
handler : FileSystemHandler
The handler object implementing custom filesystem behavior.
Examples
--------
Create an fsspec-based filesystem object for GitHub:
>>> from fsspec.implementations import github
>>> gfs = github.GithubFileSystem('apache', 'arrow') # doctest: +SKIP
Get a PyArrow FileSystem object:
>>> from pyarrow.fs import PyFileSystem, FSSpecHandler
>>> pa_fs = PyFileSystem(FSSpecHandler(gfs)) # doctest: +SKIP
Use :func:`~pyarrow.fs.FileSystem` functionality ``get_file_info()``:
>>> pa_fs.get_file_info('README.md') # doctest: +SKIP
<FileInfo for 'README.md': type=FileType.File, size=...>
"""
def __init__(self, handler):
cdef:
CPyFileSystemVtable vtable
shared_ptr[CPyFileSystem] wrapped
if not isinstance(handler, FileSystemHandler):
raise TypeError("Expected a FileSystemHandler instance, got {0}"
.format(type(handler)))
vtable.get_type_name = _cb_get_type_name
vtable.equals = _cb_equals
vtable.get_file_info = _cb_get_file_info
vtable.get_file_info_vector = _cb_get_file_info_vector
vtable.get_file_info_selector = _cb_get_file_info_selector
vtable.create_dir = _cb_create_dir
vtable.delete_dir = _cb_delete_dir
vtable.delete_dir_contents = _cb_delete_dir_contents
vtable.delete_root_dir_contents = _cb_delete_root_dir_contents
vtable.delete_file = _cb_delete_file
vtable.move = _cb_move
vtable.copy_file = _cb_copy_file
vtable.open_input_stream = _cb_open_input_stream
vtable.open_input_file = _cb_open_input_file
vtable.open_output_stream = _cb_open_output_stream
vtable.open_append_stream = _cb_open_append_stream
vtable.normalize_path = _cb_normalize_path
wrapped = CPyFileSystem.Make(handler, move(vtable))
self.init(<shared_ptr[CFileSystem]> wrapped)
cdef init(self, const shared_ptr[CFileSystem]& wrapped):
FileSystem.init(self, wrapped)
self.pyfs = <CPyFileSystem*> wrapped.get()
@property
def handler(self):
"""
The filesystem's underlying handler.
Returns
-------
handler : FileSystemHandler
"""
return <object> self.pyfs.handler()
def __reduce__(self):
return PyFileSystem, (self.handler,)
class FileSystemHandler(ABC):
"""
An abstract class exposing methods to implement PyFileSystem's behavior.
"""
@abstractmethod
def get_type_name(self):
"""
Implement PyFileSystem.type_name.
"""
@abstractmethod
def get_file_info(self, paths):
"""
Implement PyFileSystem.get_file_info(paths).
Parameters
----------
paths : list of str
paths for which we want to retrieve the info.
"""
@abstractmethod
def get_file_info_selector(self, selector):
"""
Implement PyFileSystem.get_file_info(selector).
Parameters
----------
selector : FileSelector
selector for which we want to retrieve the info.
"""
@abstractmethod
def create_dir(self, path, recursive):
"""
Implement PyFileSystem.create_dir(...).
Parameters
----------
path : str
path of the directory.
recursive : bool
if the parent directories should be created too.
"""
@abstractmethod
def delete_dir(self, path):
"""
Implement PyFileSystem.delete_dir(...).
Parameters
----------
path : str
path of the directory.
"""
@abstractmethod
def delete_dir_contents(self, path, missing_dir_ok=False):
"""
Implement PyFileSystem.delete_dir_contents(...).
Parameters
----------
path : str
path of the directory.
missing_dir_ok : bool
if False an error should be raised if path does not exist
"""
@abstractmethod
def delete_root_dir_contents(self):
"""
Implement PyFileSystem.delete_dir_contents("/", accept_root_dir=True).
"""
@abstractmethod
def delete_file(self, path):
"""
Implement PyFileSystem.delete_file(...).
Parameters
----------
path : str
path of the file.
"""
@abstractmethod
def move(self, src, dest):
"""
Implement PyFileSystem.move(...).
Parameters
----------
src : str
path of what should be moved.
dest : str
path of where it should be moved to.
"""
@abstractmethod
def copy_file(self, src, dest):
"""
Implement PyFileSystem.copy_file(...).
Parameters
----------
src : str
path of what should be copied.
dest : str
path of where it should be copied to.
"""
@abstractmethod
def open_input_stream(self, path):
"""
Implement PyFileSystem.open_input_stream(...).
Parameters
----------
path : str
path of what should be opened.
"""
@abstractmethod
def open_input_file(self, path):
"""
Implement PyFileSystem.open_input_file(...).
Parameters
----------
path : str
path of what should be opened.
"""
@abstractmethod
def open_output_stream(self, path, metadata):
"""
Implement PyFileSystem.open_output_stream(...).
Parameters
----------
path : str
path of what should be opened.
metadata : mapping
Mapping of string keys to string values.
Some filesystems support storing metadata along the file
(such as "Content-Type").
"""
@abstractmethod
def open_append_stream(self, path, metadata):
"""
Implement PyFileSystem.open_append_stream(...).
Parameters
----------
path : str
path of what should be opened.
metadata : mapping
Mapping of string keys to string values.
Some filesystems support storing metadata along the file
(such as "Content-Type").
"""
@abstractmethod
def normalize_path(self, path):
"""
Implement PyFileSystem.normalize_path(...).
Parameters
----------
path : str
path of what should be normalized.
"""
# Callback definitions for CPyFileSystemVtable
cdef void _cb_get_type_name(handler, c_string* out) except *:
out[0] = tobytes("py::" + handler.get_type_name())
cdef c_bool _cb_equals(handler, const CFileSystem& c_other) except False:
if c_other.type_name().startswith(b"py::"):
return <object> (<const CPyFileSystem&> c_other).handler() == handler
return False
cdef void _cb_get_file_info(handler, const c_string& path,
CFileInfo* out) except *:
infos = handler.get_file_info([frombytes(path)])
if not isinstance(infos, list) or len(infos) != 1:
raise TypeError("get_file_info should have returned a 1-element list")
out[0] = FileInfo.unwrap_safe(infos[0])
cdef void _cb_get_file_info_vector(handler, const vector[c_string]& paths,
vector[CFileInfo]* out) except *:
py_paths = [frombytes(paths[i]) for i in range(len(paths))]
infos = handler.get_file_info(py_paths)
if not isinstance(infos, list):
raise TypeError("get_file_info should have returned a list")
out[0].clear()
out[0].reserve(len(infos))
for info in infos:
out[0].push_back(FileInfo.unwrap_safe(info))
cdef void _cb_get_file_info_selector(handler, const CFileSelector& selector,
vector[CFileInfo]* out) except *:
infos = handler.get_file_info_selector(FileSelector.wrap(selector))
if not isinstance(infos, list):
raise TypeError("get_file_info_selector should have returned a list")
out[0].clear()
out[0].reserve(len(infos))
for info in infos:
out[0].push_back(FileInfo.unwrap_safe(info))
cdef void _cb_create_dir(handler, const c_string& path,
c_bool recursive) except *:
handler.create_dir(frombytes(path), recursive)
cdef void _cb_delete_dir(handler, const c_string& path) except *:
handler.delete_dir(frombytes(path))
cdef void _cb_delete_dir_contents(handler, const c_string& path,
c_bool missing_dir_ok) except *:
handler.delete_dir_contents(frombytes(path), missing_dir_ok)
cdef void _cb_delete_root_dir_contents(handler) except *:
handler.delete_root_dir_contents()
cdef void _cb_delete_file(handler, const c_string& path) except *:
handler.delete_file(frombytes(path))
cdef void _cb_move(handler, const c_string& src,
const c_string& dest) except *:
handler.move(frombytes(src), frombytes(dest))
cdef void _cb_copy_file(handler, const c_string& src,
const c_string& dest) except *:
handler.copy_file(frombytes(src), frombytes(dest))
cdef void _cb_open_input_stream(handler, const c_string& path,
shared_ptr[CInputStream]* out) except *:
stream = handler.open_input_stream(frombytes(path))
if not isinstance(stream, NativeFile):
raise TypeError("open_input_stream should have returned "
"a PyArrow file")
out[0] = (<NativeFile> stream).get_input_stream()
cdef void _cb_open_input_file(handler, const c_string& path,
shared_ptr[CRandomAccessFile]* out) except *:
stream = handler.open_input_file(frombytes(path))
if not isinstance(stream, NativeFile):
raise TypeError("open_input_file should have returned "
"a PyArrow file")
out[0] = (<NativeFile> stream).get_random_access_file()
cdef void _cb_open_output_stream(
handler, const c_string& path,
const shared_ptr[const CKeyValueMetadata]& metadata,
shared_ptr[COutputStream]* out) except *:
stream = handler.open_output_stream(
frombytes(path), pyarrow_wrap_metadata(metadata))
if not isinstance(stream, NativeFile):
raise TypeError("open_output_stream should have returned "
"a PyArrow file")
out[0] = (<NativeFile> stream).get_output_stream()
cdef void _cb_open_append_stream(
handler, const c_string& path,
const shared_ptr[const CKeyValueMetadata]& metadata,
shared_ptr[COutputStream]* out) except *:
stream = handler.open_append_stream(
frombytes(path), pyarrow_wrap_metadata(metadata))
if not isinstance(stream, NativeFile):
raise TypeError("open_append_stream should have returned "
"a PyArrow file")
out[0] = (<NativeFile> stream).get_output_stream()
cdef void _cb_normalize_path(handler, const c_string& path,
c_string* out) except *:
out[0] = tobytes(handler.normalize_path(frombytes(path)))
def _copy_files(FileSystem source_fs, str source_path,
FileSystem destination_fs, str destination_path,
int64_t chunk_size, c_bool use_threads):
# low-level helper exposed through pyarrow/fs.py::copy_files
cdef:
CFileLocator c_source
vector[CFileLocator] c_sources
CFileLocator c_destination
vector[CFileLocator] c_destinations
c_source.filesystem = source_fs.unwrap()
c_source.path = tobytes(source_path)
c_sources.push_back(c_source)
c_destination.filesystem = destination_fs.unwrap()
c_destination.path = tobytes(destination_path)
c_destinations.push_back(c_destination)
with nogil:
check_status(CCopyFiles(
c_sources, c_destinations,
c_default_io_context(), chunk_size, use_threads,
))
def _copy_files_selector(FileSystem source_fs, FileSelector source_sel,
FileSystem destination_fs, str destination_base_dir,
int64_t chunk_size, c_bool use_threads):
# low-level helper exposed through pyarrow/fs.py::copy_files
cdef c_string c_destination_base_dir = tobytes(destination_base_dir)
with nogil:
check_status(CCopyFilesWithSelector(
source_fs.unwrap(), source_sel.unwrap(),
destination_fs.unwrap(), c_destination_base_dir,
c_default_io_context(), chunk_size, use_threads,
))
|