optparse dic action

 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
from optparse import Option

class DictOption(Option):
	"""
	A parseopt option that let's me define a dictionary through
	the commandline.
	
	optparse example:
	parser.add_option(DictOption("-p","--passwords",dest="passwords",type="string",action="dic"))
	
	Commandline usage:
	--passwords=[localhost]value,[slicehost]whatever
	
	Commandline, if spaces are needed:
	--passwords="[localhost]my Password,[slicehost]Anot erPassword"
	
	This would be defined in the final options dictionary as another dictionary:
	example 1: { 'passwords':{'localhost':'value' } }
	example 2: { 'passwords':{'localhost':'my Password', 'slicehost':'Anot erPassword' } }
	"""
	ACTIONS = Option.ACTIONS + ("dic",)
	STORE_ACTIONS = Option.STORE_ACTIONS + ("dic",)
	TYPED_ACTIONS = Option.TYPED_ACTIONS + ("dic",)
	ALWAYS_TYPED_ACTIONS = Option.ALWAYS_TYPED_ACTIONS + ("dic",)
	def take_action(self,action,dest,opt,value,values,parser):
		if action=="dic":
			vals=value.split(",")
			d={}
			for val in vals:
				p=val.split("]")
				k=p[0][1:]
				v=p[1]
				d[k]=v
			setattr(values,dest,d)
		else: Option.take_action(self, action, dest, opt, value, values, parser)

More like this

  1. A action decorator for URLs by Batiste 6 years ago
  2. Action that respects the filters in changeview_list by grillermo 1 year, 6 months ago
  3. merge action in django admin by yeago 3 years, 6 months ago
  4. Dynamically create Django admin actions by pantsman 4 years, 4 months ago
  5. Admin action for a generic "CSV Export" (fix for unicode) by __alexander__ 4 months, 2 weeks ago

Comments

(Forgotten your password?)