Convert DingoObjDict to facts and associate resulting facts with this information object.
438102
def from_dict(self,
                  dingos_obj_dict,
                  config_hooks=None,
                  namespace_dict=None,
                  ):
        


        # Instantiate default parameters




        if '@@ns' in dingos_obj_dict.keys():
            top_level_namespace = (namespace_dict.get(dingos_obj_dict.get('@@ns'),None),dingos_obj_dict.get('@@ns'))
        else:
            top_level_namespace = (None,None)

        if not config_hooks:
            config_hooks = {}

        datatype_extractor = config_hooks.get('datatype_extractor', (lambda io, f, i, n, d: False))

        special_ft_handler = config_hooks.get('special_ft_handler', None)

        attr_ignore_predicate = config_hooks.get('attr_ignore_predicate', None)

        force_nonleaf_fact_predicate = config_hooks.get('force_nonleaf_fact_predicate', None)

        if not namespace_dict:
            namespace_dict = {}


        namespace_uri_2_pk_mapping = dict(self._DCM['DataTypeNameSpace'].objects.values_list('uri','id'))


        if not self.is_empty():
            logger.debug("Non-empty info object %s (timestamp %s, pk %s ) is overwritten with new information" % (self.identifier,
                                                                                                                 self.timestamp,
                                                                                                                 self.pk))
            self.clear()


        # Flatten the DingoObjDict

        (flat_list, attrs) = dingos_obj_dict.flatten(attr_ignore_predicate=attr_ignore_predicate,
                                                     force_nonleaf_fact_predicate=force_nonleaf_fact_predicate,
                                                     namespace_dict=namespace_dict)



        for fact in flat_list:

            # Collect the information about all attributes relevant
            # for this node (i.e., they occur either directly on
            # the node or on an ancestor node).

            # The following code was used to generate a attribute dictionary::
            #
            #     attr_info = ExtendedSortedDict()
            #     for attr_node in attrs.keys():
            #         if fact['node_id'].startswith(attr_node):
            #            for (key, value) in attrs[attr_node].items():
            #                attr_info.chained_set(value, 'set', key, attr_node)
            #
            # The dictionary contained full information about also the attributes given
            # to ancestor nodes. So far, we have not required this
            # information for our imports, and thus simplify to a
            # dictionary that only contains the attributes directly
            # associated with the current node.
            #
            # If, at a later stage, we find we need this kind of information,
            # we will can use Django's MultiValueDict to add additional
            # information without changing the signature of the receiving
            # predicate and handler functions.


            attr_info = dict(attrs.get(fact['node_id'],[]))

            #for attr_node in attrs.keys():
            #    if fact['node_id'] == (attr_node):
            #        for (key, value) in attrs[attr_node].items():
            #            attr_info.chained_set(value, 'set', key, attr_node)


            # Fill dictionary with arguments for call to 'add_fact'

            add_fact_kargs = {}
            add_fact_kargs['fact_dt_kind'] = FactDataType.UNKNOWN_KIND
            add_fact_kargs['fact_dt_namespace_name'] = "%s-%s" % (
                self.iobject_family.name, self.iobject_family_revision.name)

            # See whether the datatype extractor has found a datatype for the value

            datatype_found = datatype_extractor(self, fact, attr_info, namespace_dict, add_fact_kargs)

            if not datatype_found:
                add_fact_kargs = {}
                add_fact_kargs['fact_dt_kind'] = FactDataType.NO_VOCAB
                add_fact_kargs['fact_dt_namespace_name'] = DINGOS_NAMESPACE_SLUG
                add_fact_kargs['fact_dt_namespace_uri'] = DINGOS_NAMESPACE_URI
            else:
                # Check whether the datatype extractor added namespace information
                # If not, add some here
                if not 'fact_dt_namespace_uri' in add_fact_kargs:
                    add_fact_kargs['fact_dt_namespace_uri'] = namespace_dict.get(
                        add_fact_kargs['fact_dt_namespace_name'], '%s/%s' % (
                            DINGOS_NAMESPACE_URI, self.iobject_family))

            add_fact_kargs['fact_term_name'] = fact['term']
            add_fact_kargs['fact_term_attribute'] = fact['attribute']
            add_fact_kargs['values'] = [fact['value']]
            add_fact_kargs['node_id_name'] = fact['node_id']
            add_fact_kargs['namespaces'] = fact['namespaces']
            add_fact_kargs['top_level_namespace'] = top_level_namespace

            handler_return_value = True

            logger.debug("Treating fact (before special handler list) %s with attr_info %s and kargs %s" % (fact, attr_info, add_fact_kargs))
            # Below, go through the handlers in the special_ft_handler list --
            # if the predicate returns True for the fact, execute the handler
            # on the fact. If a handler returns False/None, the fact is *not*
            # added. This should only be done, if the handler has added the
            # fact -- otherwise the sequence of node identifiers is messed up!

            if special_ft_handler:
                for (predicate, handler) in special_ft_handler:
                    if predicate(fact, attr_info):
                        handler_return_value = handler(self, fact, attr_info, add_fact_kargs)
                        if not handler_return_value:
                            break
            logger.debug("Treating fact (before special handler list) %s with attr_info %s and kargs %s" % (fact, attr_info, add_fact_kargs))
            if (handler_return_value == True):
                add_fact_kargs['ns_uri_dict'] = namespace_uri_2_pk_mapping
                e2f_obj = self.add_fact(**add_fact_kargs)
            elif not handler_return_value:
                continue
            else:
                e2f_obj = handler_return_value
        self.set_name()
360306
def junos_facts(**kwargs):
    '''
    
    '''
    prep = _junos_prep_fun(napalm_device)  # pylint: disable=undefined-variable
    if not prep['result']:
        return prep
    facts = dict(napalm_device['DRIVER'].device.facts)  # pylint: disable=undefined-variable
    if 'version_info' in facts:
        facts['version_info'] = \
            dict(facts['version_info'])
    # For backward compatibility. 'junos_info' is present
    # only of in newer versions of facts.
    if 'junos_info' in facts:
        for re in facts['junos_info']:
            facts['junos_info'][re]['object'] = \
                dict(facts['junos_info'][re]['object'])
    return facts
274142
def mongo_to_object(self, statement_data):
        
        Statement = self.get_model('statement')

        statement_data['id'] = statement_data['_id']

        return Statement(**statement_data)
1171928
def get_or_create_fact(fact_term,
                       fact_dt_name='String',
                       fact_dt_namespace_uri=DINGOS_NAMESPACE_URI,
                       values=None,
                       value_iobject_id=None,
                       value_iobject_ts=None,
                       ):
    

    if not values:
        values = []


    vocab_namespace, created = dingos_class_map['DataTypeNameSpace'].objects.get_or_create(uri=fact_dt_namespace_uri)

    fact_data_type, created = dingos_class_map['FactDataType'].objects.get_or_create(name=fact_dt_name,
                                                                                    namespace=vocab_namespace)

    # Maybe we already have a fact with exactly the same fact term and the same fact values?
    # We start by looking at the number of values

    value_objects = []

    for value in values:
        storage_location=dingos.DINGOS_VALUES_TABLE
        # collect (create or get) the required value objects
        if value == None:
            value = ''
        if isinstance(value,tuple):
            # If a value is wrapped in a tuple, the second component of the tuple
            # specifies the storage location of the value.
            value, storage_location = value

        if storage_location == dingos.DINGOS_VALUES_TABLE:
            # If the value is larger than a given size, the value is written to disk, instead.
            # We use this to keep too large values out of the database. Depending on how the
            # database is set up, this may be necessary to allow indexing, which in turn is
            # required to check uniqueness on values.

            if len(value) > dingos.DINGOS_MAX_VALUE_SIZE_WRITTEN_TO_VALUE_TABLE:
                (value_hash,storage_location) = write_large_value(value)
                value = value_hash


        fact_value, created = dingos_class_map['FactValue'].objects.get_or_create(value=value,
                                                                                 fact_data_type=fact_data_type,
                                                                                 storage_location=storage_location)
        value_objects.append(fact_value)



    # Do we already have a fact with given fact term and given values?
    #
    # For understanding the query below better, see https://groups.google.com/forum/#!topic/django-users/X9TCSrBn57Y.
    # The double query is necessary, because the first count counts the number of selected
    # fact_value objects, not the number of total objects for each fact.


    possibly_matching_facts = Fact.objects.filter(fact_values__in=value_objects,
                                                  value_iobject_id=value_iobject_id,
                                                  value_iobject_ts=value_iobject_ts,
                                                  fact_term=fact_term
    ).values_list('pk',flat=True)

    matching_facts = Fact.objects.filter(pk__in=list(possibly_matching_facts)). \
        annotate(num_values=Count('fact_values')). \
        filter(num_values=len(value_objects)). \
        exclude(id__in= \
        Fact.objects.filter(pk__in=possibly_matching_facts).annotate(total_values=Count('fact_values')). \
            filter(total_values__gt=len(value_objects)))

    # Below, for educational purposes, the original query until Dingos 0.2.0, which got *really*
    # slow with lot's of objects in the system. The reason for this are the last three lines:
    # the exclude-statement required the database to count the the number of values for each
    # Fact in the system... but we are really only interested into facts with the same
    # fact_term, value_iobject_id and value_iobject_ts...

    #matching_facts = Fact.objects.filter(fact_values__in=value_objects). \
    #    annotate(num_values=Count('fact_values')). \
    #    filter(num_values=len(value_objects)). \
    #    filter(value_iobject_id=value_iobject_id). \
    #    filter(value_iobject_ts=value_iobject_ts). \
    #    filter(fact_term=fact_term). \
    #    exclude(id__in= \
    #    Fact.objects.annotate(total_values=Count('fact_values')). \
    #        filter(total_values__gt=len(value_objects)))

    created = True
    try:
        fact_obj = matching_facts[0]
        created = False
        logger.debug("FOUND MATCHING OBJECT with pk %s" % fact_obj.pk)
    except:
        fact_obj = dingos_class_map['Fact'].objects.create(fact_term=fact_term,
                                                          value_iobject_id=value_iobject_id,
                                                          value_iobject_ts=value_iobject_ts,
                                                           )

        fact_obj.fact_values.add(*value_objects)
        fact_obj.save()


    return fact_obj, created
916025
def single_node_facts(node, facts)
      fields = Hash[facts.collect { |fact| [fact.to_sym, 1] }]
      collection = connection[nodes_collection]
      result = collection.find(_id: node).limit(1).batch_size(1).projection(fields).to_a.first
      result.delete("_id") if result
      result
    end
1358184
def to_yaml
      sorted_facts = @facts.sort.to_h
      facts_hash_with_expanded_values = Hash[sorted_facts.collect { |k, v| [k, v.value] }]
      YAML.dump(facts_hash_with_expanded_values)
    end
690607
def as_dict(self, decode=False):
        
        res = self.database.hgetall(self.key)
        return decode_dict(res) if decode else res
1184159
def _to_dict(self):
        """"""
        _dict = {}
        if hasattr(self, 'type') and self.type is not None:
            _dict['type'] = self.type
        if hasattr(self, 'frequency') and self.frequency is not None:
            _dict['frequency'] = self.frequency
        if hasattr(self, 'arguments') and self.arguments is not None:
            _dict['arguments'] = [x._to_dict() for x in self.arguments]
        if hasattr(self, 'evidence') and self.evidence is not None:
            _dict['evidence'] = [x._to_dict() for x in self.evidence]
        return _dict
827826
def cybox_RAW_ft_handler(self, enrichment, fact, attr_info, add_fact_kargs):
        
        # get the value
        raw_value = add_fact_kargs['values'][0]

        if len(raw_value) >= RAW_DATA_TO_DB_FOR_LENGTH_LESS_THAN:
            # rewrite the argument for the creation of the fact: there are
            # no values to be added to the database
            (value_hash,storage_location) = write_large_value(raw_value,
                                                              dingos.DINGOS_LARGE_VALUE_DESTINATION)

            add_fact_kargs['values'] = [(value_hash,storage_location)]


        return True
222165
def info(self, abspath=True):
        
        logger.debug(str(''))
        return self._call_and_parse(['info', '--json'], abspath=abspath)
228317
def facts_to_yaml(node = @node)
      # Add the header that Puppet needs to treat this as facts. Save the results
      # as a string in the option.
      f = facts(node)
      fact_file = f.to_yaml.split(/\n/)
      fact_file[0] = '--- !ruby/object:Puppet::Node::Facts' if fact_file[0] =~ /^---/
      fact_file.join("\n")
    end
1279332
def facter_info():
    

    with suppress(FileNotFoundError):  # facter may not be installed
        proc = subprocess.Popen(['facter', '--yaml'],
                                stdout=subprocess.PIPE,
                                stderr=subprocess.PIPE)
        stdout, stderr = proc.communicate()
        if not proc.returncode:
            data = serializer.load(stdout)
            return {'facter': data}
846130
def create_info(name, info_type, url=None, parent=None, id=None,
                context=ctx_default, store=False):
    """"""
    id = str(uuid4()) if id is None else id
    pubsub = _pubsub_key(id)

    info = {'id': id,
            'type': info_type,
            'pubsub': pubsub,
            'url': url,
            'parent': parent,
            'context': context,
            'name': name,
            'status': 'Queued' if info_type == 'job' else None,
            'date_start': None,
            'date_end': None,
            'date_created': str(datetime.now()),
            'result': None}

    if store:
        r_client.set(id, json_encode(info))

        if parent is not None:
            r_client.sadd(_children_key(parent), id)

    return info
837507
def gather_facts(self):
        
        facts = []

        for file in self.yaml_files:
            facts += self.gather_facts_list(file)

        unique_facts = list(set(facts))
        self.facts = unique_facts

        return len(unique_facts)
682990
def define_fact(name, &block)
      add Schema::Builders::FactBuilder.new(self).build(name, &block)
    end
184747
def convert(self):
        
        try:
            return utils.encode_dict(self.metainfo)
        except ValueError as e:
            raise error.MetainfoError(str(e))
704717
def facts(self):
        """"""
        fact = lib.EnvGetNextFact(self._env, ffi.NULL)

        while fact != ffi.NULL:
            yield new_fact(self._env, fact)

            fact = lib.EnvGetNextFact(self._env, fact)