Passed
Push — main ( 7fb18a...baf70b )
by Jochen
04:45
created

byceps.util.templatefunctions   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Test Coverage

Coverage 78.56%

Importance

Changes 0
Metric Value
eloc 22
dl 0
loc 46
ccs 11
cts 14
cp 0.7856
rs 10
c 0
b 0
f 0
wmc 6

5 Functions

Rating   Name   Duplication   Size   Complexity  
A _format_datetime() 0 3 1
A register() 0 11 2
A _format_date() 0 2 1
A _format_number() 0 2 1
A _format_time() 0 3 1
1
"""
2
byceps.util.templatefunctions
3
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4
5
Provide and register custom template global functions.
6
7
:Copyright: 2006-2021 Jochen Kupperschmidt
8
:License: Revised BSD (see `LICENSE` file for details)
9
"""
10
11 1
import babel
12 1
from flask_babel import get_locale, get_timezone
13
14
15 1
def register(app):
16
    """Make functions available as global functions in templates."""
17 1
    functions = [
18
        (_format_date, 'format_date'),
19
        (_format_datetime, 'format_datetime'),
20
        (_format_time, 'format_time'),
21
        (_format_number, 'format_number'),
22
    ]
23
24 1
    for f, name in functions:
25 1
        app.add_template_global(f, name)
26
27
28 1
def _format_date(*args) -> str:
29
    return babel.dates.format_date(*args, locale=get_locale())
30
31
32 1
def _format_datetime(*args) -> str:
33
    return babel.dates.format_datetime(
34
        *args, locale=get_locale(), tzinfo=get_timezone()
35
    )
36
37
38 1
def _format_time(*args) -> str:
39
    return babel.dates.format_time(
40
        *args, locale=get_locale(), tzinfo=get_timezone()
41
    )
42
43
44 1
def _format_number(*args) -> str:
45
    return babel.numbers.format_number(*args, locale=get_locale())
46