#!/usr/bin/env python def usage(): print ''' %s filename[.sly extension optional] splits filename into separate files. The first two bytes are the field separator. The next line begins the list of target files terminated by a field separator at the beginning of a line. The first data (comment) field immediately follows. If it is not empty, it is written to all files. Then fields are written in turn to particular files. For safety's sake, sly will not write files if run on a bare header. A line which ends with a \"%%\" will continue to the same file. Empty fields become blank lines, except comments. Example: !! test.sly This line is not written.... dummy.ly !! target.ly !! tpt.ly !!!! m1 !! notes to target.ly !! notes to tpt.ly !! m2 !! target notes !! tpt notes comment !! m3 !! target notes !! tpt notes %% more tpt notes !!!! m4 (c)2004 David Raleigh Arnold, under GNU. 20040410''' % (sys.argv[0]) sys.exit() def inputfile(): '''Get file from argument. Supply .sly ending, test existence of file, and open it for reading. ''' 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 = open(arg + '.sly') elif os.path.isfile(arg): infile = open(arg) else: print "File " + arg + " or " + arg + ".sly does not exist." usage() return infile import string, os, sys try: file = inputfile() # which is open comment = file.readline() print comment, fsep = comment[:2] ''' Put whole file in memory. ''' lines = [] for line in file: line = line.strip() lines.append(line) lines = '\n'.join(lines) all = lines.split('\n' + fsep, 1) head, data = all[0], all[1] if len(data.split('\n'))<2 and len(data.split(fsep))<2: print "Bare header. Nothing written." usage() ''' Process head, get names into list ''' filelist = fsep.join(head.split('\n')) # sub fsep for newline filelist = filelist.split(fsep) names = [] print "Writing files:" for filename in filelist: name = filename.strip() print '\t\t' + name target = open(name, 'w') names.append(target) # "names" is list of opened target files print ''' Data written to first file, with field numbers, except that comment fields (which start at "0") were not written if they were empty:''' ''' Process data...''' data = data.split('\n') # arrange data in lines data = fsep.join(data).rstrip() # rstrip & make newline new field data = data.split('%' + fsep) # split at lily comment if at end of field data = '%\n'.join(data) # join so now fields can have multiple lines data = data.split(fsep) count = 0 # start number of fields at one 1st comment gets mod zero volte = len(names)+1 # constant: number of targets for field in data: opnum = 0 # index sts at 0, but 2nd iter=1st file. for target in names: if (count % volte)==0: # write to all files if opnum==0: print count, field if field.strip()!='': # but only if comment exists. target.write(field + '\n') if (count % volte)==opnum+1: # zero pass is comment names[opnum].write(field + '\n') if opnum==0: print count, "field 1:", field else: print count, opnum = opnum + 1 # index next target file count = count + 1 # next field except: pass #print "main-errors: ", sys.exc_type, sys.exc_value