<s> """<STR_LIT>""" <EOL> import os <EOL> import locale <EOL> from xml . etree import ElementTree as etree <EOL> from qisys import ui <EOL> import qisrc . git <EOL> import qisys . qixml <EOL> class ProjectXML ( qisys . qixml . XMLParser ) : <EOL> def __init__ ( self , target ) : <EOL> super ( ProjectXML , self ) . __init__ ( target ) <EOL> def _parse_maintainer ( self , element ) : <EOL> maintainer = { '<STR_LIT:email>' : element . get ( '<STR_LIT:email>' , "<STR_LIT>" ) , '<STR_LIT:name>' : element . text } <EOL> self . target . append ( maintainer ) <EOL> def to_str ( name = None , email = None ) : <EOL> encoding = locale . getpreferredencoding ( ) <EOL> name = name . encode ( encoding ) <EOL> email = email . encode ( encoding ) <EOL> string = "<STR_LIT>" <EOL> if name : <EOL> string += name <EOL> if name and email : <EOL> string += "<STR_LIT:U+0020>" <EOL> if email : <EOL> string += "<STR_LIT:<>" + email + "<STR_LIT:>>" <EOL> return string <EOL> def get_xml_root ( project ) : <EOL> tree = get_xml_tree ( project ) <EOL> return tree . getroot ( ) <EOL> def get_xml_tree ( project ) : <EOL> xml_path = project . qiproject_xml <EOL> if not os . path . exists ( xml_path ) : <EOL> with open ( xml_path , "<STR_LIT:w>" ) as fp : <EOL> fp . write ( """<STR_LIT>""" ) <EOL> tree = qisys . qixml . read ( xml_path ) <EOL> return tree <EOL> def exists ( project , name = None , email = None ) : <EOL> maintainers = get ( project ) <EOL> for maintainer in maintainers : <EOL> if maintainer . get ( "<STR_LIT:name>" ) == name and maintainer . get ( "<STR_LIT:email>" ) == email : <EOL> return True <EOL> return False <EOL> def get ( project , warn_if_none = False ) : <EOL> maintainers = list ( ) <EOL> project_xml = ProjectXML ( maintainers ) <EOL> project_xml . parse ( get_xml_root ( project ) ) <EOL> if not maintainers and warn_if_none : <EOL> mess = """<STR_LIT>""" <EOL> mess = mess . format ( src = project . src , qiproject_xml = project . qiproject_xml ) <EOL> ui . warning ( mess , end = "<STR_LIT>" ) <EOL> return maintainers <EOL> def remove ( project , name = None , email = None ) : <EOL> if not exists ( project , name = name , email = email ) : <EOL> return False <EOL> tree = get_xml_tree ( project ) <EOL> root = tree . getroot ( ) <EOL> for elem in root . findall ( "<STR_LIT>" + email + "<STR_LIT>" ) : <EOL> if elem . text == name : <EOL> root . remove ( elem ) <EOL> qisys . qixml . write ( tree , project . qiproject_xml ) <EOL> return True <EOL> def clear ( project ) : <EOL> maintainers = get ( project ) <EOL> if not maintainers : <EOL> return False <EOL> for maintainer in maintainers : <EOL> remove ( project , ** maintainer ) <EOL> return True <EOL> def add ( project , name = None , email = None ) : <EOL> if exists ( project , name = name , email = email ) : <EOL> return <EOL> encoding = locale . getpreferredencoding ( ) <EOL> name = name . decode ( encoding ) <EOL> email = email . decode ( encoding ) <EOL> tree = get_xml_tree ( project ) <EOL> root = tree . getroot ( ) <EOL> maint_elem = etree . Element ( "<STR_LIT>" ) <EOL> maint_elem . set ( "<STR_LIT:email>" , email ) <EOL> maint_elem . text = name <EOL> root . append ( maint_elem ) <EOL> qisys . qixml . write ( tree , project . qiproject_xml ) </s>
