Wednesday, August 24, 2011

Account management script in Python for Android


for this script, you need to download sl4a.  and also install python for android from the download page in this site. 

import android, os , datetime


droid = android.Android()


docdir = "./"
loop = 1
file_name=""


def select_account ():
 dirfile = os.listdir (docdir)
 dirfiles =[]
 for f in dirfile :
  if f[-4:]==".acc":
   dirfiles.append (f)
 if len(dirfiles) == 0:
  droid.makeToast('no accounts')
  return
 d=droid.dialogCreateAlert("select file")
 droid.dialogSetSingleChoiceItems(dirfiles)
 droid.dialogSetPositiveButtonText('ok')
 droid.dialogSetNegativeButtonText('cancel')
 droid.dialogShow()
 r=droid.dialogGetResponse().result
 if r['which']== "negative":
  return (1)
 result = droid.dialogGetSelectedItems().result[0]
 global file_name
 file_name = dirfiles [result] 
 return (0)


def add_dialogue():
 if select_account () != 0:
  return
 f = open (docdir + file_name, "a")
 droid.dialogCreateAlert(None, None)
 droid.dialogSetSingleChoiceItems(["receipt", "payment"], 0)
 droid.dialogSetPositiveButtonText('ok')
 droid.dialogShow()
 r = droid.dialogGetResponse().result
 receipt = droid.dialogGetSelectedItems().result[0] 
 amount= droid.dialogGetInput("amount", None, None).result
 try:
  famount = float (amount)
  if receipt == 1:
   famount = -famount
 except:
  return
 head = droid.dialogGetInput("description", None, None).result 
 out_string = str(datetime.date.today()) + " :: " + head + " :: " + str(famount)
 f.write (out_string +"\n")
 droid.makeToast(out_string)


def show_dialogue():
 if select_account () != 0:
  return 
 f= open ( docdir+ file_name , "r")
 lines = f.readlines ()
 message = ""
 total= 0
 for line in lines:
  values = line.split (" :: ")
  message = '%+6.0f : %-8s : %s\n' % ( float ( values[2]), values[1][:8], values[0][5:]) + message
  total += float (values[2])
 message = "Total = " + str(total)  + "\n\n" + message
 droid.dialogCreateAlert(None, message)
 droid.dialogShow()
 droid.dialogGetResponse()


def create_account ():
 f = droid.dialogGetInput('Account name', None, None).result
 if f == None:
  return
 f = docdir + f +".acc"
 file = open (f, "w")
 file.close ()
 droid.makeToast('created file: ' + f)


while loop:
 droid.dialogCreateAlert(None, None)
 droid.dialogSetSingleChoiceItems(["add transaction", "show accounts", "create account"], 0)
 droid.dialogSetPositiveButtonText('ok')
 droid.dialogSetNegativeButtonText('exit')
 droid.dialogShow()
 r=droid.dialogGetResponse().result
 if r['which']== "negative":
  os._exit (1)
 result = droid.dialogGetSelectedItems().result[0] 
 if result == 0 :
  add_dialogue()
 elif result == 1:
  show_dialogue()
 else:
  create_account ()

No comments:

Post a Comment