2013-04-26 14:59:44 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
'''
|
2016-12-21 02:04:24 +01:00
|
|
|
feedgen.util
|
|
|
|
~~~~~~~~~~~~
|
2013-04-26 14:59:44 +02:00
|
|
|
|
2016-12-21 02:04:24 +01:00
|
|
|
This file contains helper functions for the feed generator module.
|
2013-04-26 14:59:44 +02:00
|
|
|
|
2016-12-21 02:04:24 +01:00
|
|
|
:copyright: 2013, Lars Kiesow <lkiesow@uos.de>
|
|
|
|
:license: FreeBSD and LGPL, see license.* for more details.
|
2013-04-26 14:59:44 +02:00
|
|
|
'''
|
2016-12-21 02:04:24 +01:00
|
|
|
import locale
|
|
|
|
import sys
|
2013-04-26 14:59:44 +02:00
|
|
|
|
|
|
|
|
2013-11-30 22:07:28 +01:00
|
|
|
def ensure_format(val, allowed, required, allowed_values=None, defaults=None):
|
2016-12-21 02:04:24 +01:00
|
|
|
'''Takes a dictionary or a list of dictionaries and check if all keys are in
|
|
|
|
the set of allowed keys, if all required keys are present and if the values
|
|
|
|
of a specific key are ok.
|
2013-04-28 21:59:56 +02:00
|
|
|
|
2016-12-21 02:04:24 +01:00
|
|
|
:param val: Dictionaries to check.
|
|
|
|
:param allowed: Set of allowed keys.
|
|
|
|
:param required: Set of required keys.
|
|
|
|
:param allowed_values: Dictionary with keys and sets of their allowed
|
|
|
|
values.
|
|
|
|
:param defaults: Dictionary with default values.
|
|
|
|
:returns: List of checked dictionaries.
|
|
|
|
'''
|
|
|
|
if not val:
|
2017-05-21 22:06:50 +02:00
|
|
|
return []
|
2016-12-21 02:04:24 +01:00
|
|
|
if allowed_values is None:
|
|
|
|
allowed_values = {}
|
|
|
|
if defaults is None:
|
|
|
|
defaults = {}
|
|
|
|
# Make shure that we have a list of dicts. Even if there is only one.
|
|
|
|
if not isinstance(val, list):
|
|
|
|
val = [val]
|
|
|
|
for elem in val:
|
|
|
|
if not isinstance(elem, dict):
|
|
|
|
raise ValueError('Invalid data (value is no dictionary)')
|
|
|
|
# Set default values
|
2014-05-07 21:22:55 +02:00
|
|
|
|
2016-12-21 02:04:24 +01:00
|
|
|
version = sys.version_info[0]
|
2014-05-07 21:22:55 +02:00
|
|
|
|
2016-12-21 02:04:24 +01:00
|
|
|
if version == 2:
|
|
|
|
items = defaults.iteritems()
|
|
|
|
else:
|
|
|
|
items = defaults.items()
|
2014-05-07 21:22:55 +02:00
|
|
|
|
2016-12-21 02:04:24 +01:00
|
|
|
for k, v in items:
|
|
|
|
elem[k] = elem.get(k, v)
|
|
|
|
if not set(elem.keys()) <= allowed:
|
|
|
|
raise ValueError('Data contains invalid keys')
|
|
|
|
if not set(elem.keys()) >= required:
|
|
|
|
raise ValueError('Data contains not all required keys')
|
2014-05-07 21:22:55 +02:00
|
|
|
|
2016-12-21 02:04:24 +01:00
|
|
|
if version == 2:
|
|
|
|
values = allowed_values.iteritems()
|
|
|
|
else:
|
|
|
|
values = allowed_values.items()
|
2014-05-07 21:22:55 +02:00
|
|
|
|
2016-12-21 02:04:24 +01:00
|
|
|
for k, v in values:
|
|
|
|
if elem.get(k) and not elem[k] in v:
|
|
|
|
raise ValueError('Invalid value for %s' % k)
|
|
|
|
return val
|
2015-01-04 00:13:57 +01:00
|
|
|
|
|
|
|
|
2018-03-04 20:10:01 +01:00
|
|
|
def formatRFC2822(date):
|
2016-12-21 02:04:24 +01:00
|
|
|
'''Make sure the locale setting do not interfere with the time format.
|
|
|
|
'''
|
2018-03-04 20:10:01 +01:00
|
|
|
old = locale.setlocale(locale.LC_ALL)
|
2016-12-21 02:04:24 +01:00
|
|
|
locale.setlocale(locale.LC_ALL, 'C')
|
2018-03-04 20:10:01 +01:00
|
|
|
date = date.strftime('%a, %d %b %Y %H:%M:%S %z')
|
|
|
|
locale.setlocale(locale.LC_ALL, old)
|
|
|
|
return date
|