aboutsummaryrefslogtreecommitdiff
path: root/mensa/base.py
blob: 8db5b8c5ade2cde05f761812b6548c857b0f3f26 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# This file contains functions and classes necessary for the operation of backends.

class Food :
    def __init__(self,name, price="", category="Essen", veggie=False, desc=None, ingredients={}) :
        self.name = name
        self.price = price
        self.category = category
        self.veggie = veggie
        self.desc=desc
        self.ingredients=ingredients

class NoMenuError(Exception) :
    """ gets raised if there's no menu"""

class Renderer(object) :
    def __init__(self, name, human_name, module, description="", optional_args=[]) :
        self.name = name
        self.human_name = human_name
        self.description = description
        self.optional_args = []
        self.module = module
    def render(self, foods, **options) :
        self.module.render(foods, **options)
        
def formt (food) :
    cat = []
    vegkeys = [ "", "Vegetarian", "Vegan" ]
    r = ""
    food.sort(key=lambda foo: foo.category)
    for i in food:
        if not i.category in cat :
            cat.append(i.category)
            if not i.category == None : 
                r=r+ i.category+"\n"
        r=r+"\t" + i.name.ljust(80) + "\t"+ i.price.ljust(20) + vegkeys[i.veggie]+"\n"
        if i.desc :
            r = r+"\t  "+i.desc+"\n"
    return r
from math import acos, radians, pi, cos, sin
def dist(pos1, pos2) :
    # gives a rough estimate of the distance Accuracy: +/- cos(angle
    # between coordinates)*37/2pi. (about +/- 50 m for distances).
    # Distance is returned in km
    if pos1 is None or pos2 is None :
        return float("Inf")
    lat1, long1 = pos1
    lat2, long2 = pos2

    a = radians(90 - lat1);
    b = radians(90 - lat2);
    C = radians(abs(long1-long2));
    return acos(cos(a)*cos(b)+sin(a)*sin(b)*cos(C))*40040/(2*pi);


foodsources = {}
renderers = {}

class Restaurant(object):
    def __init__(self, name, human_name, module, optional_args=[], obligatory_args=(), pos=None):
        self.name = name
        self.human_name = human_name
        self.module = module
        self.optional_args = optional_args
        self.obligatory_args = obligatory_args
        self.pos = pos

    def get_food(self,**opt_args) :
        return self.module.get_food_items(*self.obligatory_args, **opt_args)

def register_restaurant(restaurant):
    global foodsources
    foodsources[restaurant.name] = restaurant
def register_renderer(renderer) :
    global renderers
    renderers[renderer.name] = renderer

def only_student_prices(price):
    return price.split("/")[0]