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] [input]
inputfolders = /mnt/koios/Band/1-sugmesties,/mnt/koios/Band/2-oefenen,/mnt/koios/Band/3-uitgewerkt inputfolders = /mnt/koios/Band/1-sugmesties,/mnt/koios/Band/2-oefenen,/mnt/koios/Band/3-uitgewerkt
supportedextensions = .txt supportedextensions = .txt
maxdepth = 3
[output] [output]
metafontfamily = fonts/CourierPrime-Regular.ttf metafontfamily = fonts/CourierPrime-Regular.ttf

View File

@ -28,8 +28,10 @@ def initConfig():
config.read('config.ini') config.read('config.ini')
# Else load defaults # Else load defaults
else: else:
config['input'] = {'inputFolders': "/mnt/koios/Band/1-sugmesties,/mnt/koios/Band/2-oefenen,/mnt/koios/Band/3-uitgewerkt", config['input'] = {'inputFolders': os.getcwd(),
'supportedExtensions': ".txt"} 'supportedExtensions': ".txt",
'maxDepth': 3
}
config['output'] = {'metafontfamily': 'fonts/CourierPrime-Regular.ttf', config['output'] = {'metafontfamily': 'fonts/CourierPrime-Regular.ttf',
'metaFontWeight': 8, 'metaFontWeight': 8,
'lyricfontfamily': 'fonts/CourierPrime-Regular.ttf', 'lyricfontfamily': 'fonts/CourierPrime-Regular.ttf',

View File

@ -12,11 +12,7 @@
# - # -
# #
# @section todo TODO # @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.dataStructures
import lib.config 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)) #print("Finished init for input file '{}'.\nBase output folder is '{}'\nSong title is '{}'\n".format(thisSong.inputFile, thisSong.outputLocation, thisSong.title))
return thisSong 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 """!@brief Returns the list of all Song objects created
This function gets all supported input files in the specified input location(s) 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 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(): def getSongObjects():
# Get config variables # Get config variables
configObj = lib.config.config['input'] configObj = lib.config.config['input']
recursionDepth = int(configObj['maxDepth'])
# path to song folders, which MAY contain a .txt source file # path to song folders, which MAY contain a .txt source file
txtFileLocations = [] txtFileLocations = []
# list of Song objects # list of Song objects
songList = [] 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(','): for inputFolder in configObj['inputfolders'].split(','):
#print("Walking directory '{}'".format(inputFolder)) for filePath in walkDirectory(inputFolder, recursionDepth):
for root, dirs, files in os.walk(inputFolder): if(filePath[filePath.rfind('.'):] in configObj['supportedextensions']):
for name in files: #print("Found .txt file '{}'".format(filePath))
if(name[name.rfind('.'):] in configObj['supportedextensions']): txtFileLocations.append(filePath)
filePath = os.path.join(root, name) #else:
#print("Found .txt file '{}'".format(filePath)) #print("Skipping file '{}' for it is not a .txt file".format(filePath))
txtFileLocations.append(filePath)
#else:
#print("Skipping file '{}' for it is not a .txt file".format(name))
# create list of Song objects # create list of Song objects
while(txtFileLocations): while(txtFileLocations):
filePath = txtFileLocations.pop() filePath = txtFileLocations.pop()