93 lines
2.9 KiB
Python
93 lines
2.9 KiB
Python
# -*- coding: utf-8 -*-
|
|
# This file is part of the cashbook-module from m-ds for Tryton.
|
|
# The COPYRIGHT file at the top level of this repository contains the
|
|
# full copyright notices and license terms.
|
|
|
|
from trytond.pool import Pool
|
|
from trytond.model import Model
|
|
|
|
|
|
class QifTool(Model):
|
|
'QIF Tool'
|
|
__name__ = 'cashbook_dataexchange.qiftool'
|
|
|
|
@classmethod
|
|
def split_by_type(cls, qifdata):
|
|
""" split file-content by type
|
|
"""
|
|
lines = qifdata.split('\n')
|
|
|
|
blocks = {}
|
|
current_type = None
|
|
for line in lines:
|
|
if line.startswith('!Type:'):
|
|
current_type = line[len('!Type:'):].strip()
|
|
else :
|
|
if current_type is None:
|
|
continue
|
|
|
|
if not current_type in blocks.keys():
|
|
blocks[current_type] = []
|
|
blocks[current_type].append(line.strip())
|
|
|
|
for block in blocks.keys():
|
|
blocks[block] = '\n'.join(blocks[block])
|
|
return blocks
|
|
|
|
@classmethod
|
|
def qif_read_categories(cls, catdata):
|
|
""" read categories from text
|
|
result: {
|
|
'in': [{
|
|
'<Category-Name>': {
|
|
'type': 'in|out',
|
|
'childs': [...],
|
|
},
|
|
},...],
|
|
'out': [{},...],
|
|
}
|
|
"""
|
|
def add_category(catdict, namelst, ctype):
|
|
""" add category to dict
|
|
"""
|
|
if not namelst[0] in catdict.keys():
|
|
catdict[namelst[0]] = {'type': ctype, 'childs': {}}
|
|
|
|
if len(namelst) > 1:
|
|
catdict[namelst[0]]['childs'] = add_category(
|
|
catdict[namelst[0]]['childs'],
|
|
namelst[1:],
|
|
ctype)
|
|
return catdict
|
|
|
|
categories = {'in': {}, 'out': {}}
|
|
for cattxt in catdata.split('^'):
|
|
if len(cattxt.strip()) == 0:
|
|
continue
|
|
catname = None
|
|
cattype = None
|
|
for line in cattxt.strip().split('\n'):
|
|
if line.startswith('N'):
|
|
catname = line[1:].strip().split(':')
|
|
elif line.startswith('E'):
|
|
cattype = 'out'
|
|
elif line.startswith('I'):
|
|
cattype = 'in'
|
|
else :
|
|
raise ValueError('invalid line: %s (%s)' % (line, cattxt))
|
|
categories[cattype] = add_category(categories[cattype], catname, cattype)
|
|
return categories
|
|
|
|
@classmethod
|
|
def qif_export_category(cls, record):
|
|
""" export single category as qif
|
|
"""
|
|
return '\n'.join([
|
|
'N%(cname)s' % {
|
|
'cname': record.rec_name.replace('/', ':'),
|
|
},
|
|
'E' if record.cattype == 'out' else 'I',
|
|
'^',
|
|
])
|
|
|
|
# end QifTool
|