Skip to content

Module dcm2bids.utils.args⚓︎

View Source
 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
# -*- coding: utf-8 -*-

import shutil

from pathlib import Path

import os

def assert_dirs_empty(parser, args, required):

    """

    Assert that all directories exist are empty.

    If dirs exist and not empty, and --force is used, delete dirs.

    Parameters

    ----------

    parser: argparse.ArgumentParser object

        Parser.

    args: argparse namespace

        Argument list.

    required: string or list of paths to files

        Required paths to be checked.

    """

    def check(path: Path):

        if path.is_dir():

            if any(path.iterdir()):

                if not args.overwrite:

                    parser.error(

                        f"Output directory {path}{os.sep} isn't empty, so some files "

                        "could be overwritten or deleted.\nRerun the command "

                        "with --force option to overwrite "

                        "existing output files.")

            else:

                for child in path.iterdir():

                    if child.is_file():

                        os.remove(child)

                    elif child.is_dir():

                        shutil.rmtree(child)

    if isinstance(required, str):

        required = Path(required)

    for cur_dir in [required]:

        check(cur_dir)

def add_overwrite_arg(parser):

    parser.add_argument(

        '--force', dest='overwrite', action='store_true',

        help='Force overwriting of the output files.')

Functions⚓︎

add_overwrite_arg⚓︎

1
2
3
def add_overwrite_arg(
    parser
)
View Source
1
2
3
4
5
6
7
def add_overwrite_arg(parser):

    parser.add_argument(

        '--force', dest='overwrite', action='store_true',

        help='Force overwriting of the output files.')

assert_dirs_empty⚓︎

1
2
3
4
5
def assert_dirs_empty(
    parser,
    args,
    required
)

Assert that all directories exist are empty.

If dirs exist and not empty, and --force is used, delete dirs.

Parameters:

Name Type Description Default
parser argparse.ArgumentParser object Parser. None
args argparse namespace Argument list. None
required string or list of paths to files Required paths to be checked. None
View Source
 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
def assert_dirs_empty(parser, args, required):

    """

    Assert that all directories exist are empty.

    If dirs exist and not empty, and --force is used, delete dirs.

    Parameters

    ----------

    parser: argparse.ArgumentParser object

        Parser.

    args: argparse namespace

        Argument list.

    required: string or list of paths to files

        Required paths to be checked.

    """

    def check(path: Path):

        if path.is_dir():

            if any(path.iterdir()):

                if not args.overwrite:

                    parser.error(

                        f"Output directory {path}{os.sep} isn't empty, so some files "

                        "could be overwritten or deleted.\nRerun the command "

                        "with --force option to overwrite "

                        "existing output files.")

            else:

                for child in path.iterdir():

                    if child.is_file():

                        os.remove(child)

                    elif child.is_dir():

                        shutil.rmtree(child)

    if isinstance(required, str):

        required = Path(required)

    for cur_dir in [required]:

        check(cur_dir)

Last update: 2024-07-09
Created: 2024-07-09