Hello, I am trying to write a simple importer (referring to reds_beancount_importers), but I hit the strange error with file handling. I am beginner in python and would appreciate some help in debugging the error. Following is some information.
Below is the importer code
''' Example importer for bofacc
'''
import csv
import datetime
import re
import logging
from os import path
from dateutil.parser import parse
from beancount.core import account
from beancount.core import amount
from beancount.core import data
from beancount.core import flags
from beancount.core import position
from beancount.core.number import D
from beancount.core.number import ZERO
from beangulp import mimetypes
from beangulp.importers import csvbase
from beangulp.testing import main
class Importer(csvbase.Importer):
date = csvbase.Date('Posted Date', '%m/%d/%Y')
narration = csvbase.Columns('Reference Number')
amount = csvbase.Amount('Amount')
payee = csvbase.Columns('Payee')
# balance = csvbase.Amount( 'Balance' )
def identify(self, filepath):
mimetype, encoding = mimetypes.guess_type(filepath)
if mimetype != 'text/csv':
print("Not txt/csv type file")
print (str (filepath) )
return False
with open(filepath, encoding=self.encoding) as fd:
head = fd.read(1024)
return head.startswith('Posted Date,Reference Number,Payee,Address,Amount')
def filename(self, filepath):
return 'bofacc.' + path.basename(filepath)
if __name__ == '__main__':
main(Importer('Liabilities:US:BofA:VISA-CC', 'USD'))
----
I hit the following error with the below command
>> bean-identify my.import Downloads/bofacc/November2022_000.csv
---
Traceback (most recent call last):
File "/usr/local/lib/python3.9/site-packages/beancount/ingest/identify.py", line 63, in find_imports
matched = importer.identify(file)
File "<path to my importer>/bofacc.py", line 32, in identify
mimetype, encoding = mimetypes.guess_type(filepath)
File "/usr/local/Cellar/python@3.9/3.9.0_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/mimetypes.py", line 292, in guess_type
return _db.guess_type(url, strict)
File "/usr/local/Cellar/python@3.9/3.9.0_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/mimetypes.py", line 116, in guess_type
url = os.fspath(url)
TypeError: expected str, bytes or os.PathLike object, not _FileMemo
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/local/bin/bean-identify", line 33, in <module>
sys.exit(load_entry_point('beancount==2.3.5', 'console_scripts', 'bean-identify')())
File "/usr/local/lib/python3.9/site-packages/beancount/ingest/scripts_utils.py", line 31, in identify_main
return trampoline_to_ingest(identify)
File "/usr/local/lib/python3.9/site-packages/beancount/ingest/scripts_utils.py", line 198, in trampoline_to_ingest
return run_import_script_and_ingest(parser)
File "/usr/local/lib/python3.9/site-packages/beancount/ingest/scripts_utils.py", line 246, in run_import_script_and_ingest
return ingest(importers_list)
File "/usr/local/lib/python3.9/site-packages/beancount/ingest/scripts_utils.py", line 140, in ingest
args.command(args, parser, importers_list, abs_downloads, hooks=hooks)
File "/usr/local/lib/python3.9/site-packages/beancount/ingest/identify.py", line 99, in run
return identify(importers_list, files_or_directories)
File "/usr/local/lib/python3.9/site-packages/beancount/ingest/identify.py", line 81, in identify
for filename, importers in find_imports(importers_list, files_or_directories,
File "/usr/local/lib/python3.9/site-packages/beancount/ingest/identify.py", line 68, in find_imports
importer.name(), exc)
TypeError: 'str' object is not callable
---
If I update my importer code to send str(filepath), I do get it to run without the errors but I find that the mimetype function call did not identify the CSV file even though the extension of the file provided was indeed CSV.
Updated the code
mimetype, encoding = mimetypes.guess_type(filepath)
to
mimetype, encoding = mimetypes.guess_type(str(filepath))
Any help to debug this is appreciated.