From 9564f214b5f08c5c77696b9de9dc10c1624c3b78 Mon Sep 17 00:00:00 2001 From: Marco van Dijk Date: Thu, 8 Jul 2021 01:03:53 +0200 Subject: [PATCH] fixes #4 Input files now get listed based on a list of input paths (to either folders or files) Also check subdirectories up to a set max recursion --- config.ini | 1 + lib/config.py | 6 ++++-- lib/initSongs.py | 45 +++++++++++++++++++++++++++++---------------- 3 files changed, 34 insertions(+), 18 deletions(-) diff --git a/config.ini b/config.ini index 2fcf73b..ce1926e 100644 --- a/config.ini +++ b/config.ini @@ -1,6 +1,7 @@ [input] inputfolders = /mnt/koios/Band/1-sugmesties,/mnt/koios/Band/2-oefenen,/mnt/koios/Band/3-uitgewerkt supportedextensions = .txt +maxdepth = 3 [output] metafontfamily = fonts/CourierPrime-Regular.ttf diff --git a/lib/config.py b/lib/config.py index e81c1b9..e5e079c 100644 --- a/lib/config.py +++ b/lib/config.py @@ -28,8 +28,10 @@ def initConfig(): config.read('config.ini') # Else load defaults else: - config['input'] = {'inputFolders': "/mnt/koios/Band/1-sugmesties,/mnt/koios/Band/2-oefenen,/mnt/koios/Band/3-uitgewerkt", - 'supportedExtensions': ".txt"} + config['input'] = {'inputFolders': os.getcwd(), + 'supportedExtensions': ".txt", + 'maxDepth': 3 + } config['output'] = {'metafontfamily': 'fonts/CourierPrime-Regular.ttf', 'metaFontWeight': 8, 'lyricfontfamily': 'fonts/CourierPrime-Regular.ttf', diff --git a/lib/initSongs.py b/lib/initSongs.py index 29d9701..3c3b3f5 100644 --- a/lib/initSongs.py +++ b/lib/initSongs.py @@ -12,11 +12,7 @@ # - # # @section todo TODO -# - Set a max recursion depth on the os.walk function -# - Support both paths to folders (like now) and to files directly -# When the input is a file, check if it is .txt and init it -# - Input locations should be set in a config file (init to CWD, overwrite by CMD arguments) -# - Use config supportedExtensions +# - import lib.dataStructures import lib.config @@ -37,6 +33,26 @@ def initSong(filePath): thisSong.title = thisSong.outputLocation[filePath.rfind('/')+1:] #print("Finished init for input file '{}'.\nBase output folder is '{}'\nSong title is '{}'\n".format(thisSong.inputFile, thisSong.outputLocation, thisSong.title)) return thisSong + +"""!@brief Creates a list of files found in a directory and its subdirectories + @param root path to the root. If it is a file it returns itself + if it is a folder it returns its contents + @param depth max recursion depth, defaults to 2 + @return list of paths to files +""" +def walkDirectory(root, depth): + pathList = [] + #print("Walking directory '{}'".format(root)) + def do_scan(start_dir,output,depth=2): + for f in os.listdir(start_dir): + ff = os.path.join(start_dir,f) + if os.path.isdir(ff): + if depth>= 0: + do_scan(ff,output,depth-1) + else: + output.append(ff) + do_scan(root,pathList,depth) + return pathList """!@brief Returns the list of all Song objects created This function gets all supported input files in the specified input location(s) @@ -47,22 +63,19 @@ def initSong(filePath): def getSongObjects(): # Get config variables configObj = lib.config.config['input'] + recursionDepth = int(configObj['maxDepth']) # path to song folders, which MAY contain a .txt source file txtFileLocations = [] # list of Song objects songList = [] - # go through all input locations. find .txt files. + # get all files we can find, then filter on supported extensions for inputFolder in configObj['inputfolders'].split(','): - #print("Walking directory '{}'".format(inputFolder)) - for root, dirs, files in os.walk(inputFolder): - for name in files: - if(name[name.rfind('.'):] in configObj['supportedextensions']): - filePath = os.path.join(root, name) - #print("Found .txt file '{}'".format(filePath)) - txtFileLocations.append(filePath) - #else: - #print("Skipping file '{}' for it is not a .txt file".format(name)) - + for filePath in walkDirectory(inputFolder, recursionDepth): + if(filePath[filePath.rfind('.'):] in configObj['supportedextensions']): + #print("Found .txt file '{}'".format(filePath)) + txtFileLocations.append(filePath) + #else: + #print("Skipping file '{}' for it is not a .txt file".format(filePath)) # create list of Song objects while(txtFileLocations): filePath = txtFileLocations.pop()