3 Klf@spdZddlmZddlmZejr.ddlmZGdddZddZd d Zd d Z d dZ ddZ ddZ dS)ai 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. ) refpolicy)util)cmpc@seZdZddZddZdS) ModuleWritercCsd|_d|_d|_d|_dS)NT)fdmodulesortrequires)selfr /usr/lib/python3.6/output.py__init__&szModuleWriter.__init__cCsJ||_|jrt|jx.tj|jddD]\}}|jdt|q(WdS)NT)Z showdepthz%s )rr sort_filterrZwalktreewritestr)r rrnodedepthr r r r,s  zModuleWriter.writeN)__name__ __module__ __qualname__r rr r r r r%srcCstj|}|jtj|}|jt|t|krFt|d|dSx4t||D]&}|d|dkrRt|d|dSqRWdS)Nr)rZ set_to_listrlenrzip)xyZxlZylvr r r id_set_cmp=s  rcCsdt|j|j}|dkr|St|j|j}|dkr4|St|j|j}|dkrN|Stt|jt|jS)Nr)r src_typesZ tgt_typesZ obj_classesrrZperms)abretr r r avrule_cmpKsr!cCs8|jd|jdkr*t|jd|jdSt|j|jS)Nr)argsrZifname)rrr r r ifcall_cmpZsr#cCsft|tjr8t|tjr"t||St|jdg|jSn*t|tjrNt||St|j|jdgSdS)Nr) isinstancer InterfaceCallr#rr"rZAVRuler!)rrr r r rule_cmp`s     r&cCst|j|jS)N)rZrole)rrr r r role_type_cmplsr'cCs&dd}x|jD] }||qWdS)z/Sort and group the output for readability. c Ssg}x(|jD]}|j||jtjqWx|jD]}|j|q8W|jtjg}|j|j|j|j|jt j t dd}g}x||D]t}t |tj r|jd}n t j|j}||kr|r|jtj|}tj} | jjd||j| |j|qW|j|g} | j|j| jt j tdt| rftj} | jjd|j| |j| x$|jD]} | |krx|j| qxW||_dS)N)keyrz============= %s ==============z"============= ROLES ==============)Zmodule_declarationsappendrCommentr extendZavrulesZinterface_callsrr cmp_to_keyr&r$r%r"firstrlinesZ role_typesr'rZchildren) rcmodZrequireZrulesZcurZ sep_rulesZrulerZcommentZrasZchildr r r sort_nodersL              zsort_filter..sort_nodeN)Znodes)rr1rr r r ros<rN) __doc__rrZPY3rrrr!r#r&r'rr r r r s