<s> """<STR_LIT>""" <EOL> import itertools <EOL> import collections <EOL> import optparse <EOL> try : <EOL> from configparser import ConfigParser <EOL> except : <EOL> from ConfigParser import ConfigParser <EOL> parser = optparse . OptionParser ( epilog = __doc__ ) <EOL> parser . add_option ( '<STR_LIT>' , '<STR_LIT>' , dest = '<STR_LIT:input>' , <EOL> default = '<STR_LIT>' , metavar = '<STR_LIT>' ) <EOL> parser . add_option ( '<STR_LIT>' , '<STR_LIT>' , dest = '<STR_LIT>' , <EOL> default = '<STR_LIT>' , metavar = '<STR_LIT>' ) <EOL> class AxisItem ( object ) : <EOL> def __init__ ( self , axis , name , config ) : <EOL> self . axis = axis <EOL> self . isdefault = name [ - <NUM_LIT:1> ] == '<STR_LIT:*>' <EOL> self . name = name [ : - <NUM_LIT:1> ] if self . isdefault else name <EOL> self . load ( config ) <EOL> def load ( self , config ) : <EOL> sectionname = '<STR_LIT>' % ( self . axis . name , self . name ) <EOL> if config . has_section ( sectionname ) : <EOL> self . options = collections . OrderedDict ( config . items ( sectionname ) ) <EOL> else : <EOL> self . options = collections . OrderedDict ( ) <EOL> for name , value in self . axis . defaults . items ( ) : <EOL> if name not in self . options : <EOL> self . options [ name ] = value <EOL> class Axis ( object ) : <EOL> def __init__ ( self , name , config ) : <EOL> self . name = name <EOL> self . load ( config ) <EOL> def load ( self , config ) : <EOL> self . items = collections . OrderedDict ( ) <EOL> values = config . get ( '<STR_LIT>' , self . name ) . split ( '<STR_LIT:U+002C>' ) <EOL> if config . has_section ( '<STR_LIT>' % self . name ) : <EOL> self . defaults = collections . OrderedDict ( <EOL> config . items ( '<STR_LIT>' % self . name ) <EOL> ) <EOL> else : <EOL> self . defaults = { } <EOL> for value in values : <EOL> self . items [ value . strip ( '<STR_LIT:*>' ) ] = AxisItem ( self , value , config ) <EOL> def render ( incfg ) : <EOL> axes = collections . OrderedDict ( ) <EOL> if incfg . has_section ( '<STR_LIT>' ) : <EOL> for axis in incfg . options ( '<STR_LIT>' ) : <EOL> axes [ axis ] = Axis ( axis , incfg ) <EOL> out = ConfigParser ( ) <EOL> for section in incfg . sections ( ) : <EOL> if section == '<STR_LIT>' or section . startswith ( '<STR_LIT>' ) : <EOL> continue <EOL> out . add_section ( section ) <EOL> for name , value in incfg . items ( section ) : <EOL> out . set ( section , name , value ) <EOL> for combination in itertools . product ( <EOL> * [ axis . items . keys ( ) for axis in axes . values ( ) ] ) : <EOL> options = collections . OrderedDict ( ) <EOL> section_name = ( <EOL> '<STR_LIT>' + '<STR_LIT:->' . join ( [ item for item in combination if item ] ) <EOL> ) <EOL> section_alt_name = ( <EOL> '<STR_LIT>' + '<STR_LIT:->' . join ( [ <EOL> itemname <EOL> for axis , itemname in zip ( axes . values ( ) , combination ) <EOL> if itemname and not axis . items [ itemname ] . isdefault <EOL> ] ) <EOL> ) <EOL> if section_alt_name == section_name : <EOL> section_alt_name = None <EOL> axes_items = [ <EOL> '<STR_LIT>' % ( axis , itemname ) <EOL> for axis , itemname in zip ( axes , combination ) <EOL> ] <EOL> for axis , itemname in zip ( axes . values ( ) , combination ) : <EOL> axis_options = axis . items [ itemname ] . options <EOL> if '<STR_LIT>' in axis_options : <EOL> constraints = axis_options [ '<STR_LIT>' ] . split ( '<STR_LIT:\n>' ) <EOL> for c in constraints : <EOL> if c . startswith ( '<STR_LIT:!>' ) and c [ <NUM_LIT:1> : ] in axes_items : <EOL> continue <EOL> for name , value in axis_options . items ( ) : <EOL> if name in options : <EOL> options [ name ] += value <EOL> else : <EOL> options [ name ] = value <EOL> constraints = options . pop ( '<STR_LIT>' , '<STR_LIT>' ) . split ( '<STR_LIT:\n>' ) <EOL> neg_constraints = [ c [ <NUM_LIT:1> : ] for c in constraints if c and c [ <NUM_LIT:0> ] == '<STR_LIT:!>' ] <EOL> if not set ( neg_constraints ) . isdisjoint ( axes_items ) : <EOL> continue <EOL> if not out . has_section ( section_name ) : <EOL> out . add_section ( section_name ) <EOL> if ( section_alt_name and not out . has_section ( section_alt_name ) ) : <EOL> out . add_section ( section_alt_name ) <EOL> for name , value in reversed ( options . items ( ) ) : <EOL> if not out . has_option ( section_name , name ) : <EOL> out . set ( section_name , name , value ) <EOL> if section_alt_name and not out . has_option ( section_alt_name , name ) : <EOL> out . set ( section_alt_name , name , value ) <EOL> return out <EOL> def main ( ) : <EOL> options , args = parser . parse_args ( ) <EOL> tmpl = ConfigParser ( ) <EOL> tmpl . read ( options . input ) <EOL> with open ( options . output , '<STR_LIT:wb>' ) as outfile : <EOL> render ( tmpl ) . write ( outfile ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> main ( ) </s>
