1#!/usr/bin/env python
2# This file consists of the common useful functions for eFuse
3#
4# Copyright (C) 2020 Espressif Systems (Shanghai) PTE LTD
5#
6# This program is free software; you can redistribute it and/or modify it under
7# the terms of the GNU General Public License as published by the Free Software
8# Foundation; either version 2 of the License, or (at your option) any later version.
9#
10# This program is distributed in the hope that it will be useful, but WITHOUT
11# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
13#
14# You should have received a copy of the GNU General Public License along with
15# this program; if not, write to the Free Software Foundation, Inc., 51 Franklin
16# Street, Fifth Floor, Boston, MA 02110-1301 USA.
17from __future__ import division, print_function
18
19import esptool
20
21
22def hexify(bitstring, separator=""):
23    try:
24        as_bytes = tuple(ord(b) for b in bitstring)
25    except TypeError:  # python 3, items in bitstring already ints
26        as_bytes = tuple(b for b in bitstring)
27    return separator.join(("%02x" % b) for b in as_bytes)
28
29
30def popcnt(b):
31    """ Return number of "1" bits set in 'b' """
32    return len([x for x in bin(b) if x == "1"])
33
34
35def check_duplicate_name_in_list(name_list):
36    duples_name = [name for i, name in enumerate(name_list) if name in name_list[:i]]
37    if duples_name != []:
38        raise esptool.FatalError("Found repeated {} in the name list".format(duples_name))
39
40
41class SdkConfig(object):
42    def __init__(self, path_to_file):
43        self.sdkconfig = dict()
44        if path_to_file is None:
45            return
46        with open(path_to_file, 'r') as file:
47            for line in file.readlines():
48                if line.startswith("#"):
49                    continue
50                config = line.strip().split('=', 1)
51                if len(config) == 2:
52                    self.sdkconfig[config[0]] = True if config[1] == 'y' else config[1].strip('"')
53
54    def __getitem__(self, config_name):
55        try:
56            return self.sdkconfig[config_name]
57        except KeyError:
58            return False
59