def _add_separator_cases(self, vt_node): current_val = vt_node.get_current_value() if vt_node.is_attr_set(dm.NodeInternals.Separator): sep_l = copy.copy(self.sep_list) try: sep_l.remove(current_val) except ValueError: print( "\n*** WARNING: separator not part of the initial set. (Could happen if " "separators are generated dynamically)") self.current_fuzz_vt_list.insert(0, vtype.String(values=sep_l)) else: sz = len(current_val) if sz > 1: fuzzy_sep_val_list = [] for sep in self.sep_list: new_val = current_val[:-1] + sep + current_val[-1:] fuzzy_sep_val_list.append(new_val) self.current_fuzz_vt_list.insert( 0, vtype.String(values=fuzzy_sep_val_list))
def consume_node(self, node): orig_val = node.to_bytes() new_values = copy.copy(self.values) if orig_val in new_values: new_values.remove(orig_val) node.cc.import_value_type(value_type=vtype.String(values=new_values)) # Note, that node attributes are not altered by this # operation, especially usefull in our case, because we have # to preserve dm.NodeInternals.Separator node.make_finite() node.make_determinist() return True
def tag_builder(tag_name, params=None, contents=None, node_name=None, codec='latin-1', tag_name_mutable=True, struct_mutable=True, determinist=True): """ Helper for modeling an XML tag. Args: tag_name (str): name of the XML tag. params (dict): optional attributes to be added in the XML tag contents: can be either None (empty tag), a :class:`framework.data_model.Node`, a dictionary (Node description), a string or a string list (string-Node values). node_name (str): name of the node to be created. codec (str): codec to be used for generating the XML tag. tag_name_mutable (bool): if ``False``, the tag name will not be mutable, meaning that its ``Mutable`` attribute will be cleared. struct_mutable (bool): if ``False`` the XML structure "will not" be mutable, meaning that each node related to the structure will have its ``Mutable`` attribute cleared. determinist (bool): if ``False``, the attribute order could change from one retrieved data to another. Returns: dict: Node-description of the XML tag. """ if params is not None: assert isinstance(params, dict) cts = [] idx = 1 for k, v in params.items(): assert gr.is_string_compatible(v) v = v if isinstance(v, list) else [v] sep_id = uuid.uuid1() cts.append({ 'name': ('attr' + str(idx), uuid.uuid1()), 'contents': [ { 'name': ('key', uuid.uuid1()), 'contents': fvt.String(values=[k], codec=codec) }, { 'name': ('eq', uuid.uuid1()), 'contents': fvt.String(values=['='], codec=codec), 'set_attrs': MH.Attr.Separator, 'mutable': struct_mutable }, { 'name': ('sep', sep_id), 'contents': fvt.String(values=['"'], codec=codec), 'set_attrs': MH.Attr.Separator, 'mutable': struct_mutable }, { 'name': ('val', uuid.uuid1()), 'contents': fvt.String(values=v, codec=codec) }, { 'name': ('sep', sep_id) }, ] }) idx += 1 if not determinist: params_desc = {'section_type': MH.FullyRandom, 'contents': cts} else: params_desc = {'section_type': MH.Ordered, 'contents': cts} else: params_desc = None prefix = '</' if contents is None else '<' tag_start_open_desc = \ {'name': ('prefix', uuid.uuid1()), 'contents': fvt.String(values=[prefix], codec=codec), 'mutable': struct_mutable, 'set_attrs': MH.Attr.Separator} tag_cts = [{ 'name': ('tag_name', uuid.uuid1()), 'contents': fvt.String(values=[tag_name], codec=codec), 'mutable': tag_name_mutable }] if params_desc is not None: tag_cts.append(params_desc) tag_start_cts_desc = \ {'name': ('content', uuid.uuid1()), 'random': not determinist, 'separator': {'contents': {'name': ('spc', uuid.uuid1()), 'contents': fvt.String(values=[' '], max_sz=100, absorb_regexp='\s+', codec=codec), 'mutable': struct_mutable, 'absorb_csts': AbsNoCsts(size=True, regexp=True)}, 'prefix': False, 'suffix': False, 'unique': False}, 'contents': tag_cts} tag_start_close_desc = \ {'name': ('suffix', uuid.uuid1()), 'contents': fvt.String(values=['>'], codec=codec), 'mutable': struct_mutable, 'set_attrs': MH.Attr.Separator} tag_start_desc = \ {'name': ('start-tag', uuid.uuid1()), 'contents': [tag_start_open_desc, tag_start_cts_desc, tag_start_close_desc]} tag_end_desc = \ {'name': ('end-tag', uuid.uuid1()), 'contents': [ {'name': ('prefix', uuid.uuid1()), 'contents': fvt.String(values=['</'], codec=codec), 'mutable': struct_mutable, 'set_attrs': MH.Attr.Separator}, {'name': ('content', uuid.uuid1()), 'contents': fvt.String(values=[tag_name], codec=codec), 'mutable': tag_name_mutable}, {'name': ('suffix', uuid.uuid1()), 'contents': fvt.String(values=['>'], codec=codec), 'mutable': struct_mutable, 'set_attrs': MH.Attr.Separator}, ]} if contents is None: tag_desc = tag_start_desc else: if isinstance(contents, Node): cts = [tag_start_desc, (contents, 1, 1), tag_end_desc] elif isinstance(contents, dict): cts = [tag_start_desc, contents, tag_end_desc] else: assert gr.is_string_compatible(contents) if not isinstance(contents, list): contents = [contents] cts = [ tag_start_desc, { 'name': 'elt-content', 'contents': fvt.String(values=contents, codec=codec) }, tag_end_desc ] tag_desc = \ {'name': tag_name if node_name is None else node_name, 'separator': {'contents': {'name': ('nl', uuid.uuid1()), 'contents': fvt.String(values=['\n'], max_sz=100, absorb_regexp='[\r\n|\n]+', codec=codec), 'absorb_csts': AbsNoCsts(regexp=True)}, 'prefix': False, 'suffix': False, 'unique': False}, 'contents': cts} return tag_desc
def json_builder(tag_name, params=None, node_name=None, codec='latin-1', tag_name_mutable=True, struct_mutable=True, determinist=True): """ Helper for modeling an JSON structure. Args: tag_name (str): name of the JSON tag. params (dict): the JSON structure to be converted to a fuddly structure node_name (str): name of the node to be created. codec (str): codec to be used for generating the JSON structure. tag_name_mutable (bool): if ``False``, the tag name will not be mutable, meaning that its ``Mutable`` attribute will be cleared. struct_mutable (bool): if ``False`` the JSON structure "will not" be mutable, meaning that each node related to the structure will have its ``Mutable`` attribute cleared. determinist (bool): if ``False``, the attribute order could change from one retrieved data to another. Returns: dict: Node-description of the JSON structure. """ if params is not None: assert isinstance(params, dict) cts = [] idx = 1 for k, v in params.items(): sep_id = uuid.uuid1() # The separator for the " in the key param. e.g., "<key>" params = [ {'name': ('sep', sep_id), 'contents' : fvt.String(values=['"'], codec=codec), 'set_attrs': MH.Attr.Separator, 'mutable': struct_mutable}, {'name': ('key', uuid.uuid1()), 'contents' : fvt.String(values=[k], codec=codec)}, {'name': ('sep', sep_id)}, {'name': ('col', uuid.uuid1()), 'contents' : fvt.String(values=[':'], codec=codec), 'set_attrs': MH.Attr.Separator, 'mutable': struct_mutable} ] if isinstance(v, list): modeled_v = [] val_id = uuid.uuid1() for subidx, value in enumerate(v): assert not isinstance(value, list) if isinstance(value, dict): # If the type of v is a dictionary, build a sub JSON structure for it. modeled_v.append(json_builder(tag_name + "_" + str(idx)+str(subidx), params=value)) else: checked_value = value if gr.is_string_compatible(value) else str(value) modeled_v.append( {'name': ('val'+str(subidx), val_id), 'contents': [ {'name': ('sep', sep_id)}, {'name': ('cts', uuid.uuid1()), 'contents': fvt.String(values=[checked_value], codec=codec)}, {'name': ('sep', sep_id)} ]} ) attr_value = \ {'name': ('cts', uuid.uuid1()), 'contents': modeled_v, 'separator': {'contents': {'name': ('comma', uuid.uuid1()), 'contents': fvt.String(values=[','], max_sz=100, absorb_regexp='\s*,\s*', codec=codec), 'mutable': struct_mutable, 'absorb_csts': AbsNoCsts(size=True, regexp=True)}, 'prefix': False, 'suffix': False, 'unique': False} } params.append({'name': ('attr_val'+str(idx), uuid.uuid1()), 'contents': [ {'contents': fvt.String(values=['['], codec=codec), 'mutable': struct_mutable, 'name': 'prefix'+str(idx)}, attr_value, {'contents': fvt.String(values=[']'], codec=codec), 'mutable': struct_mutable, 'name': 'suffix'+str(idx)} ]}) elif isinstance(v, dict): params.append(json_builder(tag_name + "_" + str(idx), params=v)) elif gr.is_string_compatible(v): params += [ {'name': ('sep', sep_id)}, {'name': ('val', uuid.uuid1()), 'contents': fvt.String(values=[v], codec=codec)}, {'name': ('sep', sep_id)} ] else: raise DataModelDefinitionError cts.append({'name': ('attr'+str(idx), uuid.uuid1()), 'contents': params}) idx += 1 if not determinist: params_desc = {'section_type': MH.FullyRandom, 'contents': cts} else: params_desc = {'section_type': MH.Ordered, 'contents': cts} else: raise DataModelDefinitionError tag_start_open_desc = \ {'name': ('prefix', uuid.uuid1()), 'contents': fvt.String(values=['{'], codec=codec), 'mutable': struct_mutable, 'set_attrs': MH.Attr.Separator} tag_start_cts_desc = \ {'name': ('contents', uuid.uuid1()), 'random': not determinist, 'separator': {'contents': {'name': ('comma', uuid.uuid1()), 'contents': fvt.String(values=[','], max_sz=100, absorb_regexp='\s*,\s*', codec=codec), 'mutable': struct_mutable, 'absorb_csts': AbsNoCsts(size=True, regexp=True)}, 'prefix': False, 'suffix': False, 'unique': False}, 'contents': [params_desc]} tag_start_close_desc = \ {'name': ('suffix', uuid.uuid1()), 'contents': fvt.String(values=['}'], codec=codec), 'mutable': struct_mutable, 'set_attrs': MH.Attr.Separator} tag_start_desc = \ {'name': tag_name if node_name is None else node_name, 'contents': [tag_start_open_desc, tag_start_cts_desc, tag_start_close_desc]} return tag_start_desc
def tag_builder(tag_name, params=None, refs=None, contents=None, node_name=None, codec='latin-1', tag_name_mutable=True, struct_mutable=True, determinist=True, condition=None, absorb_regexp=None, specific_fuzzy_vals=None, tag_type=TAG_TYPE.standard, nl_prefix=False, nl_suffix=False): """ Helper for modeling an XML tag. Args: tag_name (str): name of the XML tag. params (dict): optional attributes to be added in the XML tag refs (dict): if provided it should contain for at least one parameter key (provided in ``params`` dict) the name to be used for the node representing the corresponding value. Useful when the parameter ``condition`` is in use and needs to relate to the value of specific parameters. contents: can be either None (empty tag), a :class:`framework.data_model.Node`, a dictionary (Node description), a string or a string list (string-Node values). node_name (str): name of the node to be created. codec (str): codec to be used for generating the XML tag. tag_name_mutable (bool): if ``False``, the tag name will not be mutable, meaning that its ``Mutable`` attribute will be cleared. struct_mutable (bool): if ``False`` the XML structure "will not" be mutable, meaning that each node related to the structure will have its ``Mutable`` attribute cleared. determinist (bool): if ``False``, the attribute order could change from one retrieved data to another. condition (tuple): optional existence condition for the tag. If not ``None`` a keyword ``exists_if`` will be added to the root node with this parameter as a value. absorb_regexp (str): regex for ``contents`` absorption tag_type (TAG_TYPE): specify the type of notation specific_fuzzy_vals (dict): if provided it should contain for at least one parameter key (provided in ``params`` dict) a list of specific values that will be used by some generic disruptors like tTYPE. nl_prefix (bool): add a new line character before the tag nl_suffix (bool): add a new line character after the tag Returns: dict: Node-description of the XML tag. """ if params is not None: assert isinstance(params, dict) cts = [] idx = 1 refs = {} if refs is None else refs for k, v in params.items(): if gr.is_string_compatible(v): val_ref = refs.get(k, ('val', uuid.uuid1())) v = v if isinstance(v, list) else [v] nd_desc = {'name': val_ref, 'contents': fvt.String(values=v, codec=codec)} if specific_fuzzy_vals: nd_desc['specific_fuzzy_vals'] = specific_fuzzy_vals.get(k) elif isinstance(v, dict): nd_desc = v if specific_fuzzy_vals: nd_desc['specific_fuzzy_vals'] = specific_fuzzy_vals.get(k) elif isinstance(v, Node): nd_desc = (v, 1, 1) v.set_specific_fuzzy_vals(specific_fuzzy_vals.get(k)) else: raise ValueError sep_id = uuid.uuid1() cts.append({'name': ('attr'+str(idx), uuid.uuid1()), 'contents': [ {'name': ('key', uuid.uuid1()), 'contents': fvt.String(values=[k], codec=codec)}, {'name': ('eq', uuid.uuid1()), 'contents': fvt.String(values=['='], codec=codec), 'set_attrs': MH.Attr.Separator, 'mutable': struct_mutable}, {'name': ('sep', sep_id), 'contents': fvt.String(values=['"'], codec=codec), 'set_attrs': MH.Attr.Separator, 'mutable': struct_mutable}, nd_desc, {'name': ('sep', sep_id)}, ]}) idx += 1 if not determinist: params_desc = {'section_type': MH.FullyRandom, 'contents': cts} else: params_desc = {'section_type': MH.Ordered, 'contents': cts} else: params_desc = None if tag_type in [TAG_TYPE.comment, TAG_TYPE.proc_instr]: assert contents is None if tag_type is TAG_TYPE.proc_instr: prefix = '<?' elif tag_type is TAG_TYPE.comment: prefix = '<!--' else: raise ValueError else: prefix = '</' if contents is None else '<' tag_start_open_desc = \ {'name': ('prefix', uuid.uuid1()), 'contents': fvt.String(values=[prefix], codec=codec), 'mutable': struct_mutable, 'set_attrs': MH.Attr.Separator} tag_cts = [{'name': ('tag_name', uuid.uuid1()), 'contents': fvt.String(values=[tag_name], codec=codec), 'mutable': tag_name_mutable}] if params_desc is not None: tag_cts.append(params_desc) tag_start_cts_desc = \ {'name': ('content', uuid.uuid1()), 'random': not determinist, 'separator': {'contents': {'name': ('spc', uuid.uuid1()), 'contents': fvt.String(values=[' '], max_sz=100, absorb_regexp='\s+', codec=codec), 'mutable': struct_mutable, 'absorb_csts': AbsNoCsts(size=True, regexp=True)}, 'prefix': False, 'suffix': False, 'unique': False}, 'contents': tag_cts} if tag_type in [TAG_TYPE.comment, TAG_TYPE.proc_instr]: if tag_type is TAG_TYPE.proc_instr: suffix = '?>' elif tag_type is TAG_TYPE.comment: suffix = '-->' else: raise ValueError else: suffix = '>' tag_start_close_desc = \ {'name': ('suffix', uuid.uuid1()), 'contents': fvt.String(values=[suffix], codec=codec), 'mutable': struct_mutable, 'set_attrs': MH.Attr.Separator} if contents is None: start_tag_name = tag_name else: start_tag_name = ('start-tag', uuid.uuid1()) tag_start_desc = \ {'name': start_tag_name, 'contents': [tag_start_open_desc, tag_start_cts_desc, tag_start_close_desc]} tag_end_desc = \ {'name': ('end-tag', uuid.uuid1()), 'contents': [ {'name': ('prefix', uuid.uuid1()), 'contents': fvt.String(values=['</'], codec=codec), 'mutable': struct_mutable, 'set_attrs': MH.Attr.Separator}, {'name': ('content', uuid.uuid1()), 'contents': fvt.String(values=[tag_name], codec=codec), 'mutable': tag_name_mutable}, {'name': ('suffix', uuid.uuid1()), 'contents': fvt.String(values=['>'], codec=codec), 'mutable': struct_mutable, 'set_attrs': MH.Attr.Separator}, ]} if contents is None: tag_desc = tag_start_desc else: if isinstance(contents, Node): cts = [tag_start_desc, (contents, 1, 1), tag_end_desc] elif isinstance(contents, dict): cts = [tag_start_desc, contents, tag_end_desc] elif isinstance(contents, list) and not gr.is_string_compatible(contents[0]): cts = [tag_start_desc] for c in contents: cts.append(c) cts.append(tag_end_desc) else: assert gr.is_string_compatible(contents) if not isinstance(contents, list): contents = [contents] content_desc = {'name': ('elt-content', uuid.uuid1()), 'contents': fvt.String(values=contents, codec=codec, absorb_regexp=absorb_regexp)} if absorb_regexp is not None: content_desc['absorb_csts'] = AbsNoCsts(regexp=True) cts = [tag_start_desc, content_desc, tag_end_desc] tag_desc = \ {'name': tag_name if node_name is None else node_name, 'separator': {'contents': {'name': ('nl', uuid.uuid1()), 'contents': fvt.String(values=['\n'], max_sz=100, absorb_regexp='\s*', codec=codec), 'absorb_csts': AbsNoCsts(regexp=True)}, 'prefix': nl_prefix, 'suffix': nl_suffix, 'unique': False}, 'contents': cts} if condition: tag_desc['exists_if'] = condition return tag_desc