B
    Åû_»…  ã               @   sZ  d dl Z d dlZd dlZd dlZd dlZd dlZd dlZd dlZd dlZd dl	Z
e
jZe
jZe
jZe
jZdZdZdZdZdZdZdZd	Zd
ZdZdZdZdZdZdZdZdZ dZ!dZ"dZ#dZ$dZ%dZ&dZ'dZ(dZ)dZ*dZ+dZ,dZ-eeeeiZ.eee,e+ee-e&fZ/eeeeee&ee+e,eegZ0ee)d ie#e$d!ed"ed#e'd$e(d%ed&iee d'ie%e*d(iiZ1d)d*„ Z2d+Z3d,Z4d-Z5d.d/„ Z6e 7d0ej8¡Z9e 7d1ej8¡Z:e 7d2¡Z;d3d4„ Z<G d5d6„ d6e=ƒZ>G d7d8„ d8ƒZ?G d9d:„ d:e?ƒZ@G d;d<„ d<e?ƒZAG d=d>„ d>eAƒZBG d?d@„ d@eAƒZCe 7dAej8¡ZDe 7dBej8¡ZEG dCdD„ dDeCƒZFG dEdF„ dFeFƒZGG dGdH„ dHeAƒZHeFeCeCeBeHeGdIœZIdS )Jé    NZafterZaltZbeforeZdoctitleZexampleindentÚfilenameÚfilterZfragmentZlayoutz
line-widthZ
nofragmentZ	nogettextZnoindentÚindentznoragged-rightÚbodyZnotimeÚoutputZoutputimageZpaperZ	papersizeZpreambleZprintfilenameZquotezragged-rightÚrelativeÚ	staffsizeZtexidocZverbatimÚlilypondversionz\relative c%(relative_quotes)sz!#(set-paper-size "%(papersize)s")zindent = %(indent)szÎline-width = %(line-width)s
  %% offset the left padding, also add 1mm as lilypond creates cropped
  %% images with a little space on the right
  line-width = #(- line-width (* mm  %(padding_mm)f) (* mm 1))zèline-width = %(line-width)s - 2.0 * %(exampleindent)s
  %% offset the left padding, also add 1mm as lilypond creates cropped
  %% images with a little space on the right
  line-width = #(- line-width (* mm  %(padding_mm)f) (* mm 1))zragged-right = ##tzragged-right = ##fzg
 \context {
   \Score
   timing = ##f
 }
 \context {
   \Staff
   \remove "Time_signature_engraver"
 }z&#(set-global-staff-size %(staffsize)s)c             C   s¬   | dkrt |fS | dkr(|d kr(td fS t d| ¡}|rFd| d¡fS t d| ¡}|rdd| d¡fS | dkst| d	kr¨t d
|¡}|r¨t| d¡ƒ}| d|| d¡f fS dS )NZ	lilyquoteZ
singlelinezrelative\s*([-0-9])r   é   z
([0-9]+)ptr   r   z
line-widthz"([-.0-9]+)(cm|in|mm|pt|staffspace)z%f\%sé   )NN)ÚQUOTEÚRAGGED_RIGHTÚreÚsearchÚgroupÚmatchÚfloat)ÚkeyÚvalueÚmÚf© r   úo/home/gub/NewGub/gub/target/mingw/src/lilypond-git.sv.gnu.org--lilypond.git-stable-2.22/python/book_snippets.pyÚ#classic_lilypond_book_compatibilityŸ   s     r   a|  %%%% Generated by lilypond-book
%%%% Options: [%(option_string)s]
\include "lilypond-book-preamble.ly"


%% ****************************************************************
%% Start cut-&-pastable-section
%% ****************************************************************

%(preamble_string)s

\paper {
  %(paper_string)s
}

\layout {
  %(layout_string)s
}

%(safe_mode_string)s
a?  


%% ****************************************************************
%% ly snippet:
%% ****************************************************************
%(code)s


%% ****************************************************************
%% end ly snippet
%% ****************************************************************
ae  
%(notes_string)s
{


%% ****************************************************************
%% ly snippet contents follows:
%% ****************************************************************
%(code)s


%% ****************************************************************
%% end ly snippet
%% ****************************************************************
}
c             C   s2   t | dƒ d¡}t d|¡}|r.t| d¡ƒS dS )NÚrbi   s   
%%Pages: ([0-9]+)r
   r   )ÚopenÚreadr   r   Úintr   )Zps_nameÚheaderr   r   r   r   Úps_page_countñ   s
    r   z^([a-zA-Z]+)[\t ]*=z(%+[\t ]*)(.*)$zy\\(?:new|context)\s+(?:[a-zA-Z]*?(?:Staff(?:Group)?|Voice|FiguredBass|FretBoards|Names|Devnull))\s+=\s+"?([a-zA-Z]+)"?\s+c             C   s   |  d¡| |  d¡ƒ S )Nr
   r   )r   )Útr   r   r   r   Úly_comment_gettext   s    r!   c               @   s   e Zd ZdS )ÚCompileErrorN)Ú__name__Ú
__module__Ú__qualname__r   r   r   r   r"     s   r"   c               @   sX   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zeed	œd
d„Zedœdd„Z	edœdd„Z
dS )ÚChunkc             C   s   dS )NÚ r   )Úselfr   r   r   Úreplacement_text  s    zChunk.replacement_textc             C   s   |   ¡ S )N)r)   )r(   r   r   r   Úfilter_text  s    zChunk.filter_textc             C   s   dS )NFr   )r(   r   r   r   Úis_plain  s    zChunk.is_plainc             C   s   d| _ d| _d S )Nr'   )Ú_input_fullpathÚ_output_fullpath)r(   r   r   r   Ú__init__  s    zChunk.__init__)Úin_fpÚout_fpc             C   s   || _ || _d S )N)r,   r-   )r(   r/   r0   r   r   r   Úset_document_fullpaths  s    zChunk.set_document_fullpaths)Úreturnc             C   s   | j S )z0The input file path where this chunk comes from.)r,   )r(   r   r   r   Úinput_fullpath  s    zChunk.input_fullpathc             C   s   | j S )z0The output file path that this chunk belongs to.)r-   )r(   r   r   r   Úoutput_fullpath"  s    zChunk.output_fullpathN)r#   r$   r%   r)   r*   r+   r.   Ústrr1   r3   r4   r   r   r   r   r&     s   r&   c               @   s(   e Zd ZdZdd„ Zdd„ Zdd„ ZdS )	Ú	Substringz,A string that does not require extra memory.c             C   s"   || _ || _|| _|| _d | _d S )N)ÚsourceÚstartÚendÚline_numberÚoverride_text)r(   r7   r8   r9   r:   r   r   r   r.   *  s
    zSubstring.__init__c             C   s   dS )NTr   )r(   r   r   r   r+   1  s    zSubstring.is_plainc             C   s"   | j r| j S | j| j| j… S d S )N)r;   r7   r8   r9   )r(   r   r   r   r)   4  s    zSubstring.replacement_textN)r#   r$   r%   Ú__doc__r.   r+   r)   r   r   r   r   r6   '  s   r6   c               @   s4   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ ZdS )ÚSnippetc             C   sB   || _ || _d| _i | _|| _|| _|| _|jd tj	dœ| _
d S )Nr   Úprogram_version)r>   Úprogram_name)Útyper   ÚchecksumÚoption_dictÚ	formatterr:   Úglobal_optionsÚinformationÚlyr?   Úreplacements)r(   r@   r   rC   r:   rD   r   r   r   r.   <  s    zSnippet.__init__c             C   s   t   | j¡S )N)ÚcopyrG   )r(   r   r   r   Úget_replacementsI  s    zSnippet.get_replacementsc             C   s   | j  d¡S )Nr   )r   r   )r(   r   r   r   r)   L  s    zSnippet.replacement_textc             C   s   | j  |¡S )N)r   r   )r(   Úsr   r   r   Ú	substringO  s    zSnippet.substringc             C   s   t | jƒd | j S )Nz type = )ÚreprÚ	__class__r@   )r(   r   r   r   Ú__repr__R  s    zSnippet.__repr__N)r#   r$   r%   r.   rI   r)   rK   rN   r   r   r   r   r=   ;  s
   r=   c               @   s   e Zd Zdd„ Zdd„ ZdS )ÚIncludeSnippetc             C   s"   |   d¡}tj |¡d | jj S )Nr   r   )rK   ÚosÚpathÚsplitextrC   Zdefault_extension)r(   r   r   r   r   Úprocessed_filenameW  s    
z!IncludeSnippet.processed_filenamec             C   s(   | j  d¡}|  d¡}t ||  ¡ |¡S )Nr   r   )r   r   rK   r   ÚsubrS   )r(   rJ   r   r   r   r   r)   [  s    
zIncludeSnippet.replacement_textN)r#   r$   r%   rS   r)   r   r   r   r   rO   V  s   rO   c               @   sà   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ Zdd„ Zdd„ Z	dd„ Z
dd„ Zdd„ Zdd„ Zdd„ ZeZdd„ Zdd„ Zdd„ Zdd „ Zd!d"„ Zd#d$„ Zd%d&„ Zeeed'œd(d)„Zed*œd+d,„Zd-d.„ Zd/d0„ Zd1d2„ Zd3S )4ÚLilypondSnippetc             C   s<   t  | |||||¡ d| _d| _| d¡}|  || j¡ d S )Nr'   z.lyÚoptions)r=   r.   r   Úextr   Úparse_snippet_optionsr@   )r(   r@   r   rC   r:   rD   rP   r   r   r   r.   b  s    
zLilypondSnippet.__init__c             C   s   g S )Nr   )r(   r   r   r   Úsnippet_optionsj  s    zLilypondSnippet.snippet_optionsc                s²   | j j}|s|S ytj| ‰ W n
   |S t ‡ fdd„|¡}tj| jr®x0t 	|¡D ]"}t
 d| dˆ |ƒ d |¡}qVW x0t 	|¡D ]"}t
 d| dˆ |ƒ d |¡}qˆW |S )Nc                s
   t ˆ | ƒS )N)r!   )r   )r    r   r   Ú<lambda>|  ó    z1LilypondSnippet.verb_ly_gettext.<locals>.<lambda>z'(?m)(?<!\\clef)(^|[' \\#])%s([^a-zA-Z])z\1z\2z(\s+|")%s(\s+|"))rC   Údocument_languageZlangdefsZtranslationÚly_comment_rerT   ZLANGDICTZenable_ly_identifier_l10nÚly_var_def_reÚfindallr   Úly_context_id_re)r(   rJ   ZlangÚvÚidr   )r    r   Úverb_ly_gettextm  s$    



zLilypondSnippet.verb_ly_gettextc             C   s4   |   d¡}t| jkr|  |¡}| d¡s0|d7 }|S )NÚcodeÚ
)rK   Ú	NOGETTEXTrB   rc   Úendswith)r(   Z	verb_textr   r   r   Úverb_ly‰  s    



zLilypondSnippet.verb_lyc             C   s   |   d¡}d| jd |f S )Nrd   z\sourcefileline %d
%sr
   )rK   r:   )r(   Úcontentsr   r   r   rF   ‘  s    
zLilypondSnippet.lyc             C   s   |   ¡ }|r|  |¡S dS )Nr'   )rF   Ú
compose_ly)r(   rJ   r   r   r   Úfull_ly–  s    
zLilypondSnippet.full_lyc             C   s   | j  |¡S )N)rC   Zsplit_snippet_options)r(   Úoption_stringr   r   r   Úsplit_optionsœ  s    zLilypondSnippet.split_optionsc             C   s°  i | _ |  |¡}xÎ|D ]Æ}|d  }}d|kr>t d|¡\}}n&|tkrdt| | jkrd| j t| = d }t||ƒ\}}|rÎ|r¤ttdƒ||f ƒ ttdƒ||f ƒ n ttdƒ| ƒ ttdƒ| ƒ || }}|r|| j |< qW t	| j k}	|	r| j t	 d kr| j t	= t
| j krd | j t< tdi| _| j | jj¡ | j | j ¡ dd	„ t| j ¡ ƒD ƒ}
g }x:|
D ]2\}}|d kr†| |¡ n| |d | ¡ qhW | ¡  || _d S )
Nú=z\s*=\s*z deprecated ly-option used: %s=%sz%compatibility mode translation: %s=%szdeprecated ly-option used: %sz"compatibility mode translation: %sz0\mmc             S   s   g | ]}|d  t kr|‘qS )r   )ÚPROCESSING_INDEPENDENT_OPTIONS)Ú.0Zx_yr   r   r   ú
<listcomp>Ô  s    z9LilypondSnippet.parse_snippet_options.<locals>.<listcomp>)Zsnippet_option_dictrm   r   ÚsplitÚ
no_optionsrB   r   ÚwarningÚ_Ú
LINE_WIDTHÚRELATIVEÚFRAGMENTÚINDENTÚupdaterC   Údefault_snippet_optionsÚiterÚitemsÚappendÚsortÚoutputrelevant_option_list)r(   rl   r@   rV   Úoptionr   r   Zc_keyZc_valueZhas_line_widthZlstZoption_listr   r   r   rX   Ÿ  sP    







z%LilypondSnippet.parse_snippet_optionsc             C   s   | j S )N)r€   )r(   r   r   r   Ú!get_outputrelevant_option_stringsì  s    z1LilypondSnippet.get_outputrelevant_option_stringsc             C   sP  d}i }d|t < d|t< | | jj¡ | jj|d< d |  ¡ ¡}i }t	t
ttg}x|D ]}g ||< qVW t| j ¡ ƒ}x–|D ]Ž}	| j|	 }
|
r–|
||	< n|	|kr¦d ||	< d}x4|D ],}|	t| kr°||  t| |	 ¡ d}P q°W |sz|	tkrz|	|  ¡ krzttdƒ|	 ƒ qzW t|kr,|t r,t|t ƒ}d}|dk rJ|d|  7 }n|dkr`|d	| 7 }xBt|t ƒD ]2\}}| d
¡rn|t  d|t  |¡¡ P qnW d |t ¡| }d |t ¡| }d |t	 ¡tƒ  }d |t
 ¡| }| jj}| jjrd}nd}tƒ  ¡ }| tƒ ¡ | | jj ¡ t!| jkr@t"}nt#}t$| | S )Nr
   z0.4\inz5\inÚ
padding_mmú,r   zignoring unknown ly option: %sr'   ú'z#(set-paper-sizez
  z#(ly:set-option 'safe #t))%ÚEXAMPLEINDENTrv   rz   rC   r{   rD   rƒ   Újoinr‚   ÚNOTESÚPREAMBLEÚLAYOUTÚPAPERÚsortedrB   ÚkeysrY   r~   Úsimple_optionsrt   ru   rw   r   Ú	enumerateÚ
startswithÚinsertÚpopÚvarsZ	safe_modeÚglobalsrH   ÚlocalsrE   rx   ÚFRAGMENT_LYÚFULL_LYÚPREAMBLE_LY)r(   rd   r   Úoverriderl   Zcompose_dictZcompose_typesÚaZoption_namesr   r   ÚfoundÚtypZrelative_quotesÚiÚelemZpaper_stringZlayout_stringZnotes_stringZpreamble_stringrƒ   Zsafe_mode_stringÚdr   r   r   r   rj   ï  sh    








zLilypondSnippet.compose_lyc             C   sZ   | j sTt |  |  ¡ ¡ d¡¡}x |  ¡ D ]}| | d¡¡ q*W | ¡ d d… | _ | j S )Nzutf-8é
   )	rA   ÚhashlibZmd5Úrelevant_contentsrF   Úencoder‚   rz   Z	hexdigest)r(   Úhashr   r   r   r   Úget_checksumV  s    zLilypondSnippet.get_checksumc             C   s.   |   ¡ }tj |d d… d|dd …  ¡}|S )Nr   zlily-%s)r¥   rP   rQ   r‡   )r(   ZcsÚnamer   r   r   Úbasenamee  s    "zLilypondSnippet.basenamec       	      C   sÔ   |   ¡ }tj | jj|¡}tj |¡d }tj|dd |d }tj |¡r´t	 
|dd¡ ¡ }|  |¡|  |  ¡ ¡krÐtd| ƒ |  ¡  d¡}d| }tj |  ||¡ d¡¡ nt	 
|d	d¡}| |  ¡ ¡ d S )
Nr   T)Úexist_okz.lyÚrzutf-8zd%s: duplicate filename but different contents of original file,
printing diff against existing file.zdiff -u %s -Úw)r§   rP   rQ   r‡   rD   Úlily_output_dirrr   ÚmakedirsÚexistsÚcodecsr   r   r¢   rk   rt   r£   ÚsysÚstderrÚwriteÚfilter_pipeÚdecode)	r(   ÚbaserQ   Ú	directoryr   ÚexistingÚencodedÚcmdÚoutr   r   r   Úwrite_lyl  s     

zLilypondSnippet.write_lyc             C   s   t  dd|¡S )Nz1\\(version|sourcefileline|sourcefilename)[^\n]*\nr'   )r   rT   )r(   rF   r   r   r   r¢   €  s    z!LilypondSnippet.relevant_contentsc          
   C   sà  |   |¡\}}|r4ttdƒd |¡ ƒ t|  ¡ ƒ‚x¤|D ]š}| jjržt| t	ƒržt
j |¡\}}| d¡}t|ƒdkrˆd|d  }	nd}	|  ¡ |	 | }
n|}
yt
 t
j ||
¡¡ W n tk
rÎ   Y nX t
j ||¡}t
j ||
¡}t
j |¡d }t
j|dd	 y–| jjrjt| t	ƒrjt|d
ƒ ¡ }|  ¡  d¡}|  ¡  d¡}| ||¡}t|dƒ |¡ n4yt
 ||¡ W n" tk
rœ   t ||¡ Y nX W q< ttfk
rÖ   ttdƒ| ƒ t|  ¡ ƒ‚Y q<X q<W d S )NzMissing files: %sz, ú-r   éÿÿÿÿr'   r   T)r¨   r   zutf-8ÚwbzCould not overwrite file %s)Úall_output_filesÚerrorru   r‡   r"   r§   rD   Úuse_source_file_namesÚ
isinstanceÚLilypondFileSnippetrP   rQ   rR   rr   ÚlenÚfinal_basenameÚunlinkÚOSErrorr¬   r   r   r£   Úreplacer±   ÚlinkÚAttributeErrorÚshutilZcopyfileÚIOError)r(   Ú
output_dirZdestinationr¶   Úmissingr¦   r´   rW   Z
componentsZbase_suffixZ
final_nameÚsrcZdstZdst_pathZcontentr§   rÄ   r   r   r   Úlink_all_output_filesƒ  sJ    


z%LilypondSnippet.link_all_output_filesc             C   s   g S )Nr   )r(   r´   Úfullr   r   r   Úadditional_files_to_consider¯  s    z,LilypondSnippet.additional_files_to_considerc             C   s"   g }| j dkr| || j  ¡ |S )Nz.ly)rW   r~   )r(   r´   rÐ   Úresultr   r   r   Úadditional_files_required²  s    
z)LilypondSnippet.additional_files_requiredc          	      sî  t ƒ ‰t ƒ ‰ |  ¡ }tj ˆ|¡}‡‡fdd„}‡ ‡‡fdd„}| jj}||d ƒ |sd||d ƒ d| jjkr|||d ƒ xD|d	 |d
 |d |d |d |d |d |d gD ]}||ƒ q²W | jj	rüx0|d | jj	 |d | jj	 gD ]}||ƒ qìW | j 
| ||ˆ¡}x|D ]}||ƒ qW d}	|sNˆ sNtt|d dd ¡ ƒ}	xVtd|	d ƒD ]D}
d||
f }||d
 ƒ ||d ƒ d| jjkr^||d ƒ q^W x|  ||¡D ]}||ƒ q´W x|  ||¡D ]}||ƒ qÔW ˆˆ fS )z†Return all files generated in lily_output_dir, a set.

        output_dir_files is the list of files in the output directory.
        c                s$   t j t j ˆ | ¡¡r ˆ | ¡ d S )N)rP   rQ   Úisfiler‡   Úadd)r¦   )rÌ   rÒ   r   r   Úconsider_fileÂ  s    z7LilypondSnippet.all_output_files.<locals>.consider_filec                s0   t j t j ˆ| ¡¡r"ˆ | ¡ n
ˆ  | ¡ d S )N)rP   rQ   rÔ   r‡   rÕ   )r¦   )rÍ   rÌ   rÒ   r   r   Úrequire_fileÆ  s    z6LilypondSnippet.all_output_files.<locals>.require_filez.lyz-systems.countzdseparate-log-filez.logz.texz.epsz.pdfz.texidocz	.doctitlez-systems.texiz-systems.texz-systems.pdftexir   Úutf8)Úencodingr
   z%s-%dzddump-signaturez
.signature)Úsetr§   rP   rQ   r‡   rD   Zskip_lilypond_runZprocess_cmdrC   r\   Úrequired_filesr   r   r   ÚrangerÑ   rÓ   )r(   rÌ   r´   rÐ   rÖ   r×   Z	skip_lilyr   rÛ   Zsystem_countZnumberZ
systemfiler   )rÍ   rÌ   rÒ   r   r¾   ¸  sV    
z LilypondSnippet.all_output_filesc             C   s   |   |¡\}}|S )N)r¾   )r(   rÌ   r›   rÍ   r   r   r   Úis_outdatedÿ  s    zLilypondSnippet.is_outdated)Úinputr¸   r2   c             C   sö   t tdƒ| dƒ d}tjdkr$d}tj|dtjtjtj|d}|j|j|j	  }}}| 
|¡ | ¡ }|s€d}| ¡ }	| ¡ }| ¡  d¡}
|s–d}d|@ }|sª|	sê|
rê|d	? }t td
ƒ||f ¡ t tdƒ¡ tj	 
|
¡ t|ƒ t dƒ |	S )z°Pass input through cmd, and return the result.

        Args:
          input: the input
          cmd: a shell command

        Returns:
          the filtered result
        zRunning through filter `%s'TZmingw32F)ÚshellÚstdinÚstdoutr°   Z	close_fdsr   zutf-8é   é   z`%s' failed (%d)zThe error log is as follows:re   )Údebugru   r¯   ÚplatformÚ
subprocessÚPopenÚPIPErà   rá   r°   r±   Úcloser   r³   rF   r¿   Úexit)r(   rÞ   r¸   ZclosefdsÚprà   rá   r°   Zstatusr   ÚerrÚsignalZexit_statusr   r   r   r²     s2    


zLilypondSnippet.filter_pipe)r2   c             C   s
   |   d¡S )Nrd   )rK   )r(   r   r   r   Úget_snippet_code/  s    z LilypondSnippet.get_snippet_codec             C   sT   |   ¡  d¡}|  || jj¡}| j d¡}|dkr6d}| d¡|dœ}| j 	t
|¡S )z@Run snippet bodies through a command (say: convert-ly).
        zutf-8rV   Nr'   )rd   rV   )rî   r£   r²   rD   Z
filter_cmdr   r   r³   rC   Zoutput_simple_replacementsÚFILTER)r(   rd   r   rV   rŸ   r   r   r   r*   2  s    zLilypondSnippet.filter_textc             C   s   |   ¡ }| j || ¡S )N)rÄ   rC   Zsnippet_output)r(   r´   r   r   r   r)   @  s    z LilypondSnippet.replacement_textc                s´   |   ¡ ‰ | jj}dˆ  }tj ||¡}tj |dˆ  ¡}|f}tj |¡r°tj |¡rvt |¡tj t |¡tj kr°t	tj |dˆ  ¡ƒ}‡ fdd„t
d|d ƒD ƒ}t|ƒ}|S )Nz%s.pngz%s-page1.pngz%s.epsc                s   g | ]}d ˆ |f ‘qS )z%s-page%d.pngr   )rp   Zpage)r´   r   r   rq   Q  s   z.LilypondSnippet.get_images.<locals>.<listcomp>r
   )rÄ   rD   r«   rP   rQ   r‡   r­   ÚstatÚST_MTIMEr   rÜ   Útuple)r(   ÚoutdirZsingle_baseZsingleZmultipleZimagesÚcountr   )r´   r   Ú
get_imagesD  s    
zLilypondSnippet.get_imagesN)r#   r$   r%   r.   rY   rc   rh   rF   rk   rm   rX   r‚   rj   r¥   r§   rÄ   rº   r¢   rÏ   rÑ   rÓ   r¾   rÝ   Úbytesr5   r²   rî   r*   r)   rõ   r   r   r   r   rU   a  s2   Mg,G,rU   z\s+%.*?begin verbatim.*\n*z\s+%.*?end verbatim.*$c               @   sH   e Zd Zdd„ Zedœdd„Zedœdd„Zdd	„ Zd
d„ Z	dd„ Z
dS )rÂ   c             C   s*   t  | |||||¡ |  d¡| _d | _d S )Nr   )rU   r.   rK   r   ri   )r(   r@   r   rC   r:   rD   r   r   r   r.   ]  s    zLilypondFileSnippet.__init__)r2   c             C   s0   | j s*tt | j| jj| jj¡dƒ ¡ | _ | j S )Nr   )	ri   r   Ú	book_baseZ	find_filer   rD   Zinclude_pathZoriginal_dirr   )r(   r   r   r   Úget_contentsc  s    
z LilypondFileSnippet.get_contentsc             C   s   |   ¡  d¡S )Nzutf-8)rø   r³   )r(   r   r   r   rî   i  s    z$LilypondFileSnippet.get_snippet_codec             C   sN   |   ¡ }t |¡d }t |¡d }t| jkr8|  |¡}| d¡sJ|d7 }|S )Nr¼   r   re   )rî   Úre_begin_verbatimrr   Úre_end_verbatimrf   rB   rc   rg   )r(   rJ   r   r   r   rh   l  s    


zLilypondFileSnippet.verb_lyc             C   s   | j }d||  ¡ f S )Nz)\sourcefilename "%s"
\sourcefileline 0
%s)r   rî   )r(   r¦   r   r   r   rF   v  s    zLilypondFileSnippet.lyc             C   s2   | j jr&tj tj | j¡¡d }|S |  ¡ S d S )Nr   )rD   rÀ   rP   rQ   rR   r§   r   )r(   r´   r   r   r   rÄ   {  s    z"LilypondFileSnippet.final_basenameN)r#   r$   r%   r.   rö   rø   r5   rî   rh   rF   rÄ   r   r   r   r   rÂ   \  s   
rÂ   c               @   s4   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ ZdS )ÚMusicXMLFileSnippetc             C   s\   t  | |||||¡ d| _d | _tj tj | j¡¡d | _	ddddddd	d
dddœ
| _
d S )NFr
   z	--verbosez--lxmlz--compressedz
--relativez
--absolutez--no-articulation-directionsz--no-rest-positionsz--no-page-layoutz--no-beamingz
--language)
ÚverboseZlxmlÚ
compressedr   Zabsolutezno-articulation-directionszno-rest-positionszno-page-layoutz
no-beamingZlanguage)rÂ   r.   rý   Úconverted_lyrP   rQ   rR   r§   r   rW   Úmusicxml_options_dict)r(   r@   r   rC   r:   rD   r   r   r   r.   „  s    zMusicXMLFileSnippet.__init__c             C   s   t | j ¡ ƒS )N)Úlistrÿ   r   )r(   r   r   r   rY   —  s    z#MusicXMLFileSnippet.snippet_optionsc       	      C   sÂ   | j }g }xXt| j ¡ ƒD ]F\}}| j |d ¡}|d kr:q|d krN| |¡ q| |d | ¡ qW d|kr„d|kr„| d¡ d| _d |¡}t	t
dƒ| j  ƒ d| }|  |  ¡ |¡ d¡}|S )	Nrn   z.mxlz--compressedTú z Converting MusicXML file `%s'...zmusicxml2ly %s --out=- - zutf-8)r   r   rB   r}   rÿ   Úgetr~   rý   r‡   Úprogressru   r²   rø   r³   )	r(   r¦   Zxml2ly_option_listr   r   Zcmd_keyZoptsr¸   Zly_coder   r   r   Úconvert_from_musicxmlš  s"    

z)MusicXMLFileSnippet.convert_from_musicxmlc             C   s(   | j d kr|  ¡ | _ | j}d|| j f S )Nz)\sourcefilename "%s"
\sourcefileline 0
%s)rþ   r  r   )r(   r¦   r   r   r   rF   ¯  s
    

zMusicXMLFileSnippet.lyc       
      C   sH  |   ¡ }tj | jj|¡}tj |¡d }tj|dd | jrH|d }n|d }tj 	|¡r˜|  
|  ¡ d| ¡}|r¸ttdƒ| ƒ tj | d¡¡ n t|d	ƒ}| |  ¡ ¡ | ¡  |d
 }tj 	|¡r |  ¡  d¡}d| }	|  
||	¡ d¡}|rDttdƒ| ƒ tj | d¡¡ n$t |dd¡}| |  ¡ ¡ | ¡  d S )Nr   T)r¨   z.mxlz.xmlzdiff -u %s - zd%s: duplicate filename but different contents of original file,
printing diff against existing file.zutf-8r½   z.lyzdiff -u %s -zn%s: duplicate filename but different contents of converted lilypond file,
printing diff against existing file.rª   )r§   rP   rQ   r‡   rD   r«   rr   r¬   rý   r­   r²   rø   rt   ru   r¯   r°   r±   r³   r   ré   rk   r£   r®   )
r(   r´   rQ   rµ   ZxmlfilenameZdiff_against_existingr¹   r   r·   r¸   r   r   r   rº   ¶  s<    

zMusicXMLFileSnippet.write_lyN)r#   r$   r%   r.   rY   r  rF   rº   r   r   r   r   rû   ƒ  s
   rû   c               @   s    e Zd ZdZdd„ Zdd„ ZdS )ÚLilyPondVersionStringz,A string that does not require extra memory.c             C   s   t  | |||||¡ d S )N)r=   r.   )r(   r@   r   rC   r:   rD   r   r   r   r.   á  s    zLilyPondVersionString.__init__c             C   s   | j  | j| ¡S )N)rC   Zoutput_simpler@   )r(   r   r   r   r)   å  s    z&LilyPondVersionString.replacement_textN)r#   r$   r%   r<   r.   r)   r   r   r   r   r  Þ  s   r  )Zlilypond_fileZlilypond_blockZlilypondZincluder	   Zmusicxml_file)Jr®   rH   r¡   rP   r   rÊ   ræ   r¯   r÷   ZlilylibrF   r  rt   r¿   Zdebug_outputrä   ZAFTERZALTZBEFOREZDOCTITLEr†   ZFILENAMErï   rx   rŠ   rv   Z
NOFRAGMENTrf   ZNOINDENTry   ZNORAGGED_RIGHTrˆ   ZNOTIMEZOUTPUTZOUTPUTIMAGEr‹   Z	PAPERSIZEr‰   ZPRINTFILENAMEr   r   rw   Z	STAFFSIZEZTEXIDOCZVERBATIMZVERSIONrs   ro   rŽ   rY   r   r˜   r—   r–   r   ÚcompileÚMr^   r]   r`   r!   Ú	Exceptionr"   r&   r6   r=   rO   rU   rù   rú   rÂ   rû   r  Zsnippet_type_to_classr   r   r   r   Ú<module>   sÐ   

+	   z'[