-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileManager.py
executable file
·110 lines (110 loc) · 3.96 KB
/
FileManager.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
import os
import sys
import pygame
import locale
import functools
from pygame.locals import *
pygame.init()
pygame.display.set_caption("Choose a file...")
screen = pygame.display.set_mode((1000, 500))
font = pygame.font.SysFont(None, 20)
class Button():
def __init__(self, text, command, left, top):
self.normal_color = (255, 255, 255)
self.current_color = self.normal_color
self.highlighted_color = (100, 100, 100)
self.clicked_color = (255, 0, 0)
self.text = text
self.highlighted = False
self.clicked = False
self.left = left
self.top = top
self.rect = font.render(self.text, True, self.current_color).get_rect()
self.rect.left = self.left
self.rect.top = self.top
self.command = command
def update(self, event):
mouse_pos = pygame.mouse.get_pos()
if event.type == MOUSEMOTION:
if self.rect.collidepoint(mouse_pos):
self.highlighted = True
else:
self.highlighted = False
elif event.type == MOUSEBUTTONDOWN:
if self.rect.collidepoint(mouse_pos):
self.clicked = True
elif event.type == MOUSEBUTTONUP:
if self.rect.collidepoint(mouse_pos):
self.clicked = False
if self.command != None:
self.command()
def draw(self):
self.current_color = self.normal_color
if self.highlighted:
self.current_color = self.highlighted_color
if self.clicked:
self.current_color = self.clicked_color
screen.blit(font.render(self.text, True, self.current_color), (self.left, self.top))
class fileManager():
def __init__(self):
self.current_dir = "/"
self.gui = []
self.create_gui()
def list_files(self):
files_folders = os.listdir(self.current_dir)
files = []
folders = []
for file_or_folder in files_folders:
full_path = os.path.join(self.current_dir, file_or_folder)
if os.path.isfile(full_path):
files.append(file_or_folder)
elif os.path.isdir(full_path):
folders.append(file_or_folder)
#for case-insensitive sorting - wouldn't work for all languages
#files = sorted(files, key=str.lower)
#folders = sorted(folders, key=str.lower)
return (sorted(files, key=functools.cmp_to_key(locale.strcoll)), sorted(folders, key=functools.cmp_to_key(locale.strcoll)))
def create_gui(self):
(files, folders) = self.list_files()
self.gui = []
counter = 45
for folder in folders:
self.gui.append(Button(folder, lambda folder = folder: self.change_directory(folder), 5, counter))
counter += 15
counter = 45
for file in files:
self.gui.append(Button(file, lambda file = file: self.open_file(file), 500, counter))
counter += 15
self.gui.append(Button("Back", self.go_back, 5, 5))
def change_directory(self, folder):
try:
os.listdir(os.path.join(self.current_dir, folder))
self.current_dir = os.path.join(self.current_dir, folder)
self.create_gui()
except:
return
def go_back(self):
try:
self.current_dir = os.path.split(self.current_dir)[0]
self.create_gui()
except:
return
def open_file(self, file):
os.system('xdg-open ' + os.path.join(self.current_dir, file))
def update(self, event):
for button in self.gui:
button.update(event)
def draw(self):
for button in self.gui:
button.draw()
fileManager = fileManager()
while True:
screen.fill((0, 0, 0))
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
else:
fileManager.update(event)
fileManager.draw()
pygame.display.update()