mirror of
https://github.com/kastdeur/pipeband-music.git
synced 2024-12-22 16:23:31 +01:00
[bin] Update scores2json script
This commit is contained in:
parent
8c8c704069
commit
583e5a799a
1 changed files with 74 additions and 34 deletions
106
scores2json.py
106
scores2json.py
|
@ -6,9 +6,17 @@
|
||||||
|
|
||||||
#{{{ Functions
|
#{{{ Functions
|
||||||
import os
|
import os
|
||||||
|
import re
|
||||||
|
|
||||||
def walk_through_files(path, file_extension='.ly'):
|
def walk_through_files(path, file_extension='.ly', exclude_dirs=[]):
|
||||||
for (dirpath, dirnames, filenames) in os.walk(path):
|
for (dirpath, dirnames, filenames) in os.walk(path):
|
||||||
|
if os.path.basename(dirpath) in exclude_dirs:
|
||||||
|
continue
|
||||||
|
|
||||||
|
if '.ignore_scores2json' in filenames:
|
||||||
|
dirnames = []
|
||||||
|
continue
|
||||||
|
|
||||||
if file_extension:
|
if file_extension:
|
||||||
for filename in filenames:
|
for filename in filenames:
|
||||||
if filename.endswith(file_extension):
|
if filename.endswith(file_extension):
|
||||||
|
@ -21,13 +29,9 @@ def rreplace(s, old, new, occurrence):
|
||||||
return new.join(li)
|
return new.join(li)
|
||||||
|
|
||||||
|
|
||||||
def read_config(config_path):
|
def search_file_for_tuneinfo(fname, textsearches = [], keywords = []):
|
||||||
''' return a dict created from the configfile '''
|
data = {}
|
||||||
import re
|
with open(fname, 'r') as fh:
|
||||||
config = {}
|
|
||||||
|
|
||||||
textsearches = ['title', 'meter']
|
|
||||||
with open(config_path, 'r') as fh:
|
|
||||||
for line in fh:
|
for line in fh:
|
||||||
if line.startswith('\\version'):
|
if line.startswith('\\version'):
|
||||||
continue
|
continue
|
||||||
|
@ -36,50 +40,90 @@ def read_config(config_path):
|
||||||
continue
|
continue
|
||||||
|
|
||||||
for t in textsearches:
|
for t in textsearches:
|
||||||
if not t in config:
|
if not t in data:
|
||||||
result = re.search(t +' = (?P<quote>[\'\"])(.*?)(?P=quote)', line)
|
result = re.search(t +'\s*=\s*(?P<quote>[\'\"])(.*?)(?P=quote)', line)
|
||||||
if result:
|
if result:
|
||||||
config[t] = result.group(2)
|
data[t] = result.group(2)
|
||||||
|
|
||||||
for keyword in ['\\\\tempo', '\\\\time', '\\\\partial']:
|
for keyword in keywords:
|
||||||
if not keyword in config:
|
if not keyword in data:
|
||||||
result = re.search(keyword+'\s*(.*)', line)
|
result = re.search(keyword+'\s*(.*)', line)
|
||||||
|
|
||||||
if result:
|
if result:
|
||||||
config[keyword.replace('\\','')] = result.group(1).strip()
|
data[keyword.replace('\\','')] = result.group(1).strip().replace('\\', '')
|
||||||
return config
|
return data
|
||||||
|
|
||||||
|
def read_config(config_path):
|
||||||
|
''' return a dict created from the configfile '''
|
||||||
|
tuneinfo = search_file_for_tuneinfo(
|
||||||
|
config_path,
|
||||||
|
textsearches = ['title', 'meter', 'instrument'],
|
||||||
|
keywords = ['\\\\tempo', '\\\\time', '\\\\partial', '\\\\key'],
|
||||||
|
)
|
||||||
|
|
||||||
|
if 'partial' in tuneinfo:
|
||||||
|
tuneinfo['partial'] = tuneinfo['partial'].split(' ')[0]
|
||||||
|
|
||||||
|
return tuneinfo
|
||||||
|
|
||||||
|
def read_lilypond(fname):
|
||||||
|
''' return a dict from file'''
|
||||||
|
data = read_config(fname)
|
||||||
|
|
||||||
|
return data
|
||||||
|
|
||||||
def analyse_tune_ly(tune_file):
|
def analyse_tune_ly(tune_file):
|
||||||
''' return a dict created from the tune file'''
|
''' return a dict created from the tune file'''
|
||||||
data = read_lilypond(tune_file)
|
data = read_lilypond(tune_file)
|
||||||
|
|
||||||
|
if 'instrument' not in data:
|
||||||
|
data['instrument'] = os.path.splitext(os.path.basename(tune_file))[0]
|
||||||
|
elif data['instrument']:
|
||||||
|
candidate = os.path.splitext(os.path.basename(tune_file))[0]
|
||||||
|
if data['instrument'].lower() in candidate.lower():
|
||||||
|
low_candidate = candidate.lower().split(sep=data['instrument'].lower(), maxsplit=1)
|
||||||
|
if len(low_candidate) > 1:
|
||||||
|
data['instrument'] += candidate[len(low_candidate[0]) + len(data['instrument']):]
|
||||||
|
|
||||||
|
data['instrument'] = data['instrument'].lower()
|
||||||
|
|
||||||
data['path'] = tune_file
|
data['path'] = tune_file
|
||||||
|
|
||||||
return data
|
return data
|
||||||
|
|
||||||
def read_lilypond(fname):
|
|
||||||
''' return a dict from file'''
|
|
||||||
data = {}
|
|
||||||
|
|
||||||
if not os.path.exists(fname):
|
def merge_data_and_config(data={}, config={}):
|
||||||
return data
|
"""
|
||||||
|
Merge two dictionaries.
|
||||||
|
data is from the file itself, config is from the configfile
|
||||||
|
|
||||||
return data
|
replaces data['title'] = 'My \\title' with 'My '+config['title']
|
||||||
|
"""
|
||||||
|
|
||||||
def merge_data_and_config(data, config):
|
for k in config.keys():
|
||||||
if 'title' in data and '\\title' in data['title'] and 'title' in config:
|
if k in data and '\\'+k in data[k]:
|
||||||
data['title'].replace('\\title',config['title'])
|
data[k].replace('\\'+k, config[k])
|
||||||
|
|
||||||
|
# overwrite config values with data
|
||||||
newdata = { **config, **data }
|
newdata = { **config, **data }
|
||||||
|
|
||||||
return newdata
|
return newdata
|
||||||
|
|
||||||
def config_from_lypath(lypath):
|
def config_from_lypath(lypath, default_name='config.ily'):
|
||||||
''' return the configpath belonging to the lypath '''
|
''' return the configpath belonging to the lypath '''
|
||||||
li = lypath.rsplit("/", 1)[0]
|
li = lypath.rsplit("/", 1)[0]
|
||||||
return os.path.join(li,"config.ily")
|
|
||||||
|
|
||||||
def read_path(music_paths):
|
if False:
|
||||||
|
# determine config path from file
|
||||||
|
pass
|
||||||
|
else:
|
||||||
|
configpath = os.path.join(li, default_name)
|
||||||
|
return configpath
|
||||||
|
|
||||||
|
def read_path(music_paths, exclude_dirs=[]):
|
||||||
|
"""
|
||||||
|
Return a dictionary of tunes with tune info from music_paths
|
||||||
|
"""
|
||||||
tunes = {}
|
tunes = {}
|
||||||
|
|
||||||
# Music Directory to work on
|
# Music Directory to work on
|
||||||
|
@ -87,9 +131,7 @@ def read_path(music_paths):
|
||||||
music_paths = [music_paths]
|
music_paths = [music_paths]
|
||||||
|
|
||||||
for path in music_paths:
|
for path in music_paths:
|
||||||
for f in walk_through_files(path, '.ly'):
|
for f in walk_through_files(path, '.ly', exclude_dirs=exclude_dirs):
|
||||||
if os.path.basename(os.path.dirname(f)) == 'template':
|
|
||||||
continue;
|
|
||||||
|
|
||||||
data = analyse_tune_ly(f)
|
data = analyse_tune_ly(f)
|
||||||
config_path = config_from_lypath(f)
|
config_path = config_from_lypath(f)
|
||||||
|
@ -99,7 +141,6 @@ def read_path(music_paths):
|
||||||
# merge them
|
# merge them
|
||||||
data = merge_data_and_config(data, config)
|
data = merge_data_and_config(data, config)
|
||||||
|
|
||||||
data['instrument'] = os.path.splitext(os.path.basename(f))[0]
|
|
||||||
data['files'] = {}
|
data['files'] = {}
|
||||||
data['files']['lilypond'] = f.replace(path,'',1)
|
data['files']['lilypond'] = f.replace(path,'',1)
|
||||||
for ext in ['pdf', 'midi', 'preview.pdf', 'preview.png']:
|
for ext in ['pdf', 'midi', 'preview.pdf', 'preview.png']:
|
||||||
|
@ -126,7 +167,6 @@ def read_path(music_paths):
|
||||||
# ================================================================
|
# ================================================================
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
import argparse
|
import argparse
|
||||||
|
|
||||||
import json
|
import json
|
||||||
|
|
||||||
parser = argparse.ArgumentParser(description="Read a music directory into a JSON file")
|
parser = argparse.ArgumentParser(description="Read a music directory into a JSON file")
|
||||||
|
@ -134,4 +174,4 @@ if __name__ == "__main__":
|
||||||
|
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
print(json.dumps(read_path( args.music_paths )))
|
print(json.dumps(read_path( args.music_paths, exclude_dirs=['template'] ), indent=0))
|
||||||
|
|
Loading…
Reference in a new issue