<commit_before>from __future__ import with_statement
import re
import socket
from sfa.util.namespace import *
from sfa.util.faults import *
from xmlbuilder import XMLBuilder
from lxml import etree
import sys
from StringIO import StringIO


class Sliver:
    def __init__(self, node):
        self.node = node
        self.network = node.network
        self.slice = node.network.slice
        
    def toxml(self, xml):
        with xml.sliver:
            self.slice.tags_to_xml(xml, self.node)

        
class Iface:
    def __init__(self, network, iface):
        self.network = network
        self.id = iface['interface_id']
        self.idtag = "i%s" % self.id
        self.ipv4 = iface['ip']
        self.bwlimit = iface['bwlimit']
        self.hostname = iface['hostname']
        self.primary = iface['is_primary']

    """
    Just print out bwlimit right now
    """
    def toxml(self, xml):
        if self.bwlimit:
            with xml.bw_limit(units="kbps"):
                xml << str(self.bwlimit / 1000)


class Node:
    def __init__(self, network, node):
        self.network = network
        self.id = node['node_id']
        self.idtag = "n%s" % self.id
        self.hostname = node['hostname']
        self.site_id = node['site_id']
        self.iface_ids = node['interface_ids']
        self.sliver = None
        self.whitelist = node['slice_ids_whitelist']

    def get_primary_iface(self):
        for id in self.iface_ids:
            iface = self.network.lookupIface(id)
            if iface.primary:
                return iface
        return None
        
    def get_site(self):
        return self.network.lookupSite(self.site_id)
    
    def add_sliver(self):
        self.sliver = Sliver(self)

    def toxml(self, xml):
        slice = self.network.slice
        if self.whitelist and not self.sliver:
            if not slice or slice.id not in self.whitelist:
                return

        with xml.node(id = self.idtag):
            with xml.hostname:
                xml << self.hostname
            iface = self.get_primary_iface()
            if iface:
                iface.toxml(xml)
            if self.sliver:
                self.sliver.toxml(xml)
    

class Site:
    def __init__(self, network, site):
        self.network = network
        self.id = site['site_id']
        self.idtag = "s%s" % self.id
        self.node_ids = site['node_ids']
        self.node_ids.sort()
        self.name = site['abbreviated_name']
        self.tag = site['login_base']
        self.public = site['is_public']
        self.enabled = site['enabled']
        self.links = set()
        self.whitelist = False

    def get_sitenodes(self):
        n = []
        for i in self.node_ids:
            n.append(self.network.lookupNode(i))
        return n
    
    def toxml(self, xml):
        if not (self.public and self.enabled and self.node_ids):
            return
        with xml.site(id = self.idtag):
            with xml.name:
                xml << self.name
            for node in self.get_sitenodes():
                node.toxml(xml)
   
    
class Slice:
    def __init__(self, network, hrn, slice):
        self.hrn = hrn
        self.network = network
        self.id = slice['slice_id']
        self.name = slice['name']
        self.node_ids = set(slice['node_ids'])
        self.slice_tag_ids = slice['slice_tag_ids']
    
    """
    Use with tags that can have more than one instance<commit_msg>added 'peer_id' attribute to slice object<commit_after>from __future__ import with_statement
import re
import socket
from sfa.util.namespace import *
from sfa.util.faults import *
from xmlbuilder import XMLBuilder
from lxml import etree
import sys
from StringIO import StringIO


class Sliver:
    def __init__(self, node):
        self.node = node
        self.network = node.network
        self.slice = node.network.slice
        
    def toxml(self, xml):
        with xml.sliver:
            self.slice.tags_to_xml(xml, self.node)

        
class Iface:
    def __init__(self, network, iface):
        self.network = network
        self.id = iface['interface_id']
        self.idtag = "i%s" % self.id
        self.ipv4 = iface['ip']
        self.bwlimit = iface['bwlimit']
        self.hostname = iface['hostname']
        self.primary = iface['is_primary']

    """
    Just print out bwlimit right now
    """
    def toxml(self, xml):
        if self.bwlimit:
            with xml.bw_limit(units="kbps"):
                xml << str(self.bwlimit / 1000)


class Node:
    def __init__(self, network, node):
        self.network = network
        self.id = node['node_id']
        self.idtag = "n%s" % self.id
        self.hostname = node['hostname']
        self.site_id = node['site_id']
        self.iface_ids = node['interface_ids']
        self.sliver = None
        self.whitelist = node['slice_ids_whitelist']

    def get_primary_iface(self):
        for id in self.iface_ids:
            iface = self.network.lookupIface(id)
            if iface.primary:
                return iface
        return None
        
    def get_site(self):
        return self.network.lookupSite(self.site_id)
    
    def add_sliver(self):
        self.sliver = Sliver(self)

    def toxml(self, xml):
        slice = self.network.slice
        if self.whitelist and not self.sliver:
            if not slice or slice.id not in self.whitelist:
                return

        with xml.node(id = self.idtag):
            with xml.hostname:
                xml << self.hostname
            iface = self.get_primary_iface()
            if iface:
                iface.toxml(xml)
            if self.sliver:
                self.sliver.toxml(xml)
    

class Site:
    def __init__(self, network, site):
        self.network = network
        self.id = site['site_id']
        self.idtag = "s%s" % self.id
        self.node_ids = site['node_ids']
        self.node_ids.sort()
        self.name = site['abbreviated_name']
        self.tag = site['login_base']
        self.public = site['is_public']
        self.enabled = site['enabled']
        self.links = set()
        self.whitelist = False

    def get_sitenodes(self):
        n = []
        for i in self.node_ids:
            n.append(self.network.lookupNode(i))
        return n
    
    def toxml(self, xml):
        if not (self.public and self.enabled and self.node_ids):
            return
        with xml.site(id = self.idtag):
            with xml.name:
                xml << self.name
            for node in self.get_sitenodes():
                node.toxml(xml)
   
    
class Slice:
    def __init__(self, network, hrn, slice):
        self.hrn = hrn
        self.network = network
        self.id = slice['slice_id']
        self.name = slice['name']
        self.peer_id = slice['peer_id']
        self.node_ids = set(slice['node_ids'])
        self.slice_tag_ids = slice['slice_tag_ids']
    
    """
    Use with tags that can have more than one instance
{'fraction_non_alphanumeric': 0.10464449541284404, 'fraction_numerical': 0.0014334862385321102, 'mean_word_length': 1.861771944216571, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 54}, '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': '2085098', 'n_tokens_mistral': 2172, 'n_tokens_neox': 2151, 'n_words': 510}