<s> import time <EOL> from collections import defaultdict <EOL> from . utils import is_pattern <EOL> from . node import LeafNode <EOL> from . intervals import Interval <EOL> from . readers import MultiReader <EOL> class Store ( object ) : <EOL> def __init__ ( self , finders = None ) : <EOL> self . finders = finders <EOL> def find ( self , pattern , startTime = None , endTime = None , local = True ) : <EOL> query = FindQuery ( pattern , startTime , endTime ) <EOL> matching_nodes = set ( ) <EOL> for finder in self . finders : <EOL> for node in finder . find_nodes ( query ) : <EOL> matching_nodes . add ( node ) <EOL> nodes_by_path = defaultdict ( list ) <EOL> for node in matching_nodes : <EOL> nodes_by_path [ node . path ] . append ( node ) <EOL> found_branch_nodes = set ( ) <EOL> for path , nodes in sorted ( nodes_by_path . items ( ) , key = lambda k : k [ <NUM_LIT:0> ] ) : <EOL> leaf_nodes = set ( ) <EOL> for node in nodes : <EOL> if node . is_leaf : <EOL> leaf_nodes . add ( node ) <EOL> elif node . path not in found_branch_nodes : <EOL> yield node <EOL> found_branch_nodes . add ( node . path ) <EOL> if not leaf_nodes : <EOL> continue <EOL> if len ( leaf_nodes ) == <NUM_LIT:1> : <EOL> yield leaf_nodes . pop ( ) <EOL> elif len ( leaf_nodes ) > <NUM_LIT:1> : <EOL> reader = MultiReader ( leaf_nodes ) <EOL> yield LeafNode ( path , reader ) <EOL> class FindQuery ( object ) : <EOL> def __init__ ( self , pattern , startTime , endTime ) : <EOL> self . pattern = pattern <EOL> self . startTime = startTime <EOL> self . endTime = endTime <EOL> self . isExact = is_pattern ( pattern ) <EOL> self . interval = Interval ( <EOL> float ( '<STR_LIT>' ) if startTime is None else startTime , <EOL> float ( '<STR_LIT>' ) if endTime is None else endTime ) <EOL> def __repr__ ( self ) : <EOL> if self . startTime is None : <EOL> startString = '<STR_LIT:*>' <EOL> else : <EOL> startString = time . ctime ( self . startTime ) <EOL> if self . endTime is None : <EOL> endString = '<STR_LIT:*>' <EOL> else : <EOL> endString = time . ctime ( self . endTime ) <EOL> return '<STR_LIT>' % ( self . pattern , startString , <EOL> endString ) </s>
