a h,@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 3/usr/lib/python3.9/site-packages/sepolgen/output.py__init__&szModuleWriter.__init__cCsF||_|jrt|jtj|jddD]\}}|dt|q&dS)NT)Z showdepthz%s )rr sort_filterrZwalktreewritestr)r rrnodedepthr r r r,s  zModuleWriter.writeN)__name__ __module__ __qualname__r rr r r r r%srcCst|}|t|}|t|t|krFt|d|dSt||D]*}|d|dkrPt|d|dSqPdS)Nr)rZ set_to_listrlenrzip)xyZxlZylvr r r id_set_cmp=s  rcCsRt|j|j}|dkr|St|j|j}|dkr4|St|j|j}|dkrN|SdSNr)r src_typesZ tgt_typesZ obj_classes)abretr r r avrule_cmpKsr"cCs8|jd|jdkr*t|jd|jdSt|j|jSr)argsrZifnamerr r r r ifcall_cmpZsr%cCsrt|tjr8t|tjr"t||St|jdg|jSn6t|tjsPt|tjrZt ||St|j|jdgSdSr) isinstancer InterfaceCallr%rr#rZAVRuleZ AVExtRuler"r$r r r rule_cmp`s    r(cCst|j|jS)N)rZroler$r r r role_type_cmplsr)cCs"dd}|D] }||qdS)z/Sort and group the output for readability. c Ssg}|D]}|||tq |D]}||q2|tg}|||||||j t t dd}g}|D]t}t |tjr|jd}n t |j}||kr|r|t|}t} | jd||| ||q||g} | || j t tdt| rht} | jd|| || |jD]} | |vrx|| qx||_dS)N)keyrz============= %s ==============z"============= ROLES ==============)Zmodule_declarationsappendrCommentr extendZavrulesZ avextrulesZinterface_callsrr cmp_to_keyr(r&r'r#firstrlinesZ role_typesr)rZchildren) rcmodZrequireZrulesZcurZ sep_rulesZrulerZcommentZraschildr r r sort_nodersN                 zsort_filter..sort_nodeN)Znodes)rr4rr r r ros= rN) __doc__rrZPY3rrrr"r%r(r)rr r r r s