<commit_before>#!/usr/bin/env python

from roush import backends


class NodeBackend(backends.Backend):
    def __init__(self):
        super(NodeBackend, self).__init__(__file__)

    def additional_constraints(self, action, ns):
        if action == 'set_fact':
            if not 'key' in ns:
                raise ValueError('no key in ns')
            key = ns['key']

            # see what backend this key is in...
            for name, obj in backends.backend_objects.iteritems():
                if key in obj.facts:
                    return ['"%s" in facts.backends' % name]

            return ['1=2']
        if action == 'set_parent':
            api = ns['api']
            if not 'parent' in ns:
                raise ValueError('no parent set')
            parent = api._model_get_by_id('nodes', ns['parent'])
            return ['facts.%s = "%s"' % (key, parent.facts[key])
                    for key in parent.facts.keys()]
        return []

    def set_parent(self, api, node_id, parent):
        api._model_update_by_id('nodes', node_id,
                                {'parent_id': parent})
        return True

    def set_fact(self, api, node_id, key, value):
        # if the fact exists, update it, else create it.
        oldkeys = api._model_query('facts', 'node_id=%s and key=%s' %
                                   (node_id, key))

        if len(oldkeys) > 0:
            # update
            api._model_update_by_id('facts', {'id': oldkeys[0]['id'],
                                              'value': value})
        else:
            api._model_create('facts', {'node_id': node_id,
                                        'key': key,
                                        'value': value})

        return True
<commit_msg>Only allow set_parent on containers<commit_after>#!/usr/bin/env python

from roush import backends


class NodeBackend(backends.Backend):
    def __init__(self):
        super(NodeBackend, self).__init__(__file__)

    def additional_constraints(self, action, ns):
        if action == 'set_fact':
            if not 'key' in ns:
                raise ValueError('no key in ns')
            key = ns['key']

            # see what backend this key is in...
            for name, obj in backends.backend_objects.iteritems():
                if key in obj.facts:
                    return ['"%s" in facts.backends' % name]

            return ['1=2']
        if action == 'set_parent':
            api = ns['api']
            if not 'parent' in ns:
                raise ValueError('no parent set')
            parent = api._model_get_by_id('nodes', ns['parent'])
            if 'container' in parent.facts.backends:
                return ['facts.%s = "%s"' % (key, parent.facts[key])
                        for key in parent.facts.keys()]
            else:
                return ['1=2']
        return []

    def set_parent(self, api, node_id, parent):
        api._model_update_by_id('nodes', node_id,
                                {'parent_id': parent})
        return True

    def set_fact(self, api, node_id, key, value):
        # if the fact exists, update it, else create it.
        oldkeys = api._model_query('facts', 'node_id=%s and key=%s' %
                                   (node_id, key))

        if len(oldkeys) > 0:
            # update
            api._model_update_by_id('facts', {'id': oldkeys[0]['id'],
                                              'value': value})
        else:
            api._model_create('facts', {'node_id': node_id,
                                        'key': key,
                                        'value': value})

        return True

{'fraction_non_alphanumeric': 0.11865793780687398, 'fraction_numerical': 0.002727768685215494, 'mean_word_length': 1.4777027027027028, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '644974', 'n_tokens_mistral': 1024, 'n_tokens_neox': 982, 'n_words': 277}