summaryrefslogtreecommitdiffstats
path: root/testing/mozbase/manifestparser/manifestparser/util.py
blob: 6cfe57de5c3609a3dbc7092b023861398841c07c (plain)
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
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

import ast
import os


def normsep(path):
    """
    Normalize path separators, by using forward slashes instead of whatever
    :py:const:`os.sep` is.
    """
    if os.sep != "/":
        # Python 2 is happy to do things like byte_string.replace(u'foo',
        # u'bar'), but not Python 3.
        if isinstance(path, bytes):
            path = path.replace(os.sep.encode("ascii"), b"/")
        else:
            path = path.replace(os.sep, "/")
    if os.altsep and os.altsep != "/":
        if isinstance(path, bytes):
            path = path.replace(os.altsep.encode("ascii"), b"/")
        else:
            path = path.replace(os.altsep, "/")
    return path


def evaluate_list_from_string(list_string):
    """
    This is a utility function for converting a string obtained from a manifest
    into a list. If the string is not a valid list when converted, an error will be
    raised from `ast.eval_literal`. For example, you can convert entries like this
    into a list:
    ```
        test_settings=
            ["hello", "world"],
            [1, 10, 100],
        values=
            5,
            6,
            7,
            8,
    ```
    """
    parts = [
        x.strip(",")
        for x in list_string.strip(",").replace("\r", "").split("\n")
        if x.strip()
    ]
    return ast.literal_eval("[" + ",".join(parts) + "]")