File size: 1,812 Bytes
2fe4e9d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
64
65
66
67
"""
utility functions for codedog
"""
import hashlib
import logging
import time
from logging.config import dictConfig

# -- Logging ------------------------------------------------------------------


def init_local_logging(level=logging.INFO):
    """setup logging interface for local debugging"""
    dictConfig(get_logging_config(level))


def get_logging_config(level=logging.INFO):
    return {
        "version": 1,
        "disable_existing_loggers": False,
        "formatters": {
            "plain": {
                "format": "[%(asctime)s][%(levelname)s][%(pathname)s:%(lineno)d][%(name)s]%(message)s",
                "datefmt": "%Y-%m-%d %H:%M:%S",
            },
        },
        "handlers": {
            "console_handler": {
                "level": "DEBUG",
                "formatter": "plain",
                "class": "logging.StreamHandler",
                "stream": "ext://sys.stdout",  # Default is stderr
            },
        },
        "loggers": {
            "": {
                "level": level,
                "handlers": ["console_handler"],
            },
        },
    }


# -- exception ----------------------------------------------------------------


class CodedogError(Exception):
    def __init__(self, message: str = None, code: int = -1):
        self.message = "" if not message else str(message)
        self.code = code


# -- utility ------------------------------------------------------------------


def get_ttl_hash(seconds=3600):
    """Return the same value withing `seconds` time period"""
    return round(time.time() / seconds)


def get_sha256(text: str) -> str:
    return hashlib.sha256(text.encode("utf-8", errors="ignore")).hexdigest()


def get_sha1(text: str) -> str:
    return hashlib.sha1(text.encode("utf-8", errors="ignore")).hexdigest()