summaryrefslogtreecommitdiffstats
path: root/litecli/packages/special/utils.py
blob: 4d3ad91fba7add8216504ab762da0af92f7bfc66 (plain)
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import os
import subprocess


def handle_cd_command(arg):
    """Handles a `cd` shell command by calling python's os.chdir."""
    CD_CMD = "cd"
    tokens = arg.split(CD_CMD + " ")
    directory = tokens[-1] if len(tokens) > 1 else None
    if not directory:
        return False, "No folder name was provided."
    try:
        os.chdir(directory)
        subprocess.call(["pwd"])
        return True, None
    except OSError as e:
        return False, e.strerror


def format_uptime(uptime_in_seconds):
    """Format number of seconds into human-readable string.

    :param uptime_in_seconds: The server uptime in seconds.
    :returns: A human-readable string representing the uptime.

    >>> uptime = format_uptime('56892')
    >>> print(uptime)
    15 hours 48 min 12 sec
    """

    m, s = divmod(int(uptime_in_seconds), 60)
    h, m = divmod(m, 60)
    d, h = divmod(h, 24)

    uptime_values = []

    for value, unit in ((d, "days"), (h, "hours"), (m, "min"), (s, "sec")):
        if value == 0 and not uptime_values:
            # Don't include a value/unit if the unit isn't applicable to
            # the uptime. E.g. don't do 0 days 0 hours 1 min 30 sec.
            continue
        elif value == 1 and unit.endswith("s"):
            # Remove the "s" if the unit is singular.
            unit = unit[:-1]
        uptime_values.append("{0} {1}".format(value, unit))

    uptime = " ".join(uptime_values)
    return uptime


def check_if_sqlitedotcommand(command):
    """Does a check if the command supplied is in the list of SQLite dot commands.

    :param command: A command (str) supplied from the user
    :returns: True/False
    """

    sqlite3dotcommands = [
        ".archive",
        ".auth",
        ".backup",
        ".bail",
        ".binary",
        ".cd",
        ".changes",
        ".check",
        ".clone",
        ".connection",
        ".databases",
        ".dbconfig",
        ".dbinfo",
        ".dump",
        ".echo",
        ".eqp",
        ".excel",
        ".exit",
        ".expert",
        ".explain",
        ".filectrl",
        ".fullschema",
        ".headers",
        ".help",
        ".import",
        ".imposter",
        ".indexes",
        ".limit",
        ".lint",
        ".load",
        ".log",
        ".mode",
        ".nonce",
        ".nullvalue",
        ".once",
        ".open",
        ".output",
        ".parameter",
        ".print",
        ".progress",
        ".prompt",
        ".quit",
        ".read",
        ".recover",
        ".restore",
        ".save",
        ".scanstats",
        ".schema",
        ".selftest",
        ".separator",
        ".session",
        ".sha3sum",
        ".shell",
        ".show",
        ".stats",
        ".system",
        ".tables",
        ".testcase",
        ".testctrl",
        ".timeout",
        ".timer",
        ".trace",
        ".vfsinfo",
        ".vfslist",
        ".vfsname",
        ".width",
    ]

    if isinstance(command, str):
        command = command.split(" ", 1)[0].lower()
        return command in sqlite3dotcommands
    else:
        return False