Exemplo n.º 1
0
    def from_query(name,
                   query_data,
                   impl_class=Bunch,
                   item_class=Bunch,
                   list_config=False,
                   decrypt_func=None,
                   drop_opaque=False):
        """ Return a new ConfigDict with items taken from an SQL query.
        """
        config_dict = ConfigDict(name)
        config_dict._impl = impl_class()

        if query_data:
            query, attrs = query_data

            for item in query:

                if hasattr(item, 'name'):
                    item_name = item.name
                else:
                    item_name = item.get_name()

                if list_config:
                    list_dict = Bunch()
                    if item_name not in config_dict._impl:
                        config_dict._impl[item_name] = []
                    config_dict._impl[item_name].append(list_dict)
                else:
                    config_dict._impl[item_name] = item_class()

                if list_config:
                    for attr_name in attrs.keys():
                        list_dict[attr_name] = getattr(item, attr_name)

                else:
                    config_dict._impl[item_name].config = item_class()
                    for attr_name in attrs.keys():
                        config = config_dict._impl[item_name]['config']
                        original = value = getattr(item, attr_name)
                        value = resolve_value(attr_name, value, decrypt_func)
                        config[attr_name] = value

                        # Temporarily, add a flag to indicate whether the password in ODB was encrypted or not.
                        if attr_name in SECRETS.PARAMS:
                            if original is None:
                                original = ''
                            config['_encryption_needed'] = True
                            if original.startswith(SECRETS.PREFIX):
                                config['_encrypted_in_odb'] = True
                            else:
                                config['_encrypted_in_odb'] = False

        # Post-process data before it is returned to resolve any opaque attributes
        for value in config_dict.values():
            value_config = value['config']
            if ElemsWithOpaqueMaker.has_opaque_data(value_config):
                ElemsWithOpaqueMaker.process_config_dict(
                    value_config, drop_opaque)

        return config_dict
Exemplo n.º 2
0
    def from_query(name,
                   query_data,
                   impl_class=Bunch,
                   item_class=Bunch,
                   list_config=False,
                   decrypt_func=None):
        """ Return a new ConfigDict with items taken from an SQL query.
        """
        config_dict = ConfigDict(name)
        config_dict._impl = impl_class()

        if query_data:
            query, attrs = query_data

            for item in query:

                if hasattr(item, 'name'):
                    item_name = item.name
                else:
                    item_name = item.get_name()

                if list_config:
                    list_dict = Bunch()
                    if item_name not in config_dict._impl:
                        config_dict._impl[item_name] = []
                    config_dict._impl[item_name].append(list_dict)
                else:
                    config_dict._impl[item_name] = item_class()

                if list_config:
                    for attr_name in attrs.keys():
                        list_dict[attr_name] = getattr(item, attr_name)

                else:
                    config_dict._impl[item_name].config = item_class()
                    for attr_name in attrs.keys():
                        config = config_dict._impl[item_name]['config']
                        original = value = getattr(item, attr_name)
                        value = resolve_value(attr_name, value, decrypt_func)
                        config[attr_name] = value

                        # Temporarily, add a flag to indicate whether the password in ODB was encrypted or not.
                        if attr_name in SECRETS.PARAMS:
                            config['_encryption_needed'] = True
                            if original.startswith(SECRETS.PREFIX):
                                config['_encrypted_in_odb'] = True
                            else:
                                config['_encrypted_in_odb'] = False

        return config_dict