Skip to content

Commit

Permalink
Merge pull request #46 from tarioch/feature/neon_importer
Browse files Browse the repository at this point in the history
add importer for neon
  • Loading branch information
tarioch committed May 17, 2021
2 parents 5f0d47a + 8553c65 commit 40c2ac4
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 0 deletions.
9 changes: 9 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,15 @@ The targetFolder is optional, if present, mails that had attachments which were
from tariochbctools.importers.general.mailAdapterImporter import MailAdapterImporter
CONFIG = [MailAdapterImporter([MyImporter1(), MyImporter2()])]

**neon**

Import CSV from `Neon <https://www.neon-free.ch/>`_

::

from tariochbctools.importers.neon import importer as neonimp
CONFIG = [neonimp.Importer('\d\d\d\d_account_statements\.csv', 'Assets:Neon:CHF')]


Syncing a fork
--------------
Expand Down
Empty file.
55 changes: 55 additions & 0 deletions src/tariochbctools/importers/neon/importer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
from dateutil.parser import parse
from io import StringIO

from beancount.ingest import importer
from beancount.core import data
from beancount.core import amount
from beancount.core.number import D
from beancount.ingest.importers.mixins import identifier

import csv


class Importer(identifier.IdentifyMixin, importer.ImporterProtocol):
"""An importer for Neon CSV files."""

def __init__(self, regexps, account):
identifier.IdentifyMixin.__init__(self, matchers=[
('filename', regexps)
])
self.account = account

def name(self):
return super().name() + self.account

def file_account(self, file):
return self.account

def extract(self, file, existing_entries):
entries = []

with StringIO(file.contents()) as csvfile:
print(file.name)
print(file.contents())
reader = csv.DictReader(csvfile, ['date', 'amount', 'description'], delimiter=';', skipinitialspace=True)
next(reader)
for row in reader:
book_date = parse(row['date'].strip()).date()
amt = amount.Amount(D(row['amount']), 'CHF')

meta = data.new_metadata(file.name, 0)
entry = data.Transaction(
meta,
book_date,
'*',
'',
row['description'].strip(),
data.EMPTY_SET,
data.EMPTY_SET,
[
data.Posting(self.account, amt, None, None, None, None),
]
)
entries.append(entry)

return entries

0 comments on commit 40c2ac4

Please sign in to comment.