
    Fwgb%                         d Z ddlmZ ddlmZ ddlmZmZmZm	Z	m
Z
mZmZmZmZ d Zd Zd Zd Zd	 Zd
 Z G d dej*                        Zy)a  Fixer for __metaclass__ = X -> (future.utils.with_metaclass(X)) methods.

   The various forms of classef (inherits nothing, inherits once, inherints
   many) don't parse the same in the CST so we look at ALL classes for
   a __metaclass__ and if we find one normalize the inherits to all be
   an arglist.

   For one-liner classes ('class X: pass') there is no indent/dedent so
   we normalize those into having a suite.

   Moving the __metaclass__ into the classdef can also cause the class
   body to be empty so there is some special casing for that as well.

   This fixer also tries very hard to keep original indenting and spacing
   in all those corner cases.
    )
fixer_base)token)	NamesymsNodeLeaftouch_importCallStringCommaparenthesizec                    | j                   D ]  }|j                  t        j                  k(  rt	        |      c S |j                  t        j
                  k(  sK|j                   sX|j                   d   }|j                  t        j                  k(  s|j                   s|j                   d   }t        |t              s|j                  dk(  s y y)z we have to check the cls_node without changing it.
        There are two possibilities:
          1)  clsdef => suite => simple_stmt => expr_stmt => Leaf('__meta')
          2)  clsdef => simple_stmt => expr_stmt => Leaf('__meta')
    r   __metaclass__TF)
childrentyper   suitehas_metaclasssimple_stmt	expr_stmt
isinstancer   value)parentnode	expr_node	left_sides       V/var/www/horilla/myenv/lib/python3.12/site-packages/libfuturize/fixes/fix_metaclass.pyr   r   &   s      	 99

" &&YY$***t}}a(I~~/I4F4F%..q1	i.!?:	      c                    | j                   D ]!  }|j                  t        j                  k(  s! y t	        | j                         D ]$  \  }}|j                  t
        j                  k(  s$ n t        d      t        t        j                  g       }| j                   |dz   d rT| j                   |dz      }|j                  |j                                |j                          | j                   |dz   d rT| j                  |       |}y)zf one-line classes don't get a suite in the parse tree so we add
        one to normalize the tree
    NzNo class suite and no ':'!   )r   r   r   r   	enumerater   COLON
ValueErrorr   append_childcloneremove)cls_noder   ir   	move_nodes        r   fixup_parse_treer)   9   s     !! 99

" X../ 7499#7 566 R E


AaCD
!%%ac*	9??,- 

AaCD
! % Dr   c                 x   t        |j                        D ]$  \  }}|j                  t        j                  k(  s$ n y|j                          t        t        j                  g       }t        t        j                  |g      }|j                  |d rN|j                  |   }|j                  |j                                |j                          |j                  |d rN| j                  ||       |j                  d   j                  d   }|j                  d   j                  d   }	|	j                  |_        y)z if there is a semi-colon all the parts count as part of the same
        simple_stmt.  We just want the __metaclass__ part so we move
        everything efter the semi-colon into its own simple_stmt node
    Nr   )r    r   r   r   SEMIr%   r   r   r   r   r#   r$   insert_childprefix)
r   r'   	stmt_nodesemi_indr   new_exprnew_stmtr(   	new_leaf1	old_leaf1s
             r   fixup_simple_stmtr4   S   s   
 $I$6$67 $99

" 	KKMDNNB'HD$$xj1H


XY
'&&x0	ioo/0 

XY
' 8$!!!$--a0I""1%..q1I ''Ir   c                     | j                   rI| j                   d   j                  t        j                  k(  r| j                   d   j	                          y y y )N)r   r   r   NEWLINEr%   )r   s    r   remove_trailing_newliner8   k   s@    }}r*//5==@b  " A}r   c              #   6  K   | j                   D ]!  }|j                  t        j                  k(  s! n t	        d      t        t        |j                               D ]  \  }}|j                  t        j                  k(  s$|j                   s1|j                   d   }|j                  t        j                  k(  s^|j                   sk|j                   d   }t        |t              s|j                  dk(  st        |||       t        |       |||f  y w)NzNo class suite!r   r   )r   r   r   r   r"   listr    r   r   r   r   r   r4   r8   )r&   r   r'   simple_noder   	left_nodes         r   
find_metasr=   p   s     !! ,99

", *++ y78 1;t///K4H4H#,,Q/I~~/I4F4F%..q1	i.!+;;%dA{;+K8K001s/   -DAD D,D:DD'D7"Dc                 ~   | j                   ddd   }|r1|j                         }|j                  t        j                  k(  rn|r1|rv|j                         }t        |t              r1|j                  t        j                  k7  r|j                  rd|_        y|j                  |j                   ddd          |ruyy)z If an INDENT is followed by a thing with a prefix then nuke the prefix
        Otherwise we get in trouble when removing __metaclass__ at suite start
    Nr6    )
r   popr   r   INDENTr   r   DEDENTr-   extend)r   kidsr   s      r   fixup_indentrE      s     >>$B$D
xxz99$  xxzdD!dii5<<&?{{!KKdd+, r   c                       e Zd ZdZdZd Zy)FixMetaclassTz
    classdef<any*>
    c                 d	   t        |      sy t        |       d }t        |      D ]  \  }}}|}|j                           |j                  d   j
                  }t        |j                        dk(  r|j                  d   j
                  t        j                  k(  r|j                  d   }n4|j                  d   j                         }	t        t        j                  |	g      }|j                  d|       nt        |j                        dk(  r-t        t        j                  g       }|j                  d|       nt        |j                        dk(  rt        t        j                  g       }|j                  dt        t        j                  d             |j                  d|       |j                  dt        t        j                   d             nt#        d	      |j                  d   j                  d   }
d
|
_        |
j&                  }t)        dd|       |j                  d   j                  d   j                         }d|_        |g}|j                  rt        |j                        dk(  r%|j                  d   j                         }d|_        nt+        |j                               }d|_        t-        t/        d      t1        d      t3               |t3               t        t        j4                  t        t        j6                  d      t        t        j8                  d      gd      gd      }|j;                  t3               |g       |j=                  t-        t/        d|j&                        |             t?               |j                  s^|j                          t        |d      }||_        |jA                  |       |jA                  t        t        jB                  d             y t        |j                        dkD  r|j                  d   j
                  t        jD                  k(  rt|j                  d   j
                  t        jF                  k(  rIt        |d      }|j                  d|       |j                  dt        t        jB                  d             y y y y )Nr                  )(zUnexpected class definition	metaclasszfuture.utilswith_metaclassr?   r    r   z	'NewBase'{})r-   pass
r6   )$r   r)   r=   r%   r   r   lenr   arglistr$   r   	set_childr,   r   r   RPARLPARr"   r   r-   r	   r   r
   r   r   r   atomLBRACERBRACErC   replacerE   r#   r7   rA   rB   )selfr   resultslast_metaclassr   r'   stmt	text_typerY   r   meta_txtorig_meta_prefixrP   	argumentsbasebases	pass_leafs                    r   	transformzFixMetaclass.transform   s   T" (. 	NE1d!NKKM	 MM!$))	 t}}" }}Q$$4--* q)//1t||fX6q'*1$ 4<<,Ga)1$ 4<<,Gaejj$!78a)aejj$!78:;; "**1-66q9$#?? 	_&7>"++A.77:@@B		K	7##$)''*002"
 %W]]_5#DL;'GG		ellD14d3KL#
+ 
  egt_-"7>>:
 	
 	U ~~LLNY0I/Ii(d5==%89 1$..$))U\\9..$))U\\9Y0Ir9-r4u#=>	 : : %r   N)__name__
__module____qualname__BM_compatiblePATTERNrl    r   r   rG   rG      s    MGb?r   rG   N)__doc__lib2to3r   lib2to3.pygramr   lib2to3.fixer_utilr   r   r   r   r	   r
   r   r   r   r   r)   r4   r8   r=   rE   BaseFixrG   rr   r   r   <module>rx      sS   <        &4(0#
1.-,i?:%% i?r   