def sdo_ref(self, children): """ Produces a single or list of STIX object IDs, either from a referenced variable, or from newly created objects. """ if is_token(children[0], "VARIABLE_NAME"): # ref is a variable var_name = children[0] if var_name in self.__variables: ids = self.__variables[var_name] else: raise UndeclaredVariableError(var_name) else: # ref is to inline SDO(s) ids = children[0] # A variable or inline SDO with count=1 will produce a single value; # count > 1 produces a list. We need to propagate the right thing, # so that STIX properties which expect single IDs don't get lists. # (One wishes that such properties would accept a length-1 list, but # they don't.) if len(ids) == 1: ids = ids[0] return ids
def property_block(self, children): """ Produces a mapping from prop name to ID(s) or other string literals, from the property block. """ overlay_props = {} for prop_name, value in children: if is_token(value, "ESCAPED_STRING"): value = _string_token_value(value) overlay_props[prop_name] = value return overlay_props
def _print_parse_tree(logger, tree, indent=0): """ A parse tree printer that's better than Lark's (in my opinion). It shows token types as well as values (Lark's doesn't show the types). It's done via the logger, at a custom extra-verbosity level. :param logger: The logger to use :param tree: The parse tree :param indent: An indent amount """ line = " " * indent if is_token(tree): line += tree + " (" + tree.type + ")" logger.log(stix2generator.logging.EXTRA_VERBOSE, line) else: line += tree.data logger.log(stix2generator.logging.EXTRA_VERBOSE, line) for child in tree.children: _print_parse_tree(logger, child, indent + 1)
def sdo_ref(self, tree): if is_token(tree.children[0], "VARIABLE_NAME"): self.variables.add(tree.children[0])