# Path: bankcsvtoqif/qif.py
# class QifFile(object):
#     """ Interface to .qif-file. """
# 
#     def __init__(self, transactions):
#         self.transactions = transactions
# 
#     def get_raw_data(self):
#         lines=[]
#         for source_account in self._get_unique_source_accounts():
#             lines += self._get_qif_source_account_line(source_account)
#             transactions = [t for t in self.transactions if t.source_account==source_account]
#             for t in transactions:
#                 lines += t.to_qif_line()
#         return lines
# 
#     def _get_unique_source_accounts(self):
#         return set([t.source_account for t in self.transactions])
# 
#     def _get_qif_source_account_line(self, source_account):
#         return [
#             '!Account',
#             'N' + source_account,
#             '^'
#         ]
# 
# Path: bankcsvtoqif/smartlabeler.py
# class SmartLabeler(object):
#     """ Relabels descriptions of transactions using Replacements. """
# 
#     def __init__(self):
#         self.replacements = []
# 
#     def load_replacements_from_file(self, f, account_name):
#         all_replacements = json.load(f)
#         for repdata in all_replacements[account_name]:
#             r = Replacement()
#             r.load_from_json(repdata)
#             self.replacements.append(r)
# 
#     def has_replacement(self, transaction):
#         for replacement in self.replacements:
#             if replacement.matches(transaction.description):
#                 return replacement
# 
#     def replace(self, transaction, replacement):
#         if replacement.new_description:
#             transaction.description = replacement.get_description(transaction.date)
#         transaction.target_account = replacement.account
#         return transaction
# 
#     def run_replacements(self, transactions, messenger):
#         for index, transaction in enumerate(transactions):
#             replacement = self.has_replacement(transaction)
#             if replacement:
#                 transactions[index] = self.replace(transaction, replacement)
#                 messenger.send_message("replaced: " + transactions[index].__str__())
# 
# Path: bankcsvtoqif/transaction.py
# class TransactionFactory(object):
#     """ Creates Transactions from an account_config. """
# 
#     def __init__(self, account_config):
#         self.account_config = account_config
# 
#     def create_from_line(self, line):
#         return Transaction(
#             date=self.account_config.get_date(line),
#             description=self.account_config.get_description(line),
#             debit=self.account_config.get_debit(line),
#             credit=self.account_config.get_credit(line),
#             target_account=self.account_config.get_target_account(line),
#             source_account=self.account_config.get_source_account(line)
#         )
# 
#     def read_from_file(self, f, messenger):
#         csv.register_dialect(
#             'bank_csv',
#             self.account_config.get_csv_dialect()
#         )
#         c = csv.reader(f, 'bank_csv')
#         consume(c, self.account_config.dropped_lines)  # ignore first lines
#         transactions = []
#         for line in c:
#             try:
#                 transaction = self.create_from_line(line)
#                 transactions.append(transaction)
#                 messenger.send_message("parsed: " + transaction.__str__())
#             except IndexError:
#                 messenger.send_message('skipped: %s' % line)
#                 continue
#         return transactions
# 
# Path: bankcsvtoqif/app.py



from bankcsvtoqif.qif import QifFile
from bankcsvtoqif.smartlabeler import SmartLabeler
from bankcsvtoqif.transaction import TransactionFactory
from bankcsvtoqif.app import bank_dict

    def __init__(self, on):
        self.on = on

    def send_message(self, msg, preblanks=0):
        if self.on:
            for blank in range(0, preblanks):
                print()
            print(msg)


class DataManager(object):
    """ Main class to interact with the user. """

    def __init__(self, args):

        self.account_config = bank_dict[args.type]()
        if args.source_account:
            self.account_config.default_source_account = args.source_account
        if args.target_account:
            self.account_config.default_target_account = args.target_account
        self.type = args.type
        self.csv_filename = args.csv_file
        self.qif_filename = args.qif_file if args.qif_file else args.csv_file[:-3] + 'qif'
        self.replacements_file = args.replacements
        self.messenger = Messenger(args.v)
        self.transactions = []

    def read_csv(self, f):
        self.messenger.send_message("Parsing csv-file from" + self.csv_filename + "...", preblanks=1)

# Path: bankcsvtoqif/qif.py
# class QifFile(object):
#     """ Interface to .qif-file. """
# 
#     def __init__(self, transactions):
#         self.transactions = transactions
# 
#     def get_raw_data(self):
#         lines=[]
#         for source_account in self._get_unique_source_accounts():
#             lines += self._get_qif_source_account_line(source_account)
#             transactions = [t for t in self.transactions if t.source_account==source_account]
#             for t in transactions:
#                 lines += t.to_qif_line()
#         return lines
# 
#     def _get_unique_source_accounts(self):
#         return set([t.source_account for t in self.transactions])
# 
#     def _get_qif_source_account_line(self, source_account):
#         return [
#             '!Account',
#             'N' + source_account,
#             '^'
#         ]
# 
# Path: bankcsvtoqif/smartlabeler.py
# class SmartLabeler(object):
#     """ Relabels descriptions of transactions using Replacements. """
# 
#     def __init__(self):
#         self.replacements = []
# 
#     def load_replacements_from_file(self, f, account_name):
#         all_replacements = json.load(f)
#         for repdata in all_replacements[account_name]:
#             r = Replacement()
#             r.load_from_json(repdata)
#             self.replacements.append(r)
# 
#     def has_replacement(self, transaction):
#         for replacement in self.replacements:
#             if replacement.matches(transaction.description):
#                 return replacement
# 
#     def replace(self, transaction, replacement):
#         if replacement.new_description:
#             transaction.description = replacement.get_description(transaction.date)
#         transaction.target_account = replacement.account
#         return transaction
# 
#     def run_replacements(self, transactions, messenger):
#         for index, transaction in enumerate(transactions):
#             replacement = self.has_replacement(transaction)
#             if replacement:
#                 transactions[index] = self.replace(transaction, replacement)
#                 messenger.send_message("replaced: " + transactions[index].__str__())
# 
# Path: bankcsvtoqif/transaction.py
# class TransactionFactory(object):
#     """ Creates Transactions from an account_config. """
# 
#     def __init__(self, account_config):
#         self.account_config = account_config
# 
#     def create_from_line(self, line):
#         return Transaction(
#             date=self.account_config.get_date(line),
#             description=self.account_config.get_description(line),
#             debit=self.account_config.get_debit(line),
#             credit=self.account_config.get_credit(line),
#             target_account=self.account_config.get_target_account(line),
#             source_account=self.account_config.get_source_account(line)
#         )
# 
#     def read_from_file(self, f, messenger):
#         csv.register_dialect(
#             'bank_csv',
#             self.account_config.get_csv_dialect()
#         )
#         c = csv.reader(f, 'bank_csv')
#         consume(c, self.account_config.dropped_lines)  # ignore first lines
#         transactions = []
#         for line in c:
#             try:
#                 transaction = self.create_from_line(line)
#                 transactions.append(transaction)
#                 messenger.send_message("parsed: " + transaction.__str__())
#             except IndexError:
#                 messenger.send_message('skipped: %s' % line)
#                 continue
#         return transactions
# 
# Path: bankcsvtoqif/app.py



# Path: bankcsvtoqif/io.py
from bankcsvtoqif.qif import QifFile
from bankcsvtoqif.smartlabeler import SmartLabeler
from bankcsvtoqif.transaction import TransactionFactory
from bankcsvtoqif.app import bank_dict


    def __init__(self, on):
        self.on = on

    def send_message(self, msg, preblanks=0):
        if self.on:
            for blank in range(0, preblanks):
                print()
            print(msg)


class DataManager(object):
    """ Main class to interact with the user. """

    def __init__(self, args):

        self.account_config = bank_dict[args.type]()
        if args.source_account:
            self.account_config.default_source_account = args.source_account
        if args.target_account:
            self.account_config.default_target_account = args.target_account
        self.type = args.type
        self.csv_filename = args.csv_file
        self.qif_filename = args.qif_file if args.qif_file else args.csv_file[:-3] + 'qif'
        self.replacements_file = args.replacements
        self.messenger = Messenger(args.v)
        self.transactions = []

    def read_csv(self, f):
        self.messenger.send_message("Parsing csv-file from" + self.csv_filename + "...", preblanks=1)

        transaction_factory = TransactionFactory(self.account_config)