GnuCash 2.4.99
gnc_convenience.py
Go to the documentation of this file.
00001 #!/usr/bin/python
00002 # -*- coding: UTF-8 -*-
00003 
00004 ##@file
00005 # @ingroup python_bindings_examples
00006 # @author Christoph Holtermann (c.holtermann@gmx.de)
00007 # @date May 2011
00008 # @brief some functions to make life easier when using python-bindings
00009 #
00010 
00011 from gnucash import Session, Account, Transaction, Split
00012 import gnucash
00013 
00014 
00015 def get_transaction_list(account):
00016     """Returns all transactions in account.
00017 
00018     Splits are derived from account.GetSplitList().
00019    
00020     options:
00021 
00022     account:    Account to get transactions from.
00023     
00024     """
00025     
00026     split_list=account.GetSplitList()
00027     transaction_list=[]
00028     for split in split_list:
00029         if type(split) != Split:
00030               split = Split(instance=split)
00031         transaction=split.GetParent()
00032         if not (transaction in transaction_list):       # this check may not be necessary.
00033           transaction_list.append(transaction)
00034     return transaction_list
00035 
00036 
00037 def get_splits_without_lot(account=None,split_list=None):
00038   """Returns a list of those Splits in split_list or account which do not have an according lot.
00039 
00040   options:
00041 
00042   account:      (optional) Account to search in.
00043   split_list:   (optional) List of Splits to search in.
00044 
00045   one or the other has to be provided.
00046 
00047   """
00048   if split_list==None:
00049       if account==None:
00050           return []
00051       else:
00052           split_list=account.GetSplitList()
00053   
00054   rlist=[]
00055   for split in split_list:
00056       if type(split).__name__ == 'SwigPyObject':
00057           split = Split(instance=split) 
00058       lot=split.GetLot()
00059       if lot == None:
00060           rlist.append(split)
00061   return rlist
00062 
00063 
00064 def find_account(account,name,account_list=None):
00065   """Recursively searches full names of account and descendents
00066 
00067   returns a list of accounts which contain name.
00068   
00069   options:
00070   
00071   account:      account to start search in.
00072   name:         name to search for.
00073   account_list: (optional) list to append accounts to.
00074   
00075   """
00076 
00077   if not account_list:
00078     account_list=[]
00079 
00080   for child in account.get_children():
00081     if type(child) != Account:
00082       child=Account(instance=child)
00083     account_list=find_account(child,name,account_list)
00084   
00085   account_name=account.GetName()
00086   if name in account_name:
00087     account_list.append(account)
00088   
00089   return account_list
00090 
00091 
00092 def find_lot(lot_list,search_string):
00093   """Searches lots in lot_list for search_string.
00094 
00095   returns list of lots where title contains search_string.
00096 
00097   options:
00098 
00099   lot_list:       List of Lots to search in.
00100   search_string:  String to search for.
00101   
00102   """
00103   
00104   rlist=[]
00105   for lot in lot_list:
00106     if type(lot).__name__ == 'SwigPyObject':
00107         lot = gnucash.GncLot(instance=lot)
00108     ltitle=lot.get_title()
00109     if search_string in ltitle: 
00110       rlist.append(lot)
00111   return rlist
00112 
00113 
00114 def find_split(split_list,search_string):
00115   """Searches a list of splits for search_string
00116   
00117   returns a list of splits that have search_string as part of
00118   memo or
00119   description of parent transaction.
00120   
00121   options:
00122  
00123   split_list:     List of Splits to search in.
00124   search_string:  String to search for.
00125   
00126   """
00127   
00128   rlist=[]
00129   for split in split_list:
00130       memo=split.GetMemo()
00131       transaction_description=split.GetParent().GetDescription()
00132       if (search_string in memo) or (search_string in transaction_description):
00133           rlist.append(split)
00134   return rlist
00135 
00136 
00137 def find_split_recursive(account, search_string):
00138   """Searches account and descendants for Splits containing search_string
00139   
00140   returns a list of splits that have search_string as part of
00141   memo or
00142   description of parent transaction.
00143   
00144   options:
00145  
00146   account:        Account to search in.
00147   search_string:  String to search for.
00148   
00149   """
00150   
00151   rlist = []
00152   child_account_splits = []
00153   
00154   # Get all splits in descendants
00155   for child in account.get_children():
00156       if type(child) != Account:
00157           child = Account(instance=child)
00158       childsplits = find_split_recursive(child, search_string)
00159       for split in childsplits:
00160           if type(split) != Split:
00161               split = Split(instance=split)
00162       child_account_splits += childsplits
00163 
00164   # Get all splits in account
00165   splits=account.GetSplitList()
00166   for split in splits:
00167       if type(split) != Split:
00168           split = Split(instance=split)
00169   basic_account_splits=find_split(splits,search_string)
00170 
00171   rlist=child_account_splits+basic_account_splits
00172   return rlist
00173 
00174 
00175 def find_transaction(account,name,ignore_case=True,transaction_list=None):
00176   """Searches the transactions of an account for name.
00177   
00178   Searches in description and in memo of each split.
00179   returns a list of transactions that match criteria.
00180   
00181   options:
00182   
00183   account:          Account to search in.
00184   name:             String to search for.
00185   ignore_case:      (optional, default=True) Ignore case if True.
00186   transaction_list: (optional) list of transactions to search in.
00187   
00188   """
00189 
00190   if not transaction_list:
00191       transaction_list=get_transaction_list(account)
00192 
00193   ret = []
00194   if ignore_case:
00195       name=name.lower()
00196 
00197   for transaction in transaction_list:
00198       found = False
00199       
00200       desc=transaction.GetDescription()
00201       if ignore_case:
00202           desc=desc.lower()
00203       
00204       if name in desc:
00205           found=True
00206       
00207       sl=transaction.GetSplitList()
00208       for split in sl:
00209           if type(split) != Split:
00210               split=Split(instance=split)
00211           
00212           memo = split.GetMemo()
00213           if ignore_case:
00214               memo=memo.lower()
00215 
00216           if name in memo:
00217               found=True
00218 
00219       if found:
00220           ret.append(transaction)
00221 
00222   return ret
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines