def DefinePattern(self, patternSpec): """Define the specified pattern in the query master, return the finalized (unique) name of the pattern""" try: # determine unique name for the pattern based off suggested name # in the spec rootName = quilt_data.pat_spec_tryget(patternSpec, name=True) if rootName is None: rootName = "pattern" patternName = rootName i = 1 with self.lock: while patternName in self._patterns.keys(): patternName = '_'.join([rootName, str(i)]) i += 1 # store pattern spec in the member data quilt_data.pat_spec_set(patternSpec, name=patternName) quilt_data.pat_specs_add(self._patterns, patternSpec) # return the unique name for the pattern return patternName except Exception, e: logging.exception(e) raise
def OnRegisterEnd(self): """Create a patternSpec dict from arguments (name, VARIABLES, and VARIABLE to SOURCE_VARIABLE mappings), register this pattern with the query master""" # create the spec with it's requested name requestedName = self._args.name if requestedName is None: requestedName = "pattern" else: requestedName = self._args.name[0] patternSpec = quilt_data.pat_spec_create(name=requestedName) # create the specs for the variables variables = None if self._args.variable is not None: for v in self._args.variable: # create the variables section if it does not exist # first position of the cmd line argument is name of variable varName = v[0] varSpec = quilt_data.var_spec_create(name=varName) # if description was specified on cmd line, load it in the spec if len(v) > 1: varDesc = v[1] quilt_data.var_spec_set(varSpec, description=varDesc) # if default was specified on cmd line, load it in spec if len(v) > 2: varDef = v[2] quilt_data.var_spec_set(varSpec, default=varDef) variables = quilt_data.var_specs_add(variables, varSpec) quilt_data.pat_spec_set(patternSpec, variables=variables) mappings = None # create the specs for the variable mappings if self._args.mapping is not None: for m in self._args.mapping: varName = m[0] src = m[1] srcPat = m[2] srcVar = m[3] srcPatInstance = None if len(m) > 4: srcPatInstance = m[4] # query variables are allowed to map to multiple # source variables. Initialize a blank list if it isn't present # then append the new mapping information srcVarMappingSpec = quilt_data.src_var_mapping_spec_create( name=varName, sourceName=src, sourcePattern=srcPat, sourceVariable=srcVar, sourcePatternInstance=srcPatInstance) mappings = quilt_data.src_var_mapping_specs_add( mappings, srcVarMappingSpec) if mappings is not None: quilt_data.pat_spec_set(patternSpec, mappings=mappings) # if the pattern code is specified, set it in the pattern as # a string if self._args.code is not None: # perform first pass parse on the pattern to ensure syntax # call get_pattern_vars from parser, but ignore the result # this will check the syntax codestr = str(self._args.code) # store the code in the pattern quilt_data.pat_spec_set(patternSpec, code=codestr) # syntax parsing may be dependent on variables in the pattern, so # generate the code str after replacing any pattern variables codestr = quilt_data.generate_query_code(patternSpec, None) quilt_parse.get_pattern_src_refs(codestr) # define patternSpec in the query master as a synchronous call # return will be the pattern name with self.GetQueryMasterProxy() as qm: patName = qm.DefinePattern(patternSpec) # print out pattern Name print 'Pattern', patName, ' defined' # return false (prevent event loop from beginning) return False