-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompress-xtract.py
executable file
·114 lines (95 loc) · 2.86 KB
/
compress-xtract.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#!/usr/bin/env python
## By Davoud Arsalani
## https://github.com/davoudarsalani/scripts
## https://github.com/davoudarsalani/scripts/blob/master/compress-xtract.py
## https://raw.githubusercontent.com/davoudarsalani/scripts/master/compress-xtract.py
## https://davoudarsalani.ir
from getopt import getopt
from os import path
from subprocess import run
from sys import argv
from gp import Color, fzf, invalid, get_input, get_single_input, get_password, compress_tar, xtract_tar, compress_zip, xtract_zip, xtract_rar
title = path.basename(__file__).replace('.py', '')
script_args = argv[1:]
Col = Color()
def display_help() -> None: ## {{{
run('clear', shell=True)
print(
f'''{Col.heading(f'{title}')} {Col.yellow('help')}
{Col.flag('-i|--input=')}
{Col.flag('-p|--password=')}'''
)
exit()
## }}}
def getopts() -> None: ## {{{
global inpt, password
try:
duos, duos_long = getopt(script_args, 'hi:p:', ['help', 'input=', 'password='])
except Exception as exc:
invalid(f'{exc!r}')
for opt, arg in duos:
if opt in ('-h', '--help'):
display_help()
elif opt in ('-i', '--input'):
inpt = arg
elif opt in ('-p', '--password'):
password = arg
## }}}
def prompt(*args: list[str]) -> None: ## {{{
global inpt, password
for arg in args:
if arg == '-i':
try:
inpt
except:
inpt = get_input('input')
if not path.exists(f'{inpt}'):
invalid(f'{inpt} does not exist')
elif arg == '-p':
try:
password
except:
password = get_password('password ')
## }}}
getopts()
print(Col.heading(title))
main_items = ['tar', 'untar', 'zip', 'unzip', 'unrar', 'help']
main_item = fzf(main_items)
if main_item == 'tar':
prompt('-i')
compress_tar(inpt)
elif main_item == 'untar':
prompt('-i')
xtract_tar(inpt)
elif main_item == 'zip':
use_password = get_single_input('use password (y/n)?')
if use_password == 'y':
prompt('-i', '-p')
compress_zip(inpt, password)
elif use_password == 'n':
prompt('-i')
compress_zip(inpt)
else:
invalid('wrong choice')
elif main_item == 'unzip':
has_password = get_single_input('has password (y/n)?')
if has_password == 'y':
prompt('-i', '-p')
xtract_zip(inpt, password)
elif has_password == 'n':
prompt('-i')
xtract_zip(inpt)
else:
invalid('wrong choice')
elif main_item == 'unrar':
has_password = get_single_input('has password (y/n)?')
if has_password == 'y':
prompt('-i', '-p')
xtract_rar(inpt, password)
elif has_password == 'n':
prompt('-i')
xtract_rar(inpt)
else:
invalid('wrong choice')
elif main_item == 'help':
display_help()