source: tagsplugin/tags/0.8/tractags/compat.py

Last change on this file was 14157, checked in by Steffen Hoffmann, 9 years ago

TagsPlugin: Add a versatile Trac request mockup, refs #11945.

This is partially a rework of [14146] using partial built-in for better
maintainability.

Thanks to Ryan J Ollos for designing and proposing the new utility class.

File size: 1.5 KB
Line 
1# -*- coding: utf-8 -*-
2#
3# Copyright (c) 2013,2014 Steffen Hoffmann
4#
5# This software is licensed as described in the file COPYING, which
6# you should have received as part of this distribution.
7#
8
9"""Various classes and functions to provide backwards-compatibility with
10previous versions of Python from 2.4 and Trac from 0.11 onwards.
11"""
12
13try:
14    from functools import partial
15except ImportError:
16    # Cheap fallback for Python2.4 compatibility.
17    # See http://stackoverflow.com/questions/12274814
18    def partial(func, *args, **kwds):
19        """Emulate Python2.6's functools.partial."""
20        return lambda *fargs, **fkwds: func(*(args+fargs),
21                                            **dict(kwds, **fkwds))
22
23try:
24    from trac.util.datefmt import to_utimestamp
25    from trac.util.datefmt import to_datetime
26except ImportError:
27    # Cheap fallback for Trac 0.11 compatibility.
28    from trac.util.datefmt  import to_timestamp
29    def to_utimestamp(dt):
30        return to_timestamp(dt) * 1000000L
31
32    from trac.util.datefmt import to_datetime as to_dt
33    def to_datetime(ts):
34        return to_dt(ts / 1000000)
35
36# Compatibility code for `ComponentManager.is_enabled`
37# (available since Trac 0.12)
38def is_enabled(env, cls):
39    """Return whether the given component class is enabled.
40
41    For Trac 0.11 the missing algorithm is included as fallback.
42    """
43    try:
44        return env.is_enabled(cls)
45    except AttributeError:
46        if cls not in env.enabled:
47            env.enabled[cls] = env.is_component_enabled(cls)
48        return env.enabled[cls]
Note: See TracBrowser for help on using the repository browser.