aboutsummaryrefslogtreecommitdiffstats
path: root/src/epy_reader/lib.py
blob: b01032371d8f893bca819ffd5ae66b9bf26cf572 (plain) (blame)
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
from typing import Any, Optional, Tuple
from urllib.parse import urljoin, urlparse


def is_url(string: str) -> bool:
    try:
        tmp = urlparse(string)
        return all([tmp.scheme, tmp.netloc])
    except ValueError:
        return False


def coerce_to_int(string: str) -> Optional[int]:
    try:
        return int(string)
    except ValueError:
        return None


def truncate(teks: str, subtitution_text: str, maxlen: int, startsub: int = 0) -> str:
    """
    Truncate text

    eg.
    :param teks: 'This is long silly dummy text'
    :param subtitution_text:  '...'
    :param maxlen: 12
    :param startsub: 3
    :return: 'This...ly dummy text'
    """
    if startsub > maxlen:
        raise ValueError("Var startsub cannot be bigger than maxlen.")
    elif len(teks) <= maxlen:
        return teks
    else:
        lensu = len(subtitution_text)
        beg = teks[:startsub]
        mid = (
            subtitution_text
            if lensu <= maxlen - startsub
            else subtitution_text[: maxlen - startsub]
        )
        end = teks[startsub + lensu - maxlen :] if lensu < maxlen - startsub else ""
        return beg + mid + end


def tuple_subtract(tuple_one: Tuple[Any, ...], tuple_two: Tuple[Any, ...]) -> Tuple[Any, ...]:
    """
    Returns tuple with members in tuple_one
    but not in tuple_two
    """
    return tuple(i for i in tuple_one if i not in tuple_two)


def resolve_path(current_dir: str, relative_path: str) -> str:
    """
    Resolve path containing dots
    eg. '/foo/bar/book.html' + '../img.png' = '/foo/img.png'
    NOTE: '/' suffix is important to tell that current dir in 'bar'
    """
    # can also using os.path.normpath()
    # but if the image in zipfile then posix path is mandatory
    return urljoin(current_dir, relative_path)