summaryrefslogtreecommitdiffstats
path: root/scripts/basic
Commit message (Expand)AuthorAgeFilesLines
* fixdep: fix coding style in previous fixStephen Warren2020-08-041-1/+1
* fixdep: fix CONFIG_IS_ENABLED etc. handlingStephen Warren2020-07-171-0/+1
* fixdep: Re-sync with Linux 5.7-rc1Masahiro Yamada2020-04-241-45/+47
* fixdep: handle CONFIG_IS_ENABLE() and friends for TPLMasahiro Yamada2020-04-241-5/+7
* kconfig / kbuild: re-sync with Linux 4.18Tom Rini2020-03-161-2/+4
* kbuild: fixdep: Resync this with v4.17Tom Rini2020-03-161-201/+150
* fixdep: fix U-Boot own code to handle only valid symbol charactersMasahiro Yamada2020-03-161-1/+1
* SPDX: Convert all of our single license tags to Linux Kernel styleTom Rini2018-05-071-3/+1
* fixdep: fix dependency on options surrounded by CONFIG_VAL()Masahiro Yamada2017-10-161-2/+9
* kbuild: fixdep: Check fstat(2) return valueTom Rini2016-05-231-1/+5
* Use correct spelling of "U-Boot"Bin Meng2016-02-061-2/+2
* Various Makefiles: Add SPDX-License-Identifier tagsTom Rini2015-11-101-0/+3
* kbuild: fixdep: drop meaningless hash table initializationMasahiro Yamada2015-09-151-19/+0
* linux/kconfig.h: add CPP macros useful for per-image config optionsMasahiro Yamada2015-08-181-0/+26
* kbuild: fixdep: optimize code slightlyMasahiro Yamada2015-08-181-3/+4
* fixdep: remove multiple .config support codeMasahiro Yamada2015-03-051-5/+1
* kconfig: switch to KconfigMasahiro Yamada2014-07-301-1/+5
* cosmetic: kbuild: clean-up coding style (sync with Linux 3.16-rc1)Masahiro Yamada2014-06-201-4/+4
* kbuild: import more build scripts from Linux v3.13 tagMasahiro Yamada2014-02-193-0/+478
2'>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
from __future__ import absolute_import

import base64
import json

import six
from six import iteritems, itervalues

TYPES = {}
NOTYPES = {}


def encode_cert(data):
    """base64 encode X.509 certificate

    Python 3's base64.b64encode() doesn't support ASCII text.

    :param data: data as bytes or ASCII text
    :type data: str, bytes
    :rtype: bytes
    """
    if isinstance(data, six.text_type):
        data = data.encode('ascii')
    return base64.b64encode(data)


def decode_cert(data):
    """base64 decode X.509 certificate

    :param data: data as bytes or ASCII text
    :type data: str, bytes
    :rtype: bytes
    """
    if isinstance(data, six.text_type):
        data = data.encode('ascii')
    return base64.b64decode(data)


class CustomTypeEncoder(json.JSONEncoder):
    """
    A custom JSONEncoder class that knows how to encode core custom
    objects.

    Custom objects are encoded as JSON object literals (ie, dicts) with
    one key, 'TypeName' where 'TypeName' is the actual name of the
    type to which the object belongs.  That single key maps to another
    object literal which is just the __dict__ of the object encoded.

    Reason for ignoring the error:
    E0202 - An attribute affected in json.encoder line 157 hide this method
    reported by pylint:

    The error is in json.encoder.JSONEncoder class.
    There is a default method (which is overridden here) and also a class
    attribute self.default initialized in the init method of the class.
    The intention of such usage being that a custom default method object can
    be passed to init when creating an instance of JSONEncoder, which is then
    assigned to class's default method. (which is valid)
    But pylint raises an issue due to the usage of same name for a method and
    an attribute in which case the attribute definition hides the method.
    The reason and example for the issue: (top rated comment)

        http://stackoverflow.com/questions/12949064/python-what-happens-
        when-instance-variable-name-is-same-as-method-name
    """
    # pylint: disable=E0202

    def default(self, obj):
        for k, v in iteritems(TYPES):
            if isinstance(obj, v):
                return {k: obj.__dict__}
        for t in itervalues(NOTYPES):
            if isinstance(obj, t):
                return self.attr_name_conversion(obj.__dict__, type(obj))
        return json.JSONEncoder.default(self, obj)

    @staticmethod
    def attr_name_conversion(attr_dict, object_class):
        if not hasattr(object_class, 'json_attribute_names'):
            return attr_dict
        reverse_dict = {v: k for k, v in
                        iteritems(object_class.json_attribute_names)}
        new_dict = dict()
        for k, v in iteritems(attr_dict):
            if k in reverse_dict:
                new_dict[reverse_dict[k]] = v
            else:
                new_dict[k] = v
        return new_dict


def CustomTypeDecoder(dct):  # nopep8
    if len(dct) == 1:
        type_name = list(dct)[0]
        value = dct[type_name]
        if type_name in TYPES:
            return TYPES[type_name].from_dict(value)
    return dct