Skip to content

Commit 2da7c26

Browse files
committed
improve -Implement some mini personal package: empry linux trash .
1 parent a94ff5a commit 2da7c26

File tree

2 files changed

+88
-0
lines changed

2 files changed

+88
-0
lines changed

problems/empty_linux_trash.py

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
"""
2+
for work with this mini personal package:
3+
1- install tree package: sudo apt install tree
4+
2- install all the requirements packages: pip install -r requirements.txt
5+
3- run the file: python3 empry_linux_trash.py
6+
"""
7+
8+
9+
import os
10+
import pathlib
11+
import subprocess as sp
12+
import shutil
13+
import cowsay
14+
import time
15+
from rich import pretty, panel, console, style
16+
17+
pretty.install()
18+
19+
CONSOLE = console.Console()
20+
YELLOW_STYLE = style.Style(color="yellow", bold=True)
21+
YELLOW_BLINK_STYLE = style.Style(color="yellow", blink=True, bold=True)
22+
DANGER_STYLE = style.Style(color="red", bold=True)
23+
24+
ADDRESS = '/home/{}/.local/share/Trash/files'
25+
26+
files_list = lambda address: [f for f in pathlib.Path(address).iterdir() if f.is_file()]
27+
directories_list = lambda address: [d for d in pathlib.Path(address).iterdir() if d.is_dir()]
28+
29+
def list_trash(username: str) -> str:
30+
address = ADDRESS.format(username)
31+
ls_address = os.listdir(address)
32+
files = files_list(address)
33+
directories = directories_list(address)
34+
if ls_address != []:
35+
for directory in directories:
36+
directory_name = str(directory).split('/')[-1]
37+
CONSOLE.print('Directory {} tree:'.format(directory_name), style=YELLOW_BLINK_STYLE)
38+
sp.run(['tree', directory])
39+
CONSOLE.print("\nDirectory name: {}".format(directory_name), style=YELLOW_STYLE)
40+
for file in files:
41+
file_name = str(file).split('/')[-1]
42+
CONSOLE.print('File name: {}'.format(file_name), style=YELLOW_STYLE)
43+
return 'Select empty trash processor to empty your trash...'
44+
else:
45+
return cowsay.cow('The trash is already empty...')
46+
47+
def empty_trash(username: str) -> str:
48+
address = ADDRESS.format(username)
49+
files = files_list(address)
50+
directories = directories_list(address)
51+
for file in files:
52+
os.remove(file)
53+
filename = str(file).split('/')
54+
CONSOLE.print('Removed file {}'.format(filename[-1]), style=DANGER_STYLE)
55+
time.sleep(3)
56+
for directory in directories:
57+
shutil.rmtree(directory)
58+
directory_name = str(directory).split('/')
59+
CONSOLE.print('Removed directory {}'.format(directory_name[-1]), style=DANGER_STYLE)
60+
time.sleep(3)
61+
return CONSOLE.print('Trash is empty', style=YELLOW_BLINK_STYLE)
62+
63+
def restore_trash(username: str, new_address: str) -> str:
64+
address = ADDRESS.format(username)
65+
files = files_list(address)
66+
directories = directories_list(address)
67+
for file in files:
68+
file_name = str(file).split('/')[-1]
69+
sp.run(['mv', file, new_address])
70+
CONSOLE.print('Restore file {0} to the {1} address'.format(file_name, new_address+file_name), style=YELLOW_STYLE)
71+
for directory in directories:
72+
directory_name = str(directory).split('/')[-1]
73+
sp.run(['mv', directory, new_address])
74+
CONSOLE.print('Restore directory {0} to the {1} address'.format(directory_name, new_address+directory_name), style=YELLOW_STYLE)
75+
return cowsay.cow('Files restored...')
76+
77+
process = input('Wich process?\n1- list trash\n2- empty trash\n3- restore trash\n:')
78+
if process == '1' or process == 'list trash':
79+
print(list_trash('richie'))
80+
elif process == '2' or process == 'empty trash':
81+
print(empty_trash('richie'))
82+
elif process == '3' or process == 'restore trash':
83+
print(restore_trash(username='richie', new_address='/home/richie/Desktop/'))

requirements.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1+
cowsay==6.1
12
iniconfig==2.0.0
3+
markdown-it-py==3.0.0
4+
mdurl==0.1.2
25
packaging==24.1
36
pluggy==1.5.0
7+
Pygments==2.18.0
48
pytest==8.3.2
9+
rich==13.9.4

0 commit comments

Comments
 (0)