<commit_before>
from sfa.util.xml import XpathFilter
from sfa.util.plxrn import PlXrn
from sfa.util.xrn import Xrn
from sfa.rspecs.elements.element import Element
from sfa.rspecs.elements.node import Node
from sfa.rspecs.elements.sliver import Sliver
from sfa.rspecs.elements.network import Network 
from sfa.rspecs.elements.location import Location
from sfa.rspecs.elements.hardware_type import HardwareType
from sfa.rspecs.elements.disk_image import DiskImage
from sfa.rspecs.elements.interface import Interface
from sfa.rspecs.elements.bwlimit import BWlimit
from sfa.rspecs.elements.pltag import PLTag
from sfa.rspecs.rspec_elements import RSpecElement, RSpecElements
from sfa.rspecs.elements.versions.sfav1Network import SFAv1Network
from sfa.rspecs.elements.versions.sfav1Sliver import SFAv1Sliver
from sfa.rspecs.elements.versions.sfav1PLTag import SFAv1PLTag
from sfa.rspecs.elements.versions.pgv2Services import PGv2Services

class SFAv1Node:

    @staticmethod
    def add_nodes(xml, nodes):
        network_elems = SFAv1Network.get_networks(xml)
        if len(network_elems) > 0:
            network_elem = network_elems[0]
        elif len(nodes) > 0 and nodes[0].get('component_manager_id'):
            network_elem = SFAv1Network.add_network(xml.root, {'name': nodes[0]['component_manager_id']})

        node_elems = []       
        for node in nodes:
            node_fields = ['component_manager_id', 'component_id', 'boot_state']
            elems = Element.add(network_elem, 'node', node, node_fields)
            node_elem = elems[0]  
            node_elems.append(node_elem)

            # determine network hrn
            network_hrn = None 
            if 'component_manager_id' in node and node['component_manager_id']:
                network_hrn = Xrn(node['component_manager_id']).get_hrn()

            # set component_name attribute and  hostname element
            if 'component_id' in node and node['component_id']:
                xrn = Xrn(node['component_id'])
                node_elem.set('component_name', xrn.get_leaf())
                hostname_tag = node_elem.add_element('hostname')
                hostname_tag.set_text(xrn.get_leaf())

            # set site id
            if 'authority_id' in node and node['authority_id']:
                node_elem.set('site_id', node['authority_id'])

            location_elems = Element.add(node_elem, 'location', node.get('location', []), Location.fields)
            interface_elems = Element.add(node_elem, 'interface', node.get('interfaces', []), Interface.fields)
            # need to generate the device id in the component_id
            i=0
            for interface_elem in interface_elems:
                comp_id = PlXrn(auth=network, interface='node%s:eth%s' % (interface['node_id'], i)).get_urn()
                interface_elem.set('component_id', comp_id)      
                i++ 
            
            #if 'bw_unallocated' in node and node['bw_unallocated']:
            #    bw_unallocated = etree.SubElement(node_elem, 'bw_unallocated', units='kbps').text = str(int(node['bw_unallocated'])/1000)

            PGv2Services.add_services(node_elem, node.get('services', []))
            SFAv1PLTags.add_tags(node_elem, node.get('tags', [])) 
            SFAv1Sliver.add_slivers(node_elem, node.get('slivers', []))

    @staticmethod 
    def add_slivers(xml, slivers):
        component_ids = []
        for sliver in slivers:
            filter = {}
            if isinstance(sliver, str):
                filter['component_id'] = '*%s*' % sliver
                sliver = {}
            elif 'component_id' in sliver and sliver['component_id']:
                filter['component_id'] = '*%s*' % sliver['component_id']
            nodes = SFAv1Node.get_nodes(xml, filter)
            if not nodes:
                continue
            node = nodes[0]
            SFAv1Sliver.add_slivers(node, sliver)<commit_msg>refactored<commit_after>
from sfa.util.xml import XpathFilter
from sfa.util.plxrn import PlXrn
from sfa.util.xrn import Xrn
from sfa.rspecs.elements.element import Element
from sfa.rspecs.elements.node import Node
from sfa.rspecs.elements.sliver import Sliver
from sfa.rspecs.elements.network import Network 
from sfa.rspecs.elements.location import Location
from sfa.rspecs.elements.hardware_type import HardwareType
from sfa.rspecs.elements.disk_image import DiskImage
from sfa.rspecs.elements.interface import Interface
from sfa.rspecs.elements.bwlimit import BWlimit
from sfa.rspecs.elements.pltag import PLTag
from sfa.rspecs.rspec_elements import RSpecElement, RSpecElements
from sfa.rspecs.elements.versions.sfav1Network import SFAv1Network
from sfa.rspecs.elements.versions.sfav1Sliver import SFAv1Sliver
from sfa.rspecs.elements.versions.sfav1PLTag import SFAv1PLTag
from sfa.rspecs.elements.versions.pgv2Services import PGv2Services

class SFAv1Node:

    @staticmethod
    def add_nodes(xml, nodes):
        network_elems = SFAv1Network.get_networks(xml)
        if len(network_elems) > 0:
            network_elem = network_elems[0]
        elif len(nodes) > 0 and nodes[0].get('component_manager_id'):
            network_elem = SFAv1Network.add_network(xml.root, {'name': nodes[0]['component_manager_id']})

        node_elems = []       
        for node in nodes:
            node_fields = ['component_manager_id', 'component_id', 'boot_state']
            elems = Element.add(network_elem, 'node', node, node_fields)
            node_elem = elems[0]  
            node_elems.append(node_elem)

            # determine network hrn
            network_hrn = None 
            if 'component_manager_id' in node and node['component_manager_id']:
                network_hrn = Xrn(node['component_manager_id']).get_hrn()

            # set component_name attribute and  hostname element
            if 'component_id' in node and node['component_id']:
                xrn = Xrn(node['component_id'])
                node_elem.set('component_name', xrn.get_leaf())
                hostname_tag = node_elem.add_element('hostname')
                hostname_tag.set_text(xrn.get_leaf())

            # set site id
            if 'authority_id' in node and node['authority_id']:
                node_elem.set('site_id', node['authority_id'])

            location_elems = Element.add(node_elem, 'location', node.get('location', []), Location.fields)
            interface_elems = Element.add(node_elem, 'interface', node.get('interfaces', []), Interface.fields)
            
            #if 'bw_unallocated' in node and node['bw_unallocated']:
            #    bw_unallocated = etree.SubElement(node_elem, 'bw_unallocated', units='kbps').text = str(int(node['bw_unallocated'])/1000)

            PGv2Services.add_services(node_elem, node.get('services', []))
            SFAv1PLTags.add_tags(node_elem, node.get('tags', [])) 
            SFAv1Sliver.add_slivers(node_elem, node.get('slivers', []))

    @staticmethod 
    def add_slivers(xml, slivers):
        component_ids = []
        for sliver in slivers:
            filter = {}
            if isinstance(sliver, str):
                filter['component_id'] = '*%s*' % sliver
                sliver = {}
            elif 'component_id' in sliver and sliver['component_id']:
                filter['component_id'] = '*%s*' % sliver['component_id']
            if not fliter:
                continue 
            nodes = SFAv1Node.get_nodes(xml, filter)
            if not nodes:
                continue
            node = nodes[0]
            SFAv1Sliver.add_slivers(node, sliver)
{'fraction_non_alphanumeric': 0.10337078651685393, 'fraction_numerical': 0.007270323859881031, 'mean_word_length': 2.734452122408687, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 12}, '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': '3820437', 'n_tokens_mistral': 2409, 'n_tokens_neox': 2337, 'n_words': 465}