VocalEasel/mma/MMA/auto.py

292 lines
7.9 KiB
Python
Raw Normal View History

2006-11-10 08:07:56 +00:00
# auto.py
"""
This module is an integeral part of the program
MMA - Musical Midi Accompaniment.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
2007-04-29 06:47:40 +00:00
Bob van der Poel <bob@mellowood.ca>
2006-11-10 08:07:56 +00:00
"""
import os
import sys
import pickle
import MMA.midi
import MMA.parse
2011-07-26 22:49:39 +00:00
import MMA.grooves
import MMA.swing
2009-05-17 22:34:44 +00:00
import gbl
2006-11-10 08:07:56 +00:00
from MMA.common import *
2009-05-17 22:34:44 +00:00
2011-07-26 22:49:39 +00:00
grooveDB = [] # when filled in it becomes [['dir', dict-db], ..]
2007-04-29 06:47:40 +00:00
mmadir = ".mmaDB" # constant, name of the lib database file
2006-11-10 08:07:56 +00:00
fileCount = 0
grooveCount = 0
gdDate = None
processedFiles = []
2011-07-26 22:49:39 +00:00
mkGrooveList = [] # a list of grooves defined in current file
2006-11-10 08:07:56 +00:00
def updateGrooveList(n):
2007-04-29 06:47:40 +00:00
""" Called from parse when new grooves are defined in a -g. """
2006-11-10 08:07:56 +00:00
2007-04-29 06:47:40 +00:00
global mkGrooveList
2006-11-10 08:07:56 +00:00
2007-04-29 06:47:40 +00:00
mkGrooveList.append(n)
2006-11-10 08:07:56 +00:00
def libUpdate():
2007-04-29 06:47:40 +00:00
""" Update the mma library database file(s) with -g or -G option.
2006-11-10 08:07:56 +00:00
2009-05-17 22:34:44 +00:00
This is called from the main program after the initialization
and other option parsing. No RETURN.
2007-04-29 06:47:40 +00:00
"""
2006-11-10 08:07:56 +00:00
2011-07-26 22:49:39 +00:00
global fileCount, gdDate, grooveDB, processedfiles
dupMessage = []
2006-11-10 08:07:56 +00:00
2007-04-29 06:47:40 +00:00
print "Creating MMA groove directory database(s). Standby..."
2006-11-10 08:07:56 +00:00
2011-07-26 22:49:39 +00:00
""" gbl.libPath points to one main directory tree. We create a separate
.mmaDB file for each directory found in the main tree. IE, if we have
the directories stdlib and bvstuff we end up with stdlib/.mmaDB and
2007-04-29 06:47:40 +00:00
bvstuff/.mmaDB.
"""
2006-11-10 08:07:56 +00:00
2011-07-26 22:49:39 +00:00
for dir in os.listdir(gbl.libPath):
libpath = os.path.join(gbl.libPath, dir)
2006-11-10 08:07:56 +00:00
2007-04-29 06:47:40 +00:00
if not os.path.isdir(libpath): # skip files, just process directories
continue
2006-11-10 08:07:56 +00:00
2007-04-29 06:47:40 +00:00
gdDate = None
2011-07-26 22:49:39 +00:00
grooveDB = [[dir, {}]]
# load up our database with this directory's DB file, skip if -G
2006-11-10 08:07:56 +00:00
2007-04-29 06:47:40 +00:00
if gbl.makeGrvDefs == 1:
2011-07-26 22:49:39 +00:00
g=loadDB(dir)
if g:
grooveDB=[[dir, g]]
gdDate = os.path.getmtime(os.path.join(gbl.libPath, dir, mmadir))
dolibupdate(libpath, '') # update all files in this dir
2006-11-10 08:07:56 +00:00
2007-04-29 06:47:40 +00:00
# Strip out defs of deleted (not found) files.
2006-11-10 08:07:56 +00:00
2011-07-26 22:49:39 +00:00
db = grooveDB[0][1]
for f in db.keys():
2007-04-29 06:47:40 +00:00
if f not in processedFiles:
print " Deleting: %s" % f
2011-07-26 22:49:39 +00:00
del g[f]
2006-11-10 08:07:56 +00:00
2007-04-29 06:47:40 +00:00
try:
outpath = file(os.path.join(libpath, mmadir), 'wb')
except:
2011-07-26 22:49:39 +00:00
error("Error creating lib-database file '%s'. " \
"Do you need to be root?" % libpath)
2006-11-10 08:07:56 +00:00
2007-04-29 06:47:40 +00:00
outpath.write("### mmaDB ... AUTOGENERATED BINARY DATA. "
"DO NOT EDIT!!!\n")
2011-07-26 22:49:39 +00:00
pickle.dump(db, outpath, pickle.HIGHEST_PROTOCOL )
2007-04-29 06:47:40 +00:00
outpath.close()
2006-11-10 08:07:56 +00:00
2011-07-26 22:49:39 +00:00
# check the database we just saved for duplicate entries.
dprinted = None
for f in db:
for g in db[f]:
for ff in db:
if f == ff:
continue
if g in db[ff]:
if not dprinted:
dupMessage.append(" Lib %s: %s & %s have dups." % \
(libpath, f, ff))
dprinted=1
if dprinted:
break
2007-04-29 06:47:40 +00:00
print
print "Database update complete."
print " Files processed: %s" % fileCount
print " Total number of grooves: %s" % grooveCount
2011-07-26 22:49:39 +00:00
print
if dupMessage:
print "Warning: Duplicate groove definitions found."
for a in dupMessage:
print a
2006-11-10 08:07:56 +00:00
2007-04-29 06:47:40 +00:00
sys.exit(0)
2006-11-10 08:07:56 +00:00
def dolibupdate(root, subdir):
2007-04-29 06:47:40 +00:00
""" Recursive function to read groove files in a directory. """
2006-11-10 08:07:56 +00:00
2011-07-26 22:49:39 +00:00
global fileCount, grooveCount, gdDate, grooveDB, processedFiles, mkGrooveList
db = grooveDB[0][1]
2006-11-10 08:07:56 +00:00
2007-04-29 06:47:40 +00:00
if subdir == '.':
print "Skipping: '.'"
return
2006-11-10 08:07:56 +00:00
2007-04-29 06:47:40 +00:00
if subdir:
print " Processing library directory '%s'." % subdir
2006-11-10 08:07:56 +00:00
2007-04-29 06:47:40 +00:00
""" Get a list of the files in this directory. If the list
2009-05-17 22:34:44 +00:00
includes a file called 'MMAIGNORE' the entire directory
(and subdirs) is ignored. Otherwise, each file in the
directory ending in 'mma' is parsed for groove defs.
2007-04-29 06:47:40 +00:00
"""
2006-11-10 08:07:56 +00:00
2007-04-29 06:47:40 +00:00
p = os.path.join(root,subdir)
dirfiles = os.listdir(p)
2006-11-10 08:07:56 +00:00
2007-04-29 06:47:40 +00:00
if "MMAIGNORE" in dirfiles:
print "Skipping: %s" % p
return
2006-11-10 08:07:56 +00:00
2007-04-29 06:47:40 +00:00
for fn in sorted(dirfiles):
2006-11-10 08:07:56 +00:00
2007-04-29 06:47:40 +00:00
# Ignore hidden files and emacs auto-save and dead.
2006-11-10 08:07:56 +00:00
2007-04-29 06:47:40 +00:00
if fn.startswith('.') or fn.startswith('#'):
continue
2006-11-10 08:07:56 +00:00
2009-05-17 22:34:44 +00:00
f=os.path.join(root, subdir, fn) # Create full path name
2006-11-10 08:07:56 +00:00
2007-04-29 06:47:40 +00:00
if os.path.isdir(f):
dolibupdate(root, os.path.join(subdir,fn)) # recursive!
2006-11-10 08:07:56 +00:00
2007-04-29 06:47:40 +00:00
elif f.endswith(gbl.ext):
ename = os.path.join(subdir, fn)
2006-11-10 08:07:56 +00:00
2007-04-29 06:47:40 +00:00
processedFiles.append(ename)
2011-07-26 22:49:39 +00:00
if gdDate and ename in db and os.path.getmtime(f) < gdDate:
2007-04-29 06:47:40 +00:00
print " Existing: %s" % f
2011-07-26 22:49:39 +00:00
grooveCount += len(db[ename])
2007-04-29 06:47:40 +00:00
continue
2006-11-10 08:07:56 +00:00
2011-07-26 22:49:39 +00:00
if ename in db:
2007-04-29 06:47:40 +00:00
print " Updating: %s" % f
else:
print " Creating: %s" % f
mkGrooveList = []
2011-07-26 22:49:39 +00:00
MMA.grooves.grooveClear([])
2007-04-29 06:47:40 +00:00
gbl.mtrks = {}
2011-07-26 22:49:39 +00:00
MMA.swing.mode = 0
2007-04-29 06:47:40 +00:00
for c in gbl.midiAssigns.keys():
gbl.midiAssigns[c]=[]
for a,v in enumerate(gbl.midiAvail):
gbl.midiAvail[a]=0
gbl.mtrks[0]=MMA.midi.Mtrk(0)
2011-07-26 22:49:39 +00:00
2007-04-29 06:47:40 +00:00
gbl.tnames = {}
2011-07-26 22:49:39 +00:00
2007-04-29 06:47:40 +00:00
MMA.parse.parseFile(f) # read current file, grab grooves
2006-11-10 08:07:56 +00:00
2007-04-29 06:47:40 +00:00
fileCount += 1 # just so we can report to user
grooveCount += len(mkGrooveList)
2011-07-26 22:49:39 +00:00
db[ename]=mkGrooveList
2006-11-10 08:07:56 +00:00
2007-04-29 06:47:40 +00:00
else:
if not f.endswith(mmadir):
print " Ignoring: %s" % f
2006-11-10 08:07:56 +00:00
2011-07-26 22:49:39 +00:00
def loadDB(dir):
""" Read a database file into memory.
We're assuming that not much goes wrong here...if we don't find
the database we return a Null.
"""
try:
infile = os.path.join(gbl.libPath, dir, mmadir)
f=file(infile, "rb")
f.readline() # Read/discard comment line
g = pickle.load(f)
f.close()
return g
except:
pass
return None
2006-11-10 08:07:56 +00:00
#################################################################
2011-07-26 22:49:39 +00:00
def findGroove(targ):
2007-04-29 06:47:40 +00:00
""" Try to auto-load a groove from the library.
2006-11-10 08:07:56 +00:00
2011-07-26 22:49:39 +00:00
The compilation of all the MMADIR files is stored in the
list of dicts in grooveDir[].
2006-11-10 08:07:56 +00:00
2011-07-26 22:49:39 +00:00
Check the each libpath directory for the MMADIR file. The
2007-04-29 06:47:40 +00:00
names of the files and corresponding grooves are extracted.
This is stored in a dictionary with the filename as the key
and a list of grooves as the data.
"""
2006-11-10 08:07:56 +00:00
2011-07-26 22:49:39 +00:00
global grooveDB
2006-11-10 08:07:56 +00:00
2011-07-26 22:49:39 +00:00
""" If no existing DB we load them from each dir in libpath.
2007-04-29 06:47:40 +00:00
"""
2006-11-10 08:07:56 +00:00
2011-07-26 22:49:39 +00:00
if not grooveDB:
grooveDB=[]
for dir in gbl.autoLib:
g=loadDB(dir)
if g:
grooveDB.append([dir, g])
if not grooveDB: # BS value so we don't keep trying to load
grooveDB = [['', {}]]
""" Search the dict for a match.
2006-11-10 08:07:56 +00:00
2011-07-26 22:49:39 +00:00
grooveDir[] structure ... [ [dirname, g], [] ]
2006-11-10 08:07:56 +00:00
2011-07-26 22:49:39 +00:00
g ... is a dict. Key = filename, data = list of grooves
2006-11-10 08:07:56 +00:00
2007-04-29 06:47:40 +00:00
RETURN: Lib-Filename if found
None if not found
"""
2006-11-10 08:07:56 +00:00
2011-07-26 22:49:39 +00:00
for dir, g in grooveDB:
for filename, namelist in g.items():
if targ in namelist:
return os.path.join(dir,filename)
2007-04-29 06:47:40 +00:00
return None
2006-11-10 08:07:56 +00:00