Beispiel #1
0
    def build(self, pre=None, shortest=False):
        """Build the ``Or`` instance

        :param list pre: The prerequisites list
        :param bool shortest: Whether or not the shortest reference-chain (most minimal) version of the field should be generated.
        """
        if pre is None:
            pre = []

        # self.shortest_vals will be set by the GramFuzzer and will
        # contain a list of value options that have a minimal reference
        # chain
        if shortest and self.shortest_vals is not None:
            return utils.val(rand.choice(self.shortest_vals), pre, shortest=shortest)
        else:
            return utils.val(rand.choice(self.values), pre, shortest=shortest)
Beispiel #2
0
    def get_ref(self, cat, refname):
        """Return one of the rules in the category ``cat`` with the name
        ``refname``. If multiple rule defintions exist for the defintion name
        ``refname``, use :any:`gramfuzz.rand` to choose a rule at random.

        :param str cat: The category to look for the rule in.
        :param str refname: The name of the rule definition. If the rule definition's name is
            ``"*"``, then a rule name will be chosen at random from within the category ``cat``.
        :returns: gramfuzz.fields.Def
        """
        if cat not in self.defs:
            raise errors.GramFuzzError("referenced definition category ({!r}) not defined".format(cat))
        
        if refname == "*":
            refname = rand.choice(self.defs[cat].keys())
            
        if refname not in self.defs[cat]:
            raise errors.GramFuzzError("referenced definition ({!r}) not defined".format(refname))

        return rand.choice(self.defs[cat][refname])
Beispiel #3
0
    def build(self, pre=None, shortest=False):
        """Build the ``Or`` instance

        :param list pre: The prerequisites list
        :param bool shortest: Whether or not the shortest reference-chain (most minimal)
            version of the field should be generated.
        """
        if pre is None:
            pre = []

        # self.shortest_vals will be set by the GramFuzzer and will
        # contain a list of value options that have a minimal reference
        # chain
        # 
        # see https://narly.me/posts/controlling-recursion-depth-in-grammars/
        # for an in-depth discussion
        if shortest and self.shortest_vals is not None:
            chosen_val = rand.choice(self.shortest_vals)
        else:
            chosen_val = rand.choice(self.values)

        return utils.val(chosen_val, pre, shortest=shortest)