Skip to content

Commit

Permalink
Make bake.cfg unnecessary
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexSzatmary committed Aug 24, 2011
1 parent 98c9079 commit 72d1440
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 19 deletions.
47 changes: 29 additions & 18 deletions bake/api.py
Expand Up @@ -21,17 +21,22 @@
import optparse
import re
import load
import bakedefaults

# c is a ConfigParser object, d is a dictionary representing that object
def load_config():
"""
Reads bake.cfg and makes a dict out of it
"""
import ConfigParser
c = ConfigParser.SafeConfigParser()
c.read('bake.cfg')
c.read(bakedefaults.CFGFILE)
d = {}
for l in c.sections():
d[l] = dict(c.items(l))
d['filenames']['code_files'] = d['filenames']['code_files']\
.replace('\n', '').split(',')
for section in c.sections():
d[section] = dict(c.items(section))
for k in d[section]:
# Handle newlines in values in a more useful way
# Make comma-separated lists into list objects
d[section][k] = d[section][k].replace('\n', '').split(',')
return d


Expand All @@ -40,6 +45,13 @@ def make_optparser():
# Core tasks
optparser.add_option('--file', '-f',
help="""Bake parameter file to operate from""")
optparser.add_option(
'--bake_file', '-b', action='append',
help="""File to bake. This can be a filename or a glob. This overrides
the list code_files in bake.cfg if it is present. This option can be
used repeatedly, with each -b flag specifying another file or glob to
bake.."""

optparser.add_option('--mix', '-m',
help="""Mix parameters into code files.""",
action='store_true')
Expand Down Expand Up @@ -132,7 +144,7 @@ def make_grid(config, options, lines):
return grid


def default_loop(grid, config, options):
def default_loop(grid, options):
"""
This does a loop over each item in mixIterator, that is, each combination
of the possible values.
Expand All @@ -152,17 +164,16 @@ def default_loop(grid, config, options):
if not options.remix:
os.mkdir(wd)
# String replace the keys for the values
for f in config['filenames']['code_files']:
hin = open(f + config['filenames']['file_in_suffix'], 'r')
houtcode = open(
os.path.join(wd, f +
config['filenames']['file_out_suffix']),
'w')
for line in hin.readlines():
line = grid.replace(line)
houtcode.write(line)
hin.close()
houtcode.close()
for g in options.bake_file:
for f in glob.glob(g):
hin = open(f + options.file_in_suffix, 'r')
houtcode = open(
os.path.join(wd, f + options.file_out_suffix, 'w')
for line in hin.readlines():
line = grid.replace(line)
houtcode.write(line)
hin.close()
houtcode.close()
if options.execute:
os.chdir(wd)
os.system(grid.replace(options.execute))
Expand Down
13 changes: 12 additions & 1 deletion bake/cmdline.py
Expand Up @@ -33,6 +33,17 @@ def main(args=sys.argv[1:]):
else:
lines = []

if not options.bake_file:
options.bake_file = config['filenames']['code_files']

#warn This adds secret options to the options object.
options.file_in_suffix = ''
options.file_out_suffix = ''
if 'filenames' in config and 'file_in_suffix' in config['filenames']:
options.file_in_suffix = config['filenames']['file_in_suffix']
if 'filenames' in config and 'file_out_suffix' in config['filenames']:
options.file_in_suffix = config['filenames']['file_out_suffix']

# The overwrite command pushes lines onto the top of the bake parameter
# file
if options.overwrite:
Expand All @@ -43,7 +54,7 @@ def main(args=sys.argv[1:]):


## This is the main loop, iterating over each set of values
bake.default_loop(grid, config, options)
bake.default_loop(grid, options)

if __name__ == '__main__':
main()

0 comments on commit 72d1440

Please sign in to comment.