def test_lilypondparsertools_LilyPondParser_accidentals_forced_01(): string = "{ c!4 }" parsed = lilypondparsertools.LilyPondParser()(string) assert parsed[0].note_head.is_forced == True assert format(parsed[0]) == 'c!4'
def parse(string, language='english'): r'''Parses LilyPond `string`. .. container:: example Parses LilyPond string with English note names: >>> container = abjad.parse("{c'4 d'4 e'4 f'4}") >>> abjad.show(container) # doctest: +SKIP .. container:: example Parses LilyPond string with Dutch note names: >>> container = abjad.parse( ... "{c'8 des' e' fis'}", ... language='nederlands', ... ) >>> abjad.show(container) # doctest: +SKIP Returns Abjad component. ''' from abjad.tools import rhythmtreetools from abjad.tools import lilypondparsertools if string.startswith('abj:'): return lilypondparsertools.parse_reduced_ly_syntax(string[4:]) elif string.startswith('rtm:'): return rhythmtreetools.parse_rtm_syntax(string[4:]) if language not in _lilypond_parsers_by_language: parser = lilypondparsertools.LilyPondParser(default_language=language) _lilypond_parsers_by_language[language] = parser return _lilypond_parsers_by_language[language](string)
def test_lilypondparsertools_LilyPondParser_accidentals_cautionary_01(): string = "{ c?4 }" parsed = lilypondparsertools.LilyPondParser()(string) assert parsed[0].note_head.is_cautionary == True assert format(parsed[0]) == 'c?4'
def test_lilypondparsertools_LilyPondParser_accidentals_forced_02(): string = "{ <c! e g!!>4 }" parsed = lilypondparsertools.LilyPondParser()(string) assert parsed[0].note_heads[0].is_forced == True assert parsed[0].note_heads[1].is_forced == False assert parsed[0].note_heads[2].is_forced == True assert format(parsed[0]) == '<c! e g!>4'
def __init__(self, *args): from abjad.ly import drums from abjad.tools import lilypondparsertools from abjad.tools import scoretools assert len(args) in (0, 1, 2) if len(args) == 1 and isinstance(args[0], str): string = '{{ {} }}'.format(args[0]) parsed = lilypondparsertools.LilyPondParser()(string) assert len(parsed) == 1 and isinstance(parsed[0], Leaf) args = [parsed[0]] is_cautionary = False is_forced = False is_parenthesized = False if len(args) == 1 and isinstance(args[0], Leaf): leaf = args[0] written_pitch = None written_duration = leaf.written_duration if 'written_pitch' in dir(leaf): written_pitch = leaf.note_head.written_pitch is_cautionary = leaf.note_head.is_cautionary is_forced = leaf.note_head.is_forced is_parenthesized = leaf.note_head.is_parenthesized elif 'written_pitches' in dir(leaf): written_pitches = [x.written_pitch for x in leaf.note_heads] if written_pitches: written_pitch = written_pitches[0] is_cautionary = leaf.note_heads[0].is_cautionary is_forced = leaf.note_heads[0].is_forced is_parenthesized = leaf.note_heads[0].is_parenthesized elif len(args) == 2: written_pitch, written_duration = args elif len(args) == 0: written_pitch = 'C4' written_duration = durationtools.Duration(1, 4) else: message = 'can not initialize note from {!r}.' raise ValueError(message.format(args)) Leaf.__init__(self, written_duration) if written_pitch is not None: if written_pitch not in drums: self.note_head = scoretools.NoteHead( written_pitch=written_pitch, is_cautionary=is_cautionary, is_forced=is_forced, is_parenthesized=is_parenthesized, ) else: self.note_head = scoretools.DrumNoteHead( written_pitch=written_pitch, is_cautionary=is_cautionary, is_forced=is_forced, is_parenthesized=is_parenthesized, ) else: self.note_head = None if len(args) == 1 and isinstance(args[0], Leaf): self._copy_override_and_set_from_leaf(args[0])
def test_lilypondparsertools_LilyPondParser_accidentals_cautionary_02(): string = "{ <c? e g??>4 }" parsed = lilypondparsertools.LilyPondParser()(string) assert parsed[0].note_heads[0].is_cautionary == True assert parsed[0].note_heads[1].is_cautionary == False assert parsed[0].note_heads[2].is_cautionary == True assert format(parsed[0]) == '<c? e g?>4'
def __init__(self, *args): from abjad.tools import lilypondparsertools from abjad.tools import scoretools assert len(args) in (0, 1, 2) self._note_heads = scoretools.NoteHeadInventory(client=self, ) if len(args) == 1 and isinstance(args[0], str): string = '{{ {} }}'.format(args[0]) parsed = lilypondparsertools.LilyPondParser()(string) assert len(parsed) == 1 and isinstance(parsed[0], Leaf) args = [parsed[0]] is_cautionary = [] is_forced = [] if len(args) == 1 and isinstance(args[0], Leaf): leaf = args[0] written_duration = leaf.written_duration if hasattr(leaf, 'written_pitch'): written_pitches = [leaf.written_pitch] is_cautionary = [leaf.note_head.is_cautionary] is_forced = [leaf.note_head.is_forced] elif hasattr(leaf, 'written_pitches'): written_pitches = leaf.written_pitches is_cautionary = [x.is_cautionary for x in leaf.note_heads] is_forced = [x.is_forced for x in leaf.note_heads] else: written_pitches = [] elif len(args) == 2: written_pitches, written_duration = args if isinstance(written_pitches, str): written_pitches = [x for x in written_pitches.split() if x] elif isinstance(written_pitches, type(self)): written_pitches = written_pitches.written_pitches elif len(args) == 0: written_pitches = [0, 4, 7] written_duration = durationtools.Duration(1, 4) else: message = 'can not initialize chord from {!r}.' message = message.format(args) raise ValueError(message) Leaf.__init__(self, written_duration) if not is_cautionary: is_cautionary = [False] * len(written_pitches) if not is_forced: is_forced = [False] * len(written_pitches) for written_pitch, cautionary, forced in zip(written_pitches, is_cautionary, is_forced): note_head = scoretools.NoteHead( written_pitch=written_pitch, is_cautionary=cautionary, is_forced=forced, ) self._note_heads.append(note_head) if len(args) == 1 and isinstance(args[0], Leaf): self._copy_override_and_set_from_leaf(args[0])
def __init__(self, written_duration=None): from abjad.tools import lilypondparsertools original_input = written_duration if isinstance(written_duration, str): string = '{{ {} }}'.format(written_duration) parsed = lilypondparsertools.LilyPondParser()(string) assert len(parsed) == 1 and isinstance(parsed[0], Leaf) written_duration = parsed[0] if isinstance(written_duration, Leaf): written_duration = written_duration.written_duration elif written_duration is None: written_duration = durationtools.Duration(1, 4) else: written_duration = durationtools.Duration(written_duration) Leaf.__init__(self, written_duration) if isinstance(original_input, Leaf): self._copy_override_and_set_from_leaf(original_input)
def parse(arg, language='english'): r'''Parses `arg` as LilyPond string. :: >>> parse("{c'4 d'4 e'4 f'4}") Container("c'4 d'4 e'4 f'4") :: >>> container = _ :: >>> print(format(container)) { c'4 d'4 e'4 f'4 } A pitch-name language may also be specified. :: >>> parse("{c'8 des' e' fis'}", language='nederlands') Container("c'8 df'8 e'8 fs'8") Returns Abjad expression. ''' from abjad.tools import rhythmtreetools from abjad.tools import lilypondparsertools if arg.startswith('abj:'): return lilypondparsertools.parse_reduced_ly_syntax(arg[4:]) elif arg.startswith('rtm:'): return rhythmtreetools.parse_rtm_syntax(arg[4:]) if language not in _lilypond_parsers_by_language: parser = lilypondparsertools.LilyPondParser(default_language=language) _lilypond_parsers_by_language[language] = parser return _lilypond_parsers_by_language[language](arg)
def _parse_string(self, string): from abjad.tools import lilypondfiletools from abjad.tools import lilypondparsertools from abjad.tools import rhythmtreetools user_input = string.strip() if user_input.startswith('abj:'): parser = lilypondparsertools.ReducedLyParser() parsed = parser(user_input[4:]) if parser._toplevel_component_count == 1: parsed = Container([parsed]) elif user_input.startswith('rtm:'): parsed = rhythmtreetools.parse_rtm_syntax(user_input[4:]) else: if (not user_input.startswith('<<') or not user_input.endswith('>>')): user_input = '{{ {} }}'.format(user_input) parsed = lilypondparsertools.LilyPondParser()(user_input) if isinstance(parsed, lilypondfiletools.LilyPondFile): parsed = Container(parsed.items[:]) assert isinstance(parsed, Container) return parsed
def __init__( self, contents=None, direction=None, stack_priority=0, ): from abjad.tools import lilypondparsertools from abjad.tools import markuptools if contents is None: new_contents = ('', ) elif isinstance(contents, str): to_parse = r'\markup {{ {} }}'.format(contents) parsed = lilypondparsertools.LilyPondParser()(to_parse) if all(isinstance(x, str) for x in parsed.contents): new_contents = (' '.join(parsed.contents), ) else: new_contents = tuple(parsed.contents) elif isinstance(contents, markuptools.MarkupCommand): new_contents = (contents, ) elif isinstance(contents, type(self)): direction = direction or contents._direction new_contents = tuple(contents._contents) elif isinstance(contents, (list, tuple)) and 0 < len(contents): new_contents = [] for arg in contents: if isinstance(arg, (str, markuptools.MarkupCommand)): new_contents.append(arg) else: new_contents.append(str(arg)) new_contents = tuple(new_contents) else: new_contents = (str(contents), ) self._contents = new_contents self._format_slot = 'right' direction = \ stringtools.arg_to_tridirectional_ordinal_constant(direction) self._direction = direction assert isinstance(stack_priority, int), repr(stack_priority) self._stack_priority = stack_priority
def __init__(self, *args): from abjad.tools import lilypondparsertools input_leaf = None written_duration = None if len(args) == 1 and isinstance(args[0], str): string = '{{ {} }}'.format(args[0]) parsed = lilypondparsertools.LilyPondParser()(string) assert len(parsed) == 1 and isinstance(parsed[0], Leaf) input_leaf = parsed[0] written_duration = input_leaf.written_duration elif len(args) == 1 and isinstance(args[0], Leaf): written_duration = args[0].written_duration input_leaf = args[0] elif len(args) == 1 and not isinstance(args[0], str): written_duration = args[0] elif len(args) == 0: written_duration = durationtools.Duration(1, 4) else: message = 'can not initialize skip from {!r}.' message = message.format(args) raise ValueError(message) Leaf.__init__(self, written_duration) if input_leaf is not None: self._copy_override_and_set_from_leaf(input_leaf)