Scansk a path provided and returns namespaces detected.
(
path: str,
namespace: Optional[Union[str, list[str]]] = None,
closest: bool = True,
)
| 1347 | |
| 1348 | @staticmethod |
| 1349 | def disk_scan( |
| 1350 | path: str, |
| 1351 | namespace: Optional[Union[str, list[str]]] = None, |
| 1352 | closest: bool = True, |
| 1353 | ) -> list[str]: |
| 1354 | """Scansk a path provided and returns namespaces detected.""" |
| 1355 | |
| 1356 | logger.trace("Persistent path can of: %s", path) |
| 1357 | |
| 1358 | def is_namespace(x): |
| 1359 | """Validate what was detected is a valid namespace.""" |
| 1360 | return os.path.isdir( |
| 1361 | os.path.join(path, x) |
| 1362 | ) and PersistentStore.__valid_key.match(x) |
| 1363 | |
| 1364 | # Handle our namespace searching |
| 1365 | if namespace: |
| 1366 | if isinstance(namespace, str): |
| 1367 | namespace = [namespace] |
| 1368 | |
| 1369 | elif not isinstance(namespace, (tuple, set, list)): |
| 1370 | raise AttributeError( |
| 1371 | "namespace must be None, a string, or a tuple/set/list " |
| 1372 | "of strings" |
| 1373 | ) |
| 1374 | |
| 1375 | try: |
| 1376 | # Acquire all of the files in question |
| 1377 | namespaces = ( |
| 1378 | [ |
| 1379 | ns |
| 1380 | for ns in filter(is_namespace, os.listdir(path)) |
| 1381 | if not namespace |
| 1382 | or next( |
| 1383 | (True for n in namespace if ns.startswith(n)), False |
| 1384 | ) |
| 1385 | ] |
| 1386 | if closest |
| 1387 | else [ |
| 1388 | ns |
| 1389 | for ns in filter(is_namespace, os.listdir(path)) |
| 1390 | if not namespace or ns in namespace |
| 1391 | ] |
| 1392 | ) |
| 1393 | |
| 1394 | except FileNotFoundError: |
| 1395 | # no worries; Nothing to do |
| 1396 | logger.debug("Disk Prune path not found; nothing to clean.") |
| 1397 | return [] |
| 1398 | |
| 1399 | except OSError as e: |
| 1400 | # Permission error of some kind or disk problem... |
| 1401 | # There is nothing we can do at this point |
| 1402 | logger.error("Disk Scan detetcted inaccessible path: %s", path) |
| 1403 | logger.debug("Persistent Storage Exception: %s", str(e)) |
| 1404 | return [] |
| 1405 | |
| 1406 | return namespaces |