Fix day filter
[cds-indico.git] / indico / util / patches.py
blob9080b86fdde0a3e300682ba51bd7429a211514de
1 # This file is part of Indico.
2 # Copyright (C) 2002 - 2015 European Organization for Nuclear Research (CERN).
4 # Indico is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License as
6 # published by the Free Software Foundation; either version 3 of the
7 # License, or (at your option) any later version.
9 # Indico is distributed in the hope that it will be useful, but
10 # WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 # General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with Indico; if not, see <http://www.gnu.org/licenses/>.
17 """
18 Some "monkey patches" for old Python versions
19 """
21 from __future__ import absolute_import
23 import sys
24 import re
25 import operator as ops
28 VERSION_RE = re.compile(r'(>=|>|<|<=)?(\d)(?:\.(\d))?')
29 PATCHES = []
32 def _version_data(ver):
33 m = VERSION_RE.match(ver)
34 if m:
35 m = m.groups()
36 return m[0], int(m[1]), int(m[2])
37 else:
38 raise Exception("Wrong version specification: '{0}'".format(ver))
41 def _version_matches(my_ver, op, rule_ver):
43 return {
44 None: ops.eq,
45 '>': ops.gt,
46 '<': ops.lt,
47 '>=': ops.ge,
48 '<=': ops.le
49 }[op](my_ver, rule_ver)
52 def version(ver):
53 def wrapper(f):
54 version_data = _version_data(ver)
55 PATCHES.append((version_data, f))
56 return wrapper
59 def always(f):
60 PATCHES.append((None, f))
63 @always
64 def redis_pipeline_nonzero():
65 """
66 py-redis added a __len__ method to its BasePipeline class recently.
67 For our magic to work we need redis objects to be always considered
68 nonzero - no matter if it's a redis client or a redis pipeline.
69 """
70 try:
71 import redis.client
72 except ImportError:
73 pass
74 else:
75 redis.client.BasePipeline.__nonzero__ = lambda self: True
78 def apply_patches():
79 for version_data, func in PATCHES:
80 if version_data is not None:
81 op, maj, minor = version_data
82 if not _version_matches((maj, minor), op, sys.version_info[:2]):
83 continue
84 func()