dockonsurf / modules / utilities.py @ 0dfa15ff
Historique | Voir | Annoter | Télécharger (2,81 ko)
1 |
import logging |
---|---|
2 |
logger = logging.getLogger('DockOnSurf')
|
3 |
|
4 |
|
5 |
def tail(f, lines=20): |
6 |
total_lines_wanted = lines |
7 |
|
8 |
block_size = 1024
|
9 |
f.seek(0, 2) |
10 |
block_end_byte = f.tell() |
11 |
lines_to_go = total_lines_wanted |
12 |
block_number = -1
|
13 |
blocks = [] |
14 |
while lines_to_go > 0 and block_end_byte > 0: |
15 |
if block_end_byte - block_size > 0: |
16 |
f.seek(block_number * block_size, 2)
|
17 |
blocks.append(f.read(block_size)) |
18 |
else:
|
19 |
f.seek(0, 0) |
20 |
blocks.append(f.read(block_end_byte)) |
21 |
lines_found = blocks[-1].count(b'\n') |
22 |
lines_to_go -= lines_found |
23 |
block_end_byte -= block_size |
24 |
block_number -= 1
|
25 |
all_read_text = b''.join(reversed(blocks)) |
26 |
return b'\n'.join(all_read_text.splitlines()[-total_lines_wanted:]).decode() |
27 |
|
28 |
|
29 |
def check_bak(file_name): |
30 |
"""Checks if a file already exists and backs it up if so.
|
31 |
@param file_name: file to be checked if exists
|
32 |
"""
|
33 |
import os |
34 |
new_name = file_name |
35 |
bak_num = 0
|
36 |
while os.path.isdir(new_name) or os.path.isfile(new_name): |
37 |
bak_num += 1
|
38 |
new_name = new_name.split(".bak")[0] + f".bak{bak_num}" |
39 |
if bak_num > 0: |
40 |
os.rename(file_name, new_name) |
41 |
logger.warning(f"'{file_name}' already present. Backed it up to "
|
42 |
f"{new_name}")
|
43 |
|
44 |
|
45 |
def try_command(command, expct_error_types: list, *args, **kwargs): |
46 |
"""Try to run a command and record exceptions (expected and not) on a log.
|
47 |
|
48 |
@param command: method or function, the command to be executed.
|
49 |
@param expct_error_types: list of tuples, every inner tuple is supposed to
|
50 |
contain an exception type (eg. ValueError, TypeError, etc.) to be caught and
|
51 |
a message to print in the log and on the screen explaining the exception.
|
52 |
Error types that are not allow to be called with a custom message as only
|
53 |
error argument are not supported.
|
54 |
The outer tuple encloses all couples of error types and their relative
|
55 |
messages.
|
56 |
*args and **kwargs: arguments and keyword-arguments of the command to be
|
57 |
executed.
|
58 |
When trying to run 'command' with its args and kwargs, if an exception
|
59 |
present on the 'error_types' occurs, its relative error message is recorded
|
60 |
on the log and a same type exception is raised with the custom message.
|
61 |
"""
|
62 |
unexp_error = "An unexpected error occurred"
|
63 |
|
64 |
err = False
|
65 |
try:
|
66 |
return_val = command(*args, **kwargs) |
67 |
except Exception as e: |
68 |
for expct_err in expct_error_types: |
69 |
if isinstance(e, expct_err[0]): |
70 |
logger.error(expct_err[1])
|
71 |
err = expct_err[0](expct_err[1]) |
72 |
break
|
73 |
else:
|
74 |
logger.exception(unexp_error) |
75 |
err = e |
76 |
else:
|
77 |
err = False
|
78 |
return return_val
|
79 |
finally:
|
80 |
if isinstance(err, BaseException): |
81 |
raise err
|