View Single Post
Posts: 17 | Thanked: 17 times | Joined on Aug 2008 @ Portsmouth UK
#23
Progress update!

The best way (for me) to learn a new language: inability sleep, and dive in head first. I converted my Perl script to Python. Next, if I still can't sleep, I'll try my hand at GUIing.

It's identical to the Perl script, except I didn't bother with regexes, and it's in Python:

Code:
#!/usr/bin/python
import sys
import commands
import string

try:
	arg = sys.argv[1]
	path = "/apps/modest/accounts/" + arg + "ID"
except:
	print "\nPlease choose an account from the following list:"
	print "(alternatively, pass an account name from below as a parameter to toggle it)\n"

	data = commands.getoutput("gconftool-2 --all-dirs /apps/modest/accounts")
	accounts = string.split(data, '\n')
	
	i = 0
	for acct in accounts:
		# path:   /apps/modest/accounts/xxxID
		pathParts = string.split(acct, '/')
		theId = pathParts[4]
		# theId = xxxID
		nameParts = string.split(theId, 'ID')
		# ['xxx', '']
		name = nameParts[0]
		
		status = commands.getoutput("gconftool-2 -g " + acct + "/enabled")
		if status == "true":
			statusText = "enabled"
		elif status == "false":
			statusText = "disabled"
		else:
			statusText = "state unknown"
		
		print "[" + str(i) + "] " + name + ": " + statusText 
		i = i + 1
	
	try:
		id = int(raw_input("\n\tSelection [0-" + str(i) + "]: "))
		if id > i:
			print "Invalid selection."
			sys.exit(1)
		if id < 0:
			print "Invalid selection."
			sys.exit(1)
	except:
		sys.exit(0)

	path = accounts[id]
	path = string.rstrip(path)
	
status = commands.getoutput("gconftool-2 -g " + path + "/enabled")
if status == "true":
	exitState = commands.getstatusoutput("gconftool-2 -s " + path + "/enabled --type bool false")
	if exitState[0] > 0:
		print "The account could not be disabled."
		sys.exit(exitState)
	
	print "The account has been disabled."
	sys.exit(0)
elif status == "false":
	exitState = commands.getstatusoutput("gconftool-2 -s " + path + "/enabled --type bool true")
	if exitState[0] > 0:
		print "The account could not be enabled."
		sys.exit(exitState)
	
	print "The account has been enabled."
	sys.exit(0)

else:
	print "The specified account could not be found."
	sys.exit(1)
(It may be a lack of sleep talking, but I think I like Perl better.)
 

The Following User Says Thank You to cmantito For This Useful Post: