Skip to content

Conversation

@DarrenRiedlinger
Copy link

Proposed solution for issue #241 allowing user-defined, deterministic preprocessing of imported sources.

Rather than having the pre-processing logic hard-coded within the reconcile.py, I settled on a minimal approach where reconcile.py is just very slightly modified with a hook to pass the LoadedReconciler instance to a user-defined preprocessor function.

That user-defined preprocessor function can be any user-supplied function. It has full access to the LoadedReconciler state, so it can directly modify the list of pending_entries, and also has access to the JournalEditor class for writing entries to the appropriate ledger. So it hopefully allows for many use-cases.

For simple use cases, I also created a separate BasePreprocessor class and a TransactionRuleMixin. User-defined preprocessors that subclass from these can specify pre-processing rules in a dictionary-like fashion

E.g.

from beancount_import.preprocessor import BasePreprocessor, TransactionRuleMixin, PreprocessingRule

rules = [
    PreprocessingRule(
        source_name="venmo",
        transaction_narration="Tutoring",
        posting_meta={"venmo_description": "Tutoring"},
        target_account="Expenses:Tutoring",
        action="record",
    ),
]


class RuleBasedPreprocessor(TransactionRuleMixin, BasePreprocessor):
    def preprocess(self) -> None:
        self.apply_rules(rules)

And this can be passed to the webserver like so:

beancount_import.webserver.main(
    ...
    preprocessor=RuleBasedPreprocessor,
    )

Let me know your thoughts on this approach.
If I had React experience + time it would be nice to also allow creating PreprocessingRules based on the selected pending entry directly within the web-ui, but that's unfortunately beyond what i can handle at the moment.

@Zburatorul
Copy link
Collaborator

Finally got to try this. Worked pretty well! The example you gave in the comment above was very helpful. It would have taken a lot longer to figure out how to set it up.
Could you add an example folder in here or some text in that README?

I haven't reviewed the code itself yet. Tests are failing on list[] vs typing.List[]

@DarrenRiedlinger
Copy link
Author

DarrenRiedlinger commented Dec 7, 2024 via email

@Zburatorul
Copy link
Collaborator

Did you end up sorting out the reference/copy issue?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants