#! /usr/bin/env python

from abjad.tools import iotools
import os


def format_lilypond_context_names_with_underscores(lilypond_context_names):
   context_names_with_underscores = [ ]
   for line in lilypond_context_names.split('\n'):
      try:
         context = line.split(' ')[1]
      except IndexError:
         continue
      context = context.strip('\n')
      new_context_name = context[0].lower()
      for x in context[1:]:
         if x.isupper():
            new_context_name += '_'
            new_context_name += x.lower()
         else:
            new_context_name += x
      context_names_with_underscores.append(new_context_name)
   return context_names_with_underscores


# from lilypond 2.13 internals reference on 2010-08-14
lilypond_context_names = '''
2.1.1 ChoirStaff     
2.1.2 ChordNames     
2.1.3 CueVoice    
2.1.4 Devnull     
2.1.5 DrumStaff      
2.1.6 DrumVoice      
2.1.7 Dynamics    
2.1.8 FiguredBass    
2.1.9 FretBoards     
2.1.10 Global     
2.1.11 GrandStaff    
2.1.12 GregorianTranscriptionStaff     
2.1.13 GregorianTranscriptionVoice     
2.1.14 Lyrics     
2.1.15 MensuralStaff    
2.1.16 MensuralVoice    
2.1.17 NoteNames     
2.1.18 PianoStaff    
2.1.19 RhythmicStaff    
2.1.20 Score      
2.1.21 Staff      
2.1.22 StaffGroup    
2.1.23 TabStaff      
2.1.24 TabVoice      
2.1.25 VaticanaStaff    
2.1.26 VaticanaVoice    
2.1.27 Voice      
'''

lilypond_grob_names = '''
3.1.1 Accidental     
3.1.2 AccidentalCautionary    
3.1.3 AccidentalPlacement     
3.1.4 AccidentalSuggestion    
3.1.5 Ambitus     
3.1.6 AmbitusAccidental    
3.1.7 AmbitusLine    
3.1.8 AmbitusNoteHead      
3.1.9 Arpeggio    
3.1.10 BalloonTextItem     
3.1.11 BarLine    
3.1.12 BarNumber     
3.1.13 BassFigure    
3.1.14 BassFigureAlignment    
3.1.15 BassFigureAlignmentPositioning     
3.1.16 BassFigureBracket      
3.1.17 BassFigureContinuation    
3.1.18 BassFigureLine      
3.1.19 Beam    
3.1.20 BendAfter     
3.1.21 BreakAlignGroup     
3.1.22 BreakAlignment      
3.1.23 BreathingSign    
3.1.24 ChordName     
3.1.25 Clef    
3.1.26 ClusterSpanner      
3.1.27 ClusterSpannerBeacon      
3.1.28 CombineTextScript      
3.1.29 Custos     
3.1.30 DotColumn     
3.1.31 Dots    
3.1.32 DoublePercentRepeat    
3.1.33 DoublePercentRepeatCounter      
3.1.34 DynamicLineSpanner     
3.1.35 DynamicText      
3.1.36 DynamicTextSpanner     
3.1.37 Episema    
3.1.38 Fingering     
3.1.39 FretBoard     
3.1.40 Glissando     
3.1.41 GraceSpacing     
3.1.42 GridLine      
3.1.43 GridPoint     
3.1.44 Hairpin    
3.1.45 HarmonicParenthesesItem      
3.1.46 HorizontalBracket      
3.1.47 InstrumentName      
3.1.48 InstrumentSwitch    
3.1.49 KeyCancellation     
3.1.50 KeySignature     
3.1.51 LaissezVibrerTie    
3.1.52 LaissezVibrerTieColumn    
3.1.53 LedgerLineSpanner      
3.1.54 LeftEdge      
3.1.55 LigatureBracket     
3.1.56 LyricExtender    
3.1.57 LyricHyphen      
3.1.58 LyricSpace    
3.1.59 LyricText     
3.1.60 MeasureGrouping     
3.1.61 MelodyItem    
3.1.62 MensuralLigature    
3.1.63 MetronomeMark    
3.1.64 MultiMeasureRest    
3.1.65 MultiMeasureRestNumber    
3.1.66 MultiMeasureRestText      
3.1.67 NonMusicalPaperColumn     
3.1.68 NoteCollision    
3.1.69 NoteColumn    
3.1.70 NoteHead      
3.1.71 NoteName      
3.1.72 NoteSpacing      
3.1.73 OctavateEight    
3.1.74 OttavaBracket    
3.1.75 PaperColumn      
3.1.76 ParenthesesItem     
3.1.77 PercentRepeat    
3.1.78 PercentRepeatCounter      
3.1.79 PhrasingSlur     
3.1.80 PianoPedalBracket      
3.1.81 RehearsalMark    
3.1.82 RepeatSlash      
3.1.83 RepeatTie     
3.1.84 RepeatTieColumn     
3.1.85 Rest    
3.1.86 RestCollision    
3.1.87 Script     
3.1.88 ScriptColumn     
3.1.89 ScriptRow     
3.1.90 Slur    
3.1.91 SostenutoPedal      
3.1.92 SostenutoPedalLineSpanner    
3.1.93 SpacingSpanner      
3.1.94 SpanBar    
3.1.95 StaffGrouper     
3.1.96 StaffSpacing     
3.1.97 StaffSymbol      
3.1.98 StanzaNumber     
3.1.99 Stem    
3.1.100 StemTremolo     
3.1.101 StringNumber    
3.1.102 StrokeFinger    
3.1.103 SustainPedal    
3.1.104 SustainPedalLineSpanner     
3.1.105 System    
3.1.106 SystemStartBar     
3.1.107 SystemStartBrace      
3.1.108 SystemStartBracket    
3.1.109 SystemStartSquare     
3.1.110 TabNoteHead     
3.1.111 TextScript      
3.1.112 TextSpanner     
3.1.113 Tie    
3.1.114 TieColumn    
3.1.115 TimeSignature      
3.1.116 TrillPitchAccidental     
3.1.117 TrillPitchGroup    
3.1.118 TrillPitchHead     
3.1.119 TrillSpanner    
3.1.120 TupletBracket      
3.1.121 TupletNumber    
3.1.122 UnaCordaPedal      
3.1.123 UnaCordaPedalLineSpanner    
3.1.124 VaticanaLigature      
3.1.125 VerticalAlignment     
3.1.126 VerticalAxisGroup     
3.1.127 VoiceFollower      
3.1.128 VoltaBracket    
3.1.129 VoltaBracketSpanner
'''

if __name__ == '__main__':
   iotools.clear_terminal()
   print 'Formatting LilyPond context names with underscores ...\n'
   print format_lilypond_context_names_with_underscores(lilypond_context_names)
   print ''
   print 'Formatting LilyPond grob names with underscores ...\n'
   print format_lilypond_context_names_with_underscores(lilypond_grob_names)
   print ''
