#!/usr/bin/python 
from lxml import etree
from StringIO import StringIO
from sfa.util.xrn import Xrn, urn_to_hrn
from sfa.rspecs.rspec import RSpec
from sfa.rspecs.version_manager import VersionManager

xslt='''<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="no"/>

<xsl:template match="/|comment()|processing-instruction()">
    <xsl:copy>
      <xsl:apply-templates/>
    </xsl:copy>
</xsl:template>

<xsl:template match="*">
    <xsl:element name="{local-name()}">
      <xsl:apply-templates select="@*|node()"/>
    </xsl:element>
</xsl:template>

<xsl:template match="@*">
    <xsl:attribute name="{local-name()}">
      <xsl:value-of select="."/>
    </xsl:attribute>
</xsl:template>
</xsl:stylesheet>
'''

xslt_doc=etree.parse(StringIO(xslt))
transform=etree.XSLT(xslt_doc)

class PGRSpecConverter:

    @staticmethod
    def to_sfa_rspec(rspec, content_type = None):
        if not isinstance(rspec, RSpec):
            pg_rspec = RSpec(rspec)
        else:
            pg_rspec = rspec
        
        version_manager = VersionManager()
        sfa_version = version_manager._get_version('sfa', '1')    
        sfa_rspec = RSpec(version=sfa_version)

        #nodes = pg_rspec.version.get_nodes()
        #sfa_rspec.version.add_nodes(nodes())
        #sfa_rspec.version.add_links(pg_rspec.version.get_links())
        #return sfa_rspec.toxml() 

        # get network
        networks = pg_rspec.version.get_networks()
        network_hrn = networks[0]["name"]
        network_element = sfa_rspec.xml.add_element('network', name=network_hrn, id=network_hrn)
        
        # get nodes
        pg_nodes_elements = pg_rspec.version.get_nodes()
        nodes_with_slivers = pg_rspec.version.get_nodes_with_slivers()
        i = 1
        for pg_node in pg_nodes_elements:
            attribs = dict(pg_node.items())
            attribs['id'] = 'n'+str(i)
            
            node_element = network_element.add_element('node')
            for attrib in attribs:
                if type(attribs[attrib]) == str:
                    node_element.set(attrib, attribs[attrib])
            urn = pg_node["component_id"]
            if urn:
                urn = urn[0]
                hostname = Xrn.urn_split(urn)[-1]
                hostname_element = node_element.add_element('hostname')
                hostname_element.set_text(hostname)
                if hostname in nodes_with_slivers:
                    node_element.add_element('sliver')  
                     
            # just copy over remaining child elements  
            #for child in pg_node_element.getchildren():
            #    node_element.append(transform(child).getroot())
            i = i+1
 
        return sfa_rspec.toxml()
         
if __name__ == '__main__':
    import sys
    if len(sys.argv) > 1:        
        print PGRSpecConverter.to_sfa_rspec(sys.argv[1])  

@@ -2207,16 +2207,125 @@
 if urn:%0A
+                if type(urn)==list:%0A                    # legacy code, not sure if urn is ever a list...%0A    
         
@@ -2345,16 +2345,16 @@
  urn%5B0%5D%0A
-
         
@@ -2624,18 +2624,211 @@
 sliver')
-  
+%0A%0A            for hardware_type in pg_node%5B%22hardware_types%22%5D:%0A                if %22name%22 in hardware_type:%0A                    node_element.add_element(%22hardware_type%22, name=hardware_type%5B%22name%22%5D)
 %0A       

fix hostname, add conversion of hardware_types