
    Pi                        d Z ddlmZ ddlZddlmZmZ dgZ eh d          Z	 ej
        dej                  Z ej
        dej                  ZddZddZddZddZddZdS )a   
Sed command handler for Dippy.

Sed is safe for text processing, but has several unsafe operations:
- -i flag modifies files in place
- w command writes to files (e.g., s/foo/bar/w output.txt)
- e command (GNU sed) executes pattern space as shell command
    )annotationsN)ClassificationHandlerContextsed>   -e-f--file--expressionz
    (?:
        /w\s+(\S+)          # /w filename (standalone w command or s///w)
        |
        w\s+(\S+)           # w filename at start of command
    )
    z
    (?:
        /e\s*(?:$|;)        # s///e or /pat/e (e flag at end)
        |
        (?:^|;)\s*e\s*(?:$|;|\s)  # standalone e command
    )
    tokens	list[str]returnc                   g }d}d}|t          |           k     r>| |         }|dk    s|dk    r<|dz   t          |           k     r |                    | |dz                       d}|dz  }d|                    d          r%|                    |dd	                    d}|dz  }|d
k    s|dk    r|dz  }|                    d          r|dz  }|                    d          r>|dk    s*|                    d          s|                    d          r|dz  }|dz  }|s|s|                    |           d}|dz  }|t          |           k     >|S )z/Extract sed script strings from command tokens.   Fr   r
   T   --expression=   Nr   r	   --file=--i
--in-place)lenappend
startswith)r   scriptsifound_script_argts        ./root/projects/gits/Dippy/src/dippy/cli/sed.py_extract_scriptsr   1   s   G	A
c&kk//1I 99^++1us6{{""va!e}---#' FA<<(( 	NN1RSS6"""#FA 99XFA<<	"" 	FA << 	DyyALL..y!,,|2L2LyQFA   	$ 	$NN1#	QM c&kk//P N    r   c                    g }| D ]`}t                               |          D ]C}|                    d          p|                    d          }|r|                    |           Da|S )z2Extract file paths from w commands in sed scripts.r   r   )WRITE_PATTERNfinditergroupr   )r   targetsscriptmatchpaths        r   _extract_write_targetsr)   b   sx    G % %"++F33 	% 	%E;;q>>3U[[^^D %t$$$		%
 Nr    boolc                J    | D ]}t                               |          r dS  dS )z=Check if any script contains the e command (shell execution).TF)EXECUTE_PATTERNsearch)r   r&   s     r   _has_execute_commandr.   n   s8      !!&)) 	44	5r    c                F   g }d}d}d}| dd         D ]'}|dk    s|dk    s|                     d          rd} n(d}|t          |           k     r| |         }|t          v r|dz  }*|                     d          s|                     d	          r|dz  }Z|d
k    s*|                     d
          s|                     d          r|dz  }|                     d          r|dz  }|s
|sd}|dz  }|                    |           |dz  }|t          |           k     |S )z5Extract input files that will be modified by -i flag.r   FNr   r
   r   Tr   r   r   r   r   )r   r   FLAGS_WITH_ARGr   )r   filesr   found_script
has_e_flagr   s         r   _extract_inplace_filesr4   v   s{   E	ALJ ABBZ  99^++q||O/L/L+JE ,
 	
A
c&kk//1I FA<<(( 	ALL,C,C 	FA 99T**9all<.H.H9FA << 	FA  	, 	LFA 	Q	Q= c&kk//@ Lr    ctxr   r   c                j   | j         }|r|d         nd}t          |          }t          |          rt          d| d          S t	          |          }d}|dd         D ]@}|d	k    s|                    d	          rd
} n |dk    s|                    d          rd
} nAg }|r$t          |          }|                    |           |r|                    |           |r+|r| dn| d}	t          d|	t          |                    S |rt          d| d          S t          d|          S )z Classify sed command for safety.r   r   askz e (execute))descriptionFr   Nr   Tr   z -iz wallow)r8   redirect_targets)	r   r   r.   r   r)   r   r4   extendtuple)
r5   r   baser   write_targetshas_inplacer   r:   inplace_filesdescs
             r   classifyrB      s   ZF)6!99ED v&&G G$$ HeD1F1F1FGGGG +733M KABBZ  99T**9KE\ : :KE 
  /.v66... /...  
*;$||||4"#344
 
 
 	
  ?eD>>>>'t4444r    )r   r   r   r   )r   r   r   r   )r   r   r   r*   )r5   r   r   r   )__doc__
__future__r   re	dippy.clir   r   COMMANDS	frozensetr0   compileVERBOSEr"   r,   r   r)   r.   r4   rB    r    r   <module>rL      s    # " " " " " 				 4 4 4 4 4 4 4 47 AAABB
 
 J	 	 "* J	 	. . . .b	 	 	 	   / / / /d15 15 15 15 15 15r    