print: less os dependent path handling

This commit is contained in:
alfrix 2022-12-01 08:51:15 -03:00
parent 415535eeb8
commit 91b505bfef

View File

@ -55,7 +55,7 @@ class PrintPanel(ScreenPanel):
pbox = Gtk.Box(spacing=0)
pbox.set_hexpand(True)
pbox.set_vexpand(False)
self.labels['path'] = Gtk.Label(" /")
self.labels['path'] = Gtk.Label()
pbox.add(self.labels['path'])
self.labels['path_box'] = pbox
@ -78,7 +78,7 @@ class PrintPanel(ScreenPanel):
self.change_dir(None, "gcodes")
def add_directory(self, directory, show=True):
parent_dir = '/'.join(directory.split('/')[:-1])
parent_dir = os.path.dirname(directory)
if directory not in self.filelist:
self.filelist[directory] = {'directories': [], 'files': [], 'modified': 0}
self.filelist[parent_dir]['directories'].append(directory)
@ -102,14 +102,14 @@ class PrintPanel(ScreenPanel):
fileinfo = self._screen.files.get_file_info(filepath)
if fileinfo is None:
return
filename = os.path.split(filepath)[-1]
filename = os.path.basename(filepath)
if filename.startswith("."):
return
directory = os.path.dirname(os.path.join("gcodes", filepath))
d = directory.split('/')
d = directory.split(os.sep)
for i in range(1, len(d)):
curdir = "/".join(d[:i])
newdir = "/".join(d[:i + 1])
curdir = os.path.join(*d[:i])
newdir = os.path.join(*d[:i + 1])
if newdir not in self.filelist[curdir]['directories']:
if d[i].startswith("."):
return
@ -117,7 +117,7 @@ class PrintPanel(ScreenPanel):
if filename not in self.filelist[directory]['files']:
for i in range(1, len(d)):
curdir = "/".join(d[:i + 1])
curdir = os.path.join(*d[:i + 1])
if curdir != "gcodes" and fileinfo['modified'] > self.filelist[curdir]['modified']:
self.filelist[curdir]['modified'] = fileinfo['modified']
self.labels['directories'][curdir]['info'].set_markup(
@ -221,8 +221,8 @@ class PrintPanel(ScreenPanel):
)
def back(self):
if len(self.cur_directory.split('/')) > 1:
self.change_dir(None, '/'.join(self.cur_directory.split('/')[:-1]))
if os.path.dirname(self.cur_directory):
self.change_dir(None, os.path.dirname(self.cur_directory))
return True
return False
@ -234,7 +234,7 @@ class PrintPanel(ScreenPanel):
for child in self.scroll.get_children():
self.scroll.remove(child)
self.cur_directory = directory
self.labels['path'].set_text(f" /{self.cur_directory[7:]}")
self.labels['path'].set_text(f" {self.cur_directory[7:]}")
self.scroll.add(self.dir_panels[directory])
self.content.show_all()
@ -295,27 +295,27 @@ class PrintPanel(ScreenPanel):
self._screen._ws.klippy.print_start(filename)
def delete_file(self, filename):
dir_parts = f"gcodes/{filename}".split('/')[:-1]
directory = '/'.join(dir_parts)
if directory not in self.filelist or filename.split('/')[-1].startswith("."):
directory = os.path.join("gcodes", os.path.dirname(filename)) if os.path.dirname(filename) else "gcodes"
if directory not in self.filelist or os.path.basename(filename).startswith("."):
return
self.filelist[directory]["files"].pop(self.filelist[directory]["files"].index(filename.split('/')[-1]))
self.filelist[directory]["files"].pop(self.filelist[directory]["files"].index(os.path.basename(filename)))
dir_parts = directory.split(os.sep)
i = len(dir_parts)
while i > 1:
cur_dir = '/'.join(dir_parts[:i])
cur_dir = os.path.join(*dir_parts[:i])
if len(self.filelist[cur_dir]['directories']) > 0 or len(self.filelist[cur_dir]['files']) > 0:
break
par_dir = '/'.join(cur_dir.split('/')[:-1])
parent_dir = os.path.dirname(cur_dir)
if self.cur_directory == cur_dir:
self.change_dir(None, par_dir)
self.change_dir(None, parent_dir)
del self.filelist[cur_dir]
self.filelist[par_dir]['directories'].pop(self.filelist[par_dir]['directories'].index(cur_dir))
self.dir_panels[par_dir].remove(self.directories[cur_dir])
self.filelist[parent_dir]['directories'].pop(self.filelist[parent_dir]['directories'].index(cur_dir))
self.dir_panels[parent_dir].remove(self.directories[cur_dir])
del self.directories[cur_dir]
del self.labels['directories'][cur_dir]
self.dir_panels[par_dir].show_all()
self.dir_panels[parent_dir].show_all()
i -= 1
self.dir_panels[directory].remove(self.files[filename])