#!/usr/bin/env python def usage(): print ''' %s filename[.sly extension optional] Appends the contents of files in the current directory to filename, according to the head of filename. Empty comment fields precede. Normally you would copy and modify a previous header, which should look like this: !! test.sly could be name of this file dummy.ly !! tbn.ly !! bass.ly cymbal.ly !! spoons.ly !! crwth.ly !!%Here must be the end or the last field. The data will be arranged like the header. If there is notes data left in filename, it must still end with the comment field--best empty. (c)2004 David Raleigh Arnold, under GNU. ''' % (sys.argv[0]) sys.exit() def inputfile(): '''Get file from argument. Supply .sly ending, test existence of file, and return name. ''' if not len(sys.argv) == 2: print "One argument, a file name with \".sly\" extension, is required." usage() arg = sys.argv[1] if os.path.isfile(arg + '.sly'): infile = arg + '.sly' elif os.path.isfile(arg): infile = arg else: print "File " + arg + " or " + arg + ".sly does not exist." usage() return infile import string, os, sys try: file = open(inputfile()) comment = file.readline() print comment, # should identify file to user print "Writing fields:" fsep = comment[:2] ''' Put header with trailing fsep in memory. ''' head = file.read().split('\n' + fsep, 1)[0] ''' Process head, get open files into list in lines ''' list = head.split('\n') filelist = [] for line in list: line = line.split(fsep) linelist = [] for fname in line: fname = fname.strip() file = open(fname ,"a") # empty file is created if not existent file.close() fname = open(fname) linelist.append(fname) filelist.append(linelist) outfile = open(inputfile(), 'a') #append to whole file count = 0 while 1: outfile.write(fsep) # for the comment field fields = '' # if no info left in any of the files, done! for line in filelist: for file in line: while 1: #loop is for multiple line field only field = ' ' + file.readline().rstrip() if field[-1]=='%': outfield = field + '\n' outfile.write(outfield) #print outfield, elif file==line[-1]: # if to be last file in line outfield = field + '\n' outfile.write(outfield) break #print outfield, else: outfield = field + ' ' + fsep + ' ' outfile.write(outfield) break #print outfield, fields = fields + ' ' + field.strip() # test after writing count = count + 1 if fields.strip()=='': # test complete set of fields print "Total fields written, not counting comment: ", count break except: print "Done!" print "main-errors: ", sys.exc_type, sys.exc_value # todo: find index error..