def post_save_vlanconfig(sender, instance, **kwargs): from naman.core.mappings import get_mappings from naman.core.pypelib.RuleTable import RuleTable if instance.machine.role is None: raise AttributeError("Machine %s has no role assigned" % instance.machine) if instance.machine.environment is None: raise AttributeError("Machine %s has no environment assigned" % instance.machine) table = RuleTable( "Backup, service and management", get_mappings(), "RegexParser", #rawfile, "RAWFile", None) table.setPolicy(False) for rule in Rule.objects.filter(active=True).order_by("pk"): table.addRule(rule.to_pypelib()) logging.debug(table.dump()) try: table.evaluate(instance.machine) logging.debug("Ha evaluado a True") except Exception, ex: import traceback print "Ha evaluado a False: %s" % traceback.format_exc() logging.debug("Table evaluated False") pass
def load(tableName, mappings, parser): Django.logger.info('Django.load') try: Table = PolicyRuleTableModel.objects.get(name = tableName) # Translation of exceptions: Django -> PyPElib except ObjectDoesNotExist: raise ZeroPolicyObjectsReturned("[Django Driver] There is no table with name: " + tableName) except MultipleObjectsReturned: raise MultiplePolicyObjectsReturned("[Django Driver] There are multiple tables with name: " + tableName) except Exception as e: raise Exception("[Django Driver] Some error occurred when trying to fetch table table with name: " + tableName + ". Exception: " + str(e)) print "type de table %s" % Table.type ruleTable = RuleTable(Table.name,mappings,Table.defaultParser, Table.defaultPersistence,False, eval(Table.type), Table.uuid) ruleTable._ruleSet = Django.loadRuleSet(Table.uuid) ruleTable._persist = Table.defaultPersistenceFlag ruleTable._mappings = mappings ruleTable._resolver = Resolver(mappings) return ruleTable
def evaluate(machine): mappings = get_mappings() table = RuleTable( "Decide tunned temp", mappings, "RegexParser", #rawfile, "RAWFile", None) table.setPolicy(False) table.addRule("if heater_manual = 1 then accept do tune_to_confort") for rule in Rule.objects.filter(active=True, thermostat=True).order_by("pk"): table.addRule(rule.to_pypelib()) if settings.DEBUG: table.dump() metaObj = {} try: table.evaluate(metaObj) except Exception: mappings["tune_to_economic"]() table1 = RuleTable( "Decide flame status", mappings, "RegexParser", #rawfile, "RAWFile", None) table1.addRule("if heater_on = 0 then deny") table1.addRule("if current_internal_temperature < tuned_temperature then accept") if settings.DEBUG: table1.dump() try: table1.evaluate(metaObj) try: mappings["start_flame"]() except Exception, ex: logging.error("ERROR: Cant start flame: %s" % ex) except Exception as e: try: mappings["stop_flame"]() except Exception, ex: logging.error("ERROR: Cant stop flame: %s" % ex)