2.3. Feed API

2.3.1. Base Canteen Feed Builder

class pyopenmensa.feed.BaseBuilder(version=None)

This class represents and stores all information about OpenMensa canteens. It helps writing new python parsers with helper and shortcuts methods. So the complete object can be converted to a valid OpenMensa v2 xml feed string.

static _handleDate(date)

Internal method that is used to handle/convert input date. It raises a ValueError if the type is no datetime.date. This method should be overwritten in subclasses to support other date input types.

Parameters:date – input to be handled/converted
Return type:datetime.date
addMeal(date, category, name, notes=None, prices=None)

This is the main helper, it adds a meal to the canteen. The following data are needed:

Parameters:
  • date (datetime.date) – Date for the meal
  • category (str) – Name of the meal category
  • meal (str) – Meal name.
Raises:
  • ValueError – if the meal name is empty or longer that 250 characters
  • ValueError – if the price role is unknown
  • ValueError – if the category name is empty
  • ValueError – if note list contains empty note
  • TypeError – if the price value is not an integer

Additional the following data are also supported:

Parameters:
  • notes (list) – List of notes
  • prices (dict) – Price of the meal; Every key must be a string for the role of the persons who can use this tariff; The value is the price in Euro Cents, The site of the OpenMensa project offers more detailed information.
clearDay(date)

Remove all stored information about this date (meals or closed information).

Parameters:date (datetime.date) – Date of the day
dayCount()

Return the number of dates for which information are stored.

Return type:int
hasMealsFor(date)

Checks whether for this day are information stored.

Parameters:date (datetime.date) – Date of the day
Return type:bool
setDayClosed(date)

Define that the canteen is closed on this date. If a day is closed, all stored meals for this day will be removed.

Parameters:date (datetime.date) – Date of the day
toTag(output)

This methods adds all data of this canteen as canteen xml tag to the given xml Document.

toXMLFeed() uses this method to create the XML Feed. So there is normally no need to call it directly.

Parameters:output (xml.dom.DOMImplementation.createDocument) – XML Document to which the data should be added
toXMLFeed()

Convert this cateen information into string which is a valid OpenMensa v2 xml feed

Return type:str

2.3.2. Lazy Canteen Feed Builder

class pyopenmensa.feed.LazyBuilder(*args, **kwargs)

An extended builder class which uses a set of helper and auto-converting functions to reduce common converting tasks

Variables:extra_regex – None: regex to be passed to extractNotes, Use None to use default regex provided by this module.
addMeal(date, category, name, notes=None, prices=None, roles=None)

Same as BaseBuilder.addMeal() but uses helper functions to convert input parameters into needed types. Meals names are auto-shortend to the allowed 250 characters. The following paramer is new:

Parameters:roles – Is passed as role parameter to buildPrices()
setAdditionalCharges(default, additional)

This is a helper function, which fast up the calculation of prices. It is useable if the canteen has fixed additional charges for special roles.

Parameters:
  • default (str) – specifies for which price role the price of addMeal are.
  • additional (dict) – Defines the extra costs (value) for other roles (key).
setLegendData(*args, **kwargs)

Set or genernate the legend data from this canteen. Uses buildLegend() for genernating

2.3.2.1. Dates

pyopenmensa.feed.extractDate(text)

Tries to extract a date from a given str.

Parameters:text (str) – Input date. A datetime.date object is passed thought without modification.
Return type:datetime.date

2.3.2.2. Prices

pyopenmensa.feed.convertPrice(variant, regex=None, short_regex=None, none_regex=<_sre.SRE_Pattern object>)

Helper function to convert the given input price into integers (cents count). int, float and str are supported

Parameters:
  • variant – Price
  • regex (re.compile) – Regex to convert str into price. The re should contain two named groups euro and cent
  • short_regex (re.compile) – Short regex version (no cent part) group euro should contain a valid integer.
  • none_regex (re.compile) – Regex to detect that no value is provided if the input data is str, the normal regex do not match and this regex matches None is returned.
Return type:

int/None

pyopenmensa.feed.buildPrices(data, roles=None, regex=<_sre.SRE_Pattern object>, default=None, additional={})

Create a dictionary with price information. Multiple ways are supported.

Return type:dict: keys are role as str, values are the prices as cent count
pyopenmensa.feed.default_price_regex = <_sre.SRE_Pattern object>

Compiled regular expression objects

2.3.2.3. Notes and Legends

pyopenmensa.feed.buildLegend(legend=None, text=None, regex=None, key=<function <lambda>>)

Helper method to build or extend a legend from a text. The given regex will be used to find legend inside the text.

Parameters:
  • legend (dict) – Initial legend data
  • text (str) – Text from which should legend information extracted. None means do no extraction.
  • regex (str) – Regex to find legend part inside the given text. The regex should have a named group name (key) and a named group value (value).
  • key (callable) – function to map the key to a legend key
Return type:

dict

pyopenmensa.feed.default_legend_regex = '(?P<name>(\\d|[a-z])+)\\)\\s*(?P<value>\\w+((\\s+\\w+)*[^0-9)]))'

str(object=’‘) -> string

Return a nice string representation of the object. If the argument is a string, the return value is the same object.

pyopenmensa.feed.extractNotes(name, notes, legend=None, regex=None, key=<function <lambda>>)

This functions uses legend data to extract e.g. (1) references in a meal name and add these in full text to the notes.

Parameters:
  • name (str) – The meal name
  • notes (list) – The initial list of notes for this meal
  • legend (dict) – The legend data. Use None to skip extraction. The key is searched inside the meal name (with the given regex) and if found the value is added to the notes list.
  • regex (re.compile) – The regex to find legend references in the meal name. The regex must have exactly one group which identifies the key in the legend data. If you pass None the default_extra_regex is used. Only compiled regex are supported.
  • key (callable) – function to map the key to a legend key
Return type:

tuple with name and notes

pyopenmensa.feed.default_extra_regex = <_sre.SRE_Pattern object>

Compiled regular expression objects