To define a YNAB source, you could extend the Source class in
In the __init__ method you would load the YNAB database into memory.
You would define a method named prepare that is given a JournalEditor object, which has an entries property, which has the list of directives loaded from the journal, and an accounts property that is a dict that maps the account name str -> account Open objects.
This method would basically do everything that your existing code already does, and it would be pretty easy to adapt it: you would use the accounts dict provided to find which accounts have a ynab_name metadata field and build your account_mapping dict, build up your previous_imports list (which you might want to change to a set for efficiency) by iterating through the entries in the journal, add any relevant warnings or errors to the SourceResults object provided, and add any new transactions to the SourceResults object as well.
If you wanted to avoid having to manually specify the ynab_name to beancount account mapping, you could instead create the postings with Expenses:FIXME as the account, and with a ynab_account_name metadata field on each posting indicating the ynab account name, and set:
self.example_posting_key_extractors['ynab_account_name'] = None
in the __init__ method of your Source.
Then the account names would be predicted automatically, but could be manually overridden. (It seems like what you have already is more convenient, though.)
The source that behaves closest to what you want to do is the amazon source in beancount_import/source/amazon.py.
One thing I'm unclear on is what sort of workflow you want to have.
From the documentation of your beancount-ynab project, it sounds like you manually enter transactions into YNAB, and then periodically import them automatically into beancount.
With beancount-import, you would still manually confirm each transaction as it is being imported, as that lets you choose whether to import it as a new transaction or accept a proposed match that merges it with other transactions, and also lets you specify/confirm any unknown accounts, edit the narration, etc. Even in cases where you are just confirming the transaction and there are no unknown accounts that were predicted, I still find it useful to confirm the transaction since that lets me know about it (as it is coming from an external source, so I haven't seen it before).
In your case, though, it sounds like you've already manually entered the transactions in YNAB, so you don't necessarily want to manually confirm or edit each transaction when importing into beancount.
Do you always import into YNAB manually, or do you somehow import external data into YNAB?