Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:23:46

0001 import re
0002 try:
0003     Pattern = re._pattern_type
0004 except AttributeError:
0005     # Python 3.7
0006     Pattern = re.Pattern
0007 
0008 
0009 class BranchSelection():
0010     def __init__(self, branchsel):
0011         comment = re.compile(r"#.*")
0012         ops = []
0013 
0014         if isinstance(branchsel, list):
0015             # branchsel is a list of commands
0016             lines = branchsel
0017         elif isinstance(branchsel, str):
0018             # branchsel is a filename
0019             lines=[]
0020             for line in open(branchsel, 'r'):
0021                 line = line.strip()
0022                 if len(line) == 0 or line[0] == '#':
0023                     continue
0024                 line = re.sub(comment, "", line)
0025                 while line[-1] == "\\":
0026                     line = line[:-1] + " " + file.next().strip()
0027                     line = re.sub(comment, "", line)
0028                 lines.append(line)
0029                 
0030         for line in lines:
0031             try:
0032                 (op, sel) = line.split()
0033                 if op == "keep":
0034                     ops.append((sel, 1))
0035                 elif op == "drop":
0036                     ops.append((sel, 0))
0037                 elif op == "keepmatch":
0038                     ops.append((re.compile("(:?%s)$" % sel), 1))
0039                 elif op == "dropmatch":
0040                     ops.append((re.compile("(:?%s)$" % sel), 0))
0041                 else:
0042                     print("Error in branchsel: line '%s': "% (line)
0043                         + "it's not (keep|keepmatch|drop|dropmatch) "
0044                         + "<branch_pattern>"
0045                     )
0046             except ValueError as e:
0047                 print("Error in branchsel: line '%s': " % (line)
0048                     + "it's not (keep|keepmatch|drop|dropmatch) "
0049                     + "<branch_pattern>"
0050                 )
0051         self._ops = ops
0052 
0053     def selectBranches(self, tree):
0054         tree.SetBranchStatus("*", 1)
0055         branchNames = [b.GetName() for b in tree.GetListOfBranches()]
0056         for bre, stat in self._ops:
0057             if type(bre) == Pattern:
0058                 for n in branchNames:
0059                     if re.match(bre, n):
0060                         tree.SetBranchStatus(n, stat)
0061             else:
0062                 tree.SetBranchStatus(bre, stat)