File: common.py

package info (click to toggle)
mma 21.09-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 51,828 kB
  • sloc: python: 16,751; sh: 26; makefile: 18; perl: 12
file content (356 lines) | stat: -rw-r--r-- 10,008 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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
# common.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

Bob van der Poel <bob@mellowood.ca>


These are a collection of miscellaneous routines used in various
parts of MMA. It is safe to load the whole works with:

    from MMA.common import *

without side effects (yeah, right).

"""
from random import randrange
import sys
import time
from . import gbl
from textwrap import wrap
import MMA.debug

# A buffer for various debug/warning/error messages
# this is only used if MMA_LOGFILE has been set. The
# buffer is dumped at exit.
outBuffer = []

# having the term width is nice for pretty print error/warning
from MMA.termsize import getTerminalSize
termwidth = getTerminalSize()[0]-1

def bufferPrint(a):
    if gbl.logFile:
        outBuffer.append(a)
    else:
        print(a)

def cleanPrintBuffer():
    """ Write an existing stored buffer (debug/error/warning). """
    
    if outBuffer and gbl.logFile:
        outBuffer.insert(0, "\n**** Run log: '%s' %s" % ( gbl.infile, time.asctime()))
        
        opath = open(gbl.logFile, 'a')    # Open output for append
        try:
            import fcntl
            fcntl.flock(opath, fcntl.LOCK_EX)   # make sure we print in one batch
        except:
            pass
        opath.write('\n'.join(outBuffer))   # dump entire buffer
        opath.close()                       # this will release lock as well

def prettyPrint(msg):
    """ Simple formatter for error/warning messages."""

    if isinstance(msg, list):
        msg = ' '.join(msg)
    try:
        for a in wrap(msg, termwidth, initial_indent='', subsequent_indent='    '):
            bufferPrint(a)
    except:
        bufferPrint(msg)

def error(msg):
    """ Print an error message and exit.

        If the global line number is >=0 then print the line number
        as well.
    """

    if gbl.lineno >= 0:
        linno = "<Line %d>" % gbl.lineno
    else:
        linno = ''

    if gbl.inpath:
        file = "<File:%s>" % gbl.inpath.fname
    else:
        file = ''

    prettyPrint("Error: %s %s %s" % (linno, file, msg))

    # Parse though the error message and check for illegal characters.
    # Report (first only) if any found.

    for a in msg:
        a = ord(a)
        if a < 0x20 or a >= 0x80:
            bufferPrint("Corrupt input file? Illegal character 'x%02x' found." % a)
            break

    if gbl.ignoreBadChords:  # set with -xCHORD and -xPERMISSIVE options
        return
    
    sys.exit(1)

def warning(msg):
    """ Print warning message and return. """

    if not MMA.debug.noWarn:
        if gbl.lineno >= 0:
            linno = "<Line %d>" % gbl.lineno
        else:
            linno = ''

        if gbl.inpath:
            file = "<File:%s>" % gbl.inpath.fname
        else:
            file = ''

        prettyPrint("Warning: %s %s %s" % (linno, file, msg))


def dPrint(msg):
    """ Print/buffer a debugging message. Keep separate since we might
        want to install line numbering, etc. later???
    """

    prettyPrint(msg)
    
def getOffset(ticks, ranLow=None, ranHigh=None):
    """ Calculate a midi offset into a song.

        ticks == offset into the current bar.
        ran   == random adjustment from RTIME

        When calculating the random factor the test ensures
        that a note never starts before the start of the bar.
        This is important ... voice changes, etc. will be
        buggered if we put the voice change after the first
        note-on event.

        CAUTION: If you pass either ranLow or ranHigh make sure
                 both are set. If one has a value and the other
                 is None a crash is ensured. The existing callers
                 are okay (setRtime creates both values).
    """

    p = gbl.tickOffset + int(ticks)     # int() cast is important!

    if ranLow is not None:
        r = randrange(ranLow, ranHigh+1)
        if ticks == 0 and r < 0:
            r = 0
        p += r

    return p


def stoi(s, errmsg=None):
    """ string to integer. """

    try:
        return int(s, 0)
    except ValueError:
        if errmsg:
            error(errmsg)
        else:
            error("Expecting integer value, not %s" % s)


def stof(s, errmsg=None):
    """ String to floating point. """

    try:
        return float(s)
    except:
        try:
            return int(s, 0)
        except ValueError:
            if errmsg is not None:
                error(errmsg)
            else:
                error("Expecting a  value, not %s" % s)

def stotick(s, default='B', emsg=None):
    """ Convert string to midi ticks. Will apply default M, B, or T if
        not given to string ending in a digit.
    """

    # Parse out extension (M, B, T)
    if s[-1].isdigit():
        ext = default
    else:
        ext = s[-1].upper()
        s = s[:-1]

    # convert to value for measures, beats or ticks
    try:
        mult={'M': gbl.barLen, 'B': gbl.BperQ, 'T': 1}[ext]
    except KeyError:
        error("The extension '%s' is not valid. Use M, B or T." % ext)

    # convert digits to value
    try:
        v = stof(s, emsg)
    except ValueError:
        if errmsg is not None:
            error(errmsg)
        else:
            error("Expecting a  value with optional 'B', 'M' or 'T', not %s" % s)

    return int(v * mult)  # return ticks as int


def pextract(s, open, close, onlyone=None, insert=''):
    """ Extract a parenthesized set of substrings.

        s        - original string
        open    - substring start tag \ can be multiple character
        close   - substring end tag   / strings (ie. "<<" or "-->")
        onlyone - optional, if set only the first set is extracted
        insert  - optional, insert string where extraction

        returns ( original sans subs, [subs, ...] )

        eg: pextract( "x{123}{666}y", '{',    '}' )
            Returns:  ( 'xy', [ '123', '666' ] )

    """

    subs = []
    magic = chr(1)
    while 1:
        lstart = s.find(open)
        lend   = s.find(close)

        if lstart > -1 and lstart < lend:
            subs.append(s[lstart + len(open):lend].strip())
            s = s[:lstart] + magic + s[lend+len(close):]
            if onlyone:
                break
        else:
            break

    s = s.replace(magic, insert)
    
    return s.strip(), subs


def seqBump(l):
    """ Expand/contract an existing sequence list to the current seqSize."""

    while len(l) < gbl.seqSize:
        l.extend(l)
    return l[:gbl.seqSize]


def lnExpand(ln, msg):
    """ Validate and expand a list passed to a set command. """
    
    if len(ln) > gbl.seqSize:
        if not gbl.inAllGrooves:
            warning("%s list truncated to %s patterns" % (msg, gbl.seqSize) )
        ln = ln[:gbl.seqSize]

    last = None

    for i, n in enumerate(ln):
        if n == '/':
            if last is None:
                error("%s cannot use a '/' as the first item in list." % msg)
            else:
                ln[i] = last
        else:
            last = n

    return ln


def opt2pair(ln, toupper=False, notoptstop=False):
    """ Parse a list of options. Separate out "=" option pairs.

        Returns:
           newln - original list stripped of opts
           opts  - list of options. Each option is a tuple(opt, value)

       Note: default is to leave case alone. Setting toupper converts 
                everything to upper.
             default is to parse entire line. Setting notoptstop stops parse at first
                word which is not a xx=yy pair.
    """

    opts = []
    newln = []

    # Permit the user to use stuff like "Beats = 1 , 3, 4" or "opt1 = 44 opt2 = 99"
    # So, join the line with space delminters, then strip out spaces before/after
    # all '=' and ','. This may bugger up print statements, but they aren't parsed
    # here ... so this should be fine. This might be a cause of future errors!

    # This is very useful when the user wants to use a macro expansion:
    #     Beats = $Macro1 , $Macro2
    # since macros need to have spaces around them.

    ln = ' '.join(ln)
    ln = ln.replace('  ', ' ')  # strip double spaces
    ln = ln.replace('= ', '=')  # spaces after/before =
    ln = ln.replace(' =', '=')
    ln = ln.replace(', ', ',')  # spaces after/before ,
    ln = ln.replace(' ,', ',')
    ln = ln.split()

    for v, a in enumerate(ln):
        if toupper:
            a = a.upper()
        if a == '--':   # Just in case user needs opt=val passed
            newln.extend(ln[v+1:])  # Drop/ignore '--'
            break
        try:
            o, v = a.split('=', 1)
            opts.append((o, v))
        except ValueError:   # this means no '=', split() failed
            newln.append(a)
            if notoptstop:
                newln.extend(ln[v+1:])
                break

    return newln, opts


def getTF(option, e=''):
    """ Test the STRING option for a true/false value and return the boolean
         True, On, 1 -- True
         False, Off, 0 -- False

         Optional e arg is prepended to any error message.
    """

    option = option.upper()
    if option in ("TRUE", "ON", "1"):
        return True

    if option in ("FALSE", "OFF", "0"):
        return False

    if e:
        e+=": "
    error("%sRequires True or False, not '%s'." % (e,option))