• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1"""The module contains miscellaneous helpers.
2It's not considered part of the public ufoLib API.
3"""
4from __future__ import absolute_import, unicode_literals
5import sys
6import warnings
7import functools
8from datetime import datetime
9from fontTools.misc.py23 import tounicode
10
11
12if hasattr(datetime, "timestamp"):  # python >= 3.3
13
14    def datetimeAsTimestamp(dt):
15        return dt.timestamp()
16
17else:
18    from datetime import tzinfo, timedelta
19
20    ZERO = timedelta(0)
21
22    class UTC(tzinfo):
23
24        def utcoffset(self, dt):
25            return ZERO
26
27        def tzname(self, dt):
28            return "UTC"
29
30        def dst(self, dt):
31            return ZERO
32
33    utc = UTC()
34
35    EPOCH = datetime.fromtimestamp(0, tz=utc)
36
37    def datetimeAsTimestamp(dt):
38        return (dt - EPOCH).total_seconds()
39
40
41# TODO: should import from fontTools.misc.py23
42try:
43	long = long
44except NameError:
45	long = int
46
47integerTypes = (int, long)
48numberTypes = (int, float, long)
49
50
51def deprecated(msg=""):
52    """Decorator factory to mark functions as deprecated with given message.
53
54    >>> @deprecated("Enough!")
55    ... def some_function():
56    ...    "I just print 'hello world'."
57    ...    print("hello world")
58    >>> some_function()
59    hello world
60    >>> some_function.__doc__ == "I just print 'hello world'."
61    True
62    """
63
64    def deprecated_decorator(func):
65        @functools.wraps(func)
66        def wrapper(*args, **kwargs):
67            warnings.warn(
68                "{} function is a deprecated. {}".format(func.__name__, msg),
69                category=DeprecationWarning,
70                stacklevel=2,
71            )
72            return func(*args, **kwargs)
73
74        return wrapper
75
76    return deprecated_decorator
77
78
79def fsdecode(path, encoding=sys.getfilesystemencoding()):
80    return tounicode(path, encoding=encoding)
81
82
83if __name__ == "__main__":
84    import doctest
85
86    doctest.testmod()
87