def sort_journal(self, file): # sort the given ledger journal file # !!! !!! !!! # ATTENTION: if I made a mistake, this could destroy the whole journal !!! # !!! !!! !!! # check if file exists if os.path.isfile(file): f = open(file, 'r') the_journal = f.read() f.close() else: print 'Argument is not an existing file!' return # get everything besides the transactions the_journal_other = ledgerparse.string_to_non_transactions(the_journal) if the_journal_other: the_journal_other += '\n\n' # sort it the_journal_sorted = '\n\n'.join([str(x.get_original()) for x in sorted(ledgerparse.string_to_ledger(the_journal), key=lambda y: y.date)]) # save it to the SAME file !!!!!! f = open(file, 'w') f.write(the_journal_other + the_journal_sorted) f.close()
def __init__(self, the_file): if not os.path.isfile(the_file): # exits the programm, if the given environment variable or argument is not a file print CL_INF + 'Given argument is not a file.' + CL_E exit() # parse the transactions into self.Journal f = open(the_file, 'r') the_journal = f.read() f.close() self.Journal = ledgerparse.string_to_ledger(the_journal)
def preset(self, what='', liste=False): # try to load the preset file if not os.path.isfile(path_to_project + '/ledger-add.presets'): presetjournal = [] else: f = open(path_to_project + '/ledger-add.presets', 'r') presetjournal = ledgerparse.string_to_ledger( f.read() ) f.close() # return the list, if liste == True if liste == True: return presetjournal # add the transaction with the presetname to the final_str the_sum = 0.0 for t in presetjournal: # use transaction as preset, if name matches - replace stuff as well options = self.preset_get_name_and_settings(t.payee) if what in options: # get options from preset name # name if len(options) > 0: pr_name = options[0] else: pr_name = '' # options if len(options) > 3: pr_year = options[1] pr_month = options[2] pr_day = options[3] else: pr_year = False pr_month = False pr_day = False # get sum and commodity for acc in t.accounts: try: the_sum += float( str(acc.amount).replace(',', '.') ) except Exception: pass self.str_commodity = acc.commodity # get transaction string without the options # and replace date according to options of preset tmp_trans = [] for c, x in enumerate(t.get_original().splitlines()): if c == 0: tmp_tmp_trans = self.preset_strip_options(x)[0] # year if not pr_year: tmp_tmp_trans = self.str_date[0:4] + tmp_tmp_trans[4:] # month if not pr_month: tmp_tmp_trans = tmp_tmp_trans[0:5] + self.str_date[5:7] + tmp_tmp_trans[7:] # day if not pr_day: tmp_tmp_trans = tmp_tmp_trans[0:8] + self.str_date[8:10] + tmp_tmp_trans[10:] tmp_trans.append( tmp_tmp_trans ) else: tmp_trans.append( x ) self.final_str = '\n'.join(tmp_trans) # replace preset_wildcards while preset_wildcard in self.final_str: print print CL_DEF + self.final_str + CL_E user = raw_input(CL_TXT + 'Replace first / last ' + CL_DEF + preset_wildcard + CL_TXT + ': ' + CL_E) self.final_str = self.final_str.replace(preset_wildcard, user, 1) # add the transaction to the journal print print CL_TXT + '- - - - -' + CL_E print CL_OUT + self.final_str + CL_E print CL_TXT + '- - - - -' + CL_E print CL_DIM + 'Sum of values: ' + str(the_sum).replace('.', ',') + ' ' + self.str_commodity + CL_E print # ask if output should be appended user = raw_input(CL_TXT + 'Add this entry? (yes=appends to file, p=saves as a preset) [' + CL_DEF + 'yes' + CL_TXT + ']: ' + CL_E) # go back if user == '<': self.name() if user == 'n' or user == 'no': self.date() elif user == 'p' or user == 'preset': self.save_or_delete_preset() else: end(user) self.append_file()
def afa_feature(self, trans=None): # convert transaction to ledger_transaction or take from argument if not trans: trans = ledgerparse.string_to_ledger(self.final_str)[0] # make one afa reduction for every posted account with amount > 0 all_afas = [] for acc in trans.accounts: if acc.amount.amount > 0: # get its comment for output tmp_acc_com = ' (' + ', '.join([x.strip() for x in acc.comments]) + ')' if len(acc.comments) > 0 else '' print print CL_TXT + 'Transaction: (' + CL_DEF + trans.code + CL_TXT + ') ' + CL_ACC + trans.payee + CL_E print CL_TXT + 'Account: ' + CL_ACC + acc.name + CL_DIM + tmp_acc_com + ' ' + str(acc.amount) + ' ' + acc.commodity + CL_E # ask percentage of usage user = raw_input(CL_TXT + 'Usage for the job [' + CL_DEF + '100' + CL_TXT + ']% ? ' + CL_E) if not user: user = '******' try: percentage = float(user) / 100 except Exception: percentage = 1.0 # choose type of afa stuff print CL_TXT + 'What is it? (Enter number or string for manual input.)' + CL_E for num, item in enumerate(afa_table): print CL_TXT + '(' + CL_DEF + str(num+1) + CL_TXT + ') ' + item + CL_DIM + ' (' + str(afa_table[item][0]) + ')' + CL_E # let the user chose the afa item correct = False while not correct: user = raw_input(CL_TXT + afa_def_account + ':' + CL_E) end(user) if user: try: # user enters number if afa_table.keys()[int(user)-1] in afa_table: afa_item = afa_table.keys()[int(user)-1] afa_item_years = afa_table[afa_item][0] afa_item_name = afa_table[afa_item][1] correct = True except Exception: try: # user enters string afa_item_name = alias_it( afa_def_account + ':' + user ) afa_item_years = raw_input(CL_TXT + 'Years: ' + CL_E) end(user) if not afa_item_years: afa_item_years = 1 else: afa_item_years = int(afa_item_years) correct = True except Exception: print CL_INF + 'Chose correct entry or enter string.' + CL_E # NONAFA here !! # generate the nonafa transaction, if percentage is < 1.0 # NONAFA here !! if percentage < 1.0: # get code tmp_nonafa_code = '(' + trans.code + ') ' if trans.code else '' # get comment tmp_nonafa_comment = '\n ;' + '\n ;'.join(trans.comments) if len(trans.comments) > 0 else '' tmp_nonafa_acc_comment = '\n ;' + '\n ;'.join(acc.comments) if len(acc.comments) > 0 else '' if afa_append_comment: tmp_nonafa_comment += tmp_nonafa_acc_comment tmp_nonafa_acc_comment = '' # make account tmp_nonafa_account = afa_item_name.replace('[ACCOUNT]', afanon_def_account) # get amount tmp_real_amount = get_real_amount_with_percentage(acc.amount.amount, percentage, True) # generate nonafa transaction tmp_nonafa = datetime.datetime(trans.date.year, 12, 31).strftime(date_format) + ' * ' + tmp_nonafa_code + trans.payee + tmp_nonafa_comment + '\n ' + tmp_nonafa_account + ' ' + default_commodity + ' ' + str(ledgerparse.Money(real_amount=tmp_real_amount, dec_sep=dec_sep)) + '\n ' + acc.name + tmp_nonafa_acc_comment # append it all_afas.append( (trans.date.year, tmp_nonafa) ) # generate a single afa transaction, while it's bellow the afa_threshold if acc.amount.amount < afa_threshold_amount * 10000: all_afas.extend( self.afa_generate_trans(trans, acc, afa_item_name.replace('[ACCOUNT]', afa_def_account), percentage=percentage) ) # generate transactions over X year, where X = afa_item_years else: if afa_per_day_or_month == 'day': per_amount = ledgerparse.Money( real_amount=int( str( get_real_amount_with_percentage(acc.amount.amount, percentage) / (365 * afa_item_years) ).replace('.', dec_sep)[:-2] + '00' ), dec_sep=dec_sep ) else: per_amount = ledgerparse.Money( real_amount=int( str( get_real_amount_with_percentage(acc.amount.amount, percentage) / (12 * afa_item_years) ).replace('.', dec_sep)[:-2] + '00' ), dec_sep=dec_sep ) all_afas.extend( self.afa_generate_trans(trans, acc, afa_item_name.replace('[ACCOUNT]', afa_def_account), per_amount, percentage) ) # append the transactions to the journal(s) # only append to a single journal when split_journal_into_years == False if not split_journal_into_years or not file_automatic: # generate append string appender = '\n\n' + '\n\n'.join([x[1] for x in all_afas]) # append to the file f = open(ledger_file, 'a') f.write( appender ) f.close() # cycle through the years and append to the journals (or create a new journal for this year) else: for years in all_afas: # generate filename tmp_file = journal_file(year=years[0]) # check if file exists if os.path.isfile(tmp_file): # get file contents length and let append be either \n\n on file or direct the first entry in this file f = open(tmp_file, 'r') appender_pre = '\n\n' if len(f.read()) > 0 else '' f.close() # append to file f = open(tmp_file, 'a') f.write( appender_pre + years[1] ) f.close() # file does not exist so create totally new else: f = open(tmp_file, 'w') f.write( years[1] ) f.close() print self.date()