def parse(cls_, file_handle, date_format=None): if isinstance(file_handle, type('')): raise RuntimeError( six.u("parse() takes in a file handle, not a string")) data = file_handle.read() if len(data) == 0: raise QifParserException('Data is empty') qif_obj = Qif() chunks = data.split('\n^\n') last_type = None last_account = None transactions_header = None parsers = { 'category': cls_.parseCategory, 'account': cls_.parseAccount, 'transaction': cls_.parseTransaction, 'investment': cls_.parseInvestment, 'class': cls_.parseClass, 'memorized': cls_.parseMemorizedTransaction } for chunk in chunks: if not chunk: continue first_line = chunk.split('\n')[0].strip() if first_line == '!Type:Cat': last_type = 'category' elif first_line == '!Account': last_type = 'account' elif first_line in NON_INVST_ACCOUNT_TYPES: last_type = 'transaction' transactions_header = first_line elif first_line == '!Type:Invst': last_type = 'investment' transactions_header = first_line elif first_line == '!Type:Class': last_type = 'class' elif first_line == '!Type:Memorized': last_type = 'memorized' transactions_header = first_line elif chunk.startswith('!'): raise QifParserException( 'Header not reconized: {}'.format(first_line)) # if no header is recognized then # we use the previous one item = parsers[last_type](chunk) if last_type == 'account': qif_obj.add_account(item) last_account = item elif last_type == 'transaction'\ or last_type == 'memorized' or last_type == 'investment': if last_account: last_account.add_transaction(item, header=transactions_header) else: qif_obj.add_transaction(item, header=transactions_header) elif last_type == 'category': qif_obj.add_category(item) elif last_type == 'class': qif_obj.add_class(item) return qif_obj
def parse(cls_, file_handle, date_format=None): if isinstance(file_handle, type('')): raise RuntimeError( six.u("parse() takes in a file handle, not a string")) data = file_handle.read() if len(data) == 0: raise QifParserException('Data is empty') qif_obj = Qif() chunks = data.split('\n^\n') last_type = None last_account = None transactions_header = None parsers = { 'category': cls_.parseCategory, 'account': cls_.parseAccount, 'transaction': cls_.parseTransaction, 'investment': cls_.parseInvestment, 'class': cls_.parseClass, 'memorized': cls_.parseMemorizedTransaction } for chunk in chunks: if not chunk: continue first_line = chunk.split('\n')[0] if first_line == '!Type:Cat': last_type = 'category' elif first_line == '!Account': last_type = 'account' elif first_line in NON_INVST_ACCOUNT_TYPES: last_type = 'transaction' transactions_header = first_line elif first_line == '!Type:Invst': last_type = 'investment' transactions_header = first_line elif first_line == '!Type:Class': last_type = 'class' elif first_line == '!Type:Memorized': last_type = 'memorized' transactions_header = first_line elif chunk.startswith('!'): raise QifParserException('Header not reconized') # if no header is recognized then # we use the previous one item = parsers[last_type](chunk) if last_type == 'account': qif_obj.add_account(item) last_account = item elif last_type == 'transaction'\ or last_type == 'memorized' or last_type == 'investment': if last_account: last_account.add_transaction(item, header=transactions_header) else: qif_obj.add_transaction(item, header=transactions_header) elif last_type == 'category': qif_obj.add_category(item) elif last_type == 'class': qif_obj.add_class(item) return qif_obj
def parse(cls_, file_handle, date_format=None, num_sep=None): if isinstance(file_handle, type('')): raise RuntimeError( six.u("parse() takes in a file handle, not a string")) # Read file in this way to avoid problems with different newlines separators: # Since it is not in our control how the file is opened we can't rely on # universal newlines feature data = '\n'.join(x.strip() for x in file_handle) if len(data) == 0: raise QifParserException('Data is empty') if not date_format: date_format = cls_.guessDateFormat(cls_.getDateSamples(data)) if num_sep is None: decimal_sep, thousands_sep = cls_.guessNumberFormat(cls_.getNumberSamples(data)) else: decimal_sep, thousands_sep = num_sep qif_obj = Qif() chunks = data.split('\n^\n') last_type = None last_account = None transactions_header = None parsers = { 'category': cls_.parseCategory, 'account': cls_.parseAccount, 'transaction': cls_.parseTransaction, 'investment': cls_.parseInvestment, 'class': cls_.parseClass, 'memorized': cls_.parseMemorizedTransaction } for chunk in chunks: if not chunk: continue first_line = chunk.split('\n')[0] if first_line == '!Type:Cat': last_type = 'category' elif first_line == '!Account': last_type = 'account' elif first_line in NON_INVST_ACCOUNT_TYPES: last_type = 'transaction' transactions_header = first_line elif first_line == '!Type:Invst': last_type = 'investment' transactions_header = first_line elif first_line == '!Type:Class': last_type = 'class' elif first_line == '!Type:Memorized': last_type = 'memorized' transactions_header = first_line elif chunk.startswith('!'): raise QifParserException(six.u("Header not recognized: %s") % repr(first_line)) # if no header is recognized then # we use the previous one item = parsers[last_type](chunk, date_format, decimal_sep, thousands_sep) if last_type == 'account': qif_obj.add_account(item) last_account = item elif last_type == 'transaction'\ or last_type == 'memorized' or last_type == 'investment': if last_account: last_account.add_transaction(item, header=transactions_header) else: qif_obj.add_transaction(item, header=transactions_header) elif last_type == 'category': qif_obj.add_category(item) elif last_type == 'class': qif_obj.add_class(item) return qif_obj
def parse(cls_, file_handle, date_format=None, num_sep=None): if isinstance(file_handle, type('')): raise RuntimeError( six.u("parse() takes in a file handle, not a string")) # Read file in this way to avoid problems with different newlines separators: # Since it is not in our control how the file is opened we can't rely on # universal newlines feature data = '\n'.join(x.strip() for x in file_handle) if len(data) == 0: raise QifParserException('Data is empty') if not date_format: date_format = cls_.guessDateFormat(cls_.getDateSamples(data)) if num_sep is None: decimal_sep, thousands_sep = cls_.guessNumberFormat( cls_.getNumberSamples(data)) else: decimal_sep, thousands_sep = num_sep qif_obj = Qif() chunks = data.split('\n^\n') last_type = None last_account = None transactions_header = None parsers = { 'category': cls_.parseCategory, 'account': cls_.parseAccount, 'transaction': cls_.parseTransaction, 'investment': cls_.parseInvestment, 'class': cls_.parseClass, 'memorized': cls_.parseMemorizedTransaction } for chunk in chunks: if not chunk: continue first_line = chunk.split('\n')[0] if first_line == '!Type:Cat': last_type = 'category' elif first_line == '!Account': last_type = 'account' elif first_line in NON_INVST_ACCOUNT_TYPES: last_type = 'transaction' transactions_header = first_line elif first_line == '!Type:Invst': last_type = 'investment' transactions_header = first_line elif first_line == '!Type:Class': last_type = 'class' elif first_line == '!Type:Memorized': last_type = 'memorized' transactions_header = first_line elif chunk.startswith('!'): raise QifParserException( six.u("Header not recognized: %s") % repr(first_line)) # if no header is recognized then # we use the previous one item = parsers[last_type](chunk, date_format, decimal_sep, thousands_sep) if last_type == 'account': qif_obj.add_account(item) last_account = item elif last_type == 'transaction'\ or last_type == 'memorized' or last_type == 'investment': if last_account: last_account.add_transaction(item, header=transactions_header) else: qif_obj.add_transaction(item, header=transactions_header) elif last_type == 'category': qif_obj.add_category(item) elif last_type == 'class': qif_obj.add_class(item) return qif_obj
def parse(cls_, file_handle, date_format=None): if isinstance(file_handle, type('')): raise RuntimeError( six.u("parse() takes in a file handle, not a string")) data = file_handle.read() if len(data) == 0: raise QifParserException('Data is empty') qif_obj = Qif() chunks = data.split('\n^\n') last_type = None last_account = None autoswitch = False transactions_header = None parsers = { 'category': cls_.parseCategory, 'account': cls_.parseAccount, 'transaction': cls_.parseTransaction, 'investment': cls_.parseInvestment, 'class': cls_.parseClass, 'memorized': cls_.parseMemorizedTransaction, 'security': cls_.parseSecurity, } for chunk in chunks: if not chunk: continue first_line = chunk.split('\n')[0].rstrip() while first_line in OPTIONS: # Ignore this line if first_line == '!Option:AutoSwitch': autoswitch = True elif first_line == '!Clear:AutoSwitch': autoswitch = False chunk = '\n'.join(chunk.split('\n')[1:]) # strip first line first_line = chunk.split('\n')[0].rstrip() if first_line == '!Type:Cat': last_type = 'category' elif first_line == '!Account': last_type = 'account' elif first_line in NON_INVST_ACCOUNT_TYPES: second_line = chunk.split('\n')[1].rstrip() if second_line == "!Account": chunk = '\n'.join( chunk.split('\n')[1:]) # strip first line first_line = chunk.split('\n')[0].rstrip() last_type = 'account' else: last_type = 'transaction' transactions_header = first_line elif first_line == '!Type:Invst': second_line = chunk.split('\n')[1].rstrip() if second_line == "!Account": chunk = '\n'.join( chunk.split('\n')[1:]) # strip first line first_line = chunk.split('\n')[0].rstrip() last_type = 'account' else: last_type = 'investment' transactions_header = first_line elif first_line == '!Type:Class': last_type = 'class' elif first_line == '!Type:Memorized': last_type = 'memorized' transactions_header = first_line elif first_line == '!Type:Security': last_type = 'security' elif chunk.startswith('!'): raise QifParserException( 'Header not recognized: <{}>'.format(first_line)) # if no header is recognized then # we use the previous one if last_type is None: import pdb pdb.set_trace() pass item = parsers[last_type](chunk) if last_type == 'account': qif_obj.add_account(item) last_account = qif_obj.get_accounts(name=item.name, atype=item.account_type)[0] elif last_type == 'transaction'\ or last_type == 'memorized' or last_type == 'investment': if last_account: last_account.add_transaction(item, header=transactions_header) else: qif_obj.add_transaction(item, header=transactions_header) elif last_type == 'category': qif_obj.add_category(item) elif last_type == 'class': qif_obj.add_class(item) return qif_obj