Skip to content

Commit 5a947a5

Browse files
committed
nicer code
1 parent f6743fb commit 5a947a5

20 files changed

+901
-891
lines changed

CHANGES

+6
Original file line numberDiff line numberDiff line change
@@ -530,3 +530,9 @@ different panes. Tie/untie option added for notes. Broken rhythm
530530
- MAC only: a delay was added before playing to fix first notes not being played
531531
- Added to the Help menu a link to 'ABC Quick Reference Card'
532532
- after switching to another format file in abc-settings the music score refreshes
533+
534+
1.3.7.7
535+
- German translation updated (thanks to Bernhard Weichel)
536+
- Fix for 'Follow score' for notes spanning multiple pages
537+
- Import midi fixed
538+
- Fixed: Typing Z: appeared as Z |:

abc2xml.py

+10-10
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ def abc_grammar (): # header, voice and lyrics grammar for ABC
9292
syllable = Combine (Optional ('~') + syl_chars + ZeroOrMore (Literal ('~') + syl_chars)) + Optional ('-')
9393
lyr_elem = (syllable | skip_note | extend_note | measure_end) + Optional (white).suppress ()
9494
lyr_line = Optional (white).suppress () + ZeroOrMore (lyr_elem)
95-
95+
9696
syllable.setParseAction (lambda t: pObj ('syl', t))
9797
skip_note.setParseAction (lambda t: pObj ('skip', t))
9898
extend_note.setParseAction (lambda t: pObj ('ext', t))
@@ -172,7 +172,7 @@ def abc_grammar (): # header, voice and lyrics grammar for ABC
172172
bar_left = (oneOf ('[|: |: [: :') + Optional (volta)) | Optional ('|').suppress () + volta | oneOf ('| [|')
173173
bars = ZeroOrMore (':') + ZeroOrMore ('[') + OneOrMore (oneOf ('| ]'))
174174
bar_right = invisible_barline | double_rep | Combine (bars) | dashed_barline | voice_overlay | bare_volta
175-
175+
176176
errors = ~bar_right + Optional (Word (' \n')) + CharsNotIn (':&|', exact=1)
177177
linebreak = Literal ('$') | ~decorations + Literal ('!') # no need for I:linebreak !!!
178178
element = fld_or_lyr | broken | decorations | stem | chord_or_text | grace_notes | tuplet_start | linebreak | errors
@@ -404,7 +404,7 @@ def addElemT (parent, tag, text, level):
404404
e.text = text
405405
addElem (parent, e, level)
406406
return e
407-
407+
408408
def mkTmod (tmnum, tmden, lev):
409409
tmod = E.Element ('time-modification')
410410
addElemT (tmod, 'actual-notes', str (tmnum), lev + 1)
@@ -540,7 +540,7 @@ def splitHeaderVoices (abctext):
540540
def mergeMeasure (m1, m2, slur_offset, voice_offset, rOpt, is_grand=0):
541541
slurs = m2.findall ('note/notations/slur')
542542
for slr in slurs:
543-
slrnum = int (slr.get ('number')) + slur_offset
543+
slrnum = int (slr.get ('number')) + slur_offset
544544
slr.set ('number', str (slrnum)) # make unique slurnums in m2
545545
vs = m2.findall ('note/voice') # set all voice number elements in m2
546546
for v in vs: v.text = str (voice_offset + int (v.text))
@@ -998,7 +998,7 @@ def doNotations (s, n, ptup, alter, tupnotation, tstop, nt, lev):
998998
slurnum = len (s.slurstack) + 1
999999
s.slurstack.append (slurnum)
10001000
ntn = E.Element ('slur', number='%d' % slurnum, type='start')
1001-
addElem (nots, ntn, lev + 1)
1001+
addElem (nots, ntn, lev + 1)
10021002
if nots.getchildren() != []: # only add notations if not empty
10031003
addElem (nt, nots, lev)
10041004

@@ -1068,7 +1068,7 @@ def stopBeams (s):
10681068
s.prevNote = None
10691069

10701070
def staffDecos (s, decos, maat, lev):
1071-
gstaff = s.gStaffNums.get (s.vid, 0) # staff number of the current voice
1071+
gstaff = s.gStaffNums.get (s.vid, 0) # staff number of the current voice
10721072
for d in decos:
10731073
d = s.usrSyms.get (d, d).strip ('!+') # try to replace user defined symbol
10741074
if d in s.dynaMap:
@@ -1129,7 +1129,7 @@ def doClef (field):
11291129
note, octstr = clefm.groups ()
11301130
nUp = note.upper ()
11311131
octnum = (4 if nUp == note else 5) + (len (octstr) if "'" in octstr else -len (octstr))
1132-
gtrans = (3 if nUp in 'AFD' else 4) - octnum
1132+
gtrans = (3 if nUp in 'AFD' else 4) - octnum
11331133
if clef not in ['perc', 'none']: clef = s.clefLineMap [nUp]
11341134
if clef:
11351135
s.gtrans = gtrans # only change global tranposition when a clef is really defined
@@ -1711,7 +1711,7 @@ def parse (s, abc_string, rOpt=False):
17111711
vcelyr = vce
17121712
elem1 = vcelyr [0][0] # the first element of the first measure
17131713
if elem1.name == 'inline'and elem1.t[0] == 'V': # is a voice definition
1714-
voicedef = elem1
1714+
voicedef = elem1
17151715
del vcelyr [0][0] # do not read voicedef twice
17161716
else:
17171717
voicedef = ''
@@ -1785,7 +1785,7 @@ def decodeInput (data_string):
17851785
info ('decoded from %s' % enc)
17861786
return unicode_string
17871787

1788-
xmlVersion = "<?xml version='1.0' encoding='utf-8'?>"
1788+
xmlVersion = "<?xml version='1.0' encoding='utf-8'?>"
17891789
def fixDoctype (elem):
17901790
if python3: xs = E.tostring (elem, encoding='unicode') # writing to file will auto-encode to utf-8
17911791
else: xs = E.tostring (elem, encoding='utf-8') # keep the string utf-8 encoded for writing to file
@@ -1826,7 +1826,7 @@ def convert (pad, fnm, abc_string, mxl, rOpt):
18261826
outfile = sys.stdout
18271827
outfile.write (xmldoc)
18281828
outfile.write ('\n')
1829-
1829+
18301830
#----------------
18311831
# Main Program
18321832
#----------------

abc_character_encoding.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ def unicode_text_to_html_abc(text):
184184
'\\~i': u'\u0129',
185185
'\\~U': u'\u0168',
186186
'\\~u': u'\u0169',
187-
'\\oA': u'\xc5',
187+
'\\oA': u'\xc5',
188188
'\\oa': u'\xe5',
189189
'\\oU': u'\u016e',
190190
'\\ou': u'\u016f',
@@ -467,7 +467,7 @@ def unicode_text_to_html_abc(text):
467467
'\\k,': u'\u0137',
468468
'"\\u;"': u'\u0173',
469469
'\\u?': u'\u0169',
470-
'\\u-': u'\u016b',
470+
'\\u-': u'\u016b',
471471

472472
r'{\aa}': u'\xe5',
473473
r'{\aA}': u'\xc5',}
@@ -477,4 +477,3 @@ def unicode_text_to_html_abc(text):
477477

478478
def decode_abc(abc_code): return encoded_char_re.sub(lambda m: mapping[m.group(0)], abc_code)
479479
def encode_abc(abc_code): return ''.join(reverse_mapping.get(c, c) for c in abc_code)
480-

abc_search.py

+16-16
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
all_notes = "| C,, D,, E,, F,, G,, A,, B,, C, D, E, F, G, A, B, C D E F G A B c d e f g a b c' d' e' f' g' a' b' c'' d'' e'' f'' g'' a'' b''".split()
44
note2number = dict([(n, i) for (i, n) in enumerate(all_notes)])
55
note_pattern = re.compile(r"([ABCDEFG],*|[abcdefg]'*)")
6-
repl_by_spaces = lambda m: ' ' * len(m.group(0))
6+
repl_by_spaces = lambda m: ' ' * len(m.group(0))
77

88
remove_non_notes = re.compile(r'(?xm)' + '|'.join([
9-
r'\%\%beginps(.|\s)+?\%\%endps', # remove embedded postscript
9+
r'\%\%beginps(.|\s)+?\%\%endps', # remove embedded postscript
1010
r'\%\%begintext(.|\s)+?\%\%endtext', # remove text
1111
r'\[\w:.*?\]', # remove embedded fields
1212
r'(?m)^\w:.*?$', # remove normal fields
@@ -22,47 +22,47 @@
2222
]))
2323

2424
def remove_non_note_fragments(abc):
25-
# replace non-note fragments of the text by replacing them by spaces (thereby preserving offsets), but keep also bar and repeat symbols
26-
return remove_non_notes.sub(repl_by_spaces, abc.replace('\r', '\n'))
25+
# replace non-note fragments of the text by replacing them by spaces (thereby preserving offsets), but keep also bar and repeat symbols
26+
return remove_non_notes.sub(repl_by_spaces, abc.replace('\r', '\n'))
2727

2828
def get_intervals_from_abc(abc):
2929
abc = remove_non_note_fragments(abc)
3030
notes = [note2number[m] for m in note_pattern.findall(abc)]
3131
intervals = [i2-i1 for (i1,i2) in zip(notes[:-1], notes[1:])]
3232
return ''.join([chr(74+i) for i in intervals])
3333

34-
def get_matches(abc, abc_search_intervals):
34+
def get_matches(abc, abc_search_intervals):
3535
abc = remove_non_note_fragments(abc)
3636
matches = list(note_pattern.finditer(abc)) # the matched notes in the abc
3737
notes = [note2number[m.group(0)] for m in matches] # the note number for each note
3838
offsets = [m.span(0) for m in matches] # the start/end offset in the ABC code for each note
3939
intervals = [i2-i1 for (i1,i2) in zip(notes[:-1], notes[1:])] # the intervals between pairs of notes
4040
intervals = ''.join([chr(74+i) for i in intervals]) # the intervals coded as strings
4141
# find the search string among all the intervals
42-
for m in re.finditer(re.escape(abc_search_intervals), intervals):
43-
i = m.start(0) # the offset in the search string is the same as the index of the note
42+
for m in re.finditer(re.escape(abc_search_intervals), intervals):
43+
i = m.start(0) # the offset in the search string is the same as the index of the note
4444
start, end = offsets[i][0], offsets[i+len(abc_search_intervals)][1]
4545
yield (start, end) # return start and end offset in the ABC code for the matched sequence of notes
4646

4747
def abc_matches_iter(abc, search_string):
48-
search = get_intervals_from_abc(search_string)
48+
search = get_intervals_from_abc(search_string)
4949
offset = 0 # the total character offset from the beginning of the abc text
5050
tunes = abc.split('X:') # split the code up per tune
51-
52-
for i, tune in enumerate(tunes):
51+
52+
for i, tune in enumerate(tunes):
5353
if i > 0: tune = 'X:' + tune # add back the X: to the tunes from which it was removed during the split
5454

5555
# find matches
5656
for start_offset_in_tune, end_offset_in_tune in get_matches(tune, search):
5757
# convert the offsets within the tune to global offsets
58-
start, end = (offset+start_offset_in_tune, offset+end_offset_in_tune)
58+
start, end = (offset+start_offset_in_tune, offset+end_offset_in_tune)
5959
yield (start, end)
60-
61-
offset += len(tune)
6260

63-
if __name__ == '__main__':
64-
search = 'CEG'
65-
abc = '''' GE CE GE'''
61+
offset += len(tune)
62+
63+
if __name__ == '__main__':
64+
search = 'CEG'
65+
abc = '''' GE CE GE'''
6666

6767
for r in abc_matches_iter(abc, search):
6868
pass

abc_styler.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def OnStyleNeeded(self, event):
6161
get_text_range = editor.GetTextRangeRaw
6262
set_styling = editor.SetStyling
6363
start = editor.GetEndStyled() # this is the first character that needs styling
64-
end = event.GetPosition() # this is the last character that needs styling
64+
end = event.GetPosition() # this is the last character that needs styling
6565
line_start = editor.LineFromPosition(start)
6666
start = editor.PositionFromLine(line_start)
6767
state = editor.GetStyleAt(start-1) # init style

abc_tune.py

+10-10
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ def determine_abc_structure(self, abc_code):
8282
self.tune_body_start_line_index = body_start_line_index
8383
self.abc_lines = abc_lines
8484
self.note_line_indices = note_line_indices
85-
85+
8686
def get_abc_per_voice(self):
8787
if self.__abc_per_voice is None:
8888
if self.tune_body_start_line_index:
@@ -139,7 +139,7 @@ def is_equal(self, abc_tune):
139139
return self.x_number == abc_tune.x_number \
140140
and self.tune_header_start_line_index == abc_tune.tune_header_start_line_index \
141141
and self.first_note_line_index == abc_tune.first_note_line_index
142-
142+
143143
def is_gracenote_at(self, row, col):
144144
line = self.abc_lines[row-1]
145145
i = col - 1
@@ -163,26 +163,26 @@ def get_start_of_chord(self, row, col):
163163
return i + 1
164164
i -= 1
165165
return None
166-
166+
167167
@staticmethod
168168
def byte_to_unicode_index(text, index):
169169
return len(abc_text_to_unicode(text[:index]).encode('utf-8'))
170-
170+
171171
def midi_col_to_svg_col(self, row, col):
172172
line = self.abc_lines[row-1]
173173
col = self.byte_to_unicode_index(line, col-1) + 1 # compensate for encoding differences
174-
174+
175175
try:
176176
if self.is_gracenote_at(row, col):
177-
return None # abcm2ps does not mark notes within braces
178-
179-
start_of_chord = self.get_start_of_chord(row, col)
177+
return None # abcm2ps does not mark notes within braces
178+
179+
start_of_chord = self.get_start_of_chord(row, col)
180180
if start_of_chord:
181181
return start_of_chord - 1 # svg_col is 1-based
182-
182+
183183
if line[col-1] in '~.HIJKLMNOPQRSTUVWhijklmnopqrstuvw':
184184
return col # in case of one letter symbol: abc2midi marks the symbol, abcm2ps marks the after note after the dot
185-
185+
186186
return col-1
187187
except:
188188
return None

0 commit comments

Comments
 (0)