File: mutopia-index.py

package info (click to toggle)
lilypond 2.2.6-3
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 11,260 kB
  • ctags: 7,622
  • sloc: cpp: 47,787; lisp: 11,217; python: 11,203; sh: 3,290; yacc: 2,011; lex: 831; perl: 373; ansic: 309; makefile: 132; csh: 8
file content (216 lines) | stat: -rw-r--r-- 4,865 bytes parent folder | download
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#!@PYTHON@
# mutopia-index.py

name = 'mutopia-index'

import fnmatch
import os

_debug = 0

_prune = ['(*)']

def find(pattern, dir = os.curdir):
        list = []
        names = os.listdir(dir)
        names.sort()
        for name in names:
                if name in (os.curdir, os.pardir):
                        continue
                fullname = os.path.join(dir, name)
                if fnmatch.fnmatch(name, pattern):
                        list.append(fullname)
                if os.path.isdir(fullname) and not os.path.islink(fullname):
                        for p in _prune:
                                if fnmatch.fnmatch(name, p):
                                        if _debug: print "skip", `fullname`
                                        break
                        else:
                                if _debug: print "descend into", `fullname`
                                list = list + find(pattern, fullname)
        return list


import re
import os
import sys
import stat

def gulp_file (fn):
	try:
		f = open (fn)
	except:
		raise 'not there' , fn
	return f.read ()

def file_exist_b (fn):
	try:
		f = open (fn)
		return 1
	except:
		return 0


headertext= r"""

<p>You're looking at a page with some LilyPond samples.  These files
are also included in the distribution. The output is completely
generated from the <tt>.ly</tt> source file, without any further touch
up.

<p>

The pictures are 90 dpi anti-aliased snapshots of the printed output.
If you want a better impression of the appearance, do print out one of
the PDF or PostScript files; they use scalable fonts, and should look
good at any resolution.

"""

headertext_nopics= r"""
<p>Nothing to be seen here, move along.
"""

#
# FIXME breaks on multiple strings.
#
def read_lilypond_header (fn):
	s = open(fn).read ()
	s = re.sub('%.*$', '', s)
	s = re.sub('\n', ' ', s)		

	dict = {}
	m = re.search (r"""\\header\s*{([^}]*)}""", s)

	if m:
			s = m.group(1)
	else:
			return dict

	while s:
		m = re.search (r'''\s*(\S+)\s*=\s*"([^"]+)"''', s)
		if m == None:
			s = ''
		else:
			s = s[m.end (0):]
			left  = m.group	 (1)
			right = m.group (2)

			left = re.sub ('"', '', left)
			right = re.sub ('"', '', right)
			dict[left] = right

	return dict

def help ():
	sys.stdout.write (r"""Usage: mutopia-index [OPTIONS] INFILE OUTFILE
Generate index for mutopia.

Options:
  -h, --help                 print this help
  -o, --output=FILE          write output to file
  -s, --subdirs=DIR	     add subdir
      --suffix=SUF	     specify suffix
      
"""
					  )
	sys.exit (0)

# ugh.
def gen_list(inputs, filename):
	print "generating HTML list %s\n" % filename
	if filename:
		list = open(filename, 'w')
	else:
		list = sys.stdout
	list.write ('<html><title>Rendered Examples</title>\n')
	list.write ('<body bgcolor=white>\n')
	
	if inputs:
			list.write (headertext)
	else:
			list.write (headertext_nopics)


	for ex in inputs:
		(base, ext) = os.path.splitext (ex)
		(base, ext2) = os.path.splitext (base)		
		ext = ext2 + ext
		
		header = read_lilypond_header(ex)
		def read_dict(s, default, h =header):
				try:
					ret = h[s]
				except KeyError:
					ret = default
				return ret
		head = read_dict('title', os.path.basename (base))
		composer = read_dict('composer', '')
		desc = read_dict('description', '')
		list.write('<hr>\n')
		list.write('<h1>%s</h1>\n' % head);
		if composer:
			list.write('<h2>%s</h2>\n' % composer)
		if desc:
			list.write('%s<p>' % desc)
		list.write ('<ul>\n')
		def list_item(filename, desc, type, l = list):
			if file_exist_b(filename):
				
				l.write ('<li><a href="%s">%s</a>' % (filename, desc))

				# todo: include warning if it uses \include
				# files.
				
				size=os.stat(filename)[stat.ST_SIZE]
				kB=(size + 512) / 1024
				if kB:
					l.write (' (%s %d kB)' % (type, kB))
				else:
					l.write (' (%s %d characters)' % (type, size))
				pictures = ['jpeg', 'png', 'xpm']
				l.write ('\n')

		list_item(base + ext, 'The input', 'ASCII')
		for pageno in range(1,100):
			f  = base + '-page%d.png' % pageno
			if not file_exist_b (f):
				break
			list_item(f, 'See a picture of page %d' % pageno, 'png')
		list_item(base + '.pdf', 'Print', 'PDF')
		list_item(base + '.ps.gz', 'Print', 'gzipped PostScript')
		list_item(base + '.midi', 'Listen', 'MIDI')
		list.write ("</ul>\n");

	list.write('</body></html>\n');
	list.close()

import getopt

(options, files) = getopt.getopt(sys.argv[1:], 
  'ho:', ['help', 'output='])
outfile = 'examples.html'

subdirs =[]
for opt in options:
	o = opt[0]
	a = opt[1]
	if o == '--help' or o == '-h':
		help()
	elif o == '--output' or o == '-o':
		outfile = a

dirs  = []
for f in files:
	dirs = dirs + find ('out-www', f)

if not dirs:
	dirs = ['.']

allfiles = []

for d in dirs:
	allfiles = allfiles + find ('*.ly.txt', d)

gen_list (allfiles, outfile)