-
Notifications
You must be signed in to change notification settings - Fork 0
/
disk_usage.py
93 lines (70 loc) · 3.04 KB
/
disk_usage.py
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
# -*- coding: utf-8 -*-
"""Enter point for :mod:`DiskUsage_Console`
Creates tables for output and catches possible exceptions that might occur at a runtime.
"""
from tabulate import tabulate
from DiscUsage_Console.DUCore.DUSpinner import Spinner
from DiscUsage_Console.DUCore.DUFileCrawler import FileCrawler, FileTree
from DiscUsage_Console.DUCore.DUArgParser import *
from DiscUsage_Console.DUUtilities.DUFormatTools import *
def write_to_file(path, tablefmt, tables, headers, colalign):
try:
with open(path, 'w') as out_file:
# Try to use user defined table style
try:
output_tables = tabulate(tables, headers=headers,
tablefmt=tablefmt, colalign=colalign)
out_file.write(output_tables)
except IndentationError:
output_tables = tabulate(tables, headers=headers,
tablefmt="simple", colalign=colalign)
out_file.write(output_tables)
except (IsADirectoryError, FileNotFoundError) as error:
format_print_warning(exception=error, text="File hasn't been written")
def main() -> None:
"""Main script to execute."""
# Init argument parser, threads and tables
parser = ArgParser()
args = parser.parse_args()
# Check if tree output only
if args.tree:
FileTree(path=args.search_dir).print_tree()
sys.exit(0)
tables_init = ArgParserTablesInit(args=args)
crawler = FileCrawler(path=args.search_dir, abs_path=args.absolute)
waiting_ico = Spinner()
# Catching possible errors during program execution.
# Getting file info
try:
waiting_ico.start()
files = [file for file in crawler.get_files()]
waiting_ico.stop()
for file in files:
tables_init.add_table(file)
# Check for internal tabulate function error.
# IndexError raised in case no elements in tables or invalid data passed.
try:
# Print result
# Init data for tables
tables = tables_init.tables
colalign = tables_init.colalign
headers = tables_init.headers
output_tables = tabulate(tables, headers=headers,
tablefmt="fancy_grid", colalign=colalign)
print(output_tables)
# Try to write result to file
if args.output is not None:
write_to_file(args.output[0], args.output[1], tables, headers, colalign)
except (IndexError, AttributeError) as error:
format_print_error(exception=error, text="Tabulate error")
sys.exit(4)
except (FileNotFoundError, FileExistsError) as warning:
waiting_ico.stop()
format_print_warning(exception=warning, text="Crawling has been stopped.")
sys.exit(2)
except KeyboardInterrupt as warning:
waiting_ico.stop()
format_print_warning(exception=warning, text="Crawling has been stopped.")
sys.exit(130)
if __name__ == '__main__':
main()