Skip to content

parse_argument.py

Contains a function to parse given arguments.

__test_error(argument, key, condition, message, optional=False)

Test potential errors for a given argument.

Parameters

argument : Namespace
Argument to test.

str

The name of the argument to test.

str

The condition to test.

str

The error message.

bool, optional

If it is True, check nothing if the argument is None. Else, if
False, check the error. By default False.

Returns

None

Raises

FileNotFoundError
When the given path is not found.

ValueError
When the extension is wrong.

ValueError
When the path is not a directory.

Source code in src/smiffer/utils/parse_argument.py
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
def __test_error(
    argument: Namespace,
    key: str,
    condition: str,
    message: str,
    optional=False,
):
    """Test potential errors for a given argument.

    Parameters
    ----------
    argument : `Namespace`
        Argument to test.

    key : `str`
        The name of the argument to test.

    condition : `str`
        The condition to test.

    message : `str`
        The error message.

    optional : `bool`, optional
        If it is `True`, check nothing if the argument is `None`. Else, if
        `False`, check the error. By default `False`.

    Returns
    -------
    None

    Raises
    ------
    `FileNotFoundError`
        When the given path is not found.

    `ValueError`
        When the extension is wrong.

    `ValueError`
        When the path is not a directory.
    """
    value: str = getattr(argument, key)

    # Skip if value is None and optional.
    if value is None and optional:
        return None

    # Path not found.
    if not exists(value):
        raise FileNotFoundError(
            f'[Err##] In {key}, "{value}" does not ' "exist."
        )

    if condition == "isdir":
        condition = not isdir(value)
    else:
        condition = not value.endswith(condition)

    # Check the extension or if it is a directory.
    if condition:
        raise ValueError(f"[Err##] In {key}, {message}.")

    return None

__test_parsed_argument(argument)

Test all setup arguments to see if they are correct or not.

Parameters

argument : Namespace
The parsed arguments.

Source code in src/smiffer/utils/parse_argument.py
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
def __test_parsed_argument(argument: Namespace):
    """Test all setup arguments to see if they are correct or not.

    Parameters
    ----------
    argument : `Namespace`
        The parsed arguments.
    """
    # Test argument "--input".
    __test_error(
        argument=argument,
        key="input",
        condition=".pdb",
        message='extension is not ".pdb".',
    )

    # Test argument "--output".
    __test_error(
        argument=argument,
        key="output",
        condition="isdir",
        message='extension is not ".pdb".',
    )

    # Test argument "--parameter".
    __test_error(
        argument=argument,
        key="parameter",
        condition=".yml",
        message='extension is not ".yml".',
        optional=True,
    )

    # Test argument "--apbs".
    __test_error(
        argument=argument,
        key="apbs",
        condition=".dx",
        message='extension is not ".dx".',
        optional=True,
    )

    if argument.log and argument.apbs is None:
        raise ValueError(
            "[Err##] When --log flag is used, --apbs is required."
        )

parse_argument(version='')

Parse given arguments and tests them.

Parameters

version : str, optional
The script version. By default None.

Returns

Namespace
The object with parsed arguments.

Raises

FileNotFoundError
When a file or a directory given in argument does not exist, throw an
error.

ValueError
When a file have a wrong extension or is a not directory, throw an
error.

Source code in src/smiffer/utils/parse_argument.py
 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
def parse_argument(version: str = "") -> Namespace:
    """Parse given arguments and tests them.

    Parameters
    ----------
    version : `str`, optional
        The script version. By default None.

    Returns
    -------
    `Namespace`
        The object with parsed arguments.

    Raises
    ------
    FileNotFoundError
        When a file or a directory given in argument does not exist, throw an
        error.

    ValueError
        When a file have a wrong extension or is a not directory, throw an
        error.
    """
    logo: str = """
                 _____                                 _____
                ( ___ )-------------------------------( ___ )
                 |   |                                 |   |
                 |   | ░█▀▀▀░█▄░▄█░▀█▀░█▀▀░█▀▀░█▀▀░█▀▄ |   |
                 |   | ░▀▀▀█░█░▀░█░░█ ░█▀▀░█▀▀░█▀▀░█▀▄ |   |
                 |   | ░▀▀▀▀░▀  ░▀░▀▀▀░▀  ░▀  ░▀▀▀░▀░▀ |   |
                 |   |                                 |   |
                (_____)-------------------------------(_____)
    """


    print(logo[1:])

    # Description of the program given when the help is cast.
    description: str = """
    Program to compute physical and chemical properties of a protein or a RNA.
    Input take a PDB file and gives fields in OpenDX format. To use the
    program, the simplest command line is:

    \033[1m$ smiffer -i 8C3R.pdb -o output/\033[0m

    \033[7m Legend: \033[0m
        - int: Integer.
        - [type|value]: Type of the input required, follow by the default
                        value. So if this optional arguments is not used,
                        "value" will be chosen.

    \033[7m Documentation: https://smiffer.mol3d.tech \033[0m
    """
    # ===================
    #
    # ADD ARGUMENT PARSER
    #
    # ===================

    # Setup the arguments parser object.
    parser: Namespace = ArgumentParser(
        description=dedent(description)[1:-1],
        formatter_class=RawTextHelpFormatter,
        add_help=False,
    )

    # == REQUIRED.
    parser.add_argument(
        "-i",
        "--input",
        dest="input",
        required=True,
        type=str,
        metavar="[FILE]['.pdb']",
        help='\033[7m [[MANDATORY]] \033[0m\n    > A ".pdb" file.',
    )

    parser.add_argument(
        "-o",
        "--output",
        dest="output",
        required=True,
        type=str,
        metavar="[DIRECTORY]",
        help=(
            "\033[7m [[MANDATORY]] \033[0m\n    > A folder where the "
            "results will be stored."
        ),
    )

    # == OPTIONAL.
    parser.add_argument(
        "-h",
        "--help",
        action="help",
        help="    > Display this help message, then exit the program.",
    )

    parser.add_argument(
        "-v",
        "--version",
        action="version",
        version=f"Program version is {version}",
        help="    > Display the program's version, then exit the program.",
    )

    parser.add_argument(
        "-p",
        "--parameter",
        default=None,
        type=str,
        metavar="[FILE|None]['.yml']",
        help=(
            '    > A "YAML" parameter file. If not given, default values\n'
            "      are used."
        ),
    )

    parser.add_argument(
        "-a",
        "--apbs",
        default=None,
        type=str,
        metavar="[FILE|None]['.dx']",
        help=(
            '    > A "OpenDX" file output by APBS. If not given, no APBS\n'
            "      file will be produced."
        ),
    )

    parser.add_argument(
        "--log",
        action="store_true",
        help=(
            "    > When APBS and this flags are given, an additional file\n"
            "      containing LOG of APBS while be computed."
        ),
    )

    parser.add_argument(
        "--verbose",
        required=False,
        action="store_true",
        help=(
            "    > If used, the program give more information on what is\n"
            "      happening."
        ),
    )

    argument = parser.parse_args()

    # ============
    #
    # CHECK ERRORS
    #
    # ============

    # Deleting potential "/" at directory end.
    argument.output = argument.output.rstrip("/")

    __test_parsed_argument(argument=argument)

    return argument