GnuCash  4.11-243-g1cac132214+
gnc_convenience.py
Go to the documentation of this file.
1 #!/usr/bin/env python3
2 # -*- coding: UTF-8 -*-
3 
4 
10 
11 from gnucash import Session, Account, Transaction, Split
12 import gnucash
13 
14 
15 def get_transaction_list(account):
16  """Returns all transactions in account.
17 
18  Splits are derived from account.GetSplitList().
19 
20  options:
21 
22  account: Account to get transactions from.
23 
24  """
25 
26  split_list=account.GetSplitList()
27  transaction_list=[]
28  for split in split_list:
29  if type(split) != Split:
30  split = Split(instance=split)
31  transaction=split.GetParent()
32  if not (transaction in transaction_list): # this check may not be necessary.
33  transaction_list.append(transaction)
34  return transaction_list
35 
36 
37 def get_splits_without_lot(account=None,split_list=None):
38  """Returns a list of those Splits in split_list or account which do not have an according lot.
39 
40  options:
41 
42  account: (optional) Account to search in.
43  split_list: (optional) List of Splits to search in.
44 
45  one or the other has to be provided.
46 
47  """
48  if split_list==None:
49  if account==None:
50  return []
51  else:
52  split_list=account.GetSplitList()
53 
54  rlist=[]
55  for split in split_list:
56  if type(split).__name__ == 'SwigPyObject':
57  split = Split(instance=split)
58  lot=split.GetLot()
59  if lot == None:
60  rlist.append(split)
61  return rlist
62 
63 
64 def find_account(account,name,account_list=None):
65  """Recursively searches full names of account and descendents
66 
67  returns a list of accounts which contain name.
68 
69  options:
70 
71  account: account to start search in.
72  name: name to search for.
73  account_list: (optional) list to append accounts to.
74 
75  """
76 
77  if not account_list:
78  account_list=[]
79 
80  for child in account.get_children():
81  if type(child) != Account:
82  child=Account(instance=child)
83  account_list=find_account(child,name,account_list)
84 
85  account_name=account.GetName()
86  if name in account_name:
87  account_list.append(account)
88 
89  return account_list
90 
91 
92 def find_lot(lot_list,search_string):
93  """Searches lots in lot_list for search_string.
94 
95  returns list of lots where title contains search_string.
96 
97  options:
98 
99  lot_list: List of Lots to search in.
100  search_string: String to search for.
101 
102  """
103 
104  rlist=[]
105  for lot in lot_list:
106  if type(lot).__name__ == 'SwigPyObject':
107  lot = gnucash.GncLot(instance=lot)
108  ltitle=lot.get_title()
109  if search_string in ltitle:
110  rlist.append(lot)
111  return rlist
112 
113 
114 def find_split(split_list,search_string):
115  """Searches a list of splits for search_string
116 
117  returns a list of splits that have search_string as part of
118  memo or
119  description of parent transaction.
120 
121  options:
122 
123  split_list: List of Splits to search in.
124  search_string: String to search for.
125 
126  """
127 
128  rlist=[]
129  for split in split_list:
130  memo=split.GetMemo()
131  transaction_description=split.GetParent().GetDescription()
132  if (search_string in memo) or (search_string in transaction_description):
133  rlist.append(split)
134  return rlist
135 
136 
137 def find_split_recursive(account, search_string):
138  """Searches account and descendants for Splits containing search_string
139 
140  returns a list of splits that have search_string as part of
141  memo or
142  description of parent transaction.
143 
144  options:
145 
146  account: Account to search in.
147  search_string: String to search for.
148 
149  """
150 
151  rlist = []
152  child_account_splits = []
153 
154  # Get all splits in descendants
155  for child in account.get_children():
156  if type(child) != Account:
157  child = Account(instance=child)
158  childsplits = find_split_recursive(child, search_string)
159  for split in childsplits:
160  if type(split) != Split:
161  split = Split(instance=split)
162  child_account_splits += childsplits
163 
164  # Get all splits in account
165  splits=account.GetSplitList()
166  for split in splits:
167  if type(split) != Split:
168  split = Split(instance=split)
169  basic_account_splits=find_split(splits,search_string)
170 
171  rlist=child_account_splits+basic_account_splits
172  return rlist
173 
174 
175 def find_transaction(account,name,ignore_case=True,transaction_list=None):
176  """Searches the transactions of an account for name.
177 
178  Searches in description and in memo of each split.
179  returns a list of transactions that match criteria.
180 
181  options:
182 
183  account: Account to search in.
184  name: String to search for.
185  ignore_case: (optional, default=True) Ignore case if True.
186  transaction_list: (optional) list of transactions to search in.
187 
188  """
189 
190  if not transaction_list:
191  transaction_list=get_transaction_list(account)
192 
193  ret = []
194  if ignore_case:
195  name=name.lower()
196 
197  for transaction in transaction_list:
198  found = False
199 
200  desc=transaction.GetDescription()
201  if ignore_case:
202  desc=desc.lower()
203 
204  if name in desc:
205  found=True
206 
207  sl=transaction.GetSplitList()
208  for split in sl:
209  if type(split) != Split:
210  split=Split(instance=split)
211 
212  memo = split.GetMemo()
213  if ignore_case:
214  memo=memo.lower()
215 
216  if name in memo:
217  found=True
218 
219  if found:
220  ret.append(transaction)
221 
222  return ret