nefelim4ag / Ananicy

Ananicy - is Another auto nice daemon, with community rules support (Use pull request please)
GNU General Public License v3.0
567 stars 79 forks source link

First matching rule is applied, rather than rule from highest-priority file #463

Open tehybel opened 1 year ago

tehybel commented 1 year ago

Let's say I create a rules file, 90-myrules.rules, and another rules file, 91-part2.rules. Both have a rule matching my program. The intuitive behavior would be that the rule in 91-part2.rules has highest priority, i.e., is applied last.

However the current behavior is that the first matching rule is applied, and no other rules are applied. The current behavior is not intuitive, and is the opposite of e.g. systemd.

This is caused by the line self.rules[key] = ... inside get_rule_info, which simply inserts the rules in order of discovery into the self.rules dictionary. Later, inside get_tpid_rule, the first matching rule is returned. get_tpid_rule should instead update a result variable and return the last match:

    def get_tpid_rule(self, tpid: TPID):
        rule_cmdlines = tpid.cmdline
        result = None
        for rule_name in [tpid.cmd, tpid.stat_name]:
            for key in self.rules:
                name, cmdlines = key
                if name == rule_name:
                    if cmdlines:
                        for cl in cmdlines:
                            if cl not in rule_cmdlines:
                                break
                        else:
                            result = self.rules[key]
                    else:
                        result = self.rules[key]
        return result