| 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 |
|---|
| 10 | previous versions of Python from 2.4 and Trac from 0.11 onwards. |
|---|
| 11 | """ |
|---|
| 12 | |
|---|
| 13 | try: |
|---|
| 14 | from functools import partial |
|---|
| 15 | except 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 | |
|---|
| 23 | try: |
|---|
| 24 | from trac.util.datefmt import to_utimestamp |
|---|
| 25 | from trac.util.datefmt import to_datetime |
|---|
| 26 | except 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) |
|---|
| 38 | def 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] |
|---|