#!/usr/bin/python

## @package net2ucf.py
#  @author  Matthieu Cattin, Carlos Gil Soriano.

#  This is a modified net2ucf.py by Carlos Gil Soriano,
#  previously done by Matthieu Cattin.

import re

## @class net2ucf class able to extract ucf files from an Altium NET files
class NET2UCF:

   ## @brief                        Constructor of the ucf extractor
   #  @param path_ucfFile           Path to be written the ucf file
   #  @param path_excludedNetsFile  A path with the nets files to be excluded
   #  @param ICid                   IC identificator to extract the ucf from
   def __init__(self, path_netFile, path_ucfFile, path_excludedNetsFile, ICid):
      if path_netFile.endswith('.NET'):
         try:
            self.netFile       = open(path_netFile, "r")
         except IOError as e:
            print '.NET file does not exist!'
      else:
         raise Exception('Bad extension of the net file')

      if path_ucfFile.endswith('.ucf'):
         try:
            self.ucfFile       = open(path_ucfFile, "w")
         except IOError as e:
            print '.ucf file does not exist!'
      else:
         raise Exception('Bad extension of the ucf file')

      if path_excludedNetsFile.endswith('.XNET'):
         try:
            self.excludedNetsFile  = open(path_excludedNetsFile, "r")
         except IOError as e:
            print '.XNET file does not exist!'

         print "I'm here!"
         self.listExcludedNets = []
         for line in self.excludedNetsFile:
            ln = line.split()
            self.listExcludedNets.append(ln[0])
         print 'Excluded nets:\n' + str(self.listExcludedNets)
      else:
         raise Exception('Bad extension of the excluded nets file')

      self.ICid = ICid

   ## @fn generateUCF(self)
   #  @brief  Function that process the NET file and generates the UCF one.
   def generateUCF(self):
      for line in self.netFile:
         ln   = line.split()
         skip = False
         for item in self.listExcludedNets:
            if ln[0].startswith(str(item)):
               print str(ln[0])+'Omitted because is a excluded net'
               skip = True
         if skip == False:
            try:
               print ln;
               if ln[1].startswith('IC15'):
                  self.ucfFile.write("NET \""+ln[0]+"\" LOC = "+ln[1].split('-')[1]+";\n")
            except IOError as e:
              print 'Bad line in FPGA.NET'

print '-------------------------------------'
print '--------   NET2UCF program   --------'
print '-------------------------------------\n'
print 'Case sensitive!\n'
path_NetFile = raw_input('Insert the path of the NET file. Format is \n'+'-- [PATH]/[name].NET:\t')
path_ucfFile = raw_input('Insert path of the ucf file. Format is \n'+'-- [PATH]/[name].ucf:\t')
path_excludedNetsFile = raw_input('Insert path of the file containing nets not to be included'+ '-- [PATH]/[name].XNET:\t')
ICid    = raw_input('Insert which IC to get the ucf file from:\t')

net2ucf_inst = NET2UCF(path_NetFile, path_ucfFile, path_excludedNetsFile, ICid)
net2ucf_inst.generateUCF()
