Skip to content

Commit

Permalink
can set system variables in ~/.config/moa/config
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark Fiers committed Oct 23, 2012
1 parent a928935 commit f9b6ee9
Showing 1 changed file with 105 additions and 49 deletions.
154 changes: 105 additions & 49 deletions lib/python/moa/plugin/system/configure.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
# Copyright 2009-2011 Mark Fiers
# The New Zealand Institute for Plant & Food Research
#
#
# This file is part of Moa - http://github.com/mfiers/Moa
#
#
# Licensed under the GPL license (see 'COPYING')
#
#
"""
**configure** - Configure jobs
------------------------------
Control job configuration
"""
import os

import moa.ui
import moa.utils
import moa.args
import textwrap
import optparse
import moa.logger as l

from moa.sysConf import sysConf


##
## Set command
## Show command
##
@moa.args.addFlag('-a', dest='showAll', help='show all parameters')
@moa.args.addFlag('-p', dest='showPrivate', help='show private parameters')
@moa.args.addFlag('-R', dest='showRecursive', help='show recursively defined '
+ 'parameters not specified by the local template')
@moa.args.addFlag('-u', dest='showUnrendered', help='show unrendered values '+
@moa.args.addFlag('-u', dest='showUnrendered', help='show unrendered values ' +
'(when using inline parameters)')
@moa.args.needsJob
@moa.args.doNotLog
Expand All @@ -52,25 +52,28 @@ def show(job, args):
outflags = []

rendered = job.conf.render()

for p in keys:

isPrivate = False

if p[:4] == 'moa_':
isPrivate = True

if p[0] == '_':
isPrivate = True

isPrivate = job.conf.isPrivate(p)
if p[0] == '_': isPrivate = True
if p[:4] == 'moa_': isPrivate = True

isPrivate = job.conf.isPrivate(p)

if p[0] == '_':
isPrivate = True
if p[:4] == 'moa_':
isPrivate = True

isOptional = job.template.parameters[p].get('optional', True)
isLocal = job.conf.is_local(p)
isLocal = job.conf.is_local(p)
isDefined = len(str(job.conf[p])) > 0

# args.showPrivate:
# args.showAll
# args.showRecursive
Expand All @@ -81,14 +84,14 @@ def show(job, args):
if not args.showAll:
if not isLocal and isOptional:
continue

outkeys.append(p)

#print '%s "%s" %s' % (p, job.conf[p], isDefined)

key = ''
if not isDefined:
key += '{{gray}}.{{reset}}'
key += '{{gray}}.{{reset}}'
outvals.append('')
elif isLocal:
outvals.append(str(job.conf[p]))
Expand Down Expand Up @@ -116,7 +119,7 @@ def show(job, args):
# outflags.append('{{magenta}}R{{reset}}')
# outvals.append(job.conf[p])
# else:
# #not defined - does it need to be??
# #not defined - does it need to be??
# if job.template.parameters[p].optional:
# val = job.conf[p]
# if val != None:
Expand All @@ -127,7 +130,7 @@ def show(job, args):
# moa.ui.fformat(
# '{{gray}}(undefined){{reset}}', f='j'))
# else:
# #not optional
# #not optional
# outflags.append('{{bold}}{{red}}E{{reset}}')
# outvals.append(
# moa.ui.fformat(
Expand All @@ -139,37 +142,38 @@ def show(job, args):

wrapInit = termx - (maxKeylen + 5)
spacer = ' ' * (maxKeylen + 5)
spacerR = ' ' * (maxKeylen + 1) + moa.ui.fformat('{{gray}}r ', newline=False, f='j')
spacerR = ' ' * (maxKeylen + 1) + \
moa.ui.fformat('{{gray}}r ', newline=False, f='j')
closeR = moa.ui.fformat('{{reset}}', newline=False, f='j')

#print outkeys
zipped = zip(outkeys, outvals, outflags)
zipped.sort(lambda x,y: cmp(x[0].lstrip('_'), y[0].lstrip('_')))

zipped.sort(lambda x, y: cmp(x[0].lstrip('_'), y[0].lstrip('_')))
#print outkeys

for i, zippy in enumerate(zipped):
key, val, flag = zippy
if not args.showAll:
if not val: continue
if not val:
continue

moa.ui.fprint(("%%-%ds" % maxKeylen) % key, f='jinja', newline=False)
moa.ui.fprint(" " + flag + " ", f='jinja', newline=False)
if len(str(val)) == 0:
print
print

#print str( val)
#print textwrap.wrap(str(val), wrapInit)

if args.showUnrendered: mainval = val
else:
if args.showUnrendered:
mainval = val
else:
renval = rendered[key]
if str(renval):
mainval = renval
else:
mainval = val



for j, ll in enumerate(textwrap.wrap(str(mainval), wrapInit)):
if j == 0:
Expand All @@ -178,16 +182,18 @@ def show(job, args):
moa.ui.fprint(spacer + ll, f=None)

if args.showUnrendered and rendered[key] and rendered[key] != val:
for j, ll in enumerate(textwrap.wrap(str(rendered[key]), wrapInit)):
for j, ll in enumerate(textwrap.wrap(str(rendered[key]),
wrapInit)):
moa.ui.fprint(spacerR + ll + closeR)



@moa.args.argument('parameter', nargs='+', help='parameter to unset')
@moa.args.needsJob
@moa.args.command
def unset(job, args):
"""
Remove a parameter from the configuration
Remove a configured parameter from this job. In the parameter was
defined by the job template, it reverts back to the default
value. If it was an ad-hoc parameter, it is lost from the
Expand All @@ -206,15 +212,18 @@ def unset(job, args):
pass
job.conf.save()

@moa.args.argument('parameter', nargs='+', help='arguments for this job, specify' +

@moa.args.argument('parameter', nargs='+',
help='arguments for this job, specify' +
'as KEY=VALUE without spaces')
@moa.args.addFlag('-s', '--system', help='store this a ' +
'system configuration variable')
@moa.args.forceable
@moa.args.needsJob
@moa.args.command
def set(job, args):
"""
Set one or more variables
"""Set one or more variables
This command can be used in two ways. In its first form both
parameter key and value are defined on the command line: `moa set
KEY=VALUE`. Note that the command line will be processed by bash,
Expand All @@ -226,24 +235,75 @@ def set(job, args):
Alternative use of the set command is by just specifying the key:
'moa set PARAMETER_NAME', in which case Moa will prompt the user
enter a value - circumventing problems with bash interpretation.
if -s is specified, the variable is stored as a system
configuration variable in the YAML formatted::
~/.config/moa/config
Please, use this with care!
Note that dots in the key name are interpreted as nested levels,
so, running::
moa set -s plugins.job.completion.enabled=false
will result in the following section added on top of the YAML::
plugins:
job:
completion:
enabled: false
Adding keys like this mixes safely with configuration information
that is already present. So, setting::
moa set -s plugins.job.completion.something=else
will not remove the `enabled: false` heading under `completion:`,
resulting in::
plugins:
job:
completion:
enabled: false
someting: else
"""

#see if we need to query the user for input somehwere
new_pars = []
for a in args.parameter:

if not '=' in a:
old = job.conf[a]
val = moa.ui.askUser("%s:\n> " % a, old)
job.conf[a] = val
new_pars.append((a,val))
moa.ui.message('set "%s" to "%s"' % (a, " ".join(val.split())))
else:
key,val = a.split('=',1)
key, val = a.split('=', 1)

if not args.system:
job.conf[key] = val
new_pars.append((key,val))
moa.ui.message('set "%s" to "%s"' % (a, " ".join(val.split())))

job.conf.save()
new_pars.append((key, val))
moa.ui.message('setting "%s" to "%s"' % (a, " ".join(val.split())))

if args.system:

sys_pars = dict(new_pars)

def _dictify(d):
for k in list(d.keys()):
if '.' in k:
na, nb = k.split('.', 1)
d[na] = _dictify({nb: d[k]})
del d[k]
return d

userConf = sysConf.getUser()
userConf.update(_dictify(sys_pars))
sysConf.saveUser(userConf)
else:
job.conf.save()


def hook_git_finish_set():
Expand All @@ -253,7 +313,3 @@ def hook_git_finish_set():
job = sysConf.job
sysConf.git.commitJob(job, 'moa set %s in %s' % (
" ".join(sysConf['newargs']), job.wd))




0 comments on commit f9b6ee9

Please sign in to comment.