From 427d7a21cdbcbde4ee0f4d6e07ce316b5609f468 Mon Sep 17 00:00:00 2001 From: Brendan Howell Date: Tue, 21 Dec 2021 16:26:08 +0100 Subject: [PATCH] first try with archive dept. --- screenless/bureau/archives/archives.py | 118 ++++++++++++++++++++++--- 1 file changed, 104 insertions(+), 14 deletions(-) diff --git a/screenless/bureau/archives/archives.py b/screenless/bureau/archives/archives.py index 765dbf1..834ca3e 100644 --- a/screenless/bureau/archives/archives.py +++ b/screenless/bureau/archives/archives.py @@ -3,6 +3,10 @@ import os from bureau import Bureau, add_command import pyudev +AUDIO_FILETYPES = (".mp3", ".ogg", ".oga", ".flac", ".spx", ".opus", ".wav", + ".aac", ".mka", ".m4a") +PICTURE_FILETYPES = (".jpg", ".jpeg", ".png", ".gif", ".tif", ".bmp") +PDF_FILETYPES = (".pdf", ".djvu", ".djv", ".ps", ".eps") class Archives(Bureau): """ @@ -18,25 +22,112 @@ class Archives(Bureau): def __init__(self): Bureau.__init__(self) - # get location of archive root storage from config + # TODO: get location of archive root storage from config + self.mounts = {} + + # create a db to map shortcodes to folders on external drives + self.dirmap = self.open_db("dirmap") + + def __scandir(self, dir_to_scan): + """ + recursive directory scan + returns a tuple of lists of directory paths if any interesting media + are found + ([dirs_with_audio], [dirs_with_pictures], [dirs_with_pdfs]) + """ + dirs_with_audio = [] + dirs_with_pictures = [] + dirs_with_pdfs = [] + for root, dirs, files in os.walk(dir_to_scan): + audio_found = False + pic_found = False + pdf_found = False + for file in files: + _, file_ext = os.path.splitext(file) + file_ext = file_ext.lower() + if file_ext in AUDIO_FILETYPES: + audio_found = True + elif file_ext in PICTURE_FILETYPES: + pic_found = True + elif file_ext in PDF_FILETYPES: + pdf_found = True + if audio_found: + dirs_with_audio.append(root) + if pic_found: + dirs_with_pictures.append(root) + if pdf_found: + dirs_with_pdfs.append(root) + return dirs_with_audio, dirs_with_pictures, dirs_with_pdfs def _mount_and_archive(self, dev): # TODO: small print options to import stuff - print("MOUNTING EXTERNAL MEDIUM: ", dev) + mountpoint = os.path.join("/media" os.path.split(dev)) + print("MOUNTING EXTERNAL MEDIUM: ", dev, "on", mountpoint) + if len(self.mounts < 26): + mount_index = chr(65 + len(self.mounts)) # pmount dev - os.system("pmount " + str(dev)) # path is always /media/device - - #os.walk() - - # walk through the tree looking for mp3 / ogg - - # walk through looking for photos - - # walk through looking for pdfs - - # pumount dev + os.system("pmount " + str(dev)) + self.mounts[mount_index] = {"dev": dev, "mount": mountpoint, "dirs": []} + prn = self._get_small_printer() + prn.textln("Achive has found external medium: ") + prn.textln(mountpoint) + prn.textln("EJECT DRIVE:") + prn.soft_barcode("code128", "ARej." + mount_index, module_width=0.16) + + audio_dirs, pic_dirs, pdf_dirs = self.__scandir(mountpoint) + + # small_print list of media/directories that we can import + # TODO: make short codes for each dir + # TODO: make command that will import the dir + for adir in audio_dirs: + print("could add audio DIR", adir) + prn.textln("Import Audio Folder:") + prn.textln(adir) + shortcode = self.dirmap.store_and_get_shortcode(adir) + prn.soft_barcode("code128", "ARia." + shortcode, module_width=0.16) + self.mounts[mount_index]["dirs"].append(shortcode) + + for pdir in pic_dirs: + print("could add picture DIR", pdir) + prn.textln("Import Image Folder:") + prn.textln(pdir) + shortcode = self.dirmap.store_and_get_shortcode(pdir) + prn.soft_barcode("code128", "ARii." + shortcode, module_width=0.16) + self.mounts[mount_index]["dirs"].append(shortcode) + + for pdir in pdf_dirs: + print("could add document DIR", pdir) + prn.textln("Import Document Folder:") + prn.textln(pdir) + shortcode = self.dirmap.store_and_get_shortcode(pdir) + prn.soft_barcode("code128", "ARip." + shortcode, module_width=0.16) + self.mounts[mount_index]["dirs"].append(shortcode) + + self._free_small_printer(prn) + + @add_command("ej", "Eject Drive") + def unmount_device(self, data): + """ + Safely eject external media like USB Flash Drives or SD Cards + """ + device_idx, _ = data.split(".") + dev = self.mounts[device_idx]["dev"] + os.system("pumount " + str(dev)) + # clean up the references + for shortcode in self.mounts[device_idx]["dirs"]: + self.dirmap.delete(shortcode) + del(mounts[device_idx]) + + def import_audio(self, data): + """ + Import audio files from external media into the + """ + shortcode, _ = data.split(".") + indir = self.dirmap.get(shortcode) + print("Importing audio:", indir) + print("NOT IMPLEMENTED YET") def run_io(self): # monitor for insert of any removable block storage devices (SD/USB) @@ -47,7 +138,6 @@ class Archives(Bureau): if ('ID_FS_TYPE' in device) and (device.action == "add"): self._mount_and_archive(device.device_node) - def main(): ar = Archives()