Last active
January 31, 2025 12:55
-
-
Save sldenazis/954d13fa0b2b4efcde467adb12716c2a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18,19c18,23 | |
< #self.ruleset[option] = {'depends': [], 'conflict': []} | |
< self.ruleset[option] = {'depends': [], 'conflict': [], 'enabled': False} | |
--- | |
> self.ruleset[option] = { | |
> 'depends': [], | |
> 'conflict': [], | |
> 'enabled': False, | |
> 'enabled_by_dependency': False | |
> } | |
24a29 | |
> # Add dependency if not exists | |
31a37 | |
> # Add conflict if not exist | |
35,58d40 | |
< def _checkConflictForEnabledOptions(self, option, option_rules): | |
< | |
< if self.ruleset[option]['enabled']: | |
< for conflict in option_rules['conflict']: | |
< if conflict in self.ruleset[option]['depends']: | |
< self.is_coherent = False | |
< break | |
< else: | |
< if option not in self.ruleset[conflict]['conflict']: | |
< self._checkConflictForEnabledOptions(option, | |
< self.ruleset[conflict]) | |
< | |
< for dependency in option_rules['depends']: | |
< if dependency in self.ruleset[option]['conflict']: | |
< self.is_coherent = False | |
< break | |
< | |
< else: | |
< if option not in self.ruleset[dependency]['depends']: | |
< self._checkConflictForEnabledOptions(option, | |
< self.ruleset[dependency]) | |
< | |
< return self.is_coherent | |
< | |
59a42 | |
> """ Set self.is_coherent to false if rules are incompatible""" | |
67c50 | |
< self.ruleset[conflict]) | |
--- | |
> self.ruleset[conflict]) | |
81a65,67 | |
> # Disable options that makes a conflict | |
> self._disableOptionIfConflict(option) | |
> | |
86a73 | |
> self.ruleset[dependency]['enabled_by_dependency'] = True | |
90,91d76 | |
< #if conflict != check: | |
< #self.disableOption(option, option) | |
101a87,107 | |
> for dependency in self.ruleset[option]['depends']: | |
> if dependency != check: | |
> if self.ruleset[dependency]['enabled_by_dependency']: | |
> self.disableOption(dependency, check) | |
> | |
> def _disableDependencies(self, option, check): | |
> for dependency in self.ruleset[option]['depends']: | |
> self.ruleset[dependency]['enabled'] = False | |
> | |
> # Disable option only if it was enabled by dependencies | |
> if self.ruleset[dependency]['enabled_by_dependency']: | |
> self.ruleset[dependency]['enabled_by_dependency'] = False | |
> | |
> if dependency != check: | |
> self._disableDependencies(dependency, check) | |
> | |
> def _disableOptionIfConflict(self, option_check): | |
> for option in self.ruleset: | |
> if option_check in self.ruleset[option]['conflict']: | |
> self._disableDependencies(option, option) | |
> | |
118a125 | |
> | |
131,132c138,140 | |
< def selection(self): | |
< #self.options_slice = set([]) | |
--- | |
> self.options_slice = set([]) | |
> | |
> # Setting up option_slice based on enabled options | |
136c144,145 | |
< return self.options_slice | |
\ No newline at end of file | |
--- | |
> def selection(self): | |
> return self.options_slice |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/env python | |
class RuleSet(): | |
def __init__(self): | |
self._newRuleSet() | |
self.is_coherent = True | |
def _newRuleSet(self): | |
self.ruleset = {} | |
def _optionExists(self, option, option_list): | |
if option_list.get(option) == None: | |
return False | |
def _addOption(self, option): | |
if self._optionExists(option, self.ruleset) == False: | |
#self.ruleset[option] = {'depends': [], 'conflict': []} | |
self.ruleset[option] = {'depends': [], 'conflict': [], 'enabled': False} | |
def addDep(self, option, dependency): | |
self._addOption(option) | |
self._addOption(dependency) | |
if dependency not in self.ruleset[option]['depends']: | |
self.ruleset[option]['depends'].append(dependency) | |
def addConflict(self, option, conflict): | |
self._addOption(option) | |
self._addOption(conflict) | |
if conflict not in self.ruleset[option]['conflict']: | |
self.ruleset[option]['conflict'].append(conflict) | |
def _checkConflictForEnabledOptions(self, option, option_rules): | |
if self.ruleset[option]['enabled']: | |
for conflict in option_rules['conflict']: | |
if conflict in self.ruleset[option]['depends']: | |
self.is_coherent = False | |
break | |
else: | |
if option not in self.ruleset[conflict]['conflict']: | |
self._checkConflictForEnabledOptions(option, | |
self.ruleset[conflict]) | |
for dependency in option_rules['depends']: | |
if dependency in self.ruleset[option]['conflict']: | |
self.is_coherent = False | |
break | |
else: | |
if option not in self.ruleset[dependency]['depends']: | |
self._checkConflictForEnabledOptions(option, | |
self.ruleset[dependency]) | |
return self.is_coherent | |
def _checkConflict(self, option, option_rules): | |
for conflict in option_rules['conflict']: | |
if conflict in self.ruleset[option]['depends']: | |
self.is_coherent = False | |
break | |
else: | |
if option not in self.ruleset[conflict]['conflict']: | |
self._checkConflict(option, | |
self.ruleset[conflict]) | |
for dependency in option_rules['depends']: | |
if dependency in self.ruleset[option]['conflict']: | |
self.is_coherent = False | |
break | |
else: | |
if option not in self.ruleset[dependency]['depends']: | |
self._checkConflict(option, | |
self.ruleset[dependency]) | |
return self.is_coherent | |
def enableOption(self, option, check): | |
if not self.ruleset[option]['enabled']: | |
self.ruleset[option]['enabled'] = True | |
for dependency in self.ruleset[option]['depends']: | |
if dependency != check: | |
self.enableOption(dependency, check) | |
for conflict in self.ruleset[option]['conflict']: | |
#if conflict != check: | |
#self.disableOption(option, option) | |
self.disableOption(conflict, option) | |
def disableOption(self, option, check): | |
if self.ruleset[option]['enabled']: | |
self.ruleset[option]['enabled'] = False | |
for dependency in self.ruleset[option]['conflict']: | |
if dependency != check: | |
self.disableOption(dependency, check) | |
def isCoherent(self): | |
for option in self.ruleset: | |
self._checkConflict(option, self.ruleset[option]) | |
if not self.is_coherent: | |
break | |
return self.is_coherent | |
def getActiveOptions(self): | |
active_options = [] | |
for option in self.ruleset: | |
if self.ruleset[option]['enabled']: | |
active_options.append(str(option)) | |
return active_options | |
class Options(): | |
def __init__(self, rules): | |
self.ruleset = rules | |
self.options_slice = set([]) | |
return | |
def toggle(self, option): | |
if option not in self.ruleset.getActiveOptions(): | |
self.ruleset.enableOption(option, option) | |
else: | |
self.ruleset.disableOption(option, option) | |
def selection(self): | |
#self.options_slice = set([]) | |
for opt in self.ruleset.getActiveOptions(): | |
self.options_slice.add(opt) | |
return self.options_slice |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here is the patched version that works https://gist.github.com/sldenazis/cb27df8e1d52e34f6dc0d357f3299fb7.
I've also uploaded the patch here.