Skip to content

Commit

Permalink
add generic json reader
Browse files Browse the repository at this point in the history
  • Loading branch information
DEVNODEREACT committed Oct 4, 2024
1 parent 72f9d6a commit 78db343
Showing 1 changed file with 60 additions and 0 deletions.
60 changes: 60 additions & 0 deletions beancount_reds_importers/libreader/jsonreader.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#!/usr/bin/env python3

"""JSON reader for beancount-reds-importers.
JSON files have widely varying specifications, and thus, this is a very generic reader, and most of
the logic will have to be the institution specific readers.
"""

import json

from beancount.ingest import importer
from beancount_reds_importers.libreader import reader

class Importer(reader.Reader, importer.ImporterProtocol):
FILE_EXTS = ["json"]

def initialize_reader(self, file):
if getattr(self, "file", None) != file:
self.file = file
self.reader_ready = False
with open(file.name, 'r') as f:
self.json_data = json.load(f)
self.reader_ready = self.deep_identify(file)
if self.reader_ready:
self.set_currency()

def deep_identify(self, file):
"""For overriding by institution specific importer which can check if an account name
matches, and other such things."""
# default value to False, else jsonreader.initialize_reader fail to execute because missing attribut "config"
return False

def file_date(self, file):
"""Get the ending date of the statement."""
if not getattr(self, "json_data", None):
self.initialize(file)
# TODO:
return None

def read_file(self, file):
with open(file.name, 'r') as f:
self.json_data = json.load(f)

def get_json_elements(self, json_path, json_interpreter=lambda x: x):
"""Extract a list of elements in the JSON file at the given JSON path. Typically,
transactions are stored in a JSON path, and this extracts them."""
elements = self.json_data
for key in json_path.split('.'):
if key in elements:
elements = elements[key]
else:
return []
for elem in elements:
yield json_interpreter(elem)

def get_transactions(self):
"""/Transactions/Transaction is a dummy default path for transactions that needs to be
overriden in the institution specific importer."""
yield from self.get_json_elements("Transactions.Transaction")

0 comments on commit 78db343

Please sign in to comment.