File: folder_parser.py

package info (click to toggle)
python-b2sdk 2.8.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,020 kB
  • sloc: python: 30,902; sh: 13; makefile: 8
file content (55 lines) | stat: -rw-r--r-- 2,021 bytes parent folder | download
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
######################################################################
#
# File: b2sdk/_internal/scan/folder_parser.py
#
# Copyright 2019 Backblaze Inc. All Rights Reserved.
#
# License https://www.backblaze.com/using_b2_code.html
#
######################################################################
from __future__ import annotations

from .exception import InvalidArgument
from .folder import B2Folder, LocalFolder


def parse_folder(folder_name, api, local_folder_class=LocalFolder, b2_folder_class=B2Folder):
    """
    Take either a local path, or a B2 path, and returns a Folder
    object for it.

    B2 paths look like: b2://bucketName/path/name.  The '//' is optional.

    Anything else is treated like a local folder.

    :param folder_name: a name of the folder, either local or remote
    :type folder_name: str
    :param api: an API object
    :type api: :class:`~b2sdk.v2.B2Api`
    :param local_folder_class: class to handle local folders
    :type local_folder_class: `~b2sdk.v2.AbstractFolder`
    :param b2_folder_class: class to handle B2 folders
    :type b2_folder_class: `~b2sdk.v2.AbstractFolder`
    """
    if folder_name.startswith('b2://'):
        return _parse_bucket_and_folder(folder_name[5:], api, b2_folder_class)
    elif folder_name.startswith('b2:') and folder_name[3].isalnum():
        return _parse_bucket_and_folder(folder_name[3:], api, b2_folder_class)
    else:
        return local_folder_class(folder_name)


def _parse_bucket_and_folder(bucket_and_path, api, b2_folder_class):
    """
    Turn 'my-bucket/foo' into B2Folder(my-bucket, foo).
    """
    if '//' in bucket_and_path:
        raise InvalidArgument('folder_name', "'//' not allowed in path names")
    if '/' not in bucket_and_path:
        bucket_name = bucket_and_path
        folder_name = ''
    else:
        (bucket_name, folder_name) = bucket_and_path.split('/', 1)
    if folder_name.endswith('/'):
        folder_name = folder_name[:-1]
    return b2_folder_class(bucket_name, folder_name, api)