libs.py 1.8 KB
Newer Older
1
import datetime
2
import random
3
import uuid
4
5
6
7
8
9
10
11
12
13
14
15
16

import json


class Config:
    """Read json and return objects"""
    def __init__(self, path):
        """path - path to pref.json"""
        self._path = path
        with open(self._path, "r") as f:
            js = json.loads(f.read())
            storage = js['storage']
            name_yaml_bd = js['name_yaml_bd']
17
18
19
            redis_host = js['redis_host']
            redis_port = js['redis_port']
            redis_db = js['redis_db']
20
21
22

        self.storage = storage
        self.name_yaml_bd = name_yaml_bd
23
24
25
        self.redis_host = redis_host
        self.redis_port = redis_port
        self.redis_db = redis_db
26
27
28


class LinkGenerator:
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
    """Generating link and returns a new in new_link variable
    generate_method  = "timestamp" or "uuid4" (by default) return six characters
    """

    def __init__(self, generate_method='uuid4'):
        self._generate_method = generate_method

        if self._generate_method == 'uuid4':
            self.new_link = self._uuid4()

        if self._generate_method == 'timestamp':
            self.new_link = self._timestamp()

    def _timestamp_now(self):
        """Return timestamp datetime now"""
        return str(datetime.datetime.now().timestamp())

    def _random_lower(self):
        """Return random letters lowercase"""
        return chr(random.randint(97, 122))

    def _random_upper(self):
        """Return random letters Uppercase"""
        return chr(random.randint(65, 90))

    def _timestamp(self):
        ch = str(self._random_upper() + self._random_lower())
        f = self._timestamp_now().replace('.', ch)[10:]
        t = f[:3] + f[-3:]
        return t

    def _uuid4(self):
        """Returns first six characters"""
        return str(uuid.uuid4())[:6]