title = title.decode("utf-8") dyna = dyna.decode("utf-8") auth = auth.decode("utf-8") o = by_title.get(title, None) if o is None: print title continue links = o[u"資料"] if any((link[u"標題"] == NAME) for link in links): continue d = OrderedDict() d[u"標題"] = NAME d[u"網址"] = u"https://www.kanripo.org" + url d[u"限制"] = u"无" d[u"文字"] = True d[u"影印"] = False d[u"简体"] = False links.append(d) if o[u"朝代"] is None: if dyna: o[u"朝代"] = dyna if o[u"作者"] is None: if auth: o[u"作者"] = auth
def generate_transitions(topology, policy): # switch_transition: dictionary of "boolean formula" : "transition". # This will become the case statement in the NuSMV file for the # switch transition. switch_transition = OrderedDict() # header_transitions: # dictionary of "header" : {"boolean formula" : "transition"} # This will define the transition relation on the headers in the NuSMV # file. Headers with no transitions will become FROZENVARS. header_transitions = {} # For each switch configuration: # 1. add the fowarding rules to the switch_transition dictionary as # "string representation of boolean condition" : "new switch". # 2. add the modification rules to header_transitions. for switch, switch_config in policy: assert (switch in topology) # Ignore rules for hosts if switch in topology.hosts(): continue for rule in switch_config: # Convert patterns to boolean formula bform = ' & '.join([ '%s = %s' % (field, int_to_nusmvbitfield(field, val)) for field, val in rule.pattern.to_dict().iteritems() if val is not None ]) if bform == '': bform = 'switch = %s' % switch else: bform += ' & switch = %s' % switch for action, vals in [unpack_action(a) for a in rule.actions]: if action == 'modify': # Generate transition rule if vals[0] not in header_transitions: header_transitions[vals[0]] = OrderedDict() header_transitions[vals[0]][bform] = int_to_nusmvbitfield( vals[0], vals[1]) elif action == 'forward': if vals == ['OFPP_FLOOD']: # TODO: implement flood print "print_smv: flood not yet implemented." assert (False) elif vals == ['OFPP_CONTROLLER']: switch_transition[bform] = 'CONTROLLER' else: # Find the next switch/port out_port = vals[0] next_switch = [ sw for sw, p in topology.node[switch] ['ports'].iteritems() if p == out_port ] assert (len(next_switch) == 1) next_switch = next_switch[0] new_in_port = topology.node[next_switch]['ports'][ switch] new_nusmv_in_port = int_to_nusmvbitfield( IN_PORT, new_in_port) # Generate switch transition if next_switch in topology.hosts(): switch_transition[bform] = 'WORLD' else: switch_transition[bform] = next_switch # Generate in_port transition if IN_PORT not in header_transitions: header_transitions[IN_PORT] = OrderedDict() header_transitions[IN_PORT][bform] = new_nusmv_in_port else: # TODO: better error handling assert (False) # Ensure all switch transition cases are covered for sw in topology: default = 'switch = %s' % sw if default not in switch_transition: switch_transition[default] = 'DROP' for sw in special_switches: switch_transition['switch = %s' % sw] = sw # Ensure all header transition cases are covered for header in header_transitions: header_transitions[header]['TRUE'] = header return switch_transition, header_transitions
def reload_rules(self): self.rules = OrderedDict() self.load_rules()
def __init__(self, src=None): super().__init__() self._data = OrderedDict() if src: self.update(src)
def __reginit__(self): self.rules = OrderedDict() self.load_rules()