aboutsummaryrefslogtreecommitdiff
path: root/mensa/logic.py
blob: 1ae6a965ee712a518262169fe9d3d99110b6f828 (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
79
80
81
82
83
84
85
86
87
#!/usr/bin/python3

from mensa import base
import urllib.error
import os
import sys
from math import acos, radians, pi, cos, sin
from yapsy.PluginManager import PluginManager
try:
    import multiprocessing
    parallel=True
except:
    parallel = False
    
def init_foodsources():
    backends = PluginManager()
    backends.setPluginPlaces([os.path.join(os.path.dirname(os.path.realpath(__file__)),"backends")])    
    backends.collectPlugins()
    for pluginInfo in backends.getAllPlugins():
        backends.activatePluginByName(pluginInfo.name)
        pluginInfo.plugin_object.register_restaurants()

def init_renderers():
    frontends = PluginManager()
    frontends.setPluginPlaces([os.path.join(os.path.dirname(os.path.realpath(__file__)),"frontends")])
    frontends.collectPlugins()
    for pluginInfo in frontends.getAllPlugins():
        frontends.activatePluginByName(pluginInfo.name)
        pluginInfo.plugin_object.register_renderer()
    
    
def get_food(restlist=False, no_parallel=False,**options) :
    foodl = []
    sources = []
    for k,i in base.foodsources.items() :
        if restlist and not i.name in restlist :
            continue
        if options["pos"] and options["rad"] and base.dist(options["pos"], i.pos) > float(options["rad"]) :
            continue
        sources.append((k,i))


    if parallel and not no_parallel:
        r = []
        for k,i in sources :
            r.append(i)            
        foodl = get_food_parallel(r, ignore_nudelauswahl=True)
    else : 
        for k,i in sources :
            try : 
                food = i.get_food(ignore_nudelauswahl=True)
                foodl.append((i, food))
                # print("*"*20+i.human_name+"*"*20+"\n"+base.formt(food))
            except base.NoMenuError:
                sys.stderr.write(i.human_name + ": No menu found. This could be due to a holiday or due to an error in the script.\n")
            except urllib.error.HTTPError as e :
                sys.stderr.write(i.human_name + ": Fetching menu failed: %s\n" % str(e))
            except :
                sys.stderr.write(i.human_name + ": Unknown error\n")
    return foodl

# format:
# if __name__ == "__main__":
#     pool = multiprocessing.Pool(4)
#     k = pool.map(pr_f, list(mensenliste.items()))
#     k.sort(key=lambda x: x[0])
#     for i in k :
#         print(i[1])
def get_food_parallel_helper(i) :
    try : 
        return (i[0],i[0].get_food(**i[1]))
    except:
        sys.stderr.write(i[0].human_name + ": No menu found. This could be due to a holiday or due to an error in the script.\n")
        return (i[0], [])

def get_food_parallel(rl, **options) :
    pool = multiprocessing.Pool(60)
    k = [ (i, options) for i in rl ]
    l = list(pool.map(get_food_parallel_helper, k))
    l.sort(key=lambda x: x[0].human_name)
    return l

def render(to_render, rendlist=False, **options) :
    for k,i in base.renderers.items() :
        if rendlist and not i.name in rendlist :
            continue
        i.render(to_render, **options)