<commit_before>import os
import pdb
import sys
import tempfile
sys.path.append("/opt/tosca")
from translator.toscalib.tosca_template import ToscaTemplate

from core.models import Node, NodeLabel, Site, Deployment, SiteDeployment

from xosresource import XOSResource

class XOSNode(XOSResource):
    provides = "tosca.nodes.Node"
    xos_model = Node

    def get_xos_args(self):
        args = {"name": self.obj_name}

        site = None
        siteName = self.get_requirement("tosca.relationships.MemberOfSite", throw_exception=False)
        if siteName:
            site = self.get_xos_object(Site, login_base=siteName)
            args["site"] = site

        deploymentName = self.get_requirement("tosca.relationships.MemberOfDeployment", throw_exception=False)
        if deploymentName:
            deployment = self.get_xos_object(Deployment, name=deploymentName)

            if site:
                siteDeployment = self.get_xos_object(SiteDeployment, site=site, deployment=deployment, throw_exception=True)
                args["site_deployment"] = siteDeployment

        return args

    def postprocess(self, obj):
        # We can't set the labels when we create a Node, because they're
        # ManyToMany related, and the node doesn't exist yet.
        labels=[]
        for label_name in self.get_requirements("tosca.relationships.HasLabel"):
            labels.append(self.get_xos_object(NodeLabel, name=label_name))
        if labels:
            self.info("Updated labels for node '%s'" % obj)
            obj.labels = labels
            obj.save()

    def create(self):
        xos_args = self.get_xos_args()

        if not xos_args.get("site", None):
            raise Exception("Site is a required field of Node")
        if not xos_args.get("site_deployment", None):
            raise Exception("Deployment is a required field of Node")

        node = Node(**xos_args)
        node.caller = self.user<commit_msg>Adding labels to nodes via TOSCA<commit_after>import os
import pdb
import sys
import tempfile
sys.path.append("/opt/tosca")
from translator.toscalib.tosca_template import ToscaTemplate

from core.models import Node, NodeLabel, Site, Deployment, SiteDeployment

from xosresource import XOSResource

class XOSNode(XOSResource):
    provides = "tosca.nodes.Node"
    xos_model = Node

    def get_xos_args(self):
        args = {"name": self.obj_name}

        site = None
        siteName = self.get_requirement("tosca.relationships.MemberOfSite", throw_exception=False)
        if siteName:
            site = self.get_xos_object(Site, login_base=siteName)
            args["site"] = site

        deploymentName = self.get_requirement("tosca.relationships.MemberOfDeployment", throw_exception=False)
        if deploymentName:
            deployment = self.get_xos_object(Deployment, name=deploymentName)

            if site:
                siteDeployment = self.get_xos_object(SiteDeployment, site=site, deployment=deployment, throw_exception=True)
                args["site_deployment"] = siteDeployment

        return args

    def postprocess(self, obj):
        # We can't set the labels when we create a Node, because they're
        # ManyToMany related, and the node doesn't exist yet.
        for label_name in self.get_requirements("tosca.relationships.HasLabel"):
            # labels.append(self.get_xos_object(NodeLabel, name=label_name))
            label = NodeLabel.objects.get(name=label_name)
            obj.nodelabels.add(label)
            self.info("Added label '%s' for node '%s'" % (label_name, obj))

    def create(self):
        xos_args = self.get_xos_args()

        if not xos_args.get("site", None):
            raise Exception("Site is a required field of Node")
        if not xos_args.get("site_deployment", None):
            raise Exception("Deployment is a required field of Node")

        node = Node(**xos_args)
        node.caller = self.user
{'fraction_non_alphanumeric': 0.09155645981688708, 'fraction_numerical': 0.0, 'mean_word_length': 2.8863636363636362, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3488207', 'n_tokens_mistral': 1167, 'n_tokens_neox': 1147, 'n_words': 283}