refactor filter modul
This commit is contained in:
parent
415562c7e7
commit
478f783596
3 changed files with 65 additions and 39 deletions
|
@ -10,6 +10,7 @@ parameter_failed_list = {}
|
|||
count_used = 0
|
||||
count_hit = 0
|
||||
|
||||
|
||||
def init_filter():
|
||||
global white_list
|
||||
global black_list
|
||||
|
@ -40,7 +41,7 @@ def end_filter():
|
|||
logging.info("(%d/%d) stations filtered by: <no filter used>")
|
||||
|
||||
|
||||
def parameter_hit(param_name):
|
||||
def parameter_failed_evt(param_name):
|
||||
count = 1
|
||||
old = None
|
||||
if parameter_failed_list:
|
||||
|
@ -50,6 +51,29 @@ def parameter_hit(param_name):
|
|||
parameter_failed_list[param_name] = count
|
||||
|
||||
|
||||
def verify_value(ref_val, val):
|
||||
if ref_val == val:
|
||||
return True
|
||||
if ref_val is None:
|
||||
return len(val) == 0
|
||||
if type(val) is int:
|
||||
return val == int(ref_val)
|
||||
if val:
|
||||
return ref_val.find(val) >= 0
|
||||
return False
|
||||
|
||||
|
||||
def chk_paramter(parameter_name, val):
|
||||
if black_list:
|
||||
if parameter_name in black_list:
|
||||
if verify_value(black_list[parameter_name], val):
|
||||
return False
|
||||
if white_list:
|
||||
if parameter_name in white_list:
|
||||
return verify_value(white_list[parameter_name], val)
|
||||
return True
|
||||
|
||||
|
||||
def check_station(station_json):
|
||||
global count_used
|
||||
global count_hit
|
||||
|
@ -61,43 +85,24 @@ def check_station(station_json):
|
|||
return False
|
||||
# oder verknüpft
|
||||
if black_list:
|
||||
black_list_hit = False
|
||||
for param_name in black_list:
|
||||
unvalid_elements = black_list[param_name]
|
||||
val = get_json_attr(station_json, param_name)
|
||||
if not val == None:
|
||||
# attribut in json vorhanden
|
||||
if unvalid_elements:
|
||||
if val:
|
||||
pos = unvalid_elements.find(val)
|
||||
black_list_hit = pos >= 0
|
||||
else:
|
||||
if not val:
|
||||
black_list_hit = True
|
||||
if black_list_hit:
|
||||
parameter_hit(param_name)
|
||||
# logging.debug("FAIL '%s' blacklist hit on '%s' '%s' == '%s'",
|
||||
# station_name, param_name, unvalid_elements, val)
|
||||
return False
|
||||
if verify_value(black_list[param_name], val):
|
||||
parameter_failed_evt(param_name)
|
||||
logging.debug("FAIL '%s' blacklist failed on '%s' '%s' == '%s'",
|
||||
station_name, param_name, black_list[param_name], val)
|
||||
return False
|
||||
|
||||
# und verknüpft
|
||||
# und verknüpft
|
||||
if white_list:
|
||||
white_list_hit = True
|
||||
for param_name in white_list:
|
||||
val = get_json_attr(station_json, param_name)
|
||||
if not val == None:
|
||||
if val is not None:
|
||||
# attribut in json vorhanden
|
||||
valid_elements = white_list[param_name]
|
||||
if type(val) is int:
|
||||
white_list_hit = val == valid_elements
|
||||
else:
|
||||
if val:
|
||||
pos = valid_elements.find(val)
|
||||
white_list_hit = pos >= 0
|
||||
if not white_list_hit:
|
||||
parameter_hit(param_name)
|
||||
# logging.debug("FAIL '%s' whitelist failed on '%s' '%s' == '%s'",
|
||||
# station_name, param_name, valid_elements, val)
|
||||
if not verify_value(white_list[param_name], val):
|
||||
parameter_failed_evt(param_name)
|
||||
logging.debug("FAIL '%s' whitelist failed on '%s' '%s' == '%s'",
|
||||
station_name, param_name, white_list[param_name], val)
|
||||
return False
|
||||
# logging.debug("OK '%s' passed", station_name)
|
||||
count_hit = count_hit + 1
|
||||
|
|
|
@ -26,9 +26,10 @@ class StationVote:
|
|||
text_line = self.url + '|' + self.icon + '|' + str(self.vote)
|
||||
return text_line
|
||||
|
||||
def to_server_station(self,cathegory):
|
||||
def to_server_station(self, cathegory):
|
||||
return my_stations.Station(self.name, self.url, cathegory, self.icon)
|
||||
|
||||
|
||||
def signal_station_selected(name, url, icon):
|
||||
recently_station_list = get_stations_list()
|
||||
station_hit = StationVote(name, url + '|' + icon)
|
||||
|
@ -93,6 +94,7 @@ def directory_name():
|
|||
return list(get_recently_stations_dictionary().keys())[0]
|
||||
return DIRECTORY_NAME
|
||||
|
||||
|
||||
# used in landing page
|
||||
def get_stations_by_vote():
|
||||
station_list = get_stations_list()
|
||||
|
|
|
@ -13,6 +13,23 @@ class MyTestCase(unittest.TestCase):
|
|||
|
||||
logging.getLogger().setLevel(logging.DEBUG)
|
||||
|
||||
def test_verify_values(self):
|
||||
assert my_filter.verify_value( None, None )
|
||||
assert my_filter.verify_value( '', '' )
|
||||
assert my_filter.verify_value( None, '' )
|
||||
assert my_filter.verify_value( 3, 3 )
|
||||
assert my_filter.verify_value( '3', 3 )
|
||||
assert my_filter.verify_value( '3', '3' )
|
||||
assert my_filter.verify_value( '3,4,5,6', '5' )
|
||||
|
||||
assert not my_filter.verify_value( '', None )
|
||||
assert not my_filter.verify_value( '', '3' )
|
||||
assert not my_filter.verify_value( 3, 4 )
|
||||
assert not my_filter.verify_value( '3', 4 )
|
||||
assert not my_filter.verify_value( '4', '3' )
|
||||
assert not my_filter.verify_value( '3,4,5,6', '9' )
|
||||
|
||||
|
||||
def test_init_filter(self):
|
||||
|
||||
filter.init_filter()
|
||||
|
@ -28,22 +45,24 @@ class MyTestCase(unittest.TestCase):
|
|||
|
||||
|
||||
def test_valid_station(self):
|
||||
filter.init_filter()
|
||||
my_filter.init_filter()
|
||||
test_lines = generic.readlns_txt_file(generic.get_var_path()+"/test.json")
|
||||
|
||||
test_lines = radiobrowser.get_stations_by_votes()
|
||||
|
||||
io = StringIO(test_lines[0])
|
||||
stations_json = json.load(io)
|
||||
count = 0
|
||||
for station_json in stations_json:
|
||||
if filter.check_station(station_json):
|
||||
if my_filter.check_station(station_json):
|
||||
station = radiobrowser.Station(station_json)
|
||||
count = count + 1
|
||||
|
||||
logging.info("Stations (%d/%d)" , count, len(stations_json) )
|
||||
logging.info("Used filter parameter", filter.parameter_failed_list)
|
||||
my_filter.end_filter()
|
||||
|
||||
|
||||
def test_popular_station(self):
|
||||
stations = radiobrowser.get_stations_by_votes()
|
||||
def test_life_popular_station(self):
|
||||
#hard test for filter
|
||||
stations = radiobrowser.get_stations_by_votes(10000000)
|
||||
logging.info("Stations (%d)", len(stations))
|
||||
|
||||
def test_recently_hit(self):
|
||||
|
|
Loading…
Add table
Reference in a new issue