python实现的宏解析工具

import csv, sys, os

def is_num_by_except(num):
  try:
    int(num)
    return True
  except ValueError:
    return False

def calcVal(dic, name):
  val = dic[name]
  ret = ""
  
  num = 0
  while 1:
    fm  = ""
    i   = 0
    num = 0
    for v in val.split("+"):
      if i != 0:
        fm += "+"
      if is_num_by_except(v):
        fm += v
      else:
        num+=1
        fm += dic[v]
      i+=1
      
    val = fm
    if num == 0:
      break

  ret = fm
  
  return eval(ret)

def parseMacro(file1, file2):
  f1  = open(file1, "r")
  f2  = open(file2, "w")
  
  pl  = ""
  pl2 = ""
  pl3 = ""
  dic = {}
  lst = []
  
  for line in f1:
    pl = ""
    pl2= ""
    pl3= ""
    if line[0:7] == "#define":
      for sp in line.split(" "):
        for sp2 in sp.split("\t"):
          if sp2 != "":
            pl += (" " + sp2)
      pl = pl.split("/")[0]
      for p in pl.split("("):
        if p != "":
          pl2 += p
      for p in pl2.split(")"):
        if p != "":
          pl3 += p
      
      i   = 0
      key = ""
      val = ""
      for p in pl3.split(" "):
        if i < 2:
          i=i+1
          continue
        elif i == 2:
          key = p.strip("\r\n")
          i=i+1
        else:
          val += p.strip("\r\n")
          i=i+1
      
      dic[key]=val
      lst.append(key)

  for l in lst:
    str = "%d" % calcVal(dic, l)        
    f2.write(l)
    f2.write("=")
    f2.write(str)
    f2.write("\n")
  return

def main(argv):
  if len(sys.argv) != 3:
    print "argv error!"
    print "usage: python filename tablename primarykey "
    return
  
  macroFileName   = sys.argv[1]
  macroFileName2  = sys.argv[2]
  
  parseMacro(macroFileName, macroFileName2)

if __name__ == '__main__':
 main(sys.argv[1:])

前面我定义产线要我提供Defect code给他们,而我的程序中是用宏定义的,大概如下

// User Defined Error Codes, (40 ~ 399)
#define R_FAIL_BASE 40
// electrical Items, reserved 10
#define R_FAIL_ELECTRICAL_BASE (R_FAIL_BASE + 0 )
#define R_FAIL_openShort (R_FAIL_ELECTRICAL_BASE + 0 )
#define R_FAIL_DynamicCurrent (R_FAIL_ELECTRICAL_BASE + 1 )
#define R_FAIL_StandbyCurrent (R_FAIL_ELECTRICAL_BASE + 2 )

// Auto Focus Items, reserved 50#define R_FAIL_AF_BASE (R_FAIL_BASE + 10 )
#define R_FAIL_AF_PREFOCUS (R_FAIL_AF_BASE + 0 )
#define R_FAIL_contrast_BF_60CM (R_FAIL_AF_BASE + 1 )
#define R_FAIL_sfr_BF_60CM_n4_cen_l (R_FAIL_AF_BASE + 2 )
#define R_FAIL_sfr_BF_60CM_n4_cen_t (R_FAIL_AF_BASE + 3 )


 

由于要这些信息名称要直接对应成数字,而我定义的时候有些宏用到了另外的宏,于是我写了以上工具直接算出每个defect code对应的数值如下:

R_FAIL_BASE=40
R_FAIL_ELECTRICAL_BASE=40
R_FAIL_openShort=40
R_FAIL_DynamicCurrent=41
R_FAIL_StandbyCurrent=42
R_FAIL_AF_BASE=50
R_FAIL_AF_PREFOCUS=50
R_FAIL_contrast_BF_60CM=51
R_FAIL_sfr_BF_60CM_n4_cen_l=52
R_FAIL_sfr_BF_60CM_n4_cen_t=53


 

猜你喜欢

转载自blog.csdn.net/mishara/article/details/8915066