File: timezone_compat.py

package info (click to toggle)
python-ical 12.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,776 kB
  • sloc: python: 15,157; sh: 9; makefile: 5
file content (39 lines) | stat: -rw-r--r-- 1,153 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
"""Compatibility layer for allowing extended timezones in iCalendar files."""

from collections.abc import Generator
import contextlib
import contextvars


_invalide_timezones = contextvars.ContextVar("invalid_timezones", default=False)
_extended_timezones = contextvars.ContextVar("extended_timezones", default=False)


@contextlib.contextmanager
def enable_extended_timezones() -> Generator[None]:
    """Context manager to allow extended timezones in iCalendar files."""
    token = _extended_timezones.set(True)
    try:
        yield
    finally:
        _extended_timezones.reset(token)


def is_extended_timezones_enabled() -> bool:
    """Check if extended timezones are enabled."""
    return _extended_timezones.get()


@contextlib.contextmanager
def enable_allow_invalid_timezones() -> Generator[None]:
    """Context manager to allow invalid timezones in iCalendar files."""
    token = _invalide_timezones.set(True)
    try:
        yield
    finally:
        _invalide_timezones.reset(token)


def is_allow_invalid_timezones_enabled() -> bool:
    """Check if allowing invalid timezones is enabled."""
    return _invalide_timezones.get()