1 68
from datetime import timedelta
2 68
import weakref
3 68
from collections import OrderedDict
4

5 68
from six.moves import _thread
6

7

8 68
class _TzSingleton(type):
9 68
    def __init__(cls, *args, **kwargs):
10 68
        cls.__instance = None
11 68
        super(_TzSingleton, cls).__init__(*args, **kwargs)
12

13 68
    def __call__(cls):
14 68
        if cls.__instance is None:
15 68
            cls.__instance = super(_TzSingleton, cls).__call__()
16 68
        return cls.__instance
17

18

19 68
class _TzFactory(type):
20 68
    def instance(cls, *args, **kwargs):
21
        """Alternate constructor that returns a fresh instance"""
22 68
        return type.__call__(cls, *args, **kwargs)
23

24

25 68
class _TzOffsetFactory(_TzFactory):
26 68
    def __init__(cls, *args, **kwargs):
27 68
        cls.__instances = weakref.WeakValueDictionary()
28 68
        cls.__strong_cache = OrderedDict()
29 68
        cls.__strong_cache_size = 8
30

31 68
        cls._cache_lock = _thread.allocate_lock()
32

33 68
    def __call__(cls, name, offset):
34 68
        if isinstance(offset, timedelta):
35 68
            key = (name, offset.total_seconds())
36
        else:
37 68
            key = (name, offset)
38

39 68
        instance = cls.__instances.get(key, None)
40 68
        if instance is None:
41 68
            instance = cls.__instances.setdefault(key,
42
                                                  cls.instance(name, offset))
43

44
        # This lock may not be necessary in Python 3. See GH issue #901
45 68
        with cls._cache_lock:
46 68
            cls.__strong_cache[key] = cls.__strong_cache.pop(key, instance)
47

48
            # Remove an item if the strong cache is overpopulated
49 68
            if len(cls.__strong_cache) > cls.__strong_cache_size:
50 68
                cls.__strong_cache.popitem(last=False)
51

52 68
        return instance
53

54

55 68
class _TzStrFactory(_TzFactory):
56 68
    def __init__(cls, *args, **kwargs):
57 68
        cls.__instances = weakref.WeakValueDictionary()
58 68
        cls.__strong_cache = OrderedDict()
59 68
        cls.__strong_cache_size = 8
60

61 68
        cls.__cache_lock = _thread.allocate_lock()
62

63 68
    def __call__(cls, s, posix_offset=False):
64 68
        key = (s, posix_offset)
65 68
        instance = cls.__instances.get(key, None)
66

67 68
        if instance is None:
68 68
            instance = cls.__instances.setdefault(key,
69
                cls.instance(s, posix_offset))
70

71
        # This lock may not be necessary in Python 3. See GH issue #901
72 68
        with cls.__cache_lock:
73 68
            cls.__strong_cache[key] = cls.__strong_cache.pop(key, instance)
74

75
            # Remove an item if the strong cache is overpopulated
76 68
            if len(cls.__strong_cache) > cls.__strong_cache_size:
77 68
                cls.__strong_cache.popitem(last=False)
78

79 68
        return instance
80

Read our documentation on viewing source code .

Loading