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
This commit is contained in:
Marco van Dijk 2021-07-08 01:03:53 +02:00
parent e6a0e920af
commit 9564f214b5
3 changed files with 34 additions and 18 deletions

View File

@ -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

View File

@ -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',

View File

@ -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
@ -38,6 +34,26 @@ def initSong(filePath):
#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)
For each of these files it creates a Song object, ready to be read and then parsed
@ -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)
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(name))
#print("Skipping file '{}' for it is not a .txt file".format(filePath))
# create list of Song objects
while(txtFileLocations):
filePath = txtFileLocations.pop()