diff options
-rwxr-xr-x | bin/mensa | 33 | ||||
-rw-r--r-- | mensa/__init__.py | 0 | ||||
-rw-r--r-- | mensa/backends/base.py | 43 | ||||
-rw-r--r-- | mensa/backends/mathemensa.py (renamed from backends/mathemensa.py) | 2 | ||||
-rw-r--r-- | mensa/backends/mathemensa.yapsy-plugin (renamed from backends/mathemensa.yapsy-plugin) | 0 | ||||
-rw-r--r-- | mensa/backends/satyam.py (renamed from backends/satyam.py) | 2 | ||||
-rw-r--r-- | mensa/backends/satyam.yapsy-plugin (renamed from backends/satyam.yapsy-plugin) | 0 | ||||
-rw-r--r-- | mensa/backends/singh.py (renamed from backends/singh.py) | 2 | ||||
-rw-r--r-- | mensa/backends/singh.yapsy-plugin (renamed from backends/singh.yapsy-plugin) | 0 | ||||
-rw-r--r-- | mensa/backends/studentenwerk.py (renamed from backends/studentenwerk.py) | 2 | ||||
-rw-r--r-- | mensa/backends/studentenwerk.yapsy-plugin (renamed from backends/studentenwerk.yapsy-plugin) | 0 | ||||
-rw-r--r-- | mensa/base.py (renamed from base.py) | 15 | ||||
-rw-r--r-- | mensa/frontends/plain-text.py (renamed from frontends/plain-text.py) | 5 | ||||
-rw-r--r-- | mensa/frontends/plain-text.yapsy-plugin (renamed from frontends/plain-text.yapsy-plugin) | 0 | ||||
-rwxr-xr-x | mensa/logic.py | 45 | ||||
-rwxr-xr-x | run.py | 59 |
16 files changed, 139 insertions, 69 deletions
diff --git a/bin/mensa b/bin/mensa new file mode 100755 index 0000000..b141990 --- /dev/null +++ b/bin/mensa @@ -0,0 +1,33 @@ +#!/usr/bin/python3 +import argparse +from mensa import logic +from mensa import base + +parser = argparse.ArgumentParser(description='Fetch menus from various sources') +# parser.add_argument('integers', metavar='N', type=int, nargs='+', +# help='an integer for the accumulator') +parser.add_argument('-r', '--restaurants', dest='rest', action='store', + metavar='LIST', + help='Comma-separated list of restaurants to fetch the menus from.') +parser.add_argument('-l', '--list-restaurants', dest='list', action='store_true', + help='get list of restaurants') + + +args = parser.parse_args() +## Load backends +logic.init_foodsources() +## Load frontends (not yet implemented) +logic.init_renderers() + + + +if args.list : + for k,i in base.foodsources.items(): + print(i.name, i.human_name) + exit() +restlist = None +if args.rest : + restlist = args.rest.split(",") + +foodl = logic.get_food(restlist) +logic.render(foodl, ["plain-text"]) diff --git a/mensa/__init__.py b/mensa/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/mensa/__init__.py diff --git a/mensa/backends/base.py b/mensa/backends/base.py new file mode 100644 index 0000000..6c57336 --- /dev/null +++ b/mensa/backends/base.py @@ -0,0 +1,43 @@ +# Common classes and methods. + +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 + +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 + +foodsources = {} + +class Restaurant(object): + def __init__(self, name, human_name, module, optional_args=[], obligatory_args=()): + self.name = name + self.human_name = human_name + self.module = module + self.optional_args = optional_args + self.obligatory_args = obligatory_args + + def get_food(**opt_args) : + self.module.get_food_items(*obligatory_args, **optional_args) + + +def register_restaurant(restaurant): + global foodsources + foodsources[restaurant.name] = (restaurant) diff --git a/backends/mathemensa.py b/mensa/backends/mathemensa.py index ea0cf72..8f66805 100644 --- a/backends/mathemensa.py +++ b/mensa/backends/mathemensa.py @@ -8,7 +8,7 @@ import re import xml.sax.saxutils as saxutils import html5lib import datetime -from base import * +from mensa.base import * from yapsy.IPlugin import IPlugin class Mathemensa(IPlugin) : diff --git a/backends/mathemensa.yapsy-plugin b/mensa/backends/mathemensa.yapsy-plugin index d122795..d122795 100644 --- a/backends/mathemensa.yapsy-plugin +++ b/mensa/backends/mathemensa.yapsy-plugin diff --git a/backends/satyam.py b/mensa/backends/satyam.py index ec488f3..7ac53a0 100644 --- a/backends/satyam.py +++ b/mensa/backends/satyam.py @@ -2,7 +2,7 @@ import urllib.request, urllib.error, urllib.parse from lxml import etree from lxml.cssselect import CSSSelector import html5lib -from base import * +from mensa.base import * import re import sys from yapsy.IPlugin import IPlugin diff --git a/backends/satyam.yapsy-plugin b/mensa/backends/satyam.yapsy-plugin index 59207ad..59207ad 100644 --- a/backends/satyam.yapsy-plugin +++ b/mensa/backends/satyam.yapsy-plugin diff --git a/backends/singh.py b/mensa/backends/singh.py index ae8b291..844cc9c 100644 --- a/backends/singh.py +++ b/mensa/backends/singh.py @@ -5,7 +5,7 @@ import html5lib import re import sys import datetime -from base import * +from mensa.base import * from yapsy.IPlugin import IPlugin class Signh(IPlugin) : diff --git a/backends/singh.yapsy-plugin b/mensa/backends/singh.yapsy-plugin index 7faf774..7faf774 100644 --- a/backends/singh.yapsy-plugin +++ b/mensa/backends/singh.yapsy-plugin diff --git a/backends/studentenwerk.py b/mensa/backends/studentenwerk.py index 48b15d3..a8f6c2b 100644 --- a/backends/studentenwerk.py +++ b/mensa/backends/studentenwerk.py @@ -5,7 +5,7 @@ import urllib.request, urllib.error, urllib.parse from lxml import etree from lxml.cssselect import CSSSelector import html5lib -from base import * +from mensa.base import * from yapsy.IPlugin import IPlugin import multiprocessing diff --git a/backends/studentenwerk.yapsy-plugin b/mensa/backends/studentenwerk.yapsy-plugin index ad2a994..ad2a994 100644 --- a/backends/studentenwerk.yapsy-plugin +++ b/mensa/backends/studentenwerk.yapsy-plugin @@ -1,4 +1,4 @@ -# Common classes and methods. +# 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={}) : @@ -12,12 +12,15 @@ class Food : class NoMenuError(Exception) : """ gets raised if there's no menu""" -class Frontend(object) : - def __init__(self, name, human_name, description="", optional_args=[]) : +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 = [] @@ -35,6 +38,7 @@ def formt (food) : return r foodsources = {} +renderers = {} class Restaurant(object): def __init__(self, name, human_name, module, optional_args=[], obligatory_args=()): @@ -49,4 +53,7 @@ class Restaurant(object): def register_restaurant(restaurant): global foodsources - foodsources[restaurant.name] = (restaurant) + foodsources[restaurant.name] = restaurant +def register_renderer(renderer) : + global renderers + renderers[renderer.name] = renderer diff --git a/frontends/plain-text.py b/mensa/frontends/plain-text.py index 71cbf42..501be54 100644 --- a/frontends/plain-text.py +++ b/mensa/frontends/plain-text.py @@ -1,4 +1,4 @@ -import base +from mensa import base from yapsy.IPlugin import IPlugin class TextRenderer(IPlugin) : def render (self, foods, **options) : @@ -19,4 +19,5 @@ class TextRenderer(IPlugin) : r = r+"\t "+i.desc+"\n" print(r) - + def register_renderer(self) : + base.register_renderer(base.Renderer("plain-text", "Plain Text Renderer", self)) diff --git a/frontends/plain-text.yapsy-plugin b/mensa/frontends/plain-text.yapsy-plugin index 103cdc4..103cdc4 100644 --- a/frontends/plain-text.yapsy-plugin +++ b/mensa/frontends/plain-text.yapsy-plugin diff --git a/mensa/logic.py b/mensa/logic.py new file mode 100755 index 0000000..b0165f0 --- /dev/null +++ b/mensa/logic.py @@ -0,0 +1,45 @@ +#!/usr/bin/python3 + +from mensa import base +import urllib.error +import os +from yapsy.PluginManager import PluginManager + + +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, options={}) : + foodl = [] + for k,i in base.foodsources.items() : + if restlist and not i.name in restlist : + continue + 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: + print(i.human_name + ": No menu found. This could be due to a holiday or due to an error in the script.") + except urllib.error.HTTPError as e : + print(i.human_name + ": Fetching menu failed: %s" % str(e)) + return foodl + +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) @@ -1,59 +0,0 @@ -#!/usr/bin/python3 - -import base -import urllib.error - -import argparse - -parser = argparse.ArgumentParser(description='Fetch menus from various sources') -# parser.add_argument('integers', metavar='N', type=int, nargs='+', -# help='an integer for the accumulator') -parser.add_argument('-r', '--restaurants', dest='rest', action='store', - metavar='LIST', - help='Comma-separated list of restaurants to fetch the menus from.') -parser.add_argument('-l', '--list-restaurants', dest='list', action='store_true', - help='get list of restaurants') - - -args = parser.parse_args() -## Load backends -from yapsy.PluginManager import PluginManager -backends = PluginManager() -backends.setPluginPlaces(["./backends"]) -backends.collectPlugins() -## Load frontends (not yet implemented) -from yapsy.PluginManager import PluginManager -frontends = PluginManager() -frontends.setPluginPlaces(["./frontends"]) -frontends.collectPlugins() - -for pluginInfo in backends.getAllPlugins(): - backends.activatePluginByName(pluginInfo.name) - pluginInfo.plugin_object.register_restaurants() -for pluginInfo in frontends.getAllPlugins(): - frontends.activatePluginByName(pluginInfo.name) - - -if args.list : - for k,i in base.foodsources.items(): - print(i.name, i.human_name) - exit() -restlist = None -if args.rest : - restlist = args.rest.split(",") -to_render = [] -for k,i in base.foodsources.items() : - if restlist and not i.name in restlist : - continue - try : - food = i.get_food(ignore_nudelauswahl=True) - to_render.append((i, food)) - # print("*"*20+i.human_name+"*"*20+"\n"+base.formt(food)) - except base.NoMenuError: - print(i.human_name + ": No menu found. This could be due to a holiday or due to an error in the script.") - except urllib.error.HTTPError as e : - print(i.human_name + ": Fetching menu failed: %s" % str(e)) - -for i in frontends.getAllPlugins(): - print(i.name) - i.plugin_object.render(to_render) |