xref: /test/testfwk/xdevice/plugins/ohos/src/ohos/utils.py (revision 76e6818a)
1#!/usr/bin/env python3
2# coding=utf-8
3
4#
5# Copyright (c) 2020-2022 Huawei Device Co., Ltd.
6# Licensed under the Apache License, Version 2.0 (the "License");
7# you may not use this file except in compliance with the License.
8# You may obtain a copy of the License at
9#
10#     http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS,
14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15# See the License for the specific language governing permissions and
16# limitations under the License.
17#
18
19from ohos.constants import Constant
20
21__all__ = ["parse_line_key_value", "parse_strings_key_value"]
22
23
24def parse_line_key_value(line):
25    """parse line which should format as 'key = value'"""
26    param = {}
27    if "=" in line:
28        arr = line.split("=")
29        if len(arr) == 2:
30            param.setdefault(arr[0].strip(), arr[1].strip())
31    return param
32
33
34def parse_strings_key_value(in_str):
35    """parse string which should format as 'key = value'"""
36    is_param, params = False, {}
37    for line in in_str.split("\n"):
38        if Constant.PRODUCT_PARAMS_START in line:
39            is_param = True
40        elif Constant.PRODUCT_PARAMS_END in line:
41            is_param = False
42        if is_param:
43            params.update(parse_line_key_value(line))
44    return params
45