ó
â„^c           @   s   d  Z  d d l m Z d d l m Z e j rB d d l m Z n  d d d „  ƒ  YZ d „  Z d „  Z d	 „  Z	 d
 „  Z
 d „  Z d „  Z d S(   si  
Classes and functions for the output of reference policy modules.

This module takes a refpolicy.Module object and formats it for
output using the ModuleWriter object. By separating the output
in this way the other parts of Madison can focus solely on
generating policy. This keeps the semantic / syntactic issues
cleanly separated from the formatting issues.
i   (   t	   refpolicy(   t   util(   t   cmpt   ModuleWriterc           B   s   e  Z d  „  Z d „  Z RS(   c         C   s(   d  |  _ d  |  _ t |  _ t |  _ d  S(   N(   t   Nonet   fdt   modulet   Truet   sortt   requires(   t   self(    (    s5   /usr/lib64/python2.7/site-packages/sepolgen/output.pyt   __init__&   s    			c         C   sf   | |  _  |  j r" t |  j  ƒ n  x= t j |  j  d t ƒD]# \ } } | j d t | ƒ ƒ q; Wd  S(   Nt	   showdepths   %s
(   R   R   t   sort_filterR    t   walktreeR   t   writet   str(   R
   R   R   t   nodet   depth(    (    s5   /usr/lib64/python2.7/site-packages/sepolgen/output.pyR   ,   s
    		%(   t   __name__t
   __module__R   R   (    (    (    s5   /usr/lib64/python2.7/site-packages/sepolgen/output.pyR   %   s   	c         C   s¦   t  j |  ƒ } | j ƒ  t  j | ƒ } | j ƒ  t | ƒ t | ƒ k r_ t | d | d ƒ Sx@ t | | ƒ D]/ } | d | d k ro t | d | d ƒ Sqo Wd S(   Ni    i   (   R   t   set_to_listR   t   lenR   t   zip(   t   xt   yt   xlt   ylt   v(    (    s5   /usr/lib64/python2.7/site-packages/sepolgen/output.pyt
   id_set_cmp=   s    

c         C   sŽ   t  |  j | j ƒ } | d k	 r% | St  |  j | j ƒ } | d k	 rJ | St  |  j | j ƒ } | d k	 ro | St t |  j ƒ t | j ƒ ƒ S(   Ni    (   R   t	   src_typest	   tgt_typest   obj_classesR   R   t   perms(   t   at   bt   ret(    (    s5   /usr/lib64/python2.7/site-packages/sepolgen/output.pyt
   avrule_cmpK   s    c         C   sH   |  j  d | j  d k r5 t |  j  d | j  d ƒ St |  j | j ƒ S(   Ni    (   t   argsR   t   ifname(   R"   R#   (    (    s5   /usr/lib64/python2.7/site-packages/sepolgen/output.pyt
   ifcall_cmpZ   s    c         C   s‹   t  |  t j ƒ rN t  | t j ƒ r1 t |  | ƒ St |  j d g | j ƒ Sn9 t  | t j ƒ rm t |  | ƒ St |  j | j d g ƒ Sd  S(   Ni    (	   t
   isinstanceR    t   InterfaceCallR(   R   R&   R   t   AVRuleR%   (   R"   R#   (    (    s5   /usr/lib64/python2.7/site-packages/sepolgen/output.pyt   rule_cmp`   s    c         C   s   t  |  j | j ƒ S(   N(   R   t   role(   R"   R#   (    (    s5   /usr/lib64/python2.7/site-packages/sepolgen/output.pyt   role_type_cmpl   s    c         C   s.   d „  } x |  j  ƒ  D] } | | ƒ q Wd S(   s/   Sort and group the output for readability.
    c         S   s6  g  } x4 |  j  ƒ  D]& } | j | ƒ | j t j ƒ  ƒ q Wx! |  j ƒ  D] } | j | ƒ qJ W| j t j ƒ  ƒ g  } | j |  j ƒ  ƒ | j |  j ƒ  ƒ | j d t	 j
 t ƒ ƒ d  } g  } x­ | D]¥ } t | t j ƒ rô | j d } n t	 j | j ƒ } | | k rd| r.| j t j ƒ  ƒ n  | } t j ƒ  }	 |	 j j d | ƒ | j |	 ƒ n  | j | ƒ qÌ W| j | ƒ g  }
 |
 j |  j ƒ  ƒ |
 j d t	 j
 t ƒ ƒ t |
 ƒ rìt j ƒ  }	 |	 j j d ƒ | j |	 ƒ n  | j |
 ƒ x- |  j D]" } | | k r| j | ƒ qqW| |  _ d  S(   Nt   keyi    s   ============= %s ==============s"   ============= ROLES ==============(   t   module_declarationst   appendR    t   CommentR	   t   extendt   avrulest   interface_callsR   R   t
   cmp_to_keyR,   R   R)   R*   R&   t   firstR   t   linest
   role_typesR.   R   t   children(   R   t   ct   modt   requiret   rulest   curt	   sep_rulest   ruleR   t   commentt   rast   child(    (    s5   /usr/lib64/python2.7/site-packages/sepolgen/output.pyt	   sort_noder   sL    N(   t   nodes(   R   RE   R   (    (    s5   /usr/lib64/python2.7/site-packages/sepolgen/output.pyR   o   s    	<N(    (   t   __doc__t    R    R   t   PY3R   R   R   R%   R(   R,   R.   R   (    (    (    s5   /usr/lib64/python2.7/site-packages/sepolgen/output.pyt   <module>   s   						