/usr/bin/mma-renum is in mma 15.12-1.
This file is owned by root:root, with mode 0o755.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 | #! /usr/bin/python
# Works with python 2 and 3
# Renumber a mma song file. Just take any lines
# which start with a number and do those sequenially.
import sys, os
def error(m):
""" Abort on error with message. """
print(m)
sys.exit(1)
def usage():
""" Print usage message and exit. """
print("""
Mma-renum, (c) Bob van der Poel
Re-numbers a mma song file and
cleans up chord tabbing.
Overwrites existing file!
""")
sys.exit(1)
##########################
if len(sys.argv[1:]) != 1:
print("mma-renum: requires 1 filename argument.")
usage()
filename = sys.argv[1]
if filename[0] == '-':
usage()
try:
inpath = open(filename, 'r')
except:
error("Can't access the file '%s'" % filename)
tempfile = "%s.%s.tmp" % (filename, os.getpid())
try:
outpath = open( tempfile, 'w')
except:
error("Can't open scratchfile '%s', error '%s'" % (tempfile, sys.exc_info()[0]) )
linenum = 1
for l in inpath:
l=l.rstrip()
if l and l[0].isdigit():
try:
x = int(l.split()[0])
except:
x = None
else:
x = None
if x != None:
otherstuff = '' # break off non-chord items
cmt = ''
if l.count('//'):
l, cmt=l.split('//', 1)
for i,a in enumerate(l):
if a in('{[*'):
otherstuff=l[i:]
l=l[:i]
l=l.strip()
break
newl=['%-5s' % linenum]
linenum += 1 # the global line number
for a in l.split()[1:]: # do each chord item
newl.append(" %6s" % a)
if otherstuff: # join on non-chord stuff
newl.append(" ")
newl.append(otherstuff)
if cmt:
newl.append(" //")
newl.append(cmt)
newl=''.join(newl)
else:
newl = l
outpath.write(newl + "\n")
inpath.close()
outpath.close()
try:
os.remove(filename)
except:
error("Cannot delete '%s', new file '%s' remains" % (filename, tempfile) )
try:
os.rename(tempfile, filename)
except:
error("Cannot rename '%s' to '%s'." (tempfile, filename) )
|