Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
mkexp
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Package registry
Operate
Terraform modules
Analyze
Contributor analytics
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
esmenv
mkexp
Commits
65ed5329
Commit
65ed5329
authored
5 years ago
by
Karl-Hermann Wieners
Browse files
Options
Downloads
Patches
Plain Diff
mkexp: added 'setconfig' filter tool to alter config files via command line
parent
a122f125
No related branches found
No related tags found
No related merge requests found
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
CHANGES.txt
+10
-0
10 additions, 0 deletions
CHANGES.txt
expargparse.py
+17
-10
17 additions, 10 deletions
expargparse.py
getexp
+2
-12
2 additions, 12 deletions
getexp
setconfig
+82
-0
82 additions, 0 deletions
setconfig
with
111 additions
and
22 deletions
CHANGES.txt
+
10
−
0
View file @
65ed5329
...
...
@@ -6,6 +6,16 @@ Make Experiments!
Release Changes
---------------
Release 1.0.7
=============
Global
------
* Added 'setconfig' filter tool to alter config files via command line.
Supports adding, altering, and deleting of keys in arbitrary sections,
and amendments of header comment
Release 1.0.6
=============
...
...
This diff is collapsed.
Click to expand it.
expargparse.py
+
17
−
10
View file @
65ed5329
...
...
@@ -32,6 +32,21 @@ class MkexpArgumentParser(argparse.ArgumentParser):
self
.
add_argument
(
'
-g
'
,
'
--getexp
'
,
action
=
'
store_true
'
,
help
=
'
load flat config (from getexp -vv)
'
)
def
get_key_chain
(
key
):
sections
=
[]
pos
=
key
.
rfind
(
'
.
'
)
while
(
pos
>=
0
):
if
key
[
pos
-
1
]
==
'
.
'
:
key
=
key
[
0
:
pos
-
1
]
+
key
[
pos
:]
pos
=
key
.
rfind
(
'
.
'
,
0
,
pos
-
1
)
else
:
sections
.
append
(
key
[
pos
+
1
:])
key
=
key
[
0
:
pos
]
pos
=
key
.
rfind
(
'
.
'
)
if
key
:
sections
.
append
(
key
)
return
sections
def
assigns_to_dicts
(
args
):
def
value_to_list
(
value
):
...
...
@@ -42,17 +57,9 @@ def assigns_to_dicts(args):
def
assign_to_dict
(
assign
):
(
key
,
value
)
=
assign
.
split
(
'
=
'
,
1
)
chain
=
get_key_chain
(
key
)
current
=
value_to_list
(
value
)
pos
=
key
.
rfind
(
'
.
'
)
while
(
pos
>=
0
):
if
key
[
pos
-
1
]
==
'
.
'
:
key
=
key
[
0
:
pos
-
1
]
+
key
[
pos
:]
pos
=
key
.
rfind
(
'
.
'
,
0
,
pos
-
1
)
else
:
current
=
{
key
[
pos
+
1
:]:
current
}
key
=
key
[
0
:
pos
]
pos
=
key
.
rfind
(
'
.
'
)
if
key
:
for
key
in
chain
:
current
=
{
key
:
current
}
return
current
...
...
This diff is collapsed.
Click to expand it.
getexp
+
2
−
12
View file @
65ed5329
...
...
@@ -9,6 +9,7 @@ import argparse
import
os
import
sys
from
expargparse
import
get_key_chain
from
expconfig
import
ExpConfig
,
ExpConfigError
from
feedback
import
setLevel
,
INFO
,
die
,
info
...
...
@@ -19,18 +20,7 @@ from feedback import setLevel, INFO, die, info
# Get config value, parsing dotted keys
def
get_value
(
config
,
key
):
sections
=
[]
pos
=
key
.
rfind
(
'
.
'
)
while
(
pos
>=
0
):
if
key
[
pos
-
1
]
==
'
.
'
:
key
=
key
[
0
:
pos
-
1
]
+
key
[
pos
:]
pos
=
key
.
rfind
(
'
.
'
,
0
,
pos
-
1
)
else
:
sections
.
append
(
key
[
pos
+
1
:])
key
=
key
[
0
:
pos
]
pos
=
key
.
rfind
(
'
.
'
)
if
key
:
sections
.
append
(
key
)
sections
=
get_key_chain
(
key
)
sections
.
reverse
()
key
=
sections
.
pop
()
for
section
in
sections
:
...
...
This diff is collapsed.
Click to expand it.
setconfig
0 → 100755
+
82
−
0
View file @
65ed5329
#! /usr/bin/env python
#
# Change configuration using command line
#
# $Id$
#
import
argparse
import
re
import
sys
from
configobj
import
ConfigObj
from
expargparse
import
assigns_to_dicts
,
get_key_chain
from
feedback
import
die
import
package_info
#
# Main routine
#
# Check command line
command_line
=
argparse
.
ArgumentParser
(
description
=
'
Change configuration using command line.
'
)
command_line
.
add_argument
(
'
config
'
,
nargs
=
'
?
'
,
default
=
'
-
'
,
help
=
'
original configuration file name [%(default)s]
'
)
command_line
.
add_argument
(
'
assigns
'
,
metavar
=
'
key=value
'
,
nargs
=
'
*
'
,
help
=
'
override configuration file settings
'
)
command_line
.
add_argument
(
'
-V
'
,
'
--version
'
,
action
=
'
version
'
,
version
=
package_info
.
version
)
command_line
.
add_argument
(
'
--delete
'
,
'
-d
'
,
action
=
'
append
'
,
default
=
[],
help
=
'
remove given keys from configuration
'
)
command_line
.
add_argument
(
'
--header
'
,
'
-H
'
,
action
=
'
append
'
,
default
=
[],
help
=
'
append text to header (initial comment)
'
)
args
=
command_line
.
parse_args
()
data_arg
=
args
.
config
if
data_arg
==
'
-
'
:
data_arg
=
sys
.
stdin
try
:
config_data
=
ConfigObj
(
data_arg
,
interpolation
=
False
,
file_error
=
True
,
write_empty_values
=
True
)
except
IOError
as
error
:
die
(
error
.
message
)
# Remove keys from --delete command line option
for
current
in
args
.
delete
:
config
=
config_data
chain
=
get_key_chain
(
current
)
chain
.
reverse
()
key
=
chain
.
pop
()
for
section
in
chain
:
config
=
config
[
section
]
del
config
[
key
]
# Merge key=value assignments from command line
if
args
.
assigns
:
for
d
in
assigns_to_dicts
(
args
):
config_data
.
merge
(
d
)
# Add lines to header
back_plate
=
[]
for
line
in
reversed
(
config_data
.
initial_comment
):
if
re
.
match
(
r
'
^[\s#]*$
'
,
line
):
back_plate
.
insert
(
0
,
config_data
.
initial_comment
.
pop
())
else
:
break
for
line
in
args
.
header
:
config_data
.
initial_comment
.
append
(
line
)
for
line
in
back_plate
:
config_data
.
initial_comment
.
append
(
line
)
# Ready to roll out
config_data
.
write
(
sys
.
stdout
)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment