TechplexEngineer
2/19/2017 - 2:28 PM

renamepcb.py

#!/usr/bin/env python
# License MIT

import os, re, sys

where = '.'
try:
	where = sys.argv[1]
except:
	pass

# Maps the euffix of the filename to the OSHPark format
layerMapping = {
	"F_Cu.gbr": 		"gtl",
	"B_Cu.gbr": 		"gbl",
	"F_Mask.gbr": 		"gts",
	"B_Mask.gbr": 		"gbs",
	"F_SilkS.gbr": 		"gto",
	"B_SilkS.gbr": 		"gbo",
	"Edge_Cuts.gbr":	"gko",
	'drl':				"xln"
}

pattern = re.compile(r'^(?P<basename>.*)-(?P<ident>[^-]*)$')
p2 = re.compile(r'^(?P<basename>.*)\.(?P<ident>.*)$')
os.chdir(where)
for filename in os.listdir('.'):
	match = False
	if filename.endswith(".gbr"):
		match = re.search(pattern,filename)
	elif filename.endswith(".drl"):
		match = re.search(p2,filename)

	if match:
		newFilename = match.group('basename')+'.'+layerMapping[match.group('ident')]
		print 'Renaming \'{0}\' to \'{1}\''.format(filename, newFilename)
		os.rename(filename, newFilename)
	else:
		print 'No match.', filename

sys.exit(0)

layerMapping = {
	"top": 			"gtl",
	"bottom": 		"gbl",
	"topmask": 		"gts",
	"bottommask": 	"gbs",
	"topsilk": 		"gto",
	"bottomsilk": 	"gbo",
	"plated-drill": "drl",### DIFFERENT EXTENSION
	"group2": 		"g2",
	"group3": 		"g3",
	"toppaste":		"gtp",
	"bottompaste":	"gbp",
	"outline":		"outline",
}



pattern = re.compile(r'^(?P<basename>[^\.]*)\.(?P<layer>[^\.]*)\.(?P<ext>(gbr)|(cnc))$')
os.chdir(where)
for filename in os.listdir('.'):
	if filename.endswith(".gbr") or filename.endswith(".cnc"):
		match = re.search(pattern,filename)
		if match:
			if match.group('layer') in layerMapping:
				newFilename = match.group('basename')+'.'+match.group('layer')+'.'+layerMapping[match.group('layer')]
				print 'Renaming \'{0}\' to \'{1}\''.format(filename, newFilename)
				os.rename(filename, newFilename)
		else:
			print 'No match.', filename


sys.exit(0)