def create( cls, name: str, config: configparser.SectionProxy, ) -> "NetworkBandwidth": try: interfaces = config["interfaces"].split(",") interfaces = [i.strip() for i in interfaces if i.strip()] if not interfaces: raise ConfigurationError("No interfaces configured") host_interfaces = psutil.net_if_addrs().keys() for interface in interfaces: if interface not in host_interfaces: raise ConfigurationError( "Network interface {} does not exist".format( interface)) threshold_send = config.getfloat("threshold_send", fallback=100) threshold_receive = config.getfloat("threshold_receive", fallback=100) return cls(name, interfaces, threshold_send, threshold_receive) except KeyError as error: raise ConfigurationError( "Missing configuration key: {}".format(error)) from error except ValueError as error: raise ConfigurationError( "Threshold in wrong format: {}".format(error)) from error
def setup(self, config: SectionProxy) -> None: """Set up the counters for config.""" client_limit = config.getint("limit_client_tests", fallback=0) if client_limit: client_period = config.getfloat("limit_client_period", fallback=1) * 3600 self._setup("client_id", client_limit, client_period) origin_limit = config.getint("limit_origin_tests", fallback=0) if origin_limit: origin_period = config.getfloat("limit_origin_period", fallback=1) * 3600 self._setup("origin", origin_limit, origin_period) slack_user_limit = config.getint("limit_slack_user_tests", fallback=0) if slack_user_limit: slack_user_period = ( config.getfloat("limit_slack_user_period", fallback=1) * 3600 ) self._setup("slack_user", slack_user_limit, slack_user_period) slack_team_limit = config.getint("limit_slack_team_tests", fallback=0) if slack_team_limit: slack_team_period = ( config.getfloat("limit_slack_team_period", fallback=1) * 3600 ) self._setup("slack_team", slack_team_limit, slack_team_period) self.running = True
def __init__( self, config: SectionProxy, method: str, query_string: bytes, req_headers: RawHeaderListType, response_start: Callable[..., None], response_body: Callable[..., None], response_done: Callable[..., None], error_log: Callable[[str], int] = sys.stderr.write, ) -> None: self.config = config # type: SectionProxy self.charset_bytes = self.config["charset"].encode("ascii") self.method = method self.req_headers = req_headers self.response_start = response_start self.response_body = response_body self._response_done = response_done self.error_log = error_log # function to log errors to self.test_uri = None # type: str self.test_id = None # type: str self.robot_time = None # type: str self.robot_hmac = None # type: str self.req_hdrs = None # type: StrHeaderListType self.format = None # type: str self.check_name = None # type: str self.descend = None # type: bool self.save = None # type: bool self.save_path = None # type: str self.timeout = None # type: Any self.referer_spam_domains = [] # type: List[str] if config.get("limit_client_tests", fallback=""): if self._client_period is None: self._client_period = ( config.getfloat("limit_client_period", fallback=1) * 3600) thor.schedule(self._client_period, self.client_ratelimit_cleanup) if config.get("limit_origin_tests", fallback=""): if self._origin_period is None: self._origin_period = ( config.getfloat("limit_origin_period", fallback=1) * 3600) thor.schedule(self._origin_period, self.origin_ratelimit_cleanup) if config.get("referer_spam_domains", fallback=""): self.referer_spam_domains = [ i.strip() for i in config["referer_spam_domains"].split() ] self.run(query_string)
def __get_option_with_default_value(section: configparser.SectionProxy, option, default_value, data_type: str = ""): if section is not None and option in section: try: if data_type == "bool": temp = section.getboolean(option) elif data_type == "int": temp = section.getint(option) elif data_type == "float": temp = section.getfloat(option) else: temp = section.get(option) if option is None: log.warning( f"Setting '{option}' is present but has no value. Please refer to 'config.default' for an example config." ) return default_value else: return temp except ValueError: return default_value log.error( f"Setting '{option}' is missing from config. Please refer to 'config.default' for an example config." ) return default_value
def create(cls, name: str, config: configparser.SectionProxy) -> "Load": try: return cls(name, config.getfloat("threshold", fallback=2.5)) except ValueError as error: raise ConfigurationError( "Unable to parse threshold as float: {}".format( error)) from error
def __init__(self, config: SectionProxy, method: str, query_string: bytes, req_headers: RawHeaderListType, response_start: Callable[..., None], response_body: Callable[..., None], response_done: Callable[..., None], error_log: Callable[[str], int] = sys.stderr.write) -> None: self.config = config # type: SectionProxy self.charset_bytes = self.config['charset'].encode('ascii') self.method = method self.req_headers = req_headers self.response_start = response_start self.response_body = response_body self._response_done = response_done self.error_log = error_log # function to log errors to self.test_uri = None # type: str self.test_id = None # type: str self.robot_time = None # type: str self.robot_hmac = None # type: str self.req_hdrs = None # type: StrHeaderListType self.format = None # type: str self.check_name = None # type: str self.descend = None # type: bool self.save = None # type: bool self.save_path = None # type: str self.timeout = None # type: Any self.referer_spam_domains = [] # type: List[str] if config.get("limit_client_tests", fallback=""): if self._client_period is None: self._client_period = config.getfloat("limit_client_period", fallback=1) * 3600 thor.schedule(self._client_period, self.client_ratelimit_cleanup) if config.get("limit_origin_tests", fallback=""): if self._origin_period is None: self._origin_period = config.getfloat("limit_origin_period", fallback=1) * 3600 thor.schedule(self._origin_period, self.origin_ratelimit_cleanup) if config.get("referer_spam_domains", fallback=""): self.referer_spam_domains = [i.strip() for i in \ config["referer_spam_domains"].split()] self.run(query_string)
def param_validation(params: configparser.SectionProxy) -> None: """ Validation of the params definitions from config.cfg. validates relations between objects, as the agent or the targets are in the Area If not valid, raise InvalidParamsException """ N = params.getint('N') num_cells = N * N target_locations = eval(params['TARGET_LOCATIONS']) agent_position = eval(params['AGENT_POSITION']) if target_locations is None: assert params.getint('NUM_TARGETS') <= num_cells else: assert len(target_locations) <= num_cells assert all( [target_location[0] <= N - 1 and target_location[1] <= N - 1 for target_location in target_locations]) if isinstance(agent_position, tuple): assert agent_position[0] <= N - 1 assert agent_position[1] <= N - 1 assert 0 < params.getfloat('pta') <= 1 assert 0 < params.getfloat('alpha') <= 1 assert 0 < params.getfloat('INITIAL_PRIOR_P') < 1
def __init__(self, config: SectionProxy, config_path=""): # if intersection of two regions is less than given parameter w.r.t. both regions, intersection doesn't count self.intersect_param = config.getfloat('FakeIntersectionParameter', fallback=0.1)