Rename variables for better clarity and consistency

This commit is contained in:
Julius Freudenberger 2023-02-04 18:29:55 +01:00
parent 64e52acbdb
commit d60ce9d0bc
3 changed files with 9 additions and 8 deletions

2
app.py
View file

@ -22,6 +22,6 @@ def index(parser_name):
@app.route('/mensa/<parser_name>/feed/<canteen_name>') @app.route('/mensa/<parser_name>/feed/<canteen_name>')
def mensa(parser_name, canteen_name): def mensa(parser_name, canteen_name):
try: try:
return utils.get_parser(parser_name).parse(canteen_name) return utils.get_parser(parser_name).get_meal_data(canteen_name)
except KeyError: except KeyError:
abort(404) abort(404)

View file

@ -52,7 +52,7 @@ legend = {
multiple_whitespaces_regex = re.compile('\\s{2,}') multiple_whitespaces_regex = re.compile('\\s{2,}')
def parse_url(url, today=False): def get_meal_data(url, today=False):
canteen = LazyBuilder() canteen = LazyBuilder()
with urlopen(Request(url, None, {'User-Agent': 'Mozilla/5.0'})) as response: with urlopen(Request(url, None, {'User-Agent': 'Mozilla/5.0'})) as response:
data = json.loads(response.read()) data = json.loads(response.read())
@ -88,6 +88,7 @@ def define_parsers():
with open('parsers/tuebingen.json') as canteen_file: with open('parsers/tuebingen.json') as canteen_file:
canteen_json = json.load(canteen_file) canteen_json = json.load(canteen_file)
parser = Parser(canteen_json['name'], handler=parse_url, shared_prefix=canteen_json['base_url']) parser = Parser(canteen_json['name'], meal_data_handler=get_meal_data,
base_url=canteen_json['base_url'])
for canteen in canteen_json['canteens']: for canteen in canteen_json['canteens']:
parser.define(Canteen(canteen['id'], canteen['suffix'], canteen['name'], canteen['street'], canteen['city'])) parser.define(Canteen(canteen['id'], canteen['suffix'], canteen['name'], canteen['street'], canteen['city']))

View file

@ -22,17 +22,17 @@ class Canteen:
class Parser: class Parser:
canteens: dict[str, Canteen] = {} canteens: dict[str, Canteen] = {}
def __init__(self, name, handler, shared_prefix: str): def __init__(self, name, meal_data_handler, base_url: str):
self.name = name self.name = name
self.handler = handler self.meal_data_handler = meal_data_handler
self.shared_prefix = shared_prefix self.base_url = base_url
parser_list[name] = self parser_list[name] = self
def define(self, canteen: Canteen): def define(self, canteen: Canteen):
self.canteens[canteen.canteen_id] = canteen self.canteens[canteen.canteen_id] = canteen
def parse(self, canteen: str): def get_meal_data(self, canteen: str):
return self.handler(self.shared_prefix + self.canteens[canteen].suffix) return self.meal_data_handler(self.base_url + self.canteens[canteen].suffix)
def get_canteen_index(self, prefix: str): def get_canteen_index(self, prefix: str):
index: dict[str, str] = {} index: dict[str, str] = {}