sbsv2/raptor/python/filter_list.py
changeset 18 de5b887c98f7
parent 13 c327db0664bb
child 28 b8fa7dfeeaa1
equal deleted inserted replaced
14:eb060913c963 18:de5b887c98f7
     1 #
     1 #
     2 # Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
     2 # Copyright (c) 2008-2010 Nokia Corporation and/or its subsidiary(-ies).
     3 # All rights reserved.
     3 # All rights reserved.
     4 # This component and the accompanying materials are made available
     4 # This component and the accompanying materials are made available
     5 # under the terms of the License "Eclipse Public License v1.0"
     5 # under the terms of the License "Eclipse Public License v1.0"
     6 # which accompanies this distribution, and is available
     6 # which accompanies this distribution, and is available
     7 # at the URL "http://www.eclipse.org/legal/epl-v10.html".
     7 # at the URL "http://www.eclipse.org/legal/epl-v10.html".
    17 #
    17 #
    18 
    18 
    19 import os
    19 import os
    20 import sys
    20 import sys
    21 import raptor
    21 import raptor
       
    22 import re
    22 import filter_interface
    23 import filter_interface
    23 import pluginbox
    24 import pluginbox
    24 import traceback
    25 import traceback
    25 
    26 
    26 class FilterWriteException(Exception):
    27 class FilterWriteException(Exception):
    62 	
    63 	
    63 	def close(self):
    64 	def close(self):
    64 		"""Nothing to do for stdout"""
    65 		"""Nothing to do for stdout"""
    65 		return True
    66 		return True
    66 
    67 
    67 
    68 def SplitList(listString):
    68 
    69 	"""turn a CLI filter string into a list of (class, param) pairs.
       
    70 	
       
    71 	for example, "foo[a,b],bar[c,d]"
       
    72 	
       
    73 	becomes [ ("foo", ["a","b"]) , ("bar", ["c","d"]) ]
       
    74 	"""
       
    75 	matches = re.findall("(\w+)(\[([^\[\]]*)\])?,?", listString)
       
    76 	
       
    77 	pairs = []
       
    78 	for m in matches:
       
    79 		classname = m[0]
       
    80 		if len(m[2]) > 0:
       
    81 			pairs.append( (classname, m[2].split(",")) )
       
    82 		else:
       
    83 			pairs.append( (classname, []) )
       
    84 	return pairs
       
    85 	
    69 class FilterList(filter_interface.Filter):
    86 class FilterList(filter_interface.Filter):
    70 
    87 
    71 	def __init__(self):
    88 	def __init__(self):
    72 		self.out = [BootstrapFilter()]
    89 		self.out = [BootstrapFilter()]
    73 		self.filters = []
    90 		self.filters = []
    79 			Returns: Boolean: Have the functions succeeded in opening the files?
    96 			Returns: Boolean: Have the functions succeeded in opening the files?
    80 		"""
    97 		"""
    81 		# Find all the filter plugins
    98 		# Find all the filter plugins
    82 		self.pbox = pbox
    99 		self.pbox = pbox
    83 		possiblefilters = self.pbox.classesof(filter_interface.Filter)
   100 		possiblefilters = self.pbox.classesof(filter_interface.Filter)
       
   101 		filterdict = {}
       
   102 		for p in possiblefilters:
       
   103 			name = p.__name__.lower()
       
   104 			if name in filterdict:
       
   105 				raise ValueError("filters found in SBS_HOME/python/plugins which have duplicate name: %s " % p.__name__)
       
   106 			else:
       
   107 				filterdict[name] = p
       
   108 		
       
   109 		# turn "filternames" into a list of (classname, parameters) pairs
       
   110 		filterCalls = SplitList(filternames)
       
   111 		
       
   112 		# look for each filter class in the box
    84 		unfound = []
   113 		unfound = []
    85 		self.filters = []
   114 		self.filters = []
    86 		for f in filternames:
   115 		for (f, params) in filterCalls:
    87 			unfound.append(f) # unfound unless we find it
   116 			# if the filter exists and is a valid filter use it
    88 			for pl in possiblefilters:
   117 			if f.lower() in filterdict:
    89 				if pl.__name__.upper() == f.upper():
   118 				if params:
    90 					self.filters.append(pl())
   119 					self.filters.append(filterdict[f.lower()](params))
    91 					unfound = unfound[:-1]
   120 				else:
       
   121 					self.filters.append(filterdict[f.lower()]())
       
   122 			else:
       
   123 				# record missing filters
       
   124 				unfound.append(f)
       
   125 
    92 		if unfound != []:
   126 		if unfound != []:
    93 			raise ValueError("requested filters not found: %s \
   127 			raise ValueError("requested filters not found: %s \
    94 			\nAvailable filters are: %s" % (str(unfound), self.format_output_list(possiblefilters)))
   128 			\nAvailable filters are: %s" % (str(unfound), self.format_output_list(possiblefilters)))
    95 
   129 
    96 		if self.filters == []:
   130 		if self.filters == []: