This repository has been archived by the owner on Nov 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 108
/
divide.py
73 lines (60 loc) · 2.51 KB
/
divide.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
# -*- coding: utf-8 -*-
# divide.py
"""
@author: Maxime Dréan.
Github: https://github.com/maximedrn
Telegram: https://t.me/maximedrn
"""
# Utils functions for GUI and CLI.
from app.utils.func import cls
from app.utils.colors import RED, YELLOW, RESET
from app.utils.const import FIRST_PAGE, ENTER, SECOND_PAGE
from app.utils.values import UPLOAD, SALE, UPLOAD_AND_SALE
# Utils functions for user choices.
from app.utils.user import perform_action, data_file
# Common functions for the metadata file.
from app.common.reader import Reader
from app.common.structure import Structure
from app.common.save import Save
# Mode and details constants.
MODE = {'1': 'upload', '2': 'sale', '1 2': 'upload_and_sale'}
DETAILS = {'1': UPLOAD, '2': SALE, '1 2': UPLOAD_AND_SALE}
def main() -> None:
"""Open the file and divide according to a number."""
# What the action the user wants to do.
action = perform_action()
action_str = ' '.join(str(element) for element in action)
file = data_file() # Metadata file to read.
reader = Reader(file)
print(f'\n{YELLOW}{reader.length_file} elements.{RESET}')
# Initialize the Structure class by sending the action.
structure = Structure(action, reader)
save = Save(structure) # Initialize the Save class.
# Divide the file.
from_, to_ = number('Start from:') - 1, number('End at:')
for nft_number in range(from_, to_):
print(f'Element n°{nft_number}.', end='\r')
if not structure.get_data(nft_number, True):
continue # Remove the error message and continue.
save.save(MODE[action_str], DETAILS[action_str], True if
nft_number != to_ - 1 else False)
def number(question: str) -> int:
"""Ask for a number and return it."""
while True:
length = input(f'{question} ')
if length.isdigit() and int(length) > 0:
return int(length)
print(f'{RED}Answer must be a strictly positive integer.{RESET}')
if __name__ == '__main__':
try:
cls() # Clear console.
print(FIRST_PAGE) # License, author and version.
input(ENTER) # Press enter to continue.
cls() # Clear console.
print(SECOND_PAGE) # License and author.
main() # Begin the process.
except KeyboardInterrupt:
print(f'\n\n{YELLOW}The program has been '
f'stopped by the user.{RESET}')
except Exception as error:
print(f'{RED}Something went wrong.{RESET} \n{error}')