Minor typos
[nepi.git] / src / neco / util / rmatcher.py
1
2 def match_tags(box, all_tags, exact_tags):
3     """ returns True if box has required tags """
4     tall = set(all_tags)
5     texact = set(exact_tags)
6
7     if texact and box.connections == texact:
8         return True
9
10     if tall and tall.issubset(box.connections):
11         return True
12
13     return False
14
15 def find_boxes(box, all_tags = None, exact_tags = None, max_depth = 1):
16     """ Look for the connected boxes with the required tags, doing breath-first
17     search, until max_depth ( max_depth = None will traverse the entire graph ).
18     """
19     if not all_tags and not exact_tags:
20         msg = "No matching criteria for resources."
21         raise RuntimeError(msg)
22
23     queue = set()
24     # enqueue (depth, box) 
25     queue.add((0, box))
26     
27     traversed = set()
28     traversed.add(box)
29
30     depth = 0
31
32     result = set()
33
34     while len(q) > 0: 
35         (depth, a) = queue.pop()
36         if match_tags(a, all_tags, exact_tags):
37             result.add(a)
38
39         if not max_depth or depth <= max_depth:
40             depth += 1
41             for b in sorted(a.connections):
42                 if b not in traversed:
43                     traversed.add(b)
44                     queue.add((depth, b))
45     
46     return result