-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathval.py
More file actions
executable file
·55 lines (35 loc) · 1.55 KB
/
val.py
File metadata and controls
executable file
·55 lines (35 loc) · 1.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/usr/bin/env python
import optparse, sys, os
from confparse import properties
def parse_arguments():
"""Declares 2 options 'set' and 'delete', no options defaults to 'read' """
p = optparse.OptionParser("usage: %prog [options] [initialization-file]")
p.add_option("-s", "--set", action="store_true", default=False, help="Modify the file in place")
p.add_option("-d", "--delete", action="store_true", default=False, help="Delete the option")
# Parses the command line, filters the tokens in options
# and arguments depending on what was defined previously
o, arg = p.parse_args()
# Basic options screening : no set and delete at the same time,
# filename must be a file, sets must have the '=' parameter...
if o.set and o.delete:
raise Exception, "Impossible to set and delete at the same time"
if not os.path.isfile(arg[0]):
raise Exception, "First argument must be a file"
if o.set:
for a in arg[1:]:
if a.find('=')==-1:
raise Exception, "Options to be set must follow the format: 'opt=val'"
return o, arg
if __name__ == '__main__':
try:
o, arg = parse_arguments()
if o.set:
d=dict( [ a.split('=') for a in arg[1:] ] )
properties( d ).apply_to( arg[0] )
elif o.delete:
properties().delete( arg[1:] ).apply_to( arg[0] )
else:
print "\t".join( properties( arg[0] ).get( arg[1:] ) )
except Exception,e:
print >> sys.stderr, "Problem :", e
sys.exit(-1)