def __init__(self, filename): hosts_content = [] config = ConfigParser.ConfigParser() with open(filename, 'r') as conf_file: config_content = conf_file.readline() while True: line = conf_file.readline() if line.strip() == "[hosts]": break config_content += line hosts_content = conf_file.readlines() config.readfp(io.BytesIO(config_content)) self._hosts_dic = {} for host_line in hosts_content: if host_line.startswith("!") or host_line in ("\n", "\r\n"): continue rule_matched_content = HostsParser.RULE_REGEX.search(host_line) if rule_matched_content is None: continue priority = rule_matched_content.group(2) ip_type = Rule.TYPE_REQUEST if rule_matched_content.group(4) is None else Rule.TYPE_A ip_list = rule_matched_content.group(3) if ip_type == Rule.TYPE_A else config.get("DNS Config", rule_matched_content.group(3)) ip_list = map(lambda str:str.strip(), ip_list.split(",")) rule_text = rule_matched_content.group(6) rule_type = Rule.REGEX_RULE if rule_text.startswith('/') and rule_text.endswith('/') else Rule.RAW_RULE rule = Rule(priority, ip_list, ip_type, rule_text, rule_type) rule_digest = rule.get_digest() self._hosts_dic.setdefault(rule_digest, []) self._hosts_dic[rule_digest].append(rule) self._all_dns = DNSItem("all_dns", []) self._dns_dict = {} for item in config.items("DNS Config"): self._dns_dict[item[0]] = DNSItem(item[0], map(lambda str:str.strip(), item[1].split(","))) map(lambda str:self._all_dns._dns_list.append(str), self._dns_dict[item[0]]._dns_list)
def rebound_sky_exit_rule(self): rule = Rule( name = 'rebound_sky_exit', indicators_param = [CCIParam(self.period, 0.015)], ref = {TI_CCI: TI_CCI}, f = lambda data, ref, entry_i, i: (data.at[i-1, ref[TI_CCI]] < self.sky) & (data.at[i, ref[TI_CCI]] > self.sky) ) return rule
def basic_close_rule(self): rule = Rule( name = 'basic_close_pos', indicators_param = [CCIParam(self.period, 0.015)], ref = {TI_CCI: TI_CCI}, f = lambda data, ref, entry_i, i: (data.at[i-1, ref[TI_CCI]] > self.ground) & (data.at[i, ref[TI_CCI]] < self.ground) ) return rule
def rebound_channel_rule(self): first_gate = self.ground + (self.sky - self.ground) * self.rebound_channel[0] second_gate = self.ground + (self.sky - self.ground) * self.rebound_channel[1] rule = Rule( name = 'rebound_channel_exit', indicators_param = [CCIParam(self.period, 0.015)], ref = {TI_CCI: TI_CCI}, f = lambda data, ref, entry_i, i: ((data[entry_i:i][data[ref[TI_CCI]] < first_gate].shape[0] > 0) & (data.at[i, ref[TI_CCI]] > second_gate)) ) return rule
def stop_loss_rule(self): if abs(self.stop_loss) < 1: f = lambda data, ref, entry_i, i: (data.at[i, CLOSE] - data.at[entry_i, CLOSE]) / data.at[entry_i, CLOSE] > self.stop_loss else: f = lambda data, ref, entry_i, i: (data.at[i, CLOSE] - data.at[entry_i, CLOSE]) > self.stop_loss rule = Rule( name = 'stop_loss', indicators_param = [IndicatorParam(CLOSE)], ref = {CLOSE: CLOSE}, f = f ) return rule
def p_rule(p): '''rule : literal IMPLICATION body COLON CERTAINTY PERIOD''' p[0] = Rule(p[1], p[3], p[5])
def test_rule_process(capsys): rule = Rule(Artist='Metallica') rule.process('some.mp3') out, err = capsys.readouterr() assert out == 'Update some.mp3. Set Artist to Metallica\n'
def build(self): return Rule(self._token, self._actions)